]>
Commit | Line | Data |
---|---|---|
0fcfd160 LT |
1 | /* |
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 | */ | |
0fcfd160 | 9 | #include "cache.h" |
1f688557 | 10 | #include "delta.h" |
a733cb60 | 11 | #include "pack.h" |
0fcfd160 | 12 | |
144bde78 LT |
13 | #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 | ||
88cd621d JH |
21 | const unsigned char null_sha1[20] = { 0, }; |
22 | ||
144bde78 LT |
23 | static unsigned int sha1_file_open_flag = O_NOATIME; |
24 | ||
0fcfd160 LT |
25 | static 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 | ||
36 | int 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 | ||
457f06d6 JS |
49 | int 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 | ||
b2cb9425 JH |
72 | int safe_create_leading_directories(char *path) |
73 | { | |
74 | char *pos = path; | |
67d42212 JR |
75 | struct stat st; |
76 | ||
67ffdf4c JS |
77 | if (*pos == '/') |
78 | pos++; | |
b2cb9425 JH |
79 | |
80 | while (pos) { | |
81 | pos = strchr(pos, '/'); | |
82 | if (!pos) | |
83 | break; | |
84 | *pos = 0; | |
67d42212 JR |
85 | if (!stat(path, &st)) { |
86 | /* path exists */ | |
87 | if (!S_ISDIR(st.st_mode)) { | |
b2cb9425 | 88 | *pos = '/'; |
67d42212 | 89 | return -3; |
b2cb9425 | 90 | } |
457f06d6 | 91 | } |
67d42212 JR |
92 | else if (mkdir(path, 0777)) { |
93 | *pos = '/'; | |
94 | return -1; | |
95 | } | |
457f06d6 JS |
96 | else if (adjust_shared_perm(path)) { |
97 | *pos = '/'; | |
98 | return -2; | |
99 | } | |
b2cb9425 JH |
100 | *pos++ = '/'; |
101 | } | |
102 | return 0; | |
103 | } | |
723c31fe | 104 | |
0fcfd160 LT |
105 | char * 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 | } | |
1e80e044 JS |
117 | *buf = '\0'; |
118 | ||
0fcfd160 LT |
119 | return buffer; |
120 | } | |
121 | ||
ace1534d JH |
122 | static 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 | ||
0fcfd160 LT |
134 | /* |
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. | |
ace1534d JH |
138 | * |
139 | * Also note that this returns the location for creating. Reading | |
140 | * SHA1 file can happen from any alternate directory listed in the | |
d19938ab | 141 | * DB_ENVIRONMENT environment variable if it is not found in |
ace1534d | 142 | * the primary object database. |
0fcfd160 LT |
143 | */ |
144 | char *sha1_file_name(const unsigned char *sha1) | |
145 | { | |
0fcfd160 LT |
146 | static char *name, *base; |
147 | ||
148 | if (!base) { | |
d19938ab | 149 | const char *sha1_file_directory = get_object_directory(); |
0fcfd160 | 150 | int len = strlen(sha1_file_directory); |
812666c8 | 151 | base = xmalloc(len + 60); |
0fcfd160 LT |
152 | 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 | } | |
ace1534d | 158 | fill_sha1_path(name, sha1); |
0fcfd160 LT |
159 | return base; |
160 | } | |
161 | ||
bf592c50 DB |
162 | char *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 | ||
187 | char *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 | ||
d5a63b99 JH |
212 | struct alternate_object_database *alt_odb_list; |
213 | static struct alternate_object_database **alt_odb_tail; | |
ace1534d | 214 | |
ddd5d056 JH |
215 | /* |
216 | * Prepare alternate object database registry. | |
d5a63b99 JH |
217 | * |
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, | |
1494e038 JH |
222 | * whose contents is similar to that environment variable but can be |
223 | * LF separated. Its base points at a statically allocated buffer that | |
d5a63b99 JH |
224 | * 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. | |
ddd5d056 | 229 | */ |
ccfd3e99 JH |
230 | static void link_alt_odb_entries(const char *alt, const char *ep, int sep, |
231 | const char *relative_base) | |
ace1534d | 232 | { |
ace1534d | 233 | const char *cp, *last; |
d5a63b99 | 234 | struct alternate_object_database *ent; |
1494e038 | 235 | const char *objdir = get_object_directory(); |
ccfd3e99 | 236 | int base_len = -1; |
d5a63b99 JH |
237 | |
238 | last = alt; | |
9577e7e3 JH |
239 | 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++) | |
d5a63b99 JH |
248 | ; |
249 | if (last != cp) { | |
26125f6b | 250 | struct stat st; |
1494e038 | 251 | struct alternate_object_database *alt; |
d5a63b99 JH |
252 | /* 43 = 40-byte + 2 '/' + terminating NUL */ |
253 | int pfxlen = cp - last; | |
254 | int entlen = pfxlen + 43; | |
255 | ||
ccfd3e99 JH |
256 | if (*last != '/' && relative_base) { |
257 | /* Relative alt-odb */ | |
258 | if (base_len < 0) | |
259 | base_len = strlen(relative_base) + 1; | |
260 | entlen += base_len; | |
261 | pfxlen += base_len; | |
262 | } | |
d5a63b99 | 263 | ent = xmalloc(sizeof(*ent) + entlen); |
1494e038 | 264 | |
ccfd3e99 JH |
265 | if (*last != '/' && relative_base) { |
266 | memcpy(ent->base, relative_base, base_len - 1); | |
267 | ent->base[base_len - 1] = '/'; | |
268 | memcpy(ent->base + base_len, | |
269 | last, cp - last); | |
270 | } | |
271 | else | |
272 | memcpy(ent->base, last, pfxlen); | |
26125f6b | 273 | |
d5a63b99 | 274 | ent->name = ent->base + pfxlen + 1; |
26125f6b JH |
275 | ent->base[pfxlen + 3] = '/'; |
276 | ent->base[pfxlen] = ent->base[entlen-1] = 0; | |
277 | ||
278 | /* Detect cases where alternate disappeared */ | |
279 | if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) { | |
280 | error("object directory %s does not exist; " | |
281 | "check .git/objects/info/alternates.", | |
282 | ent->base); | |
283 | goto bad; | |
284 | } | |
285 | ent->base[pfxlen] = '/'; | |
1494e038 JH |
286 | |
287 | /* Prevent the common mistake of listing the same | |
288 | * thing twice, or object directory itself. | |
289 | */ | |
290 | for (alt = alt_odb_list; alt; alt = alt->next) | |
291 | if (!memcmp(ent->base, alt->base, pfxlen)) | |
292 | goto bad; | |
293 | if (!memcmp(ent->base, objdir, pfxlen)) { | |
294 | bad: | |
295 | free(ent); | |
296 | } | |
297 | else { | |
298 | *alt_odb_tail = ent; | |
299 | alt_odb_tail = &(ent->next); | |
300 | ent->next = NULL; | |
301 | } | |
d5a63b99 | 302 | } |
9577e7e3 | 303 | while (cp < ep && *cp == sep) |
d5a63b99 JH |
304 | cp++; |
305 | last = cp; | |
9577e7e3 | 306 | } |
d5a63b99 JH |
307 | } |
308 | ||
309 | void prepare_alt_odb(void) | |
310 | { | |
311 | char path[PATH_MAX]; | |
9577e7e3 | 312 | char *map; |
d5a63b99 JH |
313 | int fd; |
314 | struct stat st; | |
c7c81b3a JR |
315 | char *alt; |
316 | ||
a9ab586a | 317 | alt = getenv(ALTERNATE_DB_ENVIRONMENT); |
c7c81b3a | 318 | if (!alt) alt = ""; |
ace1534d | 319 | |
d5a63b99 JH |
320 | if (alt_odb_tail) |
321 | return; | |
322 | alt_odb_tail = &alt_odb_list; | |
ccfd3e99 | 323 | link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL); |
d5a63b99 | 324 | |
ccfd3e99 | 325 | sprintf(path, "%s/info/alternates", get_object_directory()); |
d5a63b99 JH |
326 | fd = open(path, O_RDONLY); |
327 | if (fd < 0) | |
328 | return; | |
329 | if (fstat(fd, &st) || (st.st_size == 0)) { | |
330 | close(fd); | |
9a217f2a | 331 | return; |
ace1534d | 332 | } |
d5a63b99 JH |
333 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
334 | close(fd); | |
335 | if (map == MAP_FAILED) | |
336 | return; | |
337 | ||
ccfd3e99 JH |
338 | link_alt_odb_entries(map, map + st.st_size, '\n', |
339 | get_object_directory()); | |
d5a63b99 | 340 | munmap(map, st.st_size); |
ace1534d JH |
341 | } |
342 | ||
343 | static char *find_sha1_file(const unsigned char *sha1, struct stat *st) | |
344 | { | |
ace1534d | 345 | char *name = sha1_file_name(sha1); |
d5a63b99 | 346 | struct alternate_object_database *alt; |
ace1534d JH |
347 | |
348 | if (!stat(name, st)) | |
349 | return name; | |
9a217f2a | 350 | prepare_alt_odb(); |
d5a63b99 JH |
351 | for (alt = alt_odb_list; alt; alt = alt->next) { |
352 | name = alt->name; | |
ace1534d | 353 | fill_sha1_path(name, sha1); |
d5a63b99 JH |
354 | if (!stat(alt->base, st)) |
355 | return alt->base; | |
ace1534d JH |
356 | } |
357 | return NULL; | |
358 | } | |
359 | ||
1f688557 JH |
360 | #define PACK_MAX_SZ (1<<26) |
361 | static int pack_used_ctr; | |
362 | static unsigned long pack_mapped; | |
9a217f2a | 363 | struct packed_git *packed_git; |
1f688557 | 364 | |
1f688557 JH |
365 | static int check_packed_git_idx(const char *path, unsigned long *idx_size_, |
366 | void **idx_map_) | |
367 | { | |
368 | void *idx_map; | |
369 | unsigned int *index; | |
370 | unsigned long idx_size; | |
371 | int nr, i; | |
e93ec6f9 | 372 | int fd = open(path, O_RDONLY); |
1f688557 JH |
373 | struct stat st; |
374 | if (fd < 0) | |
375 | return -1; | |
376 | if (fstat(fd, &st)) { | |
377 | close(fd); | |
378 | return -1; | |
379 | } | |
380 | idx_size = st.st_size; | |
381 | idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
382 | close(fd); | |
383 | if (idx_map == MAP_FAILED) | |
384 | return -1; | |
385 | ||
386 | index = idx_map; | |
4d235c80 LT |
387 | *idx_map_ = idx_map; |
388 | *idx_size_ = idx_size; | |
1f688557 JH |
389 | |
390 | /* check index map */ | |
f9253394 | 391 | if (idx_size < 4*256 + 20 + 20) |
1f688557 JH |
392 | return error("index file too small"); |
393 | nr = 0; | |
394 | for (i = 0; i < 256; i++) { | |
395 | unsigned int n = ntohl(index[i]); | |
396 | if (n < nr) | |
397 | return error("non-monotonic index"); | |
398 | nr = n; | |
399 | } | |
400 | ||
401 | /* | |
402 | * Total size: | |
403 | * - 256 index entries 4 bytes each | |
404 | * - 24-byte entries * nr (20-byte sha1 + 4-byte offset) | |
405 | * - 20-byte SHA1 of the packfile | |
406 | * - 20-byte SHA1 file checksum | |
407 | */ | |
408 | if (idx_size != 4*256 + nr * 24 + 20 + 20) | |
409 | return error("wrong index file size"); | |
410 | ||
1f688557 JH |
411 | return 0; |
412 | } | |
413 | ||
f9253394 | 414 | static int unuse_one_packed_git(void) |
1f688557 | 415 | { |
f9253394 JH |
416 | struct packed_git *p, *lru = NULL; |
417 | ||
418 | for (p = packed_git; p; p = p->next) { | |
419 | if (p->pack_use_cnt || !p->pack_base) | |
420 | continue; | |
421 | if (!lru || p->pack_last_used < lru->pack_last_used) | |
422 | lru = p; | |
423 | } | |
424 | if (!lru) | |
425 | return 0; | |
426 | munmap(lru->pack_base, lru->pack_size); | |
427 | lru->pack_base = NULL; | |
428 | return 1; | |
429 | } | |
430 | ||
431 | void unuse_packed_git(struct packed_git *p) | |
432 | { | |
433 | p->pack_use_cnt--; | |
1f688557 JH |
434 | } |
435 | ||
f9253394 | 436 | int use_packed_git(struct packed_git *p) |
1f688557 | 437 | { |
bf592c50 DB |
438 | if (!p->pack_size) { |
439 | struct stat st; | |
440 | // We created the struct before we had the pack | |
441 | stat(p->pack_name, &st); | |
442 | if (!S_ISREG(st.st_mode)) | |
443 | die("packfile %s not a regular file", p->pack_name); | |
444 | p->pack_size = st.st_size; | |
445 | } | |
1f688557 JH |
446 | if (!p->pack_base) { |
447 | int fd; | |
448 | struct stat st; | |
449 | void *map; | |
450 | ||
451 | pack_mapped += p->pack_size; | |
f9253394 JH |
452 | while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git()) |
453 | ; /* nothing */ | |
1f688557 JH |
454 | fd = open(p->pack_name, O_RDONLY); |
455 | if (fd < 0) | |
f9253394 | 456 | die("packfile %s cannot be opened", p->pack_name); |
1f688557 JH |
457 | if (fstat(fd, &st)) { |
458 | close(fd); | |
f9253394 | 459 | die("packfile %s cannot be opened", p->pack_name); |
1f688557 JH |
460 | } |
461 | if (st.st_size != p->pack_size) | |
f9253394 | 462 | die("packfile %s size mismatch.", p->pack_name); |
1f688557 JH |
463 | map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0); |
464 | close(fd); | |
465 | if (map == MAP_FAILED) | |
f9253394 | 466 | die("packfile %s cannot be mapped.", p->pack_name); |
1f688557 | 467 | p->pack_base = map; |
f9253394 JH |
468 | |
469 | /* Check if the pack file matches with the index file. | |
470 | * this is cheap. | |
471 | */ | |
472 | if (memcmp((char*)(p->index_base) + p->index_size - 40, | |
bf592c50 DB |
473 | p->pack_base + p->pack_size - 20, 20)) { |
474 | ||
f9253394 | 475 | die("packfile %s does not match index.", p->pack_name); |
bf592c50 | 476 | } |
1f688557 JH |
477 | } |
478 | p->pack_last_used = pack_used_ctr++; | |
f9253394 | 479 | p->pack_use_cnt++; |
1f688557 JH |
480 | return 0; |
481 | } | |
482 | ||
9d835df2 | 483 | struct packed_git *add_packed_git(char *path, int path_len, int local) |
1f688557 JH |
484 | { |
485 | struct stat st; | |
486 | struct packed_git *p; | |
487 | unsigned long idx_size; | |
488 | void *idx_map; | |
c0bbbb1b | 489 | unsigned char sha1[20]; |
1f688557 JH |
490 | |
491 | if (check_packed_git_idx(path, &idx_size, &idx_map)) | |
492 | return NULL; | |
493 | ||
494 | /* do we have a corresponding .pack file? */ | |
495 | strcpy(path + path_len - 4, ".pack"); | |
496 | if (stat(path, &st) || !S_ISREG(st.st_mode)) { | |
497 | munmap(idx_map, idx_size); | |
498 | return NULL; | |
499 | } | |
500 | /* ok, it looks sane as far as we can check without | |
501 | * actually mapping the pack file. | |
502 | */ | |
503 | p = xmalloc(sizeof(*p) + path_len + 2); | |
504 | strcpy(p->pack_name, path); | |
505 | p->index_size = idx_size; | |
506 | p->pack_size = st.st_size; | |
507 | p->index_base = idx_map; | |
508 | p->next = NULL; | |
d85a4fee | 509 | p->pack_base = NULL; |
1f688557 | 510 | p->pack_last_used = 0; |
f9253394 | 511 | p->pack_use_cnt = 0; |
9d835df2 | 512 | p->pack_local = local; |
f4a11066 | 513 | if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1)) |
c283ab21 | 514 | memcpy(p->sha1, sha1, 20); |
1f688557 JH |
515 | return p; |
516 | } | |
517 | ||
bf592c50 | 518 | struct packed_git *parse_pack_index(unsigned char *sha1) |
c508df5e DB |
519 | { |
520 | char *path = sha1_pack_index_name(sha1); | |
521 | return parse_pack_index_file(sha1, path); | |
522 | } | |
523 | ||
2ab141a2 | 524 | struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path) |
bf592c50 DB |
525 | { |
526 | struct packed_git *p; | |
527 | unsigned long idx_size; | |
528 | void *idx_map; | |
c508df5e | 529 | char *path; |
bf592c50 | 530 | |
c508df5e | 531 | if (check_packed_git_idx(idx_path, &idx_size, &idx_map)) |
bf592c50 DB |
532 | return NULL; |
533 | ||
534 | path = sha1_pack_name(sha1); | |
535 | ||
536 | p = xmalloc(sizeof(*p) + strlen(path) + 2); | |
537 | strcpy(p->pack_name, path); | |
538 | p->index_size = idx_size; | |
539 | p->pack_size = 0; | |
540 | p->index_base = idx_map; | |
541 | p->next = NULL; | |
542 | p->pack_base = NULL; | |
543 | p->pack_last_used = 0; | |
544 | p->pack_use_cnt = 0; | |
545 | memcpy(p->sha1, sha1, 20); | |
546 | return p; | |
547 | } | |
548 | ||
549 | void install_packed_git(struct packed_git *pack) | |
550 | { | |
551 | pack->next = packed_git; | |
552 | packed_git = pack; | |
553 | } | |
554 | ||
9d835df2 | 555 | static void prepare_packed_git_one(char *objdir, int local) |
1f688557 JH |
556 | { |
557 | char path[PATH_MAX]; | |
558 | int len; | |
559 | DIR *dir; | |
560 | struct dirent *de; | |
561 | ||
562 | sprintf(path, "%s/pack", objdir); | |
563 | len = strlen(path); | |
564 | dir = opendir(path); | |
b5b16990 | 565 | if (!dir) { |
26125f6b | 566 | if (errno != ENOENT) |
bd2afde8 | 567 | error("unable to open object pack directory: %s: %s", |
26125f6b | 568 | path, strerror(errno)); |
1f688557 | 569 | return; |
b5b16990 | 570 | } |
1f688557 JH |
571 | path[len++] = '/'; |
572 | while ((de = readdir(dir)) != NULL) { | |
573 | int namelen = strlen(de->d_name); | |
574 | struct packed_git *p; | |
575 | ||
576 | if (strcmp(de->d_name + namelen - 4, ".idx")) | |
577 | continue; | |
578 | ||
579 | /* we have .idx. Is it a file we can map? */ | |
580 | strcpy(path + len, de->d_name); | |
9d835df2 | 581 | p = add_packed_git(path, len + namelen, local); |
1f688557 JH |
582 | if (!p) |
583 | continue; | |
584 | p->next = packed_git; | |
585 | packed_git = p; | |
586 | } | |
5b35bcd5 | 587 | closedir(dir); |
1f688557 JH |
588 | } |
589 | ||
9a217f2a | 590 | void prepare_packed_git(void) |
1f688557 | 591 | { |
1f688557 | 592 | static int run_once = 0; |
d5a63b99 | 593 | struct alternate_object_database *alt; |
1f688557 | 594 | |
d5a63b99 | 595 | if (run_once) |
1f688557 | 596 | return; |
9d835df2 | 597 | prepare_packed_git_one(get_object_directory(), 1); |
9a217f2a | 598 | prepare_alt_odb(); |
d5a63b99 | 599 | for (alt = alt_odb_list; alt; alt = alt->next) { |
1494e038 | 600 | alt->name[-1] = 0; |
9d835df2 | 601 | prepare_packed_git_one(alt->base, 0); |
1494e038 | 602 | alt->name[-1] = '/'; |
1f688557 | 603 | } |
d5a63b99 | 604 | run_once = 1; |
1f688557 JH |
605 | } |
606 | ||
5d6ccf5c | 607 | int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type) |
0fcfd160 | 608 | { |
d98b46f8 | 609 | char header[100]; |
0fcfd160 LT |
610 | unsigned char real_sha1[20]; |
611 | SHA_CTX c; | |
612 | ||
613 | SHA1_Init(&c); | |
d98b46f8 | 614 | SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size)); |
0fcfd160 LT |
615 | SHA1_Update(&c, map, size); |
616 | SHA1_Final(real_sha1, &c); | |
617 | return memcmp(sha1, real_sha1, 20) ? -1 : 0; | |
618 | } | |
619 | ||
1f688557 | 620 | static void *map_sha1_file_internal(const unsigned char *sha1, |
d5f1befc | 621 | unsigned long *size) |
0fcfd160 | 622 | { |
0fcfd160 LT |
623 | struct stat st; |
624 | void *map; | |
144bde78 | 625 | int fd; |
ace1534d JH |
626 | char *filename = find_sha1_file(sha1, &st); |
627 | ||
628 | if (!filename) { | |
ace1534d JH |
629 | return NULL; |
630 | } | |
0fcfd160 | 631 | |
144bde78 | 632 | fd = open(filename, O_RDONLY | sha1_file_open_flag); |
0fcfd160 | 633 | if (fd < 0) { |
144bde78 LT |
634 | /* See if it works without O_NOATIME */ |
635 | switch (sha1_file_open_flag) { | |
636 | default: | |
637 | fd = open(filename, O_RDONLY); | |
638 | if (fd >= 0) | |
639 | break; | |
640 | /* Fallthrough */ | |
641 | case 0: | |
144bde78 LT |
642 | return NULL; |
643 | } | |
644 | ||
1f688557 JH |
645 | /* If it failed once, it will probably fail again. |
646 | * Stop using O_NOATIME | |
647 | */ | |
144bde78 | 648 | sha1_file_open_flag = 0; |
0fcfd160 | 649 | } |
0fcfd160 LT |
650 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
651 | close(fd); | |
e35f9824 | 652 | if (map == MAP_FAILED) |
0fcfd160 LT |
653 | return NULL; |
654 | *size = st.st_size; | |
655 | return map; | |
656 | } | |
657 | ||
c4483576 LT |
658 | int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size) |
659 | { | |
660 | /* Get the data stream */ | |
661 | memset(stream, 0, sizeof(*stream)); | |
662 | stream->next_in = map; | |
663 | stream->avail_in = mapsize; | |
664 | stream->next_out = buffer; | |
665 | stream->avail_out = size; | |
666 | ||
667 | inflateInit(stream); | |
668 | return inflate(stream, 0); | |
669 | } | |
670 | ||
6da4016a | 671 | static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size) |
5180cacc LT |
672 | { |
673 | int bytes = strlen(buffer) + 1; | |
d565b341 | 674 | unsigned char *buf = xmalloc(1+size); |
5180cacc LT |
675 | |
676 | memcpy(buf, buffer + bytes, stream->total_out - bytes); | |
677 | bytes = stream->total_out - bytes; | |
678 | if (bytes < size) { | |
679 | stream->next_out = buf + bytes; | |
680 | stream->avail_out = size - bytes; | |
681 | while (inflate(stream, Z_FINISH) == Z_OK) | |
682 | /* nothing */; | |
683 | } | |
684 | buf[size] = 0; | |
685 | inflateEnd(stream); | |
686 | return buf; | |
687 | } | |
688 | ||
689 | /* | |
690 | * We used to just use "sscanf()", but that's actually way | |
691 | * too permissive for what we want to check. So do an anal | |
692 | * object header parse by hand. | |
693 | */ | |
694 | int parse_sha1_header(char *hdr, char *type, unsigned long *sizep) | |
695 | { | |
696 | int i; | |
697 | unsigned long size; | |
698 | ||
699 | /* | |
700 | * The type can be at most ten bytes (including the | |
701 | * terminating '\0' that we add), and is followed by | |
702 | * a space. | |
703 | */ | |
704 | i = 10; | |
705 | for (;;) { | |
706 | char c = *hdr++; | |
707 | if (c == ' ') | |
708 | break; | |
709 | if (!--i) | |
710 | return -1; | |
711 | *type++ = c; | |
712 | } | |
713 | *type = 0; | |
714 | ||
715 | /* | |
716 | * The length must follow immediately, and be in canonical | |
717 | * decimal format (ie "010" is not valid). | |
718 | */ | |
719 | size = *hdr++ - '0'; | |
720 | if (size > 9) | |
721 | return -1; | |
722 | if (size) { | |
723 | for (;;) { | |
724 | unsigned long c = *hdr - '0'; | |
725 | if (c > 9) | |
726 | break; | |
727 | hdr++; | |
728 | size = size * 10 + c; | |
729 | } | |
730 | } | |
731 | *sizep = size; | |
732 | ||
733 | /* | |
734 | * The length must be followed by a zero byte | |
735 | */ | |
736 | return *hdr ? -1 : 0; | |
737 | } | |
738 | ||
0fcfd160 LT |
739 | void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size) |
740 | { | |
5180cacc | 741 | int ret; |
0fcfd160 | 742 | z_stream stream; |
5180cacc | 743 | char hdr[8192]; |
0fcfd160 | 744 | |
5180cacc LT |
745 | ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)); |
746 | if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0) | |
0fcfd160 LT |
747 | return NULL; |
748 | ||
5180cacc | 749 | return unpack_sha1_rest(&stream, hdr, *size); |
0fcfd160 LT |
750 | } |
751 | ||
f3bf9224 JH |
752 | /* forward declaration for a mutually recursive function */ |
753 | static int packed_object_info(struct pack_entry *entry, | |
754 | char *type, unsigned long *sizep); | |
755 | ||
5db47c2b JH |
756 | static int packed_delta_info(unsigned char *base_sha1, |
757 | unsigned long delta_size, | |
758 | unsigned long left, | |
759 | char *type, | |
f3bf9224 JH |
760 | unsigned long *sizep, |
761 | struct packed_git *p) | |
5db47c2b | 762 | { |
f3bf9224 JH |
763 | struct pack_entry base_ent; |
764 | ||
5db47c2b JH |
765 | if (left < 20) |
766 | die("truncated pack file"); | |
5db47c2b | 767 | |
f3bf9224 JH |
768 | /* The base entry _must_ be in the same pack */ |
769 | if (!find_pack_entry_one(base_sha1, &base_ent, p)) | |
770 | die("failed to find delta-pack base object %s", | |
771 | sha1_to_hex(base_sha1)); | |
772 | ||
c62266f3 JH |
773 | /* We choose to only get the type of the base object and |
774 | * ignore potentially corrupt pack file that expects the delta | |
775 | * based on a base with a wrong size. This saves tons of | |
776 | * inflate() calls. | |
777 | */ | |
5db47c2b | 778 | |
f3bf9224 | 779 | if (packed_object_info(&base_ent, type, NULL)) |
c62266f3 | 780 | die("cannot get info for delta-pack base"); |
5db47c2b | 781 | |
c62266f3 JH |
782 | if (sizep) { |
783 | const unsigned char *data; | |
784 | unsigned char delta_head[64]; | |
785 | unsigned long result_size; | |
786 | z_stream stream; | |
787 | int st; | |
788 | ||
789 | memset(&stream, 0, sizeof(stream)); | |
790 | ||
791 | data = stream.next_in = base_sha1 + 20; | |
792 | stream.avail_in = left - 20; | |
793 | stream.next_out = delta_head; | |
794 | stream.avail_out = sizeof(delta_head); | |
795 | ||
796 | inflateInit(&stream); | |
797 | st = inflate(&stream, Z_FINISH); | |
798 | inflateEnd(&stream); | |
799 | if ((st != Z_STREAM_END) && | |
800 | stream.total_out != sizeof(delta_head)) | |
801 | die("delta data unpack-initial failed"); | |
802 | ||
803 | /* Examine the initial part of the delta to figure out | |
804 | * the result size. | |
805 | */ | |
806 | data = delta_head; | |
807 | get_delta_hdr_size(&data); /* ignore base size */ | |
5db47c2b | 808 | |
c62266f3 JH |
809 | /* Read the result size */ |
810 | result_size = get_delta_hdr_size(&data); | |
811 | *sizep = result_size; | |
812 | } | |
5db47c2b JH |
813 | return 0; |
814 | } | |
815 | ||
a733cb60 LT |
816 | static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset, |
817 | enum object_type *type, unsigned long *sizep) | |
818 | { | |
01247d87 | 819 | unsigned shift; |
a733cb60 LT |
820 | unsigned char *pack, c; |
821 | unsigned long size; | |
822 | ||
823 | if (offset >= p->pack_size) | |
824 | die("object offset outside of pack file"); | |
825 | ||
826 | pack = p->pack_base + offset; | |
827 | c = *pack++; | |
828 | offset++; | |
829 | *type = (c >> 4) & 7; | |
830 | size = c & 15; | |
01247d87 | 831 | shift = 4; |
a733cb60 LT |
832 | while (c & 0x80) { |
833 | if (offset >= p->pack_size) | |
834 | die("object offset outside of pack file"); | |
835 | c = *pack++; | |
836 | offset++; | |
01247d87 LT |
837 | size += (c & 0x7f) << shift; |
838 | shift += 7; | |
a733cb60 LT |
839 | } |
840 | *sizep = size; | |
841 | return offset; | |
842 | } | |
843 | ||
a49dd05f JH |
844 | int check_reuse_pack_delta(struct packed_git *p, unsigned long offset, |
845 | unsigned char *base, unsigned long *sizep, | |
846 | enum object_type *kindp) | |
847 | { | |
848 | unsigned long ptr; | |
849 | int status = -1; | |
850 | ||
851 | use_packed_git(p); | |
852 | ptr = offset; | |
853 | ptr = unpack_object_header(p, ptr, kindp, sizep); | |
854 | if (*kindp != OBJ_DELTA) | |
855 | goto done; | |
856 | memcpy(base, p->pack_base + ptr, 20); | |
857 | status = 0; | |
858 | done: | |
859 | unuse_packed_git(p); | |
860 | return status; | |
861 | } | |
862 | ||
ad8c80a5 JH |
863 | void packed_object_info_detail(struct pack_entry *e, |
864 | char *type, | |
865 | unsigned long *size, | |
866 | unsigned long *store_size, | |
f8f135c9 | 867 | unsigned int *delta_chain_length, |
ad8c80a5 JH |
868 | unsigned char *base_sha1) |
869 | { | |
870 | struct packed_git *p = e->p; | |
871 | unsigned long offset, left; | |
872 | unsigned char *pack; | |
873 | enum object_type kind; | |
874 | ||
875 | offset = unpack_object_header(p, e->offset, &kind, size); | |
876 | pack = p->pack_base + offset; | |
877 | left = p->pack_size - offset; | |
878 | if (kind != OBJ_DELTA) | |
879 | *delta_chain_length = 0; | |
880 | else { | |
f8f135c9 | 881 | unsigned int chain_length = 0; |
ad8c80a5 JH |
882 | memcpy(base_sha1, pack, 20); |
883 | do { | |
884 | struct pack_entry base_ent; | |
885 | unsigned long junk; | |
886 | ||
887 | find_pack_entry_one(pack, &base_ent, p); | |
888 | offset = unpack_object_header(p, base_ent.offset, | |
889 | &kind, &junk); | |
890 | pack = p->pack_base + offset; | |
891 | chain_length++; | |
892 | } while (kind == OBJ_DELTA); | |
893 | *delta_chain_length = chain_length; | |
894 | } | |
895 | switch (kind) { | |
896 | case OBJ_COMMIT: | |
897 | strcpy(type, "commit"); | |
898 | break; | |
899 | case OBJ_TREE: | |
900 | strcpy(type, "tree"); | |
901 | break; | |
902 | case OBJ_BLOB: | |
903 | strcpy(type, "blob"); | |
904 | break; | |
905 | case OBJ_TAG: | |
906 | strcpy(type, "tag"); | |
907 | break; | |
908 | default: | |
264b16b6 JH |
909 | die("corrupted pack file %s containing object of kind %d", |
910 | p->pack_name, kind); | |
ad8c80a5 JH |
911 | } |
912 | *store_size = 0; /* notyet */ | |
913 | } | |
914 | ||
1f688557 JH |
915 | static int packed_object_info(struct pack_entry *entry, |
916 | char *type, unsigned long *sizep) | |
917 | { | |
918 | struct packed_git *p = entry->p; | |
919 | unsigned long offset, size, left; | |
920 | unsigned char *pack; | |
a733cb60 | 921 | enum object_type kind; |
f9253394 | 922 | int retval; |
5db47c2b JH |
923 | |
924 | if (use_packed_git(p)) | |
925 | die("cannot map packed file"); | |
926 | ||
a733cb60 | 927 | offset = unpack_object_header(p, entry->offset, &kind, &size); |
1f688557 | 928 | pack = p->pack_base + offset; |
a733cb60 LT |
929 | left = p->pack_size - offset; |
930 | ||
931 | switch (kind) { | |
932 | case OBJ_DELTA: | |
f3bf9224 | 933 | retval = packed_delta_info(pack, size, left, type, sizep, p); |
f9253394 JH |
934 | unuse_packed_git(p); |
935 | return retval; | |
a733cb60 | 936 | case OBJ_COMMIT: |
1f688557 JH |
937 | strcpy(type, "commit"); |
938 | break; | |
a733cb60 | 939 | case OBJ_TREE: |
1f688557 JH |
940 | strcpy(type, "tree"); |
941 | break; | |
a733cb60 | 942 | case OBJ_BLOB: |
1f688557 JH |
943 | strcpy(type, "blob"); |
944 | break; | |
a733cb60 | 945 | case OBJ_TAG: |
a69d0943 LT |
946 | strcpy(type, "tag"); |
947 | break; | |
1f688557 | 948 | default: |
264b16b6 JH |
949 | die("corrupted pack file %s containing object of kind %d", |
950 | p->pack_name, kind); | |
1f688557 | 951 | } |
c62266f3 JH |
952 | if (sizep) |
953 | *sizep = size; | |
f9253394 | 954 | unuse_packed_git(p); |
1f688557 JH |
955 | return 0; |
956 | } | |
957 | ||
958 | /* forward declaration for a mutually recursive function */ | |
959 | static void *unpack_entry(struct pack_entry *, char *, unsigned long *); | |
960 | ||
961 | static void *unpack_delta_entry(unsigned char *base_sha1, | |
962 | unsigned long delta_size, | |
963 | unsigned long left, | |
964 | char *type, | |
f3bf9224 JH |
965 | unsigned long *sizep, |
966 | struct packed_git *p) | |
1f688557 | 967 | { |
f3bf9224 | 968 | struct pack_entry base_ent; |
1f688557 JH |
969 | void *data, *delta_data, *result, *base; |
970 | unsigned long data_size, result_size, base_size; | |
971 | z_stream stream; | |
972 | int st; | |
973 | ||
974 | if (left < 20) | |
975 | die("truncated pack file"); | |
976 | data = base_sha1 + 20; | |
977 | data_size = left - 20; | |
978 | delta_data = xmalloc(delta_size); | |
979 | ||
980 | memset(&stream, 0, sizeof(stream)); | |
981 | ||
982 | stream.next_in = data; | |
983 | stream.avail_in = data_size; | |
984 | stream.next_out = delta_data; | |
985 | stream.avail_out = delta_size; | |
986 | ||
987 | inflateInit(&stream); | |
988 | st = inflate(&stream, Z_FINISH); | |
989 | inflateEnd(&stream); | |
990 | if ((st != Z_STREAM_END) || stream.total_out != delta_size) | |
991 | die("delta data unpack failed"); | |
992 | ||
f3bf9224 JH |
993 | /* The base entry _must_ be in the same pack */ |
994 | if (!find_pack_entry_one(base_sha1, &base_ent, p)) | |
995 | die("failed to find delta-pack base object %s", | |
996 | sha1_to_hex(base_sha1)); | |
997 | base = unpack_entry_gently(&base_ent, type, &base_size); | |
1f688557 JH |
998 | if (!base) |
999 | die("failed to read delta-pack base object %s", | |
1000 | sha1_to_hex(base_sha1)); | |
1001 | result = patch_delta(base, base_size, | |
1002 | delta_data, delta_size, | |
1003 | &result_size); | |
1004 | if (!result) | |
1005 | die("failed to apply delta"); | |
1006 | free(delta_data); | |
1007 | free(base); | |
1008 | *sizep = result_size; | |
1009 | return result; | |
1010 | } | |
1011 | ||
1012 | static void *unpack_non_delta_entry(unsigned char *data, | |
1013 | unsigned long size, | |
1014 | unsigned long left) | |
1015 | { | |
1016 | int st; | |
1017 | z_stream stream; | |
4d235c80 | 1018 | unsigned char *buffer; |
1f688557 JH |
1019 | |
1020 | buffer = xmalloc(size + 1); | |
1021 | buffer[size] = 0; | |
1022 | memset(&stream, 0, sizeof(stream)); | |
1023 | stream.next_in = data; | |
1024 | stream.avail_in = left; | |
1025 | stream.next_out = buffer; | |
1026 | stream.avail_out = size; | |
1027 | ||
1028 | inflateInit(&stream); | |
1029 | st = inflate(&stream, Z_FINISH); | |
1030 | inflateEnd(&stream); | |
1031 | if ((st != Z_STREAM_END) || stream.total_out != size) { | |
1032 | free(buffer); | |
1033 | return NULL; | |
1034 | } | |
1035 | ||
1036 | return buffer; | |
1037 | } | |
1038 | ||
1039 | static void *unpack_entry(struct pack_entry *entry, | |
1040 | char *type, unsigned long *sizep) | |
1041 | { | |
1042 | struct packed_git *p = entry->p; | |
f9253394 | 1043 | void *retval; |
1f688557 JH |
1044 | |
1045 | if (use_packed_git(p)) | |
1046 | die("cannot map packed file"); | |
f3bf9224 JH |
1047 | retval = unpack_entry_gently(entry, type, sizep); |
1048 | unuse_packed_git(p); | |
1049 | if (!retval) | |
264b16b6 | 1050 | die("corrupted pack file %s", p->pack_name); |
f3bf9224 JH |
1051 | return retval; |
1052 | } | |
1053 | ||
1054 | /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */ | |
1055 | void *unpack_entry_gently(struct pack_entry *entry, | |
1056 | char *type, unsigned long *sizep) | |
1057 | { | |
1058 | struct packed_git *p = entry->p; | |
1059 | unsigned long offset, size, left; | |
1060 | unsigned char *pack; | |
1061 | enum object_type kind; | |
1062 | void *retval; | |
1f688557 | 1063 | |
a733cb60 | 1064 | offset = unpack_object_header(p, entry->offset, &kind, &size); |
1f688557 | 1065 | pack = p->pack_base + offset; |
a733cb60 LT |
1066 | left = p->pack_size - offset; |
1067 | switch (kind) { | |
1068 | case OBJ_DELTA: | |
f3bf9224 | 1069 | retval = unpack_delta_entry(pack, size, left, type, sizep, p); |
f9253394 | 1070 | return retval; |
a733cb60 | 1071 | case OBJ_COMMIT: |
1f688557 JH |
1072 | strcpy(type, "commit"); |
1073 | break; | |
a733cb60 | 1074 | case OBJ_TREE: |
1f688557 JH |
1075 | strcpy(type, "tree"); |
1076 | break; | |
a733cb60 | 1077 | case OBJ_BLOB: |
1f688557 JH |
1078 | strcpy(type, "blob"); |
1079 | break; | |
a733cb60 | 1080 | case OBJ_TAG: |
a69d0943 LT |
1081 | strcpy(type, "tag"); |
1082 | break; | |
1f688557 | 1083 | default: |
f3bf9224 | 1084 | return NULL; |
1f688557 JH |
1085 | } |
1086 | *sizep = size; | |
f9253394 | 1087 | retval = unpack_non_delta_entry(pack, size, left); |
f9253394 | 1088 | return retval; |
1f688557 JH |
1089 | } |
1090 | ||
9a217f2a JH |
1091 | int num_packed_objects(const struct packed_git *p) |
1092 | { | |
f9253394 | 1093 | /* See check_packed_git_idx() */ |
9a217f2a JH |
1094 | return (p->index_size - 20 - 20 - 4*256) / 24; |
1095 | } | |
1096 | ||
1097 | int nth_packed_object_sha1(const struct packed_git *p, int n, | |
1098 | unsigned char* sha1) | |
1099 | { | |
1100 | void *index = p->index_base + 256; | |
1101 | if (n < 0 || num_packed_objects(p) <= n) | |
1102 | return -1; | |
1103 | memcpy(sha1, (index + 24 * n + 4), 20); | |
1104 | return 0; | |
1105 | } | |
1106 | ||
f3bf9224 JH |
1107 | int find_pack_entry_one(const unsigned char *sha1, |
1108 | struct pack_entry *e, struct packed_git *p) | |
1f688557 | 1109 | { |
4d235c80 | 1110 | unsigned int *level1_ofs = p->index_base; |
1f688557 JH |
1111 | int hi = ntohl(level1_ofs[*sha1]); |
1112 | int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1])); | |
1113 | void *index = p->index_base + 256; | |
1114 | ||
1115 | do { | |
1116 | int mi = (lo + hi) / 2; | |
1117 | int cmp = memcmp(index + 24 * mi + 4, sha1, 20); | |
1118 | if (!cmp) { | |
1119 | e->offset = ntohl(*((int*)(index + 24 * mi))); | |
1120 | memcpy(e->sha1, sha1, 20); | |
1121 | e->p = p; | |
1122 | return 1; | |
1123 | } | |
1124 | if (cmp > 0) | |
1125 | hi = mi; | |
1126 | else | |
1127 | lo = mi+1; | |
1128 | } while (lo < hi); | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e) | |
1133 | { | |
1134 | struct packed_git *p; | |
1135 | prepare_packed_git(); | |
1136 | ||
1137 | for (p = packed_git; p; p = p->next) { | |
f3bf9224 | 1138 | if (find_pack_entry_one(sha1, e, p)) |
1f688557 JH |
1139 | return 1; |
1140 | } | |
1141 | return 0; | |
1142 | } | |
1143 | ||
bf592c50 DB |
1144 | struct packed_git *find_sha1_pack(const unsigned char *sha1, |
1145 | struct packed_git *packs) | |
1146 | { | |
1147 | struct packed_git *p; | |
1148 | struct pack_entry e; | |
1149 | ||
1150 | for (p = packs; p; p = p->next) { | |
1151 | if (find_pack_entry_one(sha1, &e, p)) | |
1152 | return p; | |
1153 | } | |
1154 | return NULL; | |
1155 | ||
1156 | } | |
1157 | ||
36e4d74a | 1158 | int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep) |
65c2e0c3 | 1159 | { |
36e4d74a | 1160 | int status; |
65c2e0c3 JH |
1161 | unsigned long mapsize, size; |
1162 | void *map; | |
1163 | z_stream stream; | |
36e4d74a | 1164 | char hdr[128]; |
65c2e0c3 | 1165 | |
d5f1befc | 1166 | map = map_sha1_file_internal(sha1, &mapsize); |
1f688557 JH |
1167 | if (!map) { |
1168 | struct pack_entry e; | |
1169 | ||
1170 | if (!find_pack_entry(sha1, &e)) | |
1171 | return error("unable to find %s", sha1_to_hex(sha1)); | |
c62266f3 | 1172 | return packed_object_info(&e, type, sizep); |
1f688557 | 1173 | } |
36e4d74a JH |
1174 | if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) |
1175 | status = error("unable to unpack %s header", | |
1176 | sha1_to_hex(sha1)); | |
1177 | if (parse_sha1_header(hdr, type, &size) < 0) | |
1178 | status = error("unable to parse %s header", sha1_to_hex(sha1)); | |
c4584ae3 | 1179 | else { |
65c2e0c3 | 1180 | status = 0; |
c62266f3 JH |
1181 | if (sizep) |
1182 | *sizep = size; | |
65c2e0c3 | 1183 | } |
65c2e0c3 JH |
1184 | inflateEnd(&stream); |
1185 | munmap(map, mapsize); | |
1186 | return status; | |
1187 | } | |
1188 | ||
1f688557 JH |
1189 | static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size) |
1190 | { | |
1191 | struct pack_entry e; | |
1192 | ||
1193 | if (!find_pack_entry(sha1, &e)) { | |
1194 | error("cannot read sha1_file for %s", sha1_to_hex(sha1)); | |
1195 | return NULL; | |
1196 | } | |
1197 | return unpack_entry(&e, type, size); | |
1198 | } | |
1199 | ||
0fcfd160 LT |
1200 | void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size) |
1201 | { | |
1202 | unsigned long mapsize; | |
1203 | void *map, *buf; | |
ab90ea5d | 1204 | struct pack_entry e; |
0fcfd160 | 1205 | |
ab90ea5d JH |
1206 | if (find_pack_entry(sha1, &e)) |
1207 | return read_packed_sha1(sha1, type, size); | |
d5f1befc | 1208 | map = map_sha1_file_internal(sha1, &mapsize); |
0fcfd160 LT |
1209 | if (map) { |
1210 | buf = unpack_sha1_file(map, mapsize, type, size); | |
1211 | munmap(map, mapsize); | |
1212 | return buf; | |
1213 | } | |
ab90ea5d | 1214 | return NULL; |
0fcfd160 LT |
1215 | } |
1216 | ||
40469ee9 | 1217 | void *read_object_with_reference(const unsigned char *sha1, |
bf0f910d | 1218 | const char *required_type, |
40469ee9 JH |
1219 | unsigned long *size, |
1220 | unsigned char *actual_sha1_return) | |
f4913f91 JH |
1221 | { |
1222 | char type[20]; | |
1223 | void *buffer; | |
1224 | unsigned long isize; | |
40469ee9 | 1225 | unsigned char actual_sha1[20]; |
f4913f91 | 1226 | |
40469ee9 JH |
1227 | memcpy(actual_sha1, sha1, 20); |
1228 | while (1) { | |
1229 | int ref_length = -1; | |
1230 | const char *ref_type = NULL; | |
f4913f91 | 1231 | |
40469ee9 JH |
1232 | buffer = read_sha1_file(actual_sha1, type, &isize); |
1233 | if (!buffer) | |
1234 | return NULL; | |
1235 | if (!strcmp(type, required_type)) { | |
1236 | *size = isize; | |
1237 | if (actual_sha1_return) | |
1238 | memcpy(actual_sha1_return, actual_sha1, 20); | |
1239 | return buffer; | |
1240 | } | |
1241 | /* Handle references */ | |
1242 | else if (!strcmp(type, "commit")) | |
1243 | ref_type = "tree "; | |
1244 | else if (!strcmp(type, "tag")) | |
1245 | ref_type = "object "; | |
1246 | else { | |
1247 | free(buffer); | |
1248 | return NULL; | |
1249 | } | |
1250 | ref_length = strlen(ref_type); | |
f4913f91 | 1251 | |
40469ee9 JH |
1252 | if (memcmp(buffer, ref_type, ref_length) || |
1253 | get_sha1_hex(buffer + ref_length, actual_sha1)) { | |
1254 | free(buffer); | |
1255 | return NULL; | |
1256 | } | |
1cf58e72 | 1257 | free(buffer); |
40469ee9 JH |
1258 | /* Now we have the ID of the referred-to object in |
1259 | * actual_sha1. Check again. */ | |
f4913f91 | 1260 | } |
f4913f91 JH |
1261 | } |
1262 | ||
7672db20 BL |
1263 | char *write_sha1_file_prepare(void *buf, |
1264 | unsigned long len, | |
1265 | const char *type, | |
1266 | unsigned char *sha1, | |
1267 | unsigned char *hdr, | |
1268 | int *hdrlen) | |
d410c0f5 JH |
1269 | { |
1270 | SHA_CTX c; | |
1271 | ||
1272 | /* Generate the header */ | |
1273 | *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1; | |
1274 | ||
1275 | /* Sha1.. */ | |
1276 | SHA1_Init(&c); | |
1277 | SHA1_Update(&c, hdr, *hdrlen); | |
1278 | SHA1_Update(&c, buf, len); | |
1279 | SHA1_Final(sha1, &c); | |
1280 | ||
1281 | return sha1_file_name(sha1); | |
1282 | } | |
1283 | ||
230f1322 LT |
1284 | /* |
1285 | * Link the tempfile to the final place, possibly creating the | |
1286 | * last directory level as you do so. | |
1287 | * | |
1288 | * Returns the errno on failure, 0 on success. | |
1289 | */ | |
1290 | static int link_temp_to_file(const char *tmpfile, char *filename) | |
1291 | { | |
1292 | int ret; | |
1293 | ||
1294 | if (!link(tmpfile, filename)) | |
1295 | return 0; | |
1296 | ||
1297 | /* | |
1298 | * Try to mkdir the last path component if that failed | |
1299 | * with an ENOENT. | |
1300 | * | |
1301 | * Re-try the "link()" regardless of whether the mkdir | |
1302 | * succeeds, since a race might mean that somebody | |
1303 | * else succeeded. | |
1304 | */ | |
1305 | ret = errno; | |
1306 | if (ret == ENOENT) { | |
1307 | char *dir = strrchr(filename, '/'); | |
1308 | if (dir) { | |
1309 | *dir = 0; | |
1310 | mkdir(filename, 0777); | |
457f06d6 JS |
1311 | if (adjust_shared_perm(filename)) |
1312 | return -2; | |
230f1322 LT |
1313 | *dir = '/'; |
1314 | if (!link(tmpfile, filename)) | |
1315 | return 0; | |
1316 | ret = errno; | |
1317 | } | |
1318 | } | |
1319 | return ret; | |
1320 | } | |
1321 | ||
1322 | /* | |
1323 | * Move the just written object into its final resting place | |
1324 | */ | |
b721e01f | 1325 | int move_temp_to_file(const char *tmpfile, char *filename) |
230f1322 LT |
1326 | { |
1327 | int ret = link_temp_to_file(tmpfile, filename); | |
7ebb6fca LT |
1328 | |
1329 | /* | |
1330 | * Coda hack - coda doesn't like cross-directory links, | |
1331 | * so we fall back to a rename, which will mean that it | |
1332 | * won't be able to check collisions, but that's not a | |
1333 | * big deal. | |
1334 | * | |
1335 | * The same holds for FAT formatted media. | |
1336 | * | |
1337 | * When this succeeds, we just return 0. We have nothing | |
1338 | * left to unlink. | |
1339 | */ | |
1340 | if (ret && ret != EEXIST) { | |
1341 | if (!rename(tmpfile, filename)) | |
230f1322 | 1342 | return 0; |
9e48b389 | 1343 | ret = errno; |
230f1322 LT |
1344 | } |
1345 | unlink(tmpfile); | |
1346 | if (ret) { | |
1347 | if (ret != EEXIST) { | |
7246ed43 | 1348 | fprintf(stderr, "unable to write sha1 filename %s: %s\n", filename, strerror(ret)); |
230f1322 LT |
1349 | return -1; |
1350 | } | |
1351 | /* FIXME!!! Collision check here ? */ | |
1352 | } | |
1353 | ||
1354 | return 0; | |
1355 | } | |
1356 | ||
bf0f910d | 1357 | int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1) |
0fcfd160 LT |
1358 | { |
1359 | int size; | |
bf0f910d | 1360 | unsigned char *compressed; |
0fcfd160 LT |
1361 | z_stream stream; |
1362 | unsigned char sha1[20]; | |
706bc531 | 1363 | char *filename; |
aac17941 | 1364 | static char tmpfile[PATH_MAX]; |
bf0f910d | 1365 | unsigned char hdr[50]; |
230f1322 | 1366 | int fd, hdrlen; |
a44c9a5e | 1367 | |
d410c0f5 JH |
1368 | /* Normally if we have it in the pack then we do not bother writing |
1369 | * it out into .git/objects/??/?{38} file. | |
1370 | */ | |
1371 | filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen); | |
706bc531 LT |
1372 | if (returnsha1) |
1373 | memcpy(returnsha1, sha1, 20); | |
d410c0f5 JH |
1374 | if (has_sha1_file(sha1)) |
1375 | return 0; | |
aac17941 LT |
1376 | fd = open(filename, O_RDONLY); |
1377 | if (fd >= 0) { | |
706bc531 | 1378 | /* |
aac17941 LT |
1379 | * FIXME!!! We might do collision checking here, but we'd |
1380 | * need to uncompress the old file and check it. Later. | |
706bc531 | 1381 | */ |
aac17941 | 1382 | close(fd); |
706bc531 LT |
1383 | return 0; |
1384 | } | |
1385 | ||
aac17941 | 1386 | if (errno != ENOENT) { |
7246ed43 | 1387 | fprintf(stderr, "sha1 file %s: %s\n", filename, strerror(errno)); |
aac17941 LT |
1388 | return -1; |
1389 | } | |
1390 | ||
1391 | snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory()); | |
ace1534d | 1392 | |
aac17941 LT |
1393 | fd = mkstemp(tmpfile); |
1394 | if (fd < 0) { | |
7246ed43 | 1395 | fprintf(stderr, "unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno)); |
aac17941 LT |
1396 | return -1; |
1397 | } | |
1398 | ||
0fcfd160 LT |
1399 | /* Set it up */ |
1400 | memset(&stream, 0, sizeof(stream)); | |
1401 | deflateInit(&stream, Z_BEST_COMPRESSION); | |
a44c9a5e | 1402 | size = deflateBound(&stream, len+hdrlen); |
812666c8 | 1403 | compressed = xmalloc(size); |
0fcfd160 LT |
1404 | |
1405 | /* Compress it */ | |
0fcfd160 LT |
1406 | stream.next_out = compressed; |
1407 | stream.avail_out = size; | |
a44c9a5e LT |
1408 | |
1409 | /* First header.. */ | |
1410 | stream.next_in = hdr; | |
1411 | stream.avail_in = hdrlen; | |
1412 | while (deflate(&stream, 0) == Z_OK) | |
6ffcee88 | 1413 | /* nothing */; |
a44c9a5e LT |
1414 | |
1415 | /* Then the data itself.. */ | |
1416 | stream.next_in = buf; | |
1417 | stream.avail_in = len; | |
0fcfd160 LT |
1418 | while (deflate(&stream, Z_FINISH) == Z_OK) |
1419 | /* nothing */; | |
1420 | deflateEnd(&stream); | |
1421 | size = stream.total_out; | |
1422 | ||
706bc531 LT |
1423 | if (write(fd, compressed, size) != size) |
1424 | die("unable to write file"); | |
aac17941 | 1425 | fchmod(fd, 0444); |
706bc531 | 1426 | close(fd); |
383f85b7 | 1427 | free(compressed); |
0fcfd160 | 1428 | |
230f1322 | 1429 | return move_temp_to_file(tmpfile, filename); |
0fcfd160 | 1430 | } |
8237b185 | 1431 | |
a5eda52b DB |
1432 | int write_sha1_to_fd(int fd, const unsigned char *sha1) |
1433 | { | |
1434 | ssize_t size; | |
1435 | unsigned long objsize; | |
1436 | int posn = 0; | |
bfc66daf SV |
1437 | void *map = map_sha1_file_internal(sha1, &objsize); |
1438 | void *buf = map; | |
1439 | void *temp_obj = NULL; | |
a5eda52b | 1440 | z_stream stream; |
bfc66daf | 1441 | |
a5eda52b DB |
1442 | if (!buf) { |
1443 | unsigned char *unpacked; | |
1444 | unsigned long len; | |
1445 | char type[20]; | |
1446 | char hdr[50]; | |
1447 | int hdrlen; | |
1448 | // need to unpack and recompress it by itself | |
1449 | unpacked = read_packed_sha1(sha1, type, &len); | |
1450 | ||
1451 | hdrlen = sprintf(hdr, "%s %lu", type, len) + 1; | |
1452 | ||
1453 | /* Set it up */ | |
1454 | memset(&stream, 0, sizeof(stream)); | |
1455 | deflateInit(&stream, Z_BEST_COMPRESSION); | |
1456 | size = deflateBound(&stream, len + hdrlen); | |
bfc66daf | 1457 | temp_obj = buf = xmalloc(size); |
a5eda52b DB |
1458 | |
1459 | /* Compress it */ | |
1460 | stream.next_out = buf; | |
1461 | stream.avail_out = size; | |
1462 | ||
1463 | /* First header.. */ | |
0ee19dce | 1464 | stream.next_in = (void *)hdr; |
a5eda52b DB |
1465 | stream.avail_in = hdrlen; |
1466 | while (deflate(&stream, 0) == Z_OK) | |
1467 | /* nothing */; | |
1468 | ||
1469 | /* Then the data itself.. */ | |
1470 | stream.next_in = unpacked; | |
1471 | stream.avail_in = len; | |
1472 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
1473 | /* nothing */; | |
1474 | deflateEnd(&stream); | |
bfc66daf | 1475 | free(unpacked); |
a5eda52b DB |
1476 | |
1477 | objsize = stream.total_out; | |
1478 | } | |
1479 | ||
1480 | do { | |
1481 | size = write(fd, buf + posn, objsize - posn); | |
1482 | if (size <= 0) { | |
1483 | if (!size) { | |
7246ed43 | 1484 | fprintf(stderr, "write closed\n"); |
a5eda52b DB |
1485 | } else { |
1486 | perror("write "); | |
1487 | } | |
1488 | return -1; | |
1489 | } | |
1490 | posn += size; | |
1491 | } while (posn < objsize); | |
bfc66daf SV |
1492 | |
1493 | if (map) | |
1494 | munmap(map, objsize); | |
1495 | if (temp_obj) | |
1496 | free(temp_obj); | |
1497 | ||
a5eda52b DB |
1498 | return 0; |
1499 | } | |
1500 | ||
70b9829e DB |
1501 | int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer, |
1502 | size_t bufsize, size_t *bufposn) | |
8237b185 | 1503 | { |
230f1322 | 1504 | char tmpfile[PATH_MAX]; |
8237b185 DB |
1505 | int local; |
1506 | z_stream stream; | |
1507 | unsigned char real_sha1[20]; | |
bf0f910d | 1508 | unsigned char discard[4096]; |
8237b185 DB |
1509 | int ret; |
1510 | SHA_CTX c; | |
1511 | ||
230f1322 | 1512 | snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory()); |
8237b185 | 1513 | |
230f1322 | 1514 | local = mkstemp(tmpfile); |
8237b185 | 1515 | if (local < 0) |
bd2afde8 JH |
1516 | return error("Couldn't open %s for %s", |
1517 | tmpfile, sha1_to_hex(sha1)); | |
8237b185 DB |
1518 | |
1519 | memset(&stream, 0, sizeof(stream)); | |
1520 | ||
1521 | inflateInit(&stream); | |
1522 | ||
1523 | SHA1_Init(&c); | |
1524 | ||
1525 | do { | |
1526 | ssize_t size; | |
70b9829e DB |
1527 | if (*bufposn) { |
1528 | stream.avail_in = *bufposn; | |
96ad15ae | 1529 | stream.next_in = (unsigned char *) buffer; |
70b9829e DB |
1530 | do { |
1531 | stream.next_out = discard; | |
1532 | stream.avail_out = sizeof(discard); | |
1533 | ret = inflate(&stream, Z_SYNC_FLUSH); | |
1534 | SHA1_Update(&c, discard, sizeof(discard) - | |
1535 | stream.avail_out); | |
1536 | } while (stream.avail_in && ret == Z_OK); | |
1537 | write(local, buffer, *bufposn - stream.avail_in); | |
1538 | memmove(buffer, buffer + *bufposn - stream.avail_in, | |
1539 | stream.avail_in); | |
1540 | *bufposn = stream.avail_in; | |
1541 | if (ret != Z_OK) | |
1542 | break; | |
1543 | } | |
1544 | size = read(fd, buffer + *bufposn, bufsize - *bufposn); | |
8237b185 DB |
1545 | if (size <= 0) { |
1546 | close(local); | |
230f1322 | 1547 | unlink(tmpfile); |
8237b185 DB |
1548 | if (!size) |
1549 | return error("Connection closed?"); | |
1550 | perror("Reading from connection"); | |
1551 | return -1; | |
1552 | } | |
70b9829e DB |
1553 | *bufposn += size; |
1554 | } while (1); | |
8237b185 DB |
1555 | inflateEnd(&stream); |
1556 | ||
1557 | close(local); | |
1558 | SHA1_Final(real_sha1, &c); | |
1559 | if (ret != Z_STREAM_END) { | |
230f1322 | 1560 | unlink(tmpfile); |
8237b185 DB |
1561 | return error("File %s corrupted", sha1_to_hex(sha1)); |
1562 | } | |
1563 | if (memcmp(sha1, real_sha1, 20)) { | |
230f1322 | 1564 | unlink(tmpfile); |
bd2afde8 | 1565 | return error("File %s has bad hash", sha1_to_hex(sha1)); |
8237b185 | 1566 | } |
230f1322 LT |
1567 | |
1568 | return move_temp_to_file(tmpfile, sha1_file_name(sha1)); | |
8237b185 DB |
1569 | } |
1570 | ||
bf592c50 DB |
1571 | int has_pack_index(const unsigned char *sha1) |
1572 | { | |
1573 | struct stat st; | |
1574 | if (stat(sha1_pack_index_name(sha1), &st)) | |
1575 | return 0; | |
1576 | return 1; | |
1577 | } | |
1578 | ||
1579 | int has_pack_file(const unsigned char *sha1) | |
1580 | { | |
1581 | struct stat st; | |
1582 | if (stat(sha1_pack_name(sha1), &st)) | |
1583 | return 0; | |
1584 | return 1; | |
1585 | } | |
1586 | ||
dade09c2 LT |
1587 | int has_sha1_pack(const unsigned char *sha1) |
1588 | { | |
1589 | struct pack_entry e; | |
1590 | return find_pack_entry(sha1, &e); | |
1591 | } | |
1592 | ||
8237b185 DB |
1593 | int has_sha1_file(const unsigned char *sha1) |
1594 | { | |
8237b185 | 1595 | struct stat st; |
1f688557 JH |
1596 | struct pack_entry e; |
1597 | ||
ab90ea5d | 1598 | if (find_pack_entry(sha1, &e)) |
1f688557 | 1599 | return 1; |
ab90ea5d | 1600 | return find_sha1_file(sha1, &st) ? 1 : 0; |
8237b185 | 1601 | } |
74400e71 | 1602 | |
024510c8 DB |
1603 | int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object) |
1604 | { | |
1605 | unsigned long size = 4096; | |
1606 | char *buf = malloc(size); | |
1607 | int iret, ret; | |
1608 | unsigned long off = 0; | |
1609 | unsigned char hdr[50]; | |
1610 | int hdrlen; | |
1611 | do { | |
1612 | iret = read(fd, buf + off, size - off); | |
1613 | if (iret > 0) { | |
1614 | off += iret; | |
1615 | if (off == size) { | |
1616 | size *= 2; | |
1617 | buf = realloc(buf, size); | |
1618 | } | |
1619 | } | |
1620 | } while (iret > 0); | |
1621 | if (iret < 0) { | |
1622 | free(buf); | |
1623 | return -1; | |
1624 | } | |
1625 | if (!type) | |
1626 | type = "blob"; | |
1627 | if (write_object) | |
1628 | ret = write_sha1_file(buf, off, type, sha1); | |
1629 | else { | |
1630 | write_sha1_file_prepare(buf, off, type, sha1, hdr, &hdrlen); | |
1631 | ret = 0; | |
1632 | } | |
1633 | free(buf); | |
1634 | return ret; | |
1635 | } | |
1636 | ||
7672db20 | 1637 | int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type) |
74400e71 | 1638 | { |
74400e71 | 1639 | unsigned long size = st->st_size; |
aac17941 LT |
1640 | void *buf; |
1641 | int ret; | |
7672db20 BL |
1642 | unsigned char hdr[50]; |
1643 | int hdrlen; | |
74400e71 | 1644 | |
aac17941 | 1645 | buf = ""; |
74400e71 | 1646 | if (size) |
aac17941 | 1647 | buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
74400e71 | 1648 | close(fd); |
e35f9824 | 1649 | if (buf == MAP_FAILED) |
74400e71 JH |
1650 | return -1; |
1651 | ||
7672db20 BL |
1652 | if (!type) |
1653 | type = "blob"; | |
1654 | if (write_object) | |
1655 | ret = write_sha1_file(buf, size, type, sha1); | |
1656 | else { | |
1657 | write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen); | |
1658 | ret = 0; | |
1659 | } | |
aac17941 LT |
1660 | if (size) |
1661 | munmap(buf, size); | |
1662 | return ret; | |
74400e71 | 1663 | } |
ec1fcc16 JH |
1664 | |
1665 | int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object) | |
1666 | { | |
1667 | int fd; | |
1668 | char *target; | |
1669 | ||
1670 | switch (st->st_mode & S_IFMT) { | |
1671 | case S_IFREG: | |
1672 | fd = open(path, O_RDONLY); | |
1673 | if (fd < 0) | |
1674 | return error("open(\"%s\"): %s", path, | |
1675 | strerror(errno)); | |
1676 | if (index_fd(sha1, fd, st, write_object, NULL) < 0) | |
1677 | return error("%s: failed to insert into database", | |
1678 | path); | |
1679 | break; | |
1680 | case S_IFLNK: | |
1681 | target = xmalloc(st->st_size+1); | |
1682 | if (readlink(path, target, st->st_size+1) != st->st_size) { | |
1683 | char *errstr = strerror(errno); | |
1684 | free(target); | |
1685 | return error("readlink(\"%s\"): %s", path, | |
1686 | errstr); | |
1687 | } | |
1688 | if (!write_object) { | |
1689 | unsigned char hdr[50]; | |
1690 | int hdrlen; | |
1691 | write_sha1_file_prepare(target, st->st_size, "blob", | |
1692 | sha1, hdr, &hdrlen); | |
1693 | } else if (write_sha1_file(target, st->st_size, "blob", sha1)) | |
1694 | return error("%s: failed to insert into database", | |
1695 | path); | |
1696 | free(target); | |
1697 | break; | |
1698 | default: | |
1699 | return error("%s: unsupported file type", path); | |
1700 | } | |
1701 | return 0; | |
1702 | } |