🌐 AI搜索 & 代理 主页
blob: 3d11a9bfdc56c00056c2057af5e6455742ab31f5 [file] [log] [blame]
Linus Torvalds0fcfd162005-04-18 20:04:431/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This handles basic git sha1 object files - packing, unpacking,
7 * creation etc.
8 */
Linus Torvalds0fcfd162005-04-18 20:04:439#include "cache.h"
Junio C Hamano1f688552005-06-27 10:35:3310#include "delta.h"
Linus Torvaldsa733cb62005-06-28 21:21:0211#include "pack.h"
Linus Torvalds0fcfd162005-04-18 20:04:4312
Linus Torvalds144bde72005-04-23 18:09:3213#ifndef O_NOATIME
14#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
15#define O_NOATIME 01000000
16#else
17#define O_NOATIME 0
18#endif
19#endif
20
Junio C Hamano88cd6212005-09-30 21:02:4721const unsigned char null_sha1[20] = { 0, };
22
Linus Torvalds144bde72005-04-23 18:09:3223static unsigned int sha1_file_open_flag = O_NOATIME;
24
Linus Torvalds0fcfd162005-04-18 20:04:4325static unsigned hexval(char c)
26{
27 if (c >= '0' && c <= '9')
28 return c - '0';
29 if (c >= 'a' && c <= 'f')
30 return c - 'a' + 10;
31 if (c >= 'A' && c <= 'F')
32 return c - 'A' + 10;
33 return ~0;
34}
35
36int get_sha1_hex(const char *hex, unsigned char *sha1)
37{
38 int i;
39 for (i = 0; i < 20; i++) {
40 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
41 if (val & ~0xff)
42 return -1;
43 *sha1++ = val;
44 hex += 2;
45 }
46 return 0;
47}
48
Johannes Schindelin457f06d2005-12-22 22:13:5649int adjust_shared_perm(const char *path)
50{
51 struct stat st;
52 int mode;
53
54 if (!shared_repository)
55 return 0;
56 if (lstat(path, &st) < 0)
57 return -1;
58 mode = st.st_mode;
59 if (mode & S_IRUSR)
60 mode |= S_IRGRP;
61 if (mode & S_IWUSR)
62 mode |= S_IWGRP;
63 if (mode & S_IXUSR)
64 mode |= S_IXGRP;
65 if (S_ISDIR(mode))
66 mode |= S_ISGID;
67 if (chmod(path, mode) < 0)
68 return -2;
69 return 0;
70}
71
Junio C Hamanob2cb9422005-07-06 08:11:5272int safe_create_leading_directories(char *path)
73{
74 char *pos = path;
Jason Riedy67d42212006-02-10 01:56:1375 struct stat st;
76
Johannes Schindelin67ffdf42005-11-06 23:36:1577 if (*pos == '/')
78 pos++;
Junio C Hamanob2cb9422005-07-06 08:11:5279
80 while (pos) {
81 pos = strchr(pos, '/');
82 if (!pos)
83 break;
84 *pos = 0;
Jason Riedy67d42212006-02-10 01:56:1385 if (!stat(path, &st)) {
86 /* path exists */
87 if (!S_ISDIR(st.st_mode)) {
Junio C Hamanob2cb9422005-07-06 08:11:5288 *pos = '/';
Jason Riedy67d42212006-02-10 01:56:1389 return -3;
Junio C Hamanob2cb9422005-07-06 08:11:5290 }
Johannes Schindelin457f06d2005-12-22 22:13:5691 }
Jason Riedy67d42212006-02-10 01:56:1392 else if (mkdir(path, 0777)) {
93 *pos = '/';
94 return -1;
95 }
Johannes Schindelin457f06d2005-12-22 22:13:5696 else if (adjust_shared_perm(path)) {
97 *pos = '/';
98 return -2;
99 }
Junio C Hamanob2cb9422005-07-06 08:11:52100 *pos++ = '/';
101 }
102 return 0;
103}
104
Linus Torvalds0fcfd162005-04-18 20:04:43105char * sha1_to_hex(const unsigned char *sha1)
106{
107 static char buffer[50];
108 static const char hex[] = "0123456789abcdef";
109 char *buf = buffer;
110 int i;
111
112 for (i = 0; i < 20; i++) {
113 unsigned int val = *sha1++;
114 *buf++ = hex[val >> 4];
115 *buf++ = hex[val & 0xf];
116 }
Johannes Schindelin1e80e042005-12-22 17:55:59117 *buf = '\0';
118
Linus Torvalds0fcfd162005-04-18 20:04:43119 return buffer;
120}
121
Junio C Hamanoace15342005-05-07 07:38:04122static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
123{
124 int i;
125 for (i = 0; i < 20; i++) {
126 static char hex[] = "0123456789abcdef";
127 unsigned int val = sha1[i];
128 char *pos = pathbuf + i*2 + (i > 0);
129 *pos++ = hex[val >> 4];
130 *pos = hex[val & 0xf];
131 }
132}
133
Linus Torvalds0fcfd162005-04-18 20:04:43134/*
135 * NOTE! This returns a statically allocated buffer, so you have to be
136 * careful about using it. Do a "strdup()" if you need to save the
137 * filename.
Junio C Hamanoace15342005-05-07 07:38:04138 *
139 * Also note that this returns the location for creating. Reading
140 * SHA1 file can happen from any alternate directory listed in the
Junio C Hamanod19938a2005-05-10 00:57:56141 * DB_ENVIRONMENT environment variable if it is not found in
Junio C Hamanoace15342005-05-07 07:38:04142 * the primary object database.
Linus Torvalds0fcfd162005-04-18 20:04:43143 */
144char *sha1_file_name(const unsigned char *sha1)
145{
Linus Torvalds0fcfd162005-04-18 20:04:43146 static char *name, *base;
147
148 if (!base) {
Junio C Hamanod19938a2005-05-10 00:57:56149 const char *sha1_file_directory = get_object_directory();
Linus Torvalds0fcfd162005-04-18 20:04:43150 int len = strlen(sha1_file_directory);
Christopher Li812666c2005-04-26 19:00:58151 base = xmalloc(len + 60);
Linus Torvalds0fcfd162005-04-18 20:04:43152 memcpy(base, sha1_file_directory, len);
153 memset(base+len, 0, 60);
154 base[len] = '/';
155 base[len+3] = '/';
156 name = base + len + 1;
157 }
Junio C Hamanoace15342005-05-07 07:38:04158 fill_sha1_path(name, sha1);
Linus Torvalds0fcfd162005-04-18 20:04:43159 return base;
160}
161
barkalow@iabervon.orgbf592c52005-08-01 00:53:44162char *sha1_pack_name(const unsigned char *sha1)
163{
164 static const char hex[] = "0123456789abcdef";
165 static char *name, *base, *buf;
166 int i;
167
168 if (!base) {
169 const char *sha1_file_directory = get_object_directory();
170 int len = strlen(sha1_file_directory);
171 base = xmalloc(len + 60);
172 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
173 name = base + len + 11;
174 }
175
176 buf = name;
177
178 for (i = 0; i < 20; i++) {
179 unsigned int val = *sha1++;
180 *buf++ = hex[val >> 4];
181 *buf++ = hex[val & 0xf];
182 }
183
184 return base;
185}
186
187char *sha1_pack_index_name(const unsigned char *sha1)
188{
189 static const char hex[] = "0123456789abcdef";
190 static char *name, *base, *buf;
191 int i;
192
193 if (!base) {
194 const char *sha1_file_directory = get_object_directory();
195 int len = strlen(sha1_file_directory);
196 base = xmalloc(len + 60);
197 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
198 name = base + len + 11;
199 }
200
201 buf = name;
202
203 for (i = 0; i < 20; i++) {
204 unsigned int val = *sha1++;
205 *buf++ = hex[val >> 4];
206 *buf++ = hex[val & 0xf];
207 }
208
209 return base;
210}
211
Junio C Hamanod5a63b92005-08-15 00:25:57212struct alternate_object_database *alt_odb_list;
213static struct alternate_object_database **alt_odb_tail;
Junio C Hamanoace15342005-05-07 07:38:04214
Junio C Hamanoddd5d052005-05-08 20:51:13215/*
216 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-15 00:25:57217 *
218 * The variable alt_odb_list points at the list of struct
219 * alternate_object_database. The elements on this list come from
220 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
221 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-05 06:48:43222 * whose contents is similar to that environment variable but can be
223 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-15 00:25:57224 * contains "/the/directory/corresponding/to/.git/objects/...", while
225 * its name points just after the slash at the end of ".git/objects/"
226 * in the example above, and has enough space to hold 40-byte hex
227 * SHA1, an extra slash for the first level indirection, and the
228 * terminating NUL.
Junio C Hamanoddd5d052005-05-08 20:51:13229 */
Junio C Hamanoccfd3e92005-09-13 07:05:22230static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
231 const char *relative_base)
Junio C Hamanod5a63b92005-08-15 00:25:57232{
233 const char *cp, *last;
234 struct alternate_object_database *ent;
Junio C Hamano1494e032005-12-05 06:48:43235 const char *objdir = get_object_directory();
Junio C Hamanoccfd3e92005-09-13 07:05:22236 int base_len = -1;
Junio C Hamanod5a63b92005-08-15 00:25:57237
238 last = alt;
Junio C Hamano9577e7e2005-08-17 01:22:05239 while (last < ep) {
240 cp = last;
241 if (cp < ep && *cp == '#') {
242 while (cp < ep && *cp != sep)
243 cp++;
244 last = cp + 1;
245 continue;
246 }
247 for ( ; cp < ep && *cp != sep; cp++)
Junio C Hamanod5a63b92005-08-15 00:25:57248 ;
249 if (last != cp) {
Junio C Hamano1494e032005-12-05 06:48:43250 struct alternate_object_database *alt;
Junio C Hamanod5a63b92005-08-15 00:25:57251 /* 43 = 40-byte + 2 '/' + terminating NUL */
252 int pfxlen = cp - last;
253 int entlen = pfxlen + 43;
254
Junio C Hamanoccfd3e92005-09-13 07:05:22255 if (*last != '/' && relative_base) {
256 /* Relative alt-odb */
257 if (base_len < 0)
258 base_len = strlen(relative_base) + 1;
259 entlen += base_len;
260 pfxlen += base_len;
261 }
Junio C Hamanod5a63b92005-08-15 00:25:57262 ent = xmalloc(sizeof(*ent) + entlen);
Junio C Hamano1494e032005-12-05 06:48:43263
Junio C Hamanoccfd3e92005-09-13 07:05:22264 if (*last != '/' && relative_base) {
265 memcpy(ent->base, relative_base, base_len - 1);
266 ent->base[base_len - 1] = '/';
267 memcpy(ent->base + base_len,
268 last, cp - last);
269 }
270 else
271 memcpy(ent->base, last, pfxlen);
Junio C Hamanod5a63b92005-08-15 00:25:57272 ent->name = ent->base + pfxlen + 1;
273 ent->base[pfxlen] = ent->base[pfxlen + 3] = '/';
274 ent->base[entlen-1] = 0;
Junio C Hamano1494e032005-12-05 06:48:43275
276 /* Prevent the common mistake of listing the same
277 * thing twice, or object directory itself.
278 */
279 for (alt = alt_odb_list; alt; alt = alt->next)
280 if (!memcmp(ent->base, alt->base, pfxlen))
281 goto bad;
282 if (!memcmp(ent->base, objdir, pfxlen)) {
283 bad:
284 free(ent);
285 }
286 else {
287 *alt_odb_tail = ent;
288 alt_odb_tail = &(ent->next);
289 ent->next = NULL;
290 }
Junio C Hamanod5a63b92005-08-15 00:25:57291 }
Junio C Hamano9577e7e2005-08-17 01:22:05292 while (cp < ep && *cp == sep)
Junio C Hamanod5a63b92005-08-15 00:25:57293 cp++;
294 last = cp;
Junio C Hamano9577e7e2005-08-17 01:22:05295 }
Junio C Hamanod5a63b92005-08-15 00:25:57296}
297
Junio C Hamano9a217f22005-06-28 21:56:57298void prepare_alt_odb(void)
Junio C Hamanoace15342005-05-07 07:38:04299{
Junio C Hamanod5a63b92005-08-15 00:25:57300 char path[PATH_MAX];
Junio C Hamano9577e7e2005-08-17 01:22:05301 char *map;
Junio C Hamanod5a63b92005-08-15 00:25:57302 int fd;
303 struct stat st;
Jason Riedyc7c81b32005-08-23 20:34:07304 char *alt;
305
Junio C Hamanoa9ab5862005-09-09 21:48:54306 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
Jason Riedyc7c81b32005-08-23 20:34:07307 if (!alt) alt = "";
Junio C Hamanoace15342005-05-07 07:38:04308
Junio C Hamanod5a63b92005-08-15 00:25:57309 if (alt_odb_tail)
Junio C Hamano9a217f22005-06-28 21:56:57310 return;
Junio C Hamanod5a63b92005-08-15 00:25:57311 alt_odb_tail = &alt_odb_list;
Junio C Hamanoccfd3e92005-09-13 07:05:22312 link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL);
Junio C Hamanod5a63b92005-08-15 00:25:57313
Junio C Hamanoccfd3e92005-09-13 07:05:22314 sprintf(path, "%s/info/alternates", get_object_directory());
Junio C Hamanod5a63b92005-08-15 00:25:57315 fd = open(path, O_RDONLY);
316 if (fd < 0)
317 return;
318 if (fstat(fd, &st) || (st.st_size == 0)) {
319 close(fd);
320 return;
Junio C Hamanoace15342005-05-07 07:38:04321 }
Junio C Hamanod5a63b92005-08-15 00:25:57322 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
323 close(fd);
324 if (map == MAP_FAILED)
325 return;
326
Junio C Hamanoccfd3e92005-09-13 07:05:22327 link_alt_odb_entries(map, map + st.st_size, '\n',
328 get_object_directory());
Junio C Hamanod5a63b92005-08-15 00:25:57329 munmap(map, st.st_size);
Junio C Hamanoace15342005-05-07 07:38:04330}
331
332static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
333{
Junio C Hamanoace15342005-05-07 07:38:04334 char *name = sha1_file_name(sha1);
Junio C Hamanod5a63b92005-08-15 00:25:57335 struct alternate_object_database *alt;
Junio C Hamanoace15342005-05-07 07:38:04336
337 if (!stat(name, st))
338 return name;
Junio C Hamano9a217f22005-06-28 21:56:57339 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-15 00:25:57340 for (alt = alt_odb_list; alt; alt = alt->next) {
341 name = alt->name;
Junio C Hamanoace15342005-05-07 07:38:04342 fill_sha1_path(name, sha1);
Junio C Hamanod5a63b92005-08-15 00:25:57343 if (!stat(alt->base, st))
344 return alt->base;
Junio C Hamanoace15342005-05-07 07:38:04345 }
346 return NULL;
347}
348
Junio C Hamano1f688552005-06-27 10:35:33349#define PACK_MAX_SZ (1<<26)
350static int pack_used_ctr;
351static unsigned long pack_mapped;
Junio C Hamano9a217f22005-06-28 21:56:57352struct packed_git *packed_git;
Junio C Hamano1f688552005-06-27 10:35:33353
Junio C Hamano1f688552005-06-27 10:35:33354static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
355 void **idx_map_)
356{
357 void *idx_map;
358 unsigned int *index;
359 unsigned long idx_size;
360 int nr, i;
Junio C Hamanoe93ec6f2006-01-19 04:26:14361 int fd = open(path, O_RDONLY);
Junio C Hamano1f688552005-06-27 10:35:33362 struct stat st;
363 if (fd < 0)
364 return -1;
365 if (fstat(fd, &st)) {
366 close(fd);
367 return -1;
368 }
369 idx_size = st.st_size;
370 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
371 close(fd);
372 if (idx_map == MAP_FAILED)
373 return -1;
374
375 index = idx_map;
Linus Torvalds4d235c82005-07-03 16:58:44376 *idx_map_ = idx_map;
377 *idx_size_ = idx_size;
Junio C Hamano1f688552005-06-27 10:35:33378
379 /* check index map */
Junio C Hamanof9253392005-06-29 09:51:27380 if (idx_size < 4*256 + 20 + 20)
Junio C Hamano1f688552005-06-27 10:35:33381 return error("index file too small");
382 nr = 0;
383 for (i = 0; i < 256; i++) {
384 unsigned int n = ntohl(index[i]);
385 if (n < nr)
386 return error("non-monotonic index");
387 nr = n;
388 }
389
390 /*
391 * Total size:
392 * - 256 index entries 4 bytes each
393 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
394 * - 20-byte SHA1 of the packfile
395 * - 20-byte SHA1 file checksum
396 */
397 if (idx_size != 4*256 + nr * 24 + 20 + 20)
398 return error("wrong index file size");
399
Junio C Hamano1f688552005-06-27 10:35:33400 return 0;
401}
402
Junio C Hamanof9253392005-06-29 09:51:27403static int unuse_one_packed_git(void)
Junio C Hamano1f688552005-06-27 10:35:33404{
Junio C Hamanof9253392005-06-29 09:51:27405 struct packed_git *p, *lru = NULL;
406
407 for (p = packed_git; p; p = p->next) {
408 if (p->pack_use_cnt || !p->pack_base)
409 continue;
410 if (!lru || p->pack_last_used < lru->pack_last_used)
411 lru = p;
412 }
413 if (!lru)
414 return 0;
415 munmap(lru->pack_base, lru->pack_size);
416 lru->pack_base = NULL;
417 return 1;
Junio C Hamano1f688552005-06-27 10:35:33418}
419
Junio C Hamanof9253392005-06-29 09:51:27420void unuse_packed_git(struct packed_git *p)
421{
422 p->pack_use_cnt--;
423}
424
425int use_packed_git(struct packed_git *p)
Junio C Hamano1f688552005-06-27 10:35:33426{
barkalow@iabervon.orgbf592c52005-08-01 00:53:44427 if (!p->pack_size) {
428 struct stat st;
429 // We created the struct before we had the pack
430 stat(p->pack_name, &st);
431 if (!S_ISREG(st.st_mode))
432 die("packfile %s not a regular file", p->pack_name);
433 p->pack_size = st.st_size;
434 }
Junio C Hamano1f688552005-06-27 10:35:33435 if (!p->pack_base) {
436 int fd;
437 struct stat st;
438 void *map;
439
440 pack_mapped += p->pack_size;
Junio C Hamanof9253392005-06-29 09:51:27441 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
442 ; /* nothing */
Junio C Hamano1f688552005-06-27 10:35:33443 fd = open(p->pack_name, O_RDONLY);
444 if (fd < 0)
Junio C Hamanof9253392005-06-29 09:51:27445 die("packfile %s cannot be opened", p->pack_name);
Junio C Hamano1f688552005-06-27 10:35:33446 if (fstat(fd, &st)) {
447 close(fd);
Junio C Hamanof9253392005-06-29 09:51:27448 die("packfile %s cannot be opened", p->pack_name);
Junio C Hamano1f688552005-06-27 10:35:33449 }
450 if (st.st_size != p->pack_size)
Junio C Hamanof9253392005-06-29 09:51:27451 die("packfile %s size mismatch.", p->pack_name);
Junio C Hamano1f688552005-06-27 10:35:33452 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
453 close(fd);
454 if (map == MAP_FAILED)
Junio C Hamanof9253392005-06-29 09:51:27455 die("packfile %s cannot be mapped.", p->pack_name);
Junio C Hamano1f688552005-06-27 10:35:33456 p->pack_base = map;
Junio C Hamanof9253392005-06-29 09:51:27457
458 /* Check if the pack file matches with the index file.
459 * this is cheap.
460 */
461 if (memcmp((char*)(p->index_base) + p->index_size - 40,
barkalow@iabervon.orgbf592c52005-08-01 00:53:44462 p->pack_base + p->pack_size - 20, 20)) {
463
Junio C Hamanof9253392005-06-29 09:51:27464 die("packfile %s does not match index.", p->pack_name);
barkalow@iabervon.orgbf592c52005-08-01 00:53:44465 }
Junio C Hamano1f688552005-06-27 10:35:33466 }
467 p->pack_last_used = pack_used_ctr++;
Junio C Hamanof9253392005-06-29 09:51:27468 p->pack_use_cnt++;
Junio C Hamano1f688552005-06-27 10:35:33469 return 0;
470}
471
Linus Torvalds9d835df2005-10-13 22:38:28472struct packed_git *add_packed_git(char *path, int path_len, int local)
Junio C Hamano1f688552005-06-27 10:35:33473{
474 struct stat st;
475 struct packed_git *p;
476 unsigned long idx_size;
477 void *idx_map;
Junio C Hamanoc0bbbb12005-11-15 20:51:02478 unsigned char sha1[20];
Junio C Hamano1f688552005-06-27 10:35:33479
480 if (check_packed_git_idx(path, &idx_size, &idx_map))
481 return NULL;
482
483 /* do we have a corresponding .pack file? */
484 strcpy(path + path_len - 4, ".pack");
485 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
486 munmap(idx_map, idx_size);
487 return NULL;
488 }
489 /* ok, it looks sane as far as we can check without
490 * actually mapping the pack file.
491 */
492 p = xmalloc(sizeof(*p) + path_len + 2);
493 strcpy(p->pack_name, path);
494 p->index_size = idx_size;
495 p->pack_size = st.st_size;
496 p->index_base = idx_map;
497 p->next = NULL;
Junio C Hamanod85a4fe2005-06-28 21:55:16498 p->pack_base = NULL;
Junio C Hamano1f688552005-06-27 10:35:33499 p->pack_last_used = 0;
Junio C Hamanof9253392005-06-29 09:51:27500 p->pack_use_cnt = 0;
Linus Torvalds9d835df2005-10-13 22:38:28501 p->pack_local = local;
Pavel Roskinf4a11062005-12-21 23:47:09502 if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1))
Lukas_Sandströmc283ab22005-11-09 01:22:40503 memcpy(p->sha1, sha1, 20);
Junio C Hamano1f688552005-06-27 10:35:33504 return p;
505}
506
barkalow@iabervon.orgbf592c52005-08-01 00:53:44507struct packed_git *parse_pack_index(unsigned char *sha1)
508{
Daniel Barkalowc508df52005-08-16 04:10:03509 char *path = sha1_pack_index_name(sha1);
510 return parse_pack_index_file(sha1, path);
511}
512
Peter Hagervall2ab141a2005-09-02 12:17:10513struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
Daniel Barkalowc508df52005-08-16 04:10:03514{
barkalow@iabervon.orgbf592c52005-08-01 00:53:44515 struct packed_git *p;
516 unsigned long idx_size;
517 void *idx_map;
Daniel Barkalowc508df52005-08-16 04:10:03518 char *path;
barkalow@iabervon.orgbf592c52005-08-01 00:53:44519
Daniel Barkalowc508df52005-08-16 04:10:03520 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
barkalow@iabervon.orgbf592c52005-08-01 00:53:44521 return NULL;
522
523 path = sha1_pack_name(sha1);
524
525 p = xmalloc(sizeof(*p) + strlen(path) + 2);
526 strcpy(p->pack_name, path);
527 p->index_size = idx_size;
528 p->pack_size = 0;
529 p->index_base = idx_map;
530 p->next = NULL;
531 p->pack_base = NULL;
532 p->pack_last_used = 0;
533 p->pack_use_cnt = 0;
534 memcpy(p->sha1, sha1, 20);
535 return p;
536}
537
538void install_packed_git(struct packed_git *pack)
539{
540 pack->next = packed_git;
541 packed_git = pack;
542}
543
Linus Torvalds9d835df2005-10-13 22:38:28544static void prepare_packed_git_one(char *objdir, int local)
Junio C Hamano1f688552005-06-27 10:35:33545{
546 char path[PATH_MAX];
547 int len;
548 DIR *dir;
549 struct dirent *de;
550
551 sprintf(path, "%s/pack", objdir);
552 len = strlen(path);
553 dir = opendir(path);
554 if (!dir)
555 return;
556 path[len++] = '/';
557 while ((de = readdir(dir)) != NULL) {
558 int namelen = strlen(de->d_name);
559 struct packed_git *p;
560
561 if (strcmp(de->d_name + namelen - 4, ".idx"))
562 continue;
563
564 /* we have .idx. Is it a file we can map? */
565 strcpy(path + len, de->d_name);
Linus Torvalds9d835df2005-10-13 22:38:28566 p = add_packed_git(path, len + namelen, local);
Junio C Hamano1f688552005-06-27 10:35:33567 if (!p)
568 continue;
569 p->next = packed_git;
570 packed_git = p;
571 }
Junio C Hamano5b35bcd2005-07-06 06:52:17572 closedir(dir);
Junio C Hamano1f688552005-06-27 10:35:33573}
574
Junio C Hamano9a217f22005-06-28 21:56:57575void prepare_packed_git(void)
Junio C Hamano1f688552005-06-27 10:35:33576{
Junio C Hamano1f688552005-06-27 10:35:33577 static int run_once = 0;
Junio C Hamanod5a63b92005-08-15 00:25:57578 struct alternate_object_database *alt;
Junio C Hamano1f688552005-06-27 10:35:33579
Junio C Hamanod5a63b92005-08-15 00:25:57580 if (run_once)
Junio C Hamano1f688552005-06-27 10:35:33581 return;
Linus Torvalds9d835df2005-10-13 22:38:28582 prepare_packed_git_one(get_object_directory(), 1);
Junio C Hamano9a217f22005-06-28 21:56:57583 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-15 00:25:57584 for (alt = alt_odb_list; alt; alt = alt->next) {
Junio C Hamano1494e032005-12-05 06:48:43585 alt->name[-1] = 0;
Linus Torvalds9d835df2005-10-13 22:38:28586 prepare_packed_git_one(alt->base, 0);
Junio C Hamano1494e032005-12-05 06:48:43587 alt->name[-1] = '/';
Junio C Hamano1f688552005-06-27 10:35:33588 }
Junio C Hamanod5a63b92005-08-15 00:25:57589 run_once = 1;
Junio C Hamano1f688552005-06-27 10:35:33590}
591
Jason McMullan5d6ccf52005-06-03 15:05:39592int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
Linus Torvalds0fcfd162005-04-18 20:04:43593{
Linus Torvaldsd98b46f2005-04-20 08:10:46594 char header[100];
Linus Torvalds0fcfd162005-04-18 20:04:43595 unsigned char real_sha1[20];
596 SHA_CTX c;
597
598 SHA1_Init(&c);
Linus Torvaldsd98b46f2005-04-20 08:10:46599 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
Linus Torvalds0fcfd162005-04-18 20:04:43600 SHA1_Update(&c, map, size);
601 SHA1_Final(real_sha1, &c);
602 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
603}
604
Junio C Hamano1f688552005-06-27 10:35:33605static void *map_sha1_file_internal(const unsigned char *sha1,
Daniel Barkalowd5f1bef2005-07-10 22:27:02606 unsigned long *size)
Linus Torvalds0fcfd162005-04-18 20:04:43607{
Linus Torvalds0fcfd162005-04-18 20:04:43608 struct stat st;
609 void *map;
Linus Torvalds144bde72005-04-23 18:09:32610 int fd;
Junio C Hamanoace15342005-05-07 07:38:04611 char *filename = find_sha1_file(sha1, &st);
612
613 if (!filename) {
Junio C Hamanoace15342005-05-07 07:38:04614 return NULL;
615 }
Linus Torvalds0fcfd162005-04-18 20:04:43616
Linus Torvalds144bde72005-04-23 18:09:32617 fd = open(filename, O_RDONLY | sha1_file_open_flag);
Linus Torvalds0fcfd162005-04-18 20:04:43618 if (fd < 0) {
Linus Torvalds144bde72005-04-23 18:09:32619 /* See if it works without O_NOATIME */
620 switch (sha1_file_open_flag) {
621 default:
622 fd = open(filename, O_RDONLY);
623 if (fd >= 0)
624 break;
625 /* Fallthrough */
626 case 0:
Linus Torvalds144bde72005-04-23 18:09:32627 return NULL;
628 }
629
Junio C Hamano1f688552005-06-27 10:35:33630 /* If it failed once, it will probably fail again.
631 * Stop using O_NOATIME
632 */
Linus Torvalds144bde72005-04-23 18:09:32633 sha1_file_open_flag = 0;
Linus Torvalds0fcfd162005-04-18 20:04:43634 }
Linus Torvalds0fcfd162005-04-18 20:04:43635 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
636 close(fd);
Pavel Roskine35f9822005-07-29 14:49:14637 if (map == MAP_FAILED)
Linus Torvalds0fcfd162005-04-18 20:04:43638 return NULL;
639 *size = st.st_size;
640 return map;
641}
642
Linus Torvaldsc4483572005-06-02 00:54:59643int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
644{
645 /* Get the data stream */
646 memset(stream, 0, sizeof(*stream));
647 stream->next_in = map;
648 stream->avail_in = mapsize;
649 stream->next_out = buffer;
650 stream->avail_out = size;
651
652 inflateInit(stream);
653 return inflate(stream, 0);
654}
655
Linus Torvalds6da40162005-07-03 17:10:45656static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
Linus Torvalds0fcfd162005-04-18 20:04:43657{
Linus Torvalds5180cac2005-06-02 14:57:25658 int bytes = strlen(buffer) + 1;
Mika Kukkonend565b342005-06-21 20:04:33659 unsigned char *buf = xmalloc(1+size);
Linus Torvalds0fcfd162005-04-18 20:04:43660
Linus Torvalds5180cac2005-06-02 14:57:25661 memcpy(buf, buffer + bytes, stream->total_out - bytes);
662 bytes = stream->total_out - bytes;
663 if (bytes < size) {
664 stream->next_out = buf + bytes;
665 stream->avail_out = size - bytes;
666 while (inflate(stream, Z_FINISH) == Z_OK)
Linus Torvalds0fcfd162005-04-18 20:04:43667 /* nothing */;
668 }
Linus Torvalds5180cac2005-06-02 14:57:25669 buf[size] = 0;
670 inflateEnd(stream);
Linus Torvalds0fcfd162005-04-18 20:04:43671 return buf;
672}
673
Linus Torvalds5180cac2005-06-02 14:57:25674/*
675 * We used to just use "sscanf()", but that's actually way
676 * too permissive for what we want to check. So do an anal
677 * object header parse by hand.
678 */
679int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
680{
681 int i;
682 unsigned long size;
683
684 /*
685 * The type can be at most ten bytes (including the
686 * terminating '\0' that we add), and is followed by
687 * a space.
688 */
689 i = 10;
690 for (;;) {
691 char c = *hdr++;
692 if (c == ' ')
693 break;
694 if (!--i)
695 return -1;
696 *type++ = c;
697 }
698 *type = 0;
699
700 /*
701 * The length must follow immediately, and be in canonical
702 * decimal format (ie "010" is not valid).
703 */
704 size = *hdr++ - '0';
705 if (size > 9)
706 return -1;
707 if (size) {
708 for (;;) {
709 unsigned long c = *hdr - '0';
710 if (c > 9)
711 break;
712 hdr++;
713 size = size * 10 + c;
714 }
715 }
716 *sizep = size;
717
718 /*
719 * The length must be followed by a zero byte
720 */
721 return *hdr ? -1 : 0;
722}
723
724void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
725{
726 int ret;
727 z_stream stream;
728 char hdr[8192];
729
730 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
731 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
732 return NULL;
733
734 return unpack_sha1_rest(&stream, hdr, *size);
735}
736
Junio C Hamanof3bf9222005-07-01 00:15:39737/* forward declaration for a mutually recursive function */
738static int packed_object_info(struct pack_entry *entry,
739 char *type, unsigned long *sizep);
740
Junio C Hamano5db47c22005-06-28 06:58:08741static int packed_delta_info(unsigned char *base_sha1,
742 unsigned long delta_size,
743 unsigned long left,
744 char *type,
Junio C Hamanof3bf9222005-07-01 00:15:39745 unsigned long *sizep,
746 struct packed_git *p)
Junio C Hamano5db47c22005-06-28 06:58:08747{
Junio C Hamanof3bf9222005-07-01 00:15:39748 struct pack_entry base_ent;
749
Junio C Hamano5db47c22005-06-28 06:58:08750 if (left < 20)
751 die("truncated pack file");
Junio C Hamanoc62266f2005-07-01 00:13:07752
Junio C Hamanof3bf9222005-07-01 00:15:39753 /* The base entry _must_ be in the same pack */
754 if (!find_pack_entry_one(base_sha1, &base_ent, p))
755 die("failed to find delta-pack base object %s",
756 sha1_to_hex(base_sha1));
757
Junio C Hamanoc62266f2005-07-01 00:13:07758 /* We choose to only get the type of the base object and
759 * ignore potentially corrupt pack file that expects the delta
760 * based on a base with a wrong size. This saves tons of
761 * inflate() calls.
762 */
763
Junio C Hamanof3bf9222005-07-01 00:15:39764 if (packed_object_info(&base_ent, type, NULL))
Junio C Hamano5db47c22005-06-28 06:58:08765 die("cannot get info for delta-pack base");
766
Junio C Hamanoc62266f2005-07-01 00:13:07767 if (sizep) {
768 const unsigned char *data;
769 unsigned char delta_head[64];
770 unsigned long result_size;
771 z_stream stream;
772 int st;
Junio C Hamano5db47c22005-06-28 06:58:08773
Junio C Hamanoc62266f2005-07-01 00:13:07774 memset(&stream, 0, sizeof(stream));
Junio C Hamano5db47c22005-06-28 06:58:08775
Junio C Hamanoc62266f2005-07-01 00:13:07776 data = stream.next_in = base_sha1 + 20;
777 stream.avail_in = left - 20;
778 stream.next_out = delta_head;
779 stream.avail_out = sizeof(delta_head);
Junio C Hamano5db47c22005-06-28 06:58:08780
Junio C Hamanoc62266f2005-07-01 00:13:07781 inflateInit(&stream);
782 st = inflate(&stream, Z_FINISH);
783 inflateEnd(&stream);
784 if ((st != Z_STREAM_END) &&
785 stream.total_out != sizeof(delta_head))
786 die("delta data unpack-initial failed");
Junio C Hamano5db47c22005-06-28 06:58:08787
Junio C Hamanoc62266f2005-07-01 00:13:07788 /* Examine the initial part of the delta to figure out
789 * the result size.
790 */
791 data = delta_head;
792 get_delta_hdr_size(&data); /* ignore base size */
793
794 /* Read the result size */
795 result_size = get_delta_hdr_size(&data);
796 *sizep = result_size;
797 }
Junio C Hamano5db47c22005-06-28 06:58:08798 return 0;
799}
800
Linus Torvaldsa733cb62005-06-28 21:21:02801static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
802 enum object_type *type, unsigned long *sizep)
803{
Linus Torvalds01247d82005-06-29 05:15:57804 unsigned shift;
Linus Torvaldsa733cb62005-06-28 21:21:02805 unsigned char *pack, c;
806 unsigned long size;
807
808 if (offset >= p->pack_size)
809 die("object offset outside of pack file");
810
811 pack = p->pack_base + offset;
812 c = *pack++;
813 offset++;
814 *type = (c >> 4) & 7;
815 size = c & 15;
Linus Torvalds01247d82005-06-29 05:15:57816 shift = 4;
Linus Torvaldsa733cb62005-06-28 21:21:02817 while (c & 0x80) {
818 if (offset >= p->pack_size)
819 die("object offset outside of pack file");
820 c = *pack++;
821 offset++;
Linus Torvalds01247d82005-06-29 05:15:57822 size += (c & 0x7f) << shift;
823 shift += 7;
Linus Torvaldsa733cb62005-06-28 21:21:02824 }
825 *sizep = size;
826 return offset;
827}
828
Junio C Hamanoad8c80a2005-07-01 00:17:20829void packed_object_info_detail(struct pack_entry *e,
830 char *type,
831 unsigned long *size,
832 unsigned long *store_size,
833 int *delta_chain_length,
834 unsigned char *base_sha1)
835{
836 struct packed_git *p = e->p;
837 unsigned long offset, left;
838 unsigned char *pack;
839 enum object_type kind;
840
841 offset = unpack_object_header(p, e->offset, &kind, size);
842 pack = p->pack_base + offset;
843 left = p->pack_size - offset;
844 if (kind != OBJ_DELTA)
845 *delta_chain_length = 0;
846 else {
847 int chain_length = 0;
848 memcpy(base_sha1, pack, 20);
849 do {
850 struct pack_entry base_ent;
851 unsigned long junk;
852
853 find_pack_entry_one(pack, &base_ent, p);
854 offset = unpack_object_header(p, base_ent.offset,
855 &kind, &junk);
856 pack = p->pack_base + offset;
857 chain_length++;
858 } while (kind == OBJ_DELTA);
859 *delta_chain_length = chain_length;
860 }
861 switch (kind) {
862 case OBJ_COMMIT:
863 strcpy(type, "commit");
864 break;
865 case OBJ_TREE:
866 strcpy(type, "tree");
867 break;
868 case OBJ_BLOB:
869 strcpy(type, "blob");
870 break;
871 case OBJ_TAG:
872 strcpy(type, "tag");
873 break;
874 default:
Junio C Hamano264b16b2005-09-30 07:09:04875 die("corrupted pack file %s containing object of kind %d",
876 p->pack_name, kind);
Junio C Hamanoad8c80a2005-07-01 00:17:20877 }
878 *store_size = 0; /* notyet */
879}
880
Junio C Hamano1f688552005-06-27 10:35:33881static int packed_object_info(struct pack_entry *entry,
882 char *type, unsigned long *sizep)
883{
884 struct packed_git *p = entry->p;
885 unsigned long offset, size, left;
886 unsigned char *pack;
Linus Torvaldsa733cb62005-06-28 21:21:02887 enum object_type kind;
Junio C Hamanof9253392005-06-29 09:51:27888 int retval;
Junio C Hamano5db47c22005-06-28 06:58:08889
890 if (use_packed_git(p))
891 die("cannot map packed file");
892
Linus Torvaldsa733cb62005-06-28 21:21:02893 offset = unpack_object_header(p, entry->offset, &kind, &size);
Junio C Hamano1f688552005-06-27 10:35:33894 pack = p->pack_base + offset;
Linus Torvaldsa733cb62005-06-28 21:21:02895 left = p->pack_size - offset;
896
897 switch (kind) {
898 case OBJ_DELTA:
Junio C Hamanof3bf9222005-07-01 00:15:39899 retval = packed_delta_info(pack, size, left, type, sizep, p);
Junio C Hamanof9253392005-06-29 09:51:27900 unuse_packed_git(p);
901 return retval;
Linus Torvaldsa733cb62005-06-28 21:21:02902 case OBJ_COMMIT:
Junio C Hamano1f688552005-06-27 10:35:33903 strcpy(type, "commit");
904 break;
Linus Torvaldsa733cb62005-06-28 21:21:02905 case OBJ_TREE:
Junio C Hamano1f688552005-06-27 10:35:33906 strcpy(type, "tree");
907 break;
Linus Torvaldsa733cb62005-06-28 21:21:02908 case OBJ_BLOB:
Junio C Hamano1f688552005-06-27 10:35:33909 strcpy(type, "blob");
910 break;
Linus Torvaldsa733cb62005-06-28 21:21:02911 case OBJ_TAG:
Linus Torvaldsa69d0942005-06-28 16:58:23912 strcpy(type, "tag");
913 break;
Junio C Hamano1f688552005-06-27 10:35:33914 default:
Junio C Hamano264b16b2005-09-30 07:09:04915 die("corrupted pack file %s containing object of kind %d",
916 p->pack_name, kind);
Junio C Hamano1f688552005-06-27 10:35:33917 }
Junio C Hamanoc62266f2005-07-01 00:13:07918 if (sizep)
919 *sizep = size;
Junio C Hamanof9253392005-06-29 09:51:27920 unuse_packed_git(p);
Junio C Hamano1f688552005-06-27 10:35:33921 return 0;
922}
923
924/* forward declaration for a mutually recursive function */
925static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
926
927static void *unpack_delta_entry(unsigned char *base_sha1,
928 unsigned long delta_size,
929 unsigned long left,
930 char *type,
Junio C Hamanof3bf9222005-07-01 00:15:39931 unsigned long *sizep,
932 struct packed_git *p)
Junio C Hamano1f688552005-06-27 10:35:33933{
Junio C Hamanof3bf9222005-07-01 00:15:39934 struct pack_entry base_ent;
Junio C Hamano1f688552005-06-27 10:35:33935 void *data, *delta_data, *result, *base;
936 unsigned long data_size, result_size, base_size;
937 z_stream stream;
938 int st;
939
940 if (left < 20)
941 die("truncated pack file");
942 data = base_sha1 + 20;
943 data_size = left - 20;
944 delta_data = xmalloc(delta_size);
945
946 memset(&stream, 0, sizeof(stream));
947
948 stream.next_in = data;
949 stream.avail_in = data_size;
950 stream.next_out = delta_data;
951 stream.avail_out = delta_size;
952
953 inflateInit(&stream);
954 st = inflate(&stream, Z_FINISH);
955 inflateEnd(&stream);
956 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
957 die("delta data unpack failed");
958
Junio C Hamanof3bf9222005-07-01 00:15:39959 /* The base entry _must_ be in the same pack */
960 if (!find_pack_entry_one(base_sha1, &base_ent, p))
961 die("failed to find delta-pack base object %s",
962 sha1_to_hex(base_sha1));
963 base = unpack_entry_gently(&base_ent, type, &base_size);
Junio C Hamano1f688552005-06-27 10:35:33964 if (!base)
965 die("failed to read delta-pack base object %s",
966 sha1_to_hex(base_sha1));
967 result = patch_delta(base, base_size,
968 delta_data, delta_size,
969 &result_size);
970 if (!result)
971 die("failed to apply delta");
972 free(delta_data);
973 free(base);
974 *sizep = result_size;
975 return result;
976}
977
978static void *unpack_non_delta_entry(unsigned char *data,
979 unsigned long size,
980 unsigned long left)
981{
982 int st;
983 z_stream stream;
Linus Torvalds4d235c82005-07-03 16:58:44984 unsigned char *buffer;
Junio C Hamano1f688552005-06-27 10:35:33985
986 buffer = xmalloc(size + 1);
987 buffer[size] = 0;
988 memset(&stream, 0, sizeof(stream));
989 stream.next_in = data;
990 stream.avail_in = left;
991 stream.next_out = buffer;
992 stream.avail_out = size;
993
994 inflateInit(&stream);
995 st = inflate(&stream, Z_FINISH);
996 inflateEnd(&stream);
997 if ((st != Z_STREAM_END) || stream.total_out != size) {
998 free(buffer);
999 return NULL;
1000 }
1001
1002 return buffer;
1003}
1004
1005static void *unpack_entry(struct pack_entry *entry,
1006 char *type, unsigned long *sizep)
1007{
1008 struct packed_git *p = entry->p;
Junio C Hamanof9253392005-06-29 09:51:271009 void *retval;
Junio C Hamano1f688552005-06-27 10:35:331010
1011 if (use_packed_git(p))
1012 die("cannot map packed file");
Junio C Hamanof3bf9222005-07-01 00:15:391013 retval = unpack_entry_gently(entry, type, sizep);
1014 unuse_packed_git(p);
1015 if (!retval)
Junio C Hamano264b16b2005-09-30 07:09:041016 die("corrupted pack file %s", p->pack_name);
Junio C Hamanof3bf9222005-07-01 00:15:391017 return retval;
1018}
1019
1020/* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1021void *unpack_entry_gently(struct pack_entry *entry,
1022 char *type, unsigned long *sizep)
1023{
1024 struct packed_git *p = entry->p;
1025 unsigned long offset, size, left;
1026 unsigned char *pack;
1027 enum object_type kind;
1028 void *retval;
Junio C Hamano1f688552005-06-27 10:35:331029
Linus Torvaldsa733cb62005-06-28 21:21:021030 offset = unpack_object_header(p, entry->offset, &kind, &size);
Junio C Hamano1f688552005-06-27 10:35:331031 pack = p->pack_base + offset;
Linus Torvaldsa733cb62005-06-28 21:21:021032 left = p->pack_size - offset;
1033 switch (kind) {
1034 case OBJ_DELTA:
Junio C Hamanof3bf9222005-07-01 00:15:391035 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
Junio C Hamanof9253392005-06-29 09:51:271036 return retval;
Linus Torvaldsa733cb62005-06-28 21:21:021037 case OBJ_COMMIT:
Junio C Hamano1f688552005-06-27 10:35:331038 strcpy(type, "commit");
1039 break;
Linus Torvaldsa733cb62005-06-28 21:21:021040 case OBJ_TREE:
Junio C Hamano1f688552005-06-27 10:35:331041 strcpy(type, "tree");
1042 break;
Linus Torvaldsa733cb62005-06-28 21:21:021043 case OBJ_BLOB:
Junio C Hamano1f688552005-06-27 10:35:331044 strcpy(type, "blob");
1045 break;
Linus Torvaldsa733cb62005-06-28 21:21:021046 case OBJ_TAG:
Linus Torvaldsa69d0942005-06-28 16:58:231047 strcpy(type, "tag");
1048 break;
Junio C Hamano1f688552005-06-27 10:35:331049 default:
Junio C Hamanof3bf9222005-07-01 00:15:391050 return NULL;
Junio C Hamano1f688552005-06-27 10:35:331051 }
1052 *sizep = size;
Junio C Hamanof9253392005-06-29 09:51:271053 retval = unpack_non_delta_entry(pack, size, left);
Junio C Hamanof9253392005-06-29 09:51:271054 return retval;
Junio C Hamano1f688552005-06-27 10:35:331055}
1056
Junio C Hamano9a217f22005-06-28 21:56:571057int num_packed_objects(const struct packed_git *p)
1058{
Junio C Hamanof9253392005-06-29 09:51:271059 /* See check_packed_git_idx() */
Junio C Hamano9a217f22005-06-28 21:56:571060 return (p->index_size - 20 - 20 - 4*256) / 24;
1061}
1062
1063int nth_packed_object_sha1(const struct packed_git *p, int n,
1064 unsigned char* sha1)
1065{
1066 void *index = p->index_base + 256;
1067 if (n < 0 || num_packed_objects(p) <= n)
1068 return -1;
1069 memcpy(sha1, (index + 24 * n + 4), 20);
1070 return 0;
1071}
1072
Junio C Hamanof3bf9222005-07-01 00:15:391073int find_pack_entry_one(const unsigned char *sha1,
1074 struct pack_entry *e, struct packed_git *p)
Junio C Hamano1f688552005-06-27 10:35:331075{
Linus Torvalds4d235c82005-07-03 16:58:441076 unsigned int *level1_ofs = p->index_base;
Junio C Hamano1f688552005-06-27 10:35:331077 int hi = ntohl(level1_ofs[*sha1]);
1078 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1079 void *index = p->index_base + 256;
1080
1081 do {
1082 int mi = (lo + hi) / 2;
1083 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
1084 if (!cmp) {
1085 e->offset = ntohl(*((int*)(index + 24 * mi)));
1086 memcpy(e->sha1, sha1, 20);
1087 e->p = p;
1088 return 1;
1089 }
1090 if (cmp > 0)
1091 hi = mi;
1092 else
1093 lo = mi+1;
1094 } while (lo < hi);
1095 return 0;
1096}
1097
1098static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1099{
1100 struct packed_git *p;
1101 prepare_packed_git();
1102
1103 for (p = packed_git; p; p = p->next) {
Junio C Hamanof3bf9222005-07-01 00:15:391104 if (find_pack_entry_one(sha1, e, p))
Junio C Hamano1f688552005-06-27 10:35:331105 return 1;
1106 }
1107 return 0;
1108}
1109
barkalow@iabervon.orgbf592c52005-08-01 00:53:441110struct packed_git *find_sha1_pack(const unsigned char *sha1,
1111 struct packed_git *packs)
1112{
1113 struct packed_git *p;
1114 struct pack_entry e;
1115
1116 for (p = packs; p; p = p->next) {
1117 if (find_pack_entry_one(sha1, &e, p))
1118 return p;
1119 }
1120 return NULL;
1121
1122}
1123
Junio C Hamano36e4d742005-06-27 10:34:061124int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
Junio C Hamano65c2e0c2005-06-02 22:20:541125{
Junio C Hamano36e4d742005-06-27 10:34:061126 int status;
Junio C Hamano65c2e0c2005-06-02 22:20:541127 unsigned long mapsize, size;
1128 void *map;
1129 z_stream stream;
Junio C Hamano36e4d742005-06-27 10:34:061130 char hdr[128];
Junio C Hamano65c2e0c2005-06-02 22:20:541131
Daniel Barkalowd5f1bef2005-07-10 22:27:021132 map = map_sha1_file_internal(sha1, &mapsize);
Junio C Hamano1f688552005-06-27 10:35:331133 if (!map) {
1134 struct pack_entry e;
1135
1136 if (!find_pack_entry(sha1, &e))
1137 return error("unable to find %s", sha1_to_hex(sha1));
Junio C Hamanoc62266f2005-07-01 00:13:071138 return packed_object_info(&e, type, sizep);
Junio C Hamano1f688552005-06-27 10:35:331139 }
Junio C Hamano36e4d742005-06-27 10:34:061140 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1141 status = error("unable to unpack %s header",
1142 sha1_to_hex(sha1));
1143 if (parse_sha1_header(hdr, type, &size) < 0)
1144 status = error("unable to parse %s header", sha1_to_hex(sha1));
Junio C Hamanoc4584ae2005-06-27 10:33:331145 else {
Junio C Hamano65c2e0c2005-06-02 22:20:541146 status = 0;
Junio C Hamanoc62266f2005-07-01 00:13:071147 if (sizep)
1148 *sizep = size;
Junio C Hamano65c2e0c2005-06-02 22:20:541149 }
Junio C Hamano65c2e0c2005-06-02 22:20:541150 inflateEnd(&stream);
1151 munmap(map, mapsize);
1152 return status;
1153}
1154
Junio C Hamano1f688552005-06-27 10:35:331155static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1156{
1157 struct pack_entry e;
1158
1159 if (!find_pack_entry(sha1, &e)) {
1160 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1161 return NULL;
1162 }
1163 return unpack_entry(&e, type, size);
1164}
1165
Linus Torvalds0fcfd162005-04-18 20:04:431166void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1167{
1168 unsigned long mapsize;
1169 void *map, *buf;
Junio C Hamanoab90ea52005-07-11 07:00:551170 struct pack_entry e;
Linus Torvalds0fcfd162005-04-18 20:04:431171
Junio C Hamanoab90ea52005-07-11 07:00:551172 if (find_pack_entry(sha1, &e))
1173 return read_packed_sha1(sha1, type, size);
Daniel Barkalowd5f1bef2005-07-10 22:27:021174 map = map_sha1_file_internal(sha1, &mapsize);
Linus Torvalds0fcfd162005-04-18 20:04:431175 if (map) {
1176 buf = unpack_sha1_file(map, mapsize, type, size);
1177 munmap(map, mapsize);
1178 return buf;
1179 }
Junio C Hamanoab90ea52005-07-11 07:00:551180 return NULL;
Linus Torvalds0fcfd162005-04-18 20:04:431181}
1182
Junio C Hamano40469ee2005-04-28 23:42:271183void *read_object_with_reference(const unsigned char *sha1,
Brian Gerstbf0f9102005-05-18 12:14:091184 const char *required_type,
Junio C Hamano40469ee2005-04-28 23:42:271185 unsigned long *size,
1186 unsigned char *actual_sha1_return)
Junio C Hamanof4913f92005-04-21 01:06:491187{
1188 char type[20];
1189 void *buffer;
1190 unsigned long isize;
Junio C Hamano40469ee2005-04-28 23:42:271191 unsigned char actual_sha1[20];
Junio C Hamanof4913f92005-04-21 01:06:491192
Junio C Hamano40469ee2005-04-28 23:42:271193 memcpy(actual_sha1, sha1, 20);
1194 while (1) {
1195 int ref_length = -1;
1196 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-21 01:06:491197
Junio C Hamano40469ee2005-04-28 23:42:271198 buffer = read_sha1_file(actual_sha1, type, &isize);
1199 if (!buffer)
1200 return NULL;
1201 if (!strcmp(type, required_type)) {
1202 *size = isize;
1203 if (actual_sha1_return)
1204 memcpy(actual_sha1_return, actual_sha1, 20);
1205 return buffer;
1206 }
1207 /* Handle references */
1208 else if (!strcmp(type, "commit"))
1209 ref_type = "tree ";
1210 else if (!strcmp(type, "tag"))
1211 ref_type = "object ";
1212 else {
1213 free(buffer);
1214 return NULL;
1215 }
1216 ref_length = strlen(ref_type);
1217
1218 if (memcmp(buffer, ref_type, ref_length) ||
1219 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1220 free(buffer);
1221 return NULL;
1222 }
Sergey Vlasov1cf58e72005-08-08 18:44:431223 free(buffer);
Junio C Hamano40469ee2005-04-28 23:42:271224 /* Now we have the ID of the referred-to object in
1225 * actual_sha1. Check again. */
Junio C Hamanof4913f92005-04-21 01:06:491226 }
Junio C Hamanof4913f92005-04-21 01:06:491227}
1228
Bryan Larsen7672db22005-07-08 23:51:551229char *write_sha1_file_prepare(void *buf,
1230 unsigned long len,
1231 const char *type,
1232 unsigned char *sha1,
1233 unsigned char *hdr,
1234 int *hdrlen)
Junio C Hamanod410c0f2005-06-28 02:03:131235{
1236 SHA_CTX c;
1237
1238 /* Generate the header */
1239 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1240
1241 /* Sha1.. */
1242 SHA1_Init(&c);
1243 SHA1_Update(&c, hdr, *hdrlen);
1244 SHA1_Update(&c, buf, len);
1245 SHA1_Final(sha1, &c);
1246
1247 return sha1_file_name(sha1);
1248}
1249
Linus Torvalds230f1322005-10-08 22:54:011250/*
1251 * Link the tempfile to the final place, possibly creating the
1252 * last directory level as you do so.
1253 *
1254 * Returns the errno on failure, 0 on success.
1255 */
1256static int link_temp_to_file(const char *tmpfile, char *filename)
1257{
1258 int ret;
1259
1260 if (!link(tmpfile, filename))
1261 return 0;
1262
1263 /*
1264 * Try to mkdir the last path component if that failed
1265 * with an ENOENT.
1266 *
1267 * Re-try the "link()" regardless of whether the mkdir
1268 * succeeds, since a race might mean that somebody
1269 * else succeeded.
1270 */
1271 ret = errno;
1272 if (ret == ENOENT) {
1273 char *dir = strrchr(filename, '/');
1274 if (dir) {
1275 *dir = 0;
1276 mkdir(filename, 0777);
Johannes Schindelin457f06d2005-12-22 22:13:561277 if (adjust_shared_perm(filename))
1278 return -2;
Linus Torvalds230f1322005-10-08 22:54:011279 *dir = '/';
1280 if (!link(tmpfile, filename))
1281 return 0;
1282 ret = errno;
1283 }
1284 }
1285 return ret;
1286}
1287
1288/*
1289 * Move the just written object into its final resting place
1290 */
Junio C Hamanob721e012005-10-11 06:22:011291int move_temp_to_file(const char *tmpfile, char *filename)
Linus Torvalds230f1322005-10-08 22:54:011292{
1293 int ret = link_temp_to_file(tmpfile, filename);
Linus Torvalds7ebb6fc2005-10-26 17:27:361294
1295 /*
1296 * Coda hack - coda doesn't like cross-directory links,
1297 * so we fall back to a rename, which will mean that it
1298 * won't be able to check collisions, but that's not a
1299 * big deal.
1300 *
1301 * The same holds for FAT formatted media.
1302 *
1303 * When this succeeds, we just return 0. We have nothing
1304 * left to unlink.
1305 */
1306 if (ret && ret != EEXIST) {
1307 if (!rename(tmpfile, filename))
Linus Torvalds230f1322005-10-08 22:54:011308 return 0;
Johannes Schindelin9e48b382005-10-25 23:41:201309 ret = errno;
Linus Torvalds230f1322005-10-08 22:54:011310 }
1311 unlink(tmpfile);
1312 if (ret) {
1313 if (ret != EEXIST) {
Alex Riesen7246ed42005-12-15 07:47:301314 fprintf(stderr, "unable to write sha1 filename %s: %s\n", filename, strerror(ret));
Linus Torvalds230f1322005-10-08 22:54:011315 return -1;
1316 }
1317 /* FIXME!!! Collision check here ? */
1318 }
1319
1320 return 0;
1321}
1322
Brian Gerstbf0f9102005-05-18 12:14:091323int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
Linus Torvalds0fcfd162005-04-18 20:04:431324{
1325 int size;
Brian Gerstbf0f9102005-05-18 12:14:091326 unsigned char *compressed;
Linus Torvalds0fcfd162005-04-18 20:04:431327 z_stream stream;
1328 unsigned char sha1[20];
Linus Torvalds706bc532005-04-20 16:28:051329 char *filename;
Linus Torvaldsaac17942005-05-03 18:46:161330 static char tmpfile[PATH_MAX];
Brian Gerstbf0f9102005-05-18 12:14:091331 unsigned char hdr[50];
Linus Torvalds230f1322005-10-08 22:54:011332 int fd, hdrlen;
Linus Torvaldsa44c9a52005-04-25 17:19:531333
Junio C Hamanod410c0f2005-06-28 02:03:131334 /* Normally if we have it in the pack then we do not bother writing
1335 * it out into .git/objects/??/?{38} file.
1336 */
1337 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
Linus Torvalds706bc532005-04-20 16:28:051338 if (returnsha1)
1339 memcpy(returnsha1, sha1, 20);
Junio C Hamanod410c0f2005-06-28 02:03:131340 if (has_sha1_file(sha1))
1341 return 0;
Linus Torvaldsaac17942005-05-03 18:46:161342 fd = open(filename, O_RDONLY);
1343 if (fd >= 0) {
Linus Torvalds706bc532005-04-20 16:28:051344 /*
Linus Torvaldsaac17942005-05-03 18:46:161345 * FIXME!!! We might do collision checking here, but we'd
1346 * need to uncompress the old file and check it. Later.
Linus Torvalds706bc532005-04-20 16:28:051347 */
Linus Torvaldsaac17942005-05-03 18:46:161348 close(fd);
Linus Torvalds706bc532005-04-20 16:28:051349 return 0;
1350 }
1351
Linus Torvaldsaac17942005-05-03 18:46:161352 if (errno != ENOENT) {
Alex Riesen7246ed42005-12-15 07:47:301353 fprintf(stderr, "sha1 file %s: %s\n", filename, strerror(errno));
Linus Torvaldsaac17942005-05-03 18:46:161354 return -1;
1355 }
1356
1357 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
Junio C Hamanoace15342005-05-07 07:38:041358
Linus Torvaldsaac17942005-05-03 18:46:161359 fd = mkstemp(tmpfile);
1360 if (fd < 0) {
Alex Riesen7246ed42005-12-15 07:47:301361 fprintf(stderr, "unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno));
Linus Torvaldsaac17942005-05-03 18:46:161362 return -1;
1363 }
1364
Linus Torvalds0fcfd162005-04-18 20:04:431365 /* Set it up */
1366 memset(&stream, 0, sizeof(stream));
1367 deflateInit(&stream, Z_BEST_COMPRESSION);
Linus Torvaldsa44c9a52005-04-25 17:19:531368 size = deflateBound(&stream, len+hdrlen);
Christopher Li812666c2005-04-26 19:00:581369 compressed = xmalloc(size);
Linus Torvalds0fcfd162005-04-18 20:04:431370
1371 /* Compress it */
Linus Torvalds0fcfd162005-04-18 20:04:431372 stream.next_out = compressed;
1373 stream.avail_out = size;
Linus Torvaldsa44c9a52005-04-25 17:19:531374
1375 /* First header.. */
1376 stream.next_in = hdr;
1377 stream.avail_in = hdrlen;
1378 while (deflate(&stream, 0) == Z_OK)
Thomas Glanzmann6ffcee82005-05-08 09:34:401379 /* nothing */;
Linus Torvaldsa44c9a52005-04-25 17:19:531380
1381 /* Then the data itself.. */
1382 stream.next_in = buf;
1383 stream.avail_in = len;
Linus Torvalds0fcfd162005-04-18 20:04:431384 while (deflate(&stream, Z_FINISH) == Z_OK)
1385 /* nothing */;
1386 deflateEnd(&stream);
1387 size = stream.total_out;
1388
Linus Torvalds706bc532005-04-20 16:28:051389 if (write(fd, compressed, size) != size)
1390 die("unable to write file");
Linus Torvaldsaac17942005-05-03 18:46:161391 fchmod(fd, 0444);
Linus Torvalds706bc532005-04-20 16:28:051392 close(fd);
Junio C Hamano383f85b2005-05-07 08:27:001393 free(compressed);
Linus Torvalds0fcfd162005-04-18 20:04:431394
Linus Torvalds230f1322005-10-08 22:54:011395 return move_temp_to_file(tmpfile, filename);
Linus Torvalds0fcfd162005-04-18 20:04:431396}
Daniel Barkalow8237b182005-04-24 01:47:231397
Daniel Barkalowa5eda522005-07-10 22:25:381398int write_sha1_to_fd(int fd, const unsigned char *sha1)
1399{
1400 ssize_t size;
1401 unsigned long objsize;
1402 int posn = 0;
Sergey Vlasovbfc66da2005-08-08 18:45:361403 void *map = map_sha1_file_internal(sha1, &objsize);
1404 void *buf = map;
1405 void *temp_obj = NULL;
Daniel Barkalowa5eda522005-07-10 22:25:381406 z_stream stream;
Sergey Vlasovbfc66da2005-08-08 18:45:361407
Daniel Barkalowa5eda522005-07-10 22:25:381408 if (!buf) {
1409 unsigned char *unpacked;
1410 unsigned long len;
1411 char type[20];
1412 char hdr[50];
1413 int hdrlen;
1414 // need to unpack and recompress it by itself
1415 unpacked = read_packed_sha1(sha1, type, &len);
1416
1417 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1418
1419 /* Set it up */
1420 memset(&stream, 0, sizeof(stream));
1421 deflateInit(&stream, Z_BEST_COMPRESSION);
1422 size = deflateBound(&stream, len + hdrlen);
Sergey Vlasovbfc66da2005-08-08 18:45:361423 temp_obj = buf = xmalloc(size);
Daniel Barkalowa5eda522005-07-10 22:25:381424
1425 /* Compress it */
1426 stream.next_out = buf;
1427 stream.avail_out = size;
1428
1429 /* First header.. */
Linus Torvalds0ee19dc2005-07-10 22:43:541430 stream.next_in = (void *)hdr;
Daniel Barkalowa5eda522005-07-10 22:25:381431 stream.avail_in = hdrlen;
1432 while (deflate(&stream, 0) == Z_OK)
1433 /* nothing */;
1434
1435 /* Then the data itself.. */
1436 stream.next_in = unpacked;
1437 stream.avail_in = len;
1438 while (deflate(&stream, Z_FINISH) == Z_OK)
1439 /* nothing */;
1440 deflateEnd(&stream);
Sergey Vlasovbfc66da2005-08-08 18:45:361441 free(unpacked);
Daniel Barkalowa5eda522005-07-10 22:25:381442
1443 objsize = stream.total_out;
1444 }
1445
1446 do {
1447 size = write(fd, buf + posn, objsize - posn);
1448 if (size <= 0) {
1449 if (!size) {
Alex Riesen7246ed42005-12-15 07:47:301450 fprintf(stderr, "write closed\n");
Daniel Barkalowa5eda522005-07-10 22:25:381451 } else {
1452 perror("write ");
1453 }
1454 return -1;
1455 }
1456 posn += size;
1457 } while (posn < objsize);
Sergey Vlasovbfc66da2005-08-08 18:45:361458
1459 if (map)
1460 munmap(map, objsize);
1461 if (temp_obj)
1462 free(temp_obj);
1463
Daniel Barkalowa5eda522005-07-10 22:25:381464 return 0;
1465}
1466
barkalow@iabervon.org70b98292005-08-02 23:46:291467int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1468 size_t bufsize, size_t *bufposn)
Daniel Barkalow8237b182005-04-24 01:47:231469{
Linus Torvalds230f1322005-10-08 22:54:011470 char tmpfile[PATH_MAX];
Daniel Barkalow8237b182005-04-24 01:47:231471 int local;
1472 z_stream stream;
1473 unsigned char real_sha1[20];
Brian Gerstbf0f9102005-05-18 12:14:091474 unsigned char discard[4096];
Daniel Barkalow8237b182005-04-24 01:47:231475 int ret;
1476 SHA_CTX c;
1477
Linus Torvalds230f1322005-10-08 22:54:011478 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
Daniel Barkalow8237b182005-04-24 01:47:231479
Linus Torvalds230f1322005-10-08 22:54:011480 local = mkstemp(tmpfile);
Daniel Barkalow8237b182005-04-24 01:47:231481 if (local < 0)
Linus Torvalds230f1322005-10-08 22:54:011482 return error("Couldn't open %s for %s\n", tmpfile, sha1_to_hex(sha1));
Daniel Barkalow8237b182005-04-24 01:47:231483
1484 memset(&stream, 0, sizeof(stream));
1485
1486 inflateInit(&stream);
1487
1488 SHA1_Init(&c);
1489
1490 do {
1491 ssize_t size;
barkalow@iabervon.org70b98292005-08-02 23:46:291492 if (*bufposn) {
1493 stream.avail_in = *bufposn;
Pavel Roskin96ad15a2005-08-09 20:54:401494 stream.next_in = (unsigned char *) buffer;
barkalow@iabervon.org70b98292005-08-02 23:46:291495 do {
1496 stream.next_out = discard;
1497 stream.avail_out = sizeof(discard);
1498 ret = inflate(&stream, Z_SYNC_FLUSH);
1499 SHA1_Update(&c, discard, sizeof(discard) -
1500 stream.avail_out);
1501 } while (stream.avail_in && ret == Z_OK);
1502 write(local, buffer, *bufposn - stream.avail_in);
1503 memmove(buffer, buffer + *bufposn - stream.avail_in,
1504 stream.avail_in);
1505 *bufposn = stream.avail_in;
1506 if (ret != Z_OK)
1507 break;
1508 }
1509 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
Daniel Barkalow8237b182005-04-24 01:47:231510 if (size <= 0) {
1511 close(local);
Linus Torvalds230f1322005-10-08 22:54:011512 unlink(tmpfile);
Daniel Barkalow8237b182005-04-24 01:47:231513 if (!size)
1514 return error("Connection closed?");
1515 perror("Reading from connection");
1516 return -1;
1517 }
barkalow@iabervon.org70b98292005-08-02 23:46:291518 *bufposn += size;
1519 } while (1);
Daniel Barkalow8237b182005-04-24 01:47:231520 inflateEnd(&stream);
1521
1522 close(local);
1523 SHA1_Final(real_sha1, &c);
1524 if (ret != Z_STREAM_END) {
Linus Torvalds230f1322005-10-08 22:54:011525 unlink(tmpfile);
Daniel Barkalow8237b182005-04-24 01:47:231526 return error("File %s corrupted", sha1_to_hex(sha1));
1527 }
1528 if (memcmp(sha1, real_sha1, 20)) {
Linus Torvalds230f1322005-10-08 22:54:011529 unlink(tmpfile);
Daniel Barkalow8237b182005-04-24 01:47:231530 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1531 }
Linus Torvalds230f1322005-10-08 22:54:011532
1533 return move_temp_to_file(tmpfile, sha1_file_name(sha1));
Daniel Barkalow8237b182005-04-24 01:47:231534}
1535
barkalow@iabervon.orgbf592c52005-08-01 00:53:441536int has_pack_index(const unsigned char *sha1)
1537{
1538 struct stat st;
1539 if (stat(sha1_pack_index_name(sha1), &st))
1540 return 0;
1541 return 1;
1542}
1543
1544int has_pack_file(const unsigned char *sha1)
1545{
1546 struct stat st;
1547 if (stat(sha1_pack_name(sha1), &st))
1548 return 0;
1549 return 1;
1550}
1551
Linus Torvaldsdade09c2005-07-03 20:06:361552int has_sha1_pack(const unsigned char *sha1)
1553{
1554 struct pack_entry e;
1555 return find_pack_entry(sha1, &e);
1556}
1557
Daniel Barkalow8237b182005-04-24 01:47:231558int has_sha1_file(const unsigned char *sha1)
1559{
Daniel Barkalow8237b182005-04-24 01:47:231560 struct stat st;
Junio C Hamano1f688552005-06-27 10:35:331561 struct pack_entry e;
1562
Junio C Hamanoab90ea52005-07-11 07:00:551563 if (find_pack_entry(sha1, &e))
Junio C Hamano1f688552005-06-27 10:35:331564 return 1;
Junio C Hamanoab90ea52005-07-11 07:00:551565 return find_sha1_file(sha1, &st) ? 1 : 0;
Daniel Barkalow8237b182005-04-24 01:47:231566}
Junio C Hamano74400e72005-05-02 06:45:491567
Daniel Barkalow024510c2005-12-10 22:25:241568int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
1569{
1570 unsigned long size = 4096;
1571 char *buf = malloc(size);
1572 int iret, ret;
1573 unsigned long off = 0;
1574 unsigned char hdr[50];
1575 int hdrlen;
1576 do {
1577 iret = read(fd, buf + off, size - off);
1578 if (iret > 0) {
1579 off += iret;
1580 if (off == size) {
1581 size *= 2;
1582 buf = realloc(buf, size);
1583 }
1584 }
1585 } while (iret > 0);
1586 if (iret < 0) {
1587 free(buf);
1588 return -1;
1589 }
1590 if (!type)
1591 type = "blob";
1592 if (write_object)
1593 ret = write_sha1_file(buf, off, type, sha1);
1594 else {
1595 write_sha1_file_prepare(buf, off, type, sha1, hdr, &hdrlen);
1596 ret = 0;
1597 }
1598 free(buf);
1599 return ret;
1600}
1601
Bryan Larsen7672db22005-07-08 23:51:551602int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
Junio C Hamano74400e72005-05-02 06:45:491603{
Junio C Hamano74400e72005-05-02 06:45:491604 unsigned long size = st->st_size;
Linus Torvaldsaac17942005-05-03 18:46:161605 void *buf;
1606 int ret;
Bryan Larsen7672db22005-07-08 23:51:551607 unsigned char hdr[50];
1608 int hdrlen;
Junio C Hamano74400e72005-05-02 06:45:491609
Linus Torvaldsaac17942005-05-03 18:46:161610 buf = "";
Junio C Hamano74400e72005-05-02 06:45:491611 if (size)
Linus Torvaldsaac17942005-05-03 18:46:161612 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano74400e72005-05-02 06:45:491613 close(fd);
Pavel Roskine35f9822005-07-29 14:49:141614 if (buf == MAP_FAILED)
Junio C Hamano74400e72005-05-02 06:45:491615 return -1;
1616
Bryan Larsen7672db22005-07-08 23:51:551617 if (!type)
1618 type = "blob";
1619 if (write_object)
1620 ret = write_sha1_file(buf, size, type, sha1);
1621 else {
1622 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1623 ret = 0;
1624 }
Linus Torvaldsaac17942005-05-03 18:46:161625 if (size)
1626 munmap(buf, size);
1627 return ret;
Junio C Hamano74400e72005-05-02 06:45:491628}
Junio C Hamanoec1fcc12005-10-07 10:42:001629
1630int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
1631{
1632 int fd;
1633 char *target;
1634
1635 switch (st->st_mode & S_IFMT) {
1636 case S_IFREG:
1637 fd = open(path, O_RDONLY);
1638 if (fd < 0)
1639 return error("open(\"%s\"): %s", path,
1640 strerror(errno));
1641 if (index_fd(sha1, fd, st, write_object, NULL) < 0)
1642 return error("%s: failed to insert into database",
1643 path);
1644 break;
1645 case S_IFLNK:
1646 target = xmalloc(st->st_size+1);
1647 if (readlink(path, target, st->st_size+1) != st->st_size) {
1648 char *errstr = strerror(errno);
1649 free(target);
1650 return error("readlink(\"%s\"): %s", path,
1651 errstr);
1652 }
1653 if (!write_object) {
1654 unsigned char hdr[50];
1655 int hdrlen;
1656 write_sha1_file_prepare(target, st->st_size, "blob",
1657 sha1, hdr, &hdrlen);
1658 } else if (write_sha1_file(target, st->st_size, "blob", sha1))
1659 return error("%s: failed to insert into database",
1660 path);
1661 free(target);
1662 break;
1663 default:
1664 return error("%s: unsupported file type", path);
1665 }
1666 return 0;
1667}