]>
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" |
b2141fc1 | 10 | #include "config.h" |
6eac50d8 | 11 | #include "string-list.h" |
697cc8ef | 12 | #include "lockfile.h" |
1f688557 | 13 | #include "delta.h" |
a733cb60 | 14 | #include "pack.h" |
8e440259 PE |
15 | #include "blob.h" |
16 | #include "commit.h" | |
4dd1fbc7 | 17 | #include "run-command.h" |
8e440259 PE |
18 | #include "tag.h" |
19 | #include "tree.h" | |
c879daa2 | 20 | #include "tree-walk.h" |
f35a6d3b | 21 | #include "refs.h" |
70f5d5d3 | 22 | #include "pack-revindex.h" |
628522ec | 23 | #include "sha1-lookup.h" |
568508e7 | 24 | #include "bulk-checkin.h" |
031dc927 | 25 | #include "repository.h" |
090ea126 | 26 | #include "streaming.h" |
543c5caa | 27 | #include "dir.h" |
12d95ef6 | 28 | #include "list.h" |
c4c6effa | 29 | #include "mergesort.h" |
cf3c6352 | 30 | #include "quote.h" |
4f39cd82 | 31 | #include "packfile.h" |
8b4c0103 | 32 | #include "fetch-object.h" |
90c62155 | 33 | #include "object-store.h" |
e05db0fd | 34 | |
1af64f73 | 35 | /* The maximum size for an object header. */ |
36 | #define MAX_HEADER_LEN 32 | |
37 | ||
50c5cd58 | 38 | const unsigned char null_sha1[GIT_MAX_RAWSZ]; |
3e56e724 | 39 | const struct object_id null_oid; |
8576fde6 JK |
40 | const struct object_id empty_tree_oid = { |
41 | EMPTY_TREE_SHA1_BIN_LITERAL | |
42 | }; | |
43 | const struct object_id empty_blob_oid = { | |
44 | EMPTY_BLOB_SHA1_BIN_LITERAL | |
45 | }; | |
88cd621d | 46 | |
ac73cedf | 47 | static void git_hash_sha1_init(git_hash_ctx *ctx) |
f50e766b | 48 | { |
ac73cedf | 49 | git_SHA1_Init(&ctx->sha1); |
f50e766b | 50 | } |
51 | ||
ac73cedf | 52 | static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len) |
f50e766b | 53 | { |
ac73cedf | 54 | git_SHA1_Update(&ctx->sha1, data, len); |
f50e766b | 55 | } |
56 | ||
ac73cedf | 57 | static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx) |
f50e766b | 58 | { |
ac73cedf | 59 | git_SHA1_Final(hash, &ctx->sha1); |
f50e766b | 60 | } |
61 | ||
ac73cedf | 62 | static void git_hash_unknown_init(git_hash_ctx *ctx) |
f50e766b | 63 | { |
64 | die("trying to init unknown hash"); | |
65 | } | |
66 | ||
ac73cedf | 67 | static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len) |
f50e766b | 68 | { |
69 | die("trying to update unknown hash"); | |
70 | } | |
71 | ||
ac73cedf | 72 | static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx) |
f50e766b | 73 | { |
74 | die("trying to finalize unknown hash"); | |
75 | } | |
76 | ||
77 | const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { | |
78 | { | |
79 | NULL, | |
80 | 0x00000000, | |
81 | 0, | |
82 | 0, | |
f50e766b | 83 | git_hash_unknown_init, |
84 | git_hash_unknown_update, | |
85 | git_hash_unknown_final, | |
86 | NULL, | |
87 | NULL, | |
88 | }, | |
89 | { | |
90 | "sha-1", | |
91 | /* "sha1", big-endian */ | |
92 | 0x73686131, | |
f50e766b | 93 | GIT_SHA1_RAWSZ, |
94 | GIT_SHA1_HEXSZ, | |
95 | git_hash_sha1_init, | |
96 | git_hash_sha1_update, | |
97 | git_hash_sha1_final, | |
98 | &empty_tree_oid, | |
99 | &empty_blob_oid, | |
100 | }, | |
101 | }; | |
102 | ||
d8a92ced | 103 | const char *empty_tree_oid_hex(void) |
104 | { | |
105 | static char buf[GIT_MAX_HEXSZ + 1]; | |
106 | return oid_to_hex_r(buf, the_hash_algo->empty_tree); | |
107 | } | |
108 | ||
109 | const char *empty_blob_oid_hex(void) | |
110 | { | |
111 | static char buf[GIT_MAX_HEXSZ + 1]; | |
112 | return oid_to_hex_r(buf, the_hash_algo->empty_blob); | |
113 | } | |
114 | ||
c597ba80 NTND |
115 | /* |
116 | * This is meant to hold a *small* number of objects that you would | |
117 | * want read_sha1_file() to be able to return, but yet you do not want | |
118 | * to write them into the object store (e.g. a browse-only | |
119 | * application). | |
120 | */ | |
121 | static struct cached_object { | |
122 | unsigned char sha1[20]; | |
123 | enum object_type type; | |
124 | void *buf; | |
125 | unsigned long size; | |
126 | } *cached_objects; | |
127 | static int cached_object_nr, cached_object_alloc; | |
128 | ||
129 | static struct cached_object empty_tree = { | |
dab0d410 | 130 | EMPTY_TREE_SHA1_BIN_LITERAL, |
c597ba80 NTND |
131 | OBJ_TREE, |
132 | "", | |
133 | 0 | |
134 | }; | |
135 | ||
136 | static struct cached_object *find_cached_object(const unsigned char *sha1) | |
137 | { | |
138 | int i; | |
139 | struct cached_object *co = cached_objects; | |
140 | ||
141 | for (i = 0; i < cached_object_nr; i++, co++) { | |
142 | if (!hashcmp(co->sha1, sha1)) | |
143 | return co; | |
144 | } | |
145 | if (!hashcmp(sha1, empty_tree.sha1)) | |
146 | return &empty_tree; | |
147 | return NULL; | |
148 | } | |
149 | ||
9472935d | 150 | |
8462ff43 | 151 | static int get_conv_flags(unsigned flags) |
9472935d TB |
152 | { |
153 | if (flags & HASH_RENORMALIZE) | |
8462ff43 | 154 | return CONV_EOL_RENORMALIZE; |
9472935d | 155 | else if (flags & HASH_WRITE_OBJECT) |
8462ff43 | 156 | return global_conv_flags_eol; |
9472935d | 157 | else |
8462ff43 | 158 | return 0; |
9472935d TB |
159 | } |
160 | ||
161 | ||
90a6464b JH |
162 | int mkdir_in_gitdir(const char *path) |
163 | { | |
164 | if (mkdir(path, 0777)) { | |
165 | int saved_errno = errno; | |
166 | struct stat st; | |
167 | struct strbuf sb = STRBUF_INIT; | |
168 | ||
169 | if (errno != EEXIST) | |
170 | return -1; | |
171 | /* | |
172 | * Are we looking at a path in a symlinked worktree | |
173 | * whose original repository does not yet have it? | |
174 | * e.g. .git/rr-cache pointing at its original | |
175 | * repository in which the user hasn't performed any | |
176 | * conflict resolution yet? | |
177 | */ | |
178 | if (lstat(path, &st) || !S_ISLNK(st.st_mode) || | |
179 | strbuf_readlink(&sb, path, st.st_size) || | |
180 | !is_absolute_path(sb.buf) || | |
181 | mkdir(sb.buf, 0777)) { | |
182 | strbuf_release(&sb); | |
183 | errno = saved_errno; | |
184 | return -1; | |
185 | } | |
186 | strbuf_release(&sb); | |
187 | } | |
188 | return adjust_shared_perm(path); | |
189 | } | |
190 | ||
0be0521b | 191 | enum scld_error safe_create_leading_directories(char *path) |
b2cb9425 | 192 | { |
26c8ae2a | 193 | char *next_component = path + offset_1st_component(path); |
0be0521b | 194 | enum scld_error ret = SCLD_OK; |
67d42212 | 195 | |
0be0521b | 196 | while (ret == SCLD_OK && next_component) { |
f0502332 | 197 | struct stat st; |
0f527403 | 198 | char *slash = next_component, slash_character; |
f0502332 | 199 | |
0f527403 MH |
200 | while (*slash && !is_dir_sep(*slash)) |
201 | slash++; | |
202 | ||
203 | if (!*slash) | |
b2cb9425 | 204 | break; |
bf10cf70 | 205 | |
26c8ae2a | 206 | next_component = slash + 1; |
0f527403 | 207 | while (is_dir_sep(*next_component)) |
bf10cf70 | 208 | next_component++; |
26c8ae2a | 209 | if (!*next_component) |
5f0bdf50 | 210 | break; |
831651fd | 211 | |
0f527403 | 212 | slash_character = *slash; |
831651fd | 213 | *slash = '\0'; |
67d42212 JR |
214 | if (!stat(path, &st)) { |
215 | /* path exists */ | |
204a047f MH |
216 | if (!S_ISDIR(st.st_mode)) { |
217 | errno = ENOTDIR; | |
0be0521b | 218 | ret = SCLD_EXISTS; |
204a047f | 219 | } |
53a39721 | 220 | } else if (mkdir(path, 0777)) { |
928734d9 | 221 | if (errno == EEXIST && |
9e6f885d | 222 | !stat(path, &st) && S_ISDIR(st.st_mode)) |
928734d9 | 223 | ; /* somebody created it since we checked */ |
18d37e86 MH |
224 | else if (errno == ENOENT) |
225 | /* | |
226 | * Either mkdir() failed because | |
227 | * somebody just pruned the containing | |
228 | * directory, or stat() failed because | |
229 | * the file that was in our way was | |
230 | * just removed. Either way, inform | |
231 | * the caller that it might be worth | |
232 | * trying again: | |
233 | */ | |
234 | ret = SCLD_VANISHED; | |
9e6f885d | 235 | else |
0be0521b | 236 | ret = SCLD_FAILED; |
53a39721 | 237 | } else if (adjust_shared_perm(path)) { |
0be0521b | 238 | ret = SCLD_PERMS; |
457f06d6 | 239 | } |
0f527403 | 240 | *slash = slash_character; |
b2cb9425 | 241 | } |
9e6f885d | 242 | return ret; |
b2cb9425 | 243 | } |
723c31fe | 244 | |
0be0521b | 245 | enum scld_error safe_create_leading_directories_const(const char *path) |
8e21d63b | 246 | { |
02944307 | 247 | int save_errno; |
8e21d63b JK |
248 | /* path points to cache entries, so xstrdup before messing with it */ |
249 | char *buf = xstrdup(path); | |
0be0521b | 250 | enum scld_error result = safe_create_leading_directories(buf); |
02944307 MH |
251 | |
252 | save_errno = errno; | |
8e21d63b | 253 | free(buf); |
02944307 | 254 | errno = save_errno; |
8e21d63b JK |
255 | return result; |
256 | } | |
257 | ||
177978f5 MH |
258 | int raceproof_create_file(const char *path, create_file_fn fn, void *cb) |
259 | { | |
260 | /* | |
261 | * The number of times we will try to remove empty directories | |
262 | * in the way of path. This is only 1 because if another | |
263 | * process is racily creating directories that conflict with | |
264 | * us, we don't want to fight against them. | |
265 | */ | |
266 | int remove_directories_remaining = 1; | |
267 | ||
268 | /* | |
269 | * The number of times that we will try to create the | |
270 | * directories containing path. We are willing to attempt this | |
271 | * more than once, because another process could be trying to | |
272 | * clean up empty directories at the same time as we are | |
273 | * trying to create them. | |
274 | */ | |
275 | int create_directories_remaining = 3; | |
276 | ||
277 | /* A scratch copy of path, filled lazily if we need it: */ | |
278 | struct strbuf path_copy = STRBUF_INIT; | |
279 | ||
280 | int ret, save_errno; | |
281 | ||
282 | /* Sanity check: */ | |
283 | assert(*path); | |
284 | ||
285 | retry_fn: | |
286 | ret = fn(path, cb); | |
287 | save_errno = errno; | |
288 | if (!ret) | |
289 | goto out; | |
290 | ||
291 | if (errno == EISDIR && remove_directories_remaining-- > 0) { | |
292 | /* | |
293 | * A directory is in the way. Maybe it is empty; try | |
294 | * to remove it: | |
295 | */ | |
296 | if (!path_copy.len) | |
297 | strbuf_addstr(&path_copy, path); | |
298 | ||
299 | if (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY)) | |
300 | goto retry_fn; | |
301 | } else if (errno == ENOENT && create_directories_remaining-- > 0) { | |
302 | /* | |
303 | * Maybe the containing directory didn't exist, or | |
304 | * maybe it was just deleted by a process that is | |
305 | * racing with us to clean up empty directories. Try | |
306 | * to create it: | |
307 | */ | |
308 | enum scld_error scld_result; | |
309 | ||
310 | if (!path_copy.len) | |
311 | strbuf_addstr(&path_copy, path); | |
312 | ||
313 | do { | |
314 | scld_result = safe_create_leading_directories(path_copy.buf); | |
315 | if (scld_result == SCLD_OK) | |
316 | goto retry_fn; | |
317 | } while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0); | |
318 | } | |
319 | ||
320 | out: | |
321 | strbuf_release(&path_copy); | |
322 | errno = save_errno; | |
323 | return ret; | |
324 | } | |
325 | ||
f7b7774f | 326 | static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1) |
ace1534d JH |
327 | { |
328 | int i; | |
329 | for (i = 0; i < 20; i++) { | |
330 | static char hex[] = "0123456789abcdef"; | |
331 | unsigned int val = sha1[i]; | |
f7b7774f JK |
332 | strbuf_addch(buf, hex[val >> 4]); |
333 | strbuf_addch(buf, hex[val & 0xf]); | |
afbba2f0 | 334 | if (!i) |
f7b7774f | 335 | strbuf_addch(buf, '/'); |
ace1534d JH |
336 | } |
337 | } | |
338 | ||
a68377b5 | 339 | void sha1_file_name(struct repository *r, struct strbuf *buf, const unsigned char *sha1) |
0fcfd160 | 340 | { |
a68377b5 | 341 | strbuf_addstr(buf, r->objects->objectdir); |
34498471 | 342 | strbuf_addch(buf, '/'); |
ea657730 | 343 | fill_sha1_path(buf, sha1); |
0fcfd160 LT |
344 | } |
345 | ||
38dbe5f0 JK |
346 | struct strbuf *alt_scratch_buf(struct alternate_object_database *alt) |
347 | { | |
348 | strbuf_setlen(&alt->scratch, alt->base_len); | |
349 | return &alt->scratch; | |
350 | } | |
351 | ||
29ec6af2 JK |
352 | static const char *alt_sha1_path(struct alternate_object_database *alt, |
353 | const unsigned char *sha1) | |
354 | { | |
38dbe5f0 | 355 | struct strbuf *buf = alt_scratch_buf(alt); |
f7b7774f | 356 | fill_sha1_path(buf, sha1); |
38dbe5f0 | 357 | return buf->buf; |
0fcfd160 LT |
358 | } |
359 | ||
4ea82473 JK |
360 | /* |
361 | * Return non-zero iff the path is usable as an alternate object database. | |
362 | */ | |
13313fc3 SB |
363 | static int alt_odb_usable(struct raw_object_store *o, |
364 | struct strbuf *path, | |
365 | const char *normalized_objdir) | |
4ea82473 JK |
366 | { |
367 | struct alternate_object_database *alt; | |
368 | ||
369 | /* Detect cases where alternate disappeared */ | |
370 | if (!is_directory(path->buf)) { | |
371 | error("object directory %s does not exist; " | |
372 | "check .git/objects/info/alternates.", | |
373 | path->buf); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | /* | |
378 | * Prevent the common mistake of listing the same | |
379 | * thing twice, or object directory itself. | |
380 | */ | |
13313fc3 | 381 | for (alt = o->alt_odb_list; alt; alt = alt->next) { |
ea0fc3b4 | 382 | if (!fspathcmp(path->buf, alt->path)) |
4ea82473 JK |
383 | return 0; |
384 | } | |
385 | if (!fspathcmp(path->buf, normalized_objdir)) | |
386 | return 0; | |
387 | ||
388 | return 1; | |
389 | } | |
390 | ||
ddd5d056 JH |
391 | /* |
392 | * Prepare alternate object database registry. | |
d5a63b99 JH |
393 | * |
394 | * The variable alt_odb_list points at the list of struct | |
395 | * alternate_object_database. The elements on this list come from | |
396 | * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT | |
397 | * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates, | |
1494e038 JH |
398 | * whose contents is similar to that environment variable but can be |
399 | * LF separated. Its base points at a statically allocated buffer that | |
d5a63b99 JH |
400 | * contains "/the/directory/corresponding/to/.git/objects/...", while |
401 | * its name points just after the slash at the end of ".git/objects/" | |
402 | * in the example above, and has enough space to hold 40-byte hex | |
403 | * SHA1, an extra slash for the first level indirection, and the | |
404 | * terminating NUL. | |
ddd5d056 | 405 | */ |
77f012e8 SB |
406 | static void read_info_alternates(struct repository *r, |
407 | const char *relative_base, | |
408 | int depth); | |
409 | static int link_alt_odb_entry(struct repository *r, const char *entry, | |
cfc62fc9 | 410 | const char *relative_base, int depth, const char *normalized_objdir) |
ace1534d | 411 | { |
c2f493a4 | 412 | struct alternate_object_database *ent; |
5bdf0a84 | 413 | struct strbuf pathbuf = STRBUF_INIT; |
d5a63b99 | 414 | |
85dadc38 | 415 | if (!is_absolute_path(entry) && relative_base) { |
4ac9006f | 416 | strbuf_realpath(&pathbuf, relative_base, 1); |
5bdf0a84 | 417 | strbuf_addch(&pathbuf, '/'); |
c2f493a4 | 418 | } |
6eac50d8 | 419 | strbuf_addstr(&pathbuf, entry); |
c2f493a4 | 420 | |
37a95862 | 421 | if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) { |
670c359d JK |
422 | error("unable to normalize alternate object path: %s", |
423 | pathbuf.buf); | |
424 | strbuf_release(&pathbuf); | |
425 | return -1; | |
426 | } | |
5bdf0a84 HW |
427 | |
428 | /* | |
429 | * The trailing slash after the directory name is given by | |
430 | * this function at the end. Remove duplicates. | |
431 | */ | |
4ea82473 JK |
432 | while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/') |
433 | strbuf_setlen(&pathbuf, pathbuf.len - 1); | |
5bdf0a84 | 434 | |
77f012e8 | 435 | if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) { |
4ea82473 | 436 | strbuf_release(&pathbuf); |
c2f493a4 MW |
437 | return -1; |
438 | } | |
439 | ||
7f0fa2c0 | 440 | ent = alloc_alt_odb(pathbuf.buf); |
c2f493a4 MW |
441 | |
442 | /* add the alternate entry */ | |
77f012e8 SB |
443 | *r->objects->alt_odb_tail = ent; |
444 | r->objects->alt_odb_tail = &(ent->next); | |
c2f493a4 MW |
445 | ent->next = NULL; |
446 | ||
447 | /* recursively add alternates */ | |
77f012e8 | 448 | read_info_alternates(r, pathbuf.buf, depth + 1); |
c2f493a4 | 449 | |
4ea82473 | 450 | strbuf_release(&pathbuf); |
c2f493a4 MW |
451 | return 0; |
452 | } | |
453 | ||
cf3c6352 JK |
454 | static const char *parse_alt_odb_entry(const char *string, |
455 | int sep, | |
456 | struct strbuf *out) | |
457 | { | |
458 | const char *end; | |
459 | ||
460 | strbuf_reset(out); | |
461 | ||
462 | if (*string == '#') { | |
463 | /* comment; consume up to next separator */ | |
464 | end = strchrnul(string, sep); | |
465 | } else if (*string == '"' && !unquote_c_style(out, string, &end)) { | |
466 | /* | |
467 | * quoted path; unquote_c_style has copied the | |
468 | * data for us and set "end". Broken quoting (e.g., | |
469 | * an entry that doesn't end with a quote) falls | |
470 | * back to the unquoted case below. | |
471 | */ | |
472 | } else { | |
473 | /* normal, unquoted path */ | |
474 | end = strchrnul(string, sep); | |
475 | strbuf_add(out, string, end - string); | |
476 | } | |
477 | ||
478 | if (*end) | |
479 | end++; | |
480 | return end; | |
481 | } | |
482 | ||
77f012e8 SB |
483 | static void link_alt_odb_entries(struct repository *r, const char *alt, |
484 | int sep, const char *relative_base, int depth) | |
c2f493a4 | 485 | { |
539e7506 | 486 | struct strbuf objdirbuf = STRBUF_INIT; |
cf3c6352 | 487 | struct strbuf entry = STRBUF_INIT; |
c2f493a4 | 488 | |
f28e3668 JK |
489 | if (!alt || !*alt) |
490 | return; | |
491 | ||
c2f493a4 MW |
492 | if (depth > 5) { |
493 | error("%s: ignoring alternate object stores, nesting too deep.", | |
494 | relative_base); | |
495 | return; | |
496 | } | |
497 | ||
77f012e8 | 498 | strbuf_add_absolute_path(&objdirbuf, r->objects->objectdir); |
670c359d JK |
499 | if (strbuf_normalize_path(&objdirbuf) < 0) |
500 | die("unable to normalize object directory: %s", | |
501 | objdirbuf.buf); | |
539e7506 | 502 | |
cf3c6352 JK |
503 | while (*alt) { |
504 | alt = parse_alt_odb_entry(alt, sep, &entry); | |
505 | if (!entry.len) | |
9577e7e3 | 506 | continue; |
77f012e8 | 507 | link_alt_odb_entry(r, entry.buf, |
cfc62fc9 | 508 | relative_base, depth, objdirbuf.buf); |
9577e7e3 | 509 | } |
cf3c6352 | 510 | strbuf_release(&entry); |
539e7506 | 511 | strbuf_release(&objdirbuf); |
d5a63b99 JH |
512 | } |
513 | ||
77f012e8 SB |
514 | static void read_info_alternates(struct repository *r, |
515 | const char *relative_base, | |
516 | int depth) | |
d5a63b99 | 517 | { |
5015f01c | 518 | char *path; |
dc732bd5 | 519 | struct strbuf buf = STRBUF_INIT; |
d5a63b99 | 520 | |
5015f01c | 521 | path = xstrfmt("%s/info/alternates", relative_base); |
dc732bd5 | 522 | if (strbuf_read_file(&buf, path, 1024) < 0) { |
f0f7bebe | 523 | warn_on_fopen_errors(path); |
dc732bd5 | 524 | free(path); |
9a217f2a | 525 | return; |
ace1534d | 526 | } |
d5a63b99 | 527 | |
77f012e8 | 528 | link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth); |
dc732bd5 JK |
529 | strbuf_release(&buf); |
530 | free(path); | |
ace1534d JH |
531 | } |
532 | ||
7f0fa2c0 JK |
533 | struct alternate_object_database *alloc_alt_odb(const char *dir) |
534 | { | |
535 | struct alternate_object_database *ent; | |
7f0fa2c0 | 536 | |
597f9134 | 537 | FLEX_ALLOC_STR(ent, path, dir); |
38dbe5f0 JK |
538 | strbuf_init(&ent->scratch, 0); |
539 | strbuf_addf(&ent->scratch, "%s/", dir); | |
540 | ent->base_len = ent->scratch.len; | |
7f0fa2c0 JK |
541 | |
542 | return ent; | |
543 | } | |
544 | ||
bef70b22 DB |
545 | void add_to_alternates_file(const char *reference) |
546 | { | |
f132a127 | 547 | struct lock_file lock = LOCK_INIT; |
77b9b1d1 JK |
548 | char *alts = git_pathdup("objects/info/alternates"); |
549 | FILE *in, *out; | |
f132a127 | 550 | int found = 0; |
77b9b1d1 | 551 | |
f132a127 MÅ |
552 | hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR); |
553 | out = fdopen_lock_file(&lock, "w"); | |
77b9b1d1 JK |
554 | if (!out) |
555 | die_errno("unable to fdopen alternates lockfile"); | |
556 | ||
557 | in = fopen(alts, "r"); | |
558 | if (in) { | |
559 | struct strbuf line = STRBUF_INIT; | |
77b9b1d1 | 560 | |
3f163962 | 561 | while (strbuf_getline(&line, in) != EOF) { |
77b9b1d1 JK |
562 | if (!strcmp(reference, line.buf)) { |
563 | found = 1; | |
564 | break; | |
565 | } | |
566 | fprintf_or_die(out, "%s\n", line.buf); | |
567 | } | |
568 | ||
569 | strbuf_release(&line); | |
570 | fclose(in); | |
77b9b1d1 JK |
571 | } |
572 | else if (errno != ENOENT) | |
573 | die_errno("unable to read alternates file"); | |
574 | ||
f132a127 MÅ |
575 | if (found) { |
576 | rollback_lock_file(&lock); | |
577 | } else { | |
77b9b1d1 | 578 | fprintf_or_die(out, "%s\n", reference); |
f132a127 | 579 | if (commit_lock_file(&lock)) |
77b9b1d1 | 580 | die_errno("unable to move new alternates file into place"); |
031dc927 | 581 | if (the_repository->objects->alt_odb_tail) |
93d8d1e2 SB |
582 | link_alt_odb_entries(the_repository, reference, |
583 | '\n', NULL, 0); | |
77b9b1d1 JK |
584 | } |
585 | free(alts); | |
bef70b22 DB |
586 | } |
587 | ||
a5b34d21 JK |
588 | void add_to_alternates_memory(const char *reference) |
589 | { | |
590 | /* | |
591 | * Make sure alternates are initialized, or else our entry may be | |
592 | * overwritten when they are. | |
593 | */ | |
0b209034 | 594 | prepare_alt_odb(the_repository); |
a5b34d21 | 595 | |
93d8d1e2 SB |
596 | link_alt_odb_entries(the_repository, reference, |
597 | '\n', NULL, 0); | |
a5b34d21 JK |
598 | } |
599 | ||
9eeea7d2 SB |
600 | /* |
601 | * Compute the exact path an alternate is at and returns it. In case of | |
602 | * error NULL is returned and the human readable error is added to `err` | |
603 | * `path` may be relative and should point to $GITDIR. | |
604 | * `err` must not be null. | |
605 | */ | |
606 | char *compute_alternate_path(const char *path, struct strbuf *err) | |
607 | { | |
608 | char *ref_git = NULL; | |
609 | const char *repo, *ref_git_s; | |
610 | int seen_error = 0; | |
611 | ||
612 | ref_git_s = real_path_if_valid(path); | |
613 | if (!ref_git_s) { | |
614 | seen_error = 1; | |
615 | strbuf_addf(err, _("path '%s' does not exist"), path); | |
616 | goto out; | |
617 | } else | |
618 | /* | |
619 | * Beware: read_gitfile(), real_path() and mkpath() | |
620 | * return static buffer | |
621 | */ | |
622 | ref_git = xstrdup(ref_git_s); | |
623 | ||
624 | repo = read_gitfile(ref_git); | |
625 | if (!repo) | |
626 | repo = read_gitfile(mkpath("%s/.git", ref_git)); | |
627 | if (repo) { | |
628 | free(ref_git); | |
629 | ref_git = xstrdup(repo); | |
630 | } | |
631 | ||
632 | if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) { | |
633 | char *ref_git_git = mkpathdup("%s/.git", ref_git); | |
634 | free(ref_git); | |
635 | ref_git = ref_git_git; | |
636 | } else if (!is_directory(mkpath("%s/objects", ref_git))) { | |
637 | struct strbuf sb = STRBUF_INIT; | |
638 | seen_error = 1; | |
639 | if (get_common_dir(&sb, ref_git)) { | |
640 | strbuf_addf(err, | |
641 | _("reference repository '%s' as a linked " | |
642 | "checkout is not supported yet."), | |
643 | path); | |
644 | goto out; | |
645 | } | |
646 | ||
647 | strbuf_addf(err, _("reference repository '%s' is not a " | |
648 | "local repository."), path); | |
649 | goto out; | |
650 | } | |
651 | ||
652 | if (!access(mkpath("%s/shallow", ref_git), F_OK)) { | |
653 | strbuf_addf(err, _("reference repository '%s' is shallow"), | |
654 | path); | |
655 | seen_error = 1; | |
656 | goto out; | |
657 | } | |
658 | ||
659 | if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) { | |
660 | strbuf_addf(err, | |
661 | _("reference repository '%s' is grafted"), | |
662 | path); | |
663 | seen_error = 1; | |
664 | goto out; | |
665 | } | |
666 | ||
667 | out: | |
668 | if (seen_error) { | |
6a83d902 | 669 | FREE_AND_NULL(ref_git); |
9eeea7d2 SB |
670 | } |
671 | ||
672 | return ref_git; | |
673 | } | |
674 | ||
fe1b2268 | 675 | int foreach_alt_odb(alt_odb_fn fn, void *cb) |
d79796bc JH |
676 | { |
677 | struct alternate_object_database *ent; | |
fe1b2268 | 678 | int r = 0; |
d79796bc | 679 | |
0b209034 | 680 | prepare_alt_odb(the_repository); |
031dc927 | 681 | for (ent = the_repository->objects->alt_odb_list; ent; ent = ent->next) { |
fe1b2268 JK |
682 | r = fn(ent, cb); |
683 | if (r) | |
684 | break; | |
685 | } | |
686 | return r; | |
d79796bc JH |
687 | } |
688 | ||
13068bf0 | 689 | void prepare_alt_odb(struct repository *r) |
c2f493a4 | 690 | { |
13068bf0 | 691 | if (r->objects->alt_odb_tail) |
7dc24aa5 SP |
692 | return; |
693 | ||
13068bf0 SB |
694 | r->objects->alt_odb_tail = &r->objects->alt_odb_list; |
695 | link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0); | |
c2f493a4 | 696 | |
13068bf0 | 697 | read_info_alternates(r, r->objects->objectdir, 0); |
c2f493a4 MW |
698 | } |
699 | ||
3096b2ec | 700 | /* Returns 1 if we have successfully freshened the file, 0 otherwise. */ |
33d4221c | 701 | static int freshen_file(const char *fn) |
ace1534d | 702 | { |
33d4221c JK |
703 | struct utimbuf t; |
704 | t.actime = t.modtime = time(NULL); | |
705 | return !utime(fn, &t); | |
0f4dc14a | 706 | } |
ace1534d | 707 | |
3096b2ec JK |
708 | /* |
709 | * All of the check_and_freshen functions return 1 if the file exists and was | |
710 | * freshened (if freshening was requested), 0 otherwise. If they return | |
711 | * 0, you should not assume that it is safe to skip a write of the object (it | |
712 | * either does not exist on disk, or has a stale mtime and may be subject to | |
713 | * pruning). | |
714 | */ | |
6a5e6f5e | 715 | int check_and_freshen_file(const char *fn, int freshen) |
33d4221c JK |
716 | { |
717 | if (access(fn, F_OK)) | |
718 | return 0; | |
3096b2ec | 719 | if (freshen && !freshen_file(fn)) |
33d4221c JK |
720 | return 0; |
721 | return 1; | |
722 | } | |
723 | ||
6862ebbf | 724 | static int check_and_freshen_local(const struct object_id *oid, int freshen) |
33d4221c | 725 | { |
ea657730 CC |
726 | static struct strbuf buf = STRBUF_INIT; |
727 | ||
728 | strbuf_reset(&buf); | |
6862ebbf | 729 | sha1_file_name(the_repository, &buf, oid->hash); |
ea657730 CC |
730 | |
731 | return check_and_freshen_file(buf.buf, freshen); | |
33d4221c JK |
732 | } |
733 | ||
6862ebbf | 734 | static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen) |
0f4dc14a BC |
735 | { |
736 | struct alternate_object_database *alt; | |
0b209034 | 737 | prepare_alt_odb(the_repository); |
031dc927 | 738 | for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) { |
6862ebbf | 739 | const char *path = alt_sha1_path(alt, oid->hash); |
29ec6af2 | 740 | if (check_and_freshen_file(path, freshen)) |
c529d75a | 741 | return 1; |
ace1534d | 742 | } |
c529d75a | 743 | return 0; |
ace1534d JH |
744 | } |
745 | ||
6862ebbf | 746 | static int check_and_freshen(const struct object_id *oid, int freshen) |
33d4221c | 747 | { |
6862ebbf | 748 | return check_and_freshen_local(oid, freshen) || |
749 | check_and_freshen_nonlocal(oid, freshen); | |
33d4221c JK |
750 | } |
751 | ||
6862ebbf | 752 | int has_loose_object_nonlocal(const struct object_id *oid) |
33d4221c | 753 | { |
6862ebbf | 754 | return check_and_freshen_nonlocal(oid, 0); |
33d4221c JK |
755 | } |
756 | ||
6862ebbf | 757 | static int has_loose_object(const struct object_id *oid) |
0f4dc14a | 758 | { |
6862ebbf | 759 | return check_and_freshen(oid, 0); |
0f4dc14a BC |
760 | } |
761 | ||
02710228 SP |
762 | static void mmap_limit_check(size_t length) |
763 | { | |
764 | static size_t limit = 0; | |
765 | if (!limit) { | |
766 | limit = git_env_ulong("GIT_MMAP_LIMIT", 0); | |
767 | if (!limit) | |
768 | limit = SIZE_MAX; | |
769 | } | |
770 | if (length > limit) | |
771 | die("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX, | |
772 | (uintmax_t)length, (uintmax_t)limit); | |
773 | } | |
774 | ||
1570856b JK |
775 | void *xmmap_gently(void *start, size_t length, |
776 | int prot, int flags, int fd, off_t offset) | |
58ecbd5e | 777 | { |
02710228 SP |
778 | void *ret; |
779 | ||
780 | mmap_limit_check(length); | |
781 | ret = mmap(start, length, prot, flags, fd, offset); | |
58ecbd5e JN |
782 | if (ret == MAP_FAILED) { |
783 | if (!length) | |
784 | return NULL; | |
7c3ecb32 | 785 | release_pack_memory(length); |
58ecbd5e | 786 | ret = mmap(start, length, prot, flags, fd, offset); |
58ecbd5e JN |
787 | } |
788 | return ret; | |
789 | } | |
790 | ||
1570856b JK |
791 | void *xmmap(void *start, size_t length, |
792 | int prot, int flags, int fd, off_t offset) | |
793 | { | |
794 | void *ret = xmmap_gently(start, length, prot, flags, fd, offset); | |
795 | if (ret == MAP_FAILED) | |
9ca0aaf6 | 796 | die_errno("mmap failed"); |
1570856b JK |
797 | return ret; |
798 | } | |
799 | ||
88d0db55 | 800 | /* |
090ea126 NTND |
801 | * With an in-core object data in "map", rehash it to make sure the |
802 | * object name actually matches "sha1" to detect object corruption. | |
803 | * With "map" == NULL, try reading the object named with "sha1" using | |
804 | * the streaming interface and rehash it to do the same. | |
88d0db55 | 805 | */ |
17e65451 | 806 | int check_object_signature(const struct object_id *oid, void *map, |
807 | unsigned long size, const char *type) | |
88d0db55 | 808 | { |
f070facc | 809 | struct object_id real_oid; |
090ea126 NTND |
810 | enum object_type obj_type; |
811 | struct git_istream *st; | |
18e2588e | 812 | git_hash_ctx c; |
1af64f73 | 813 | char hdr[MAX_HEADER_LEN]; |
090ea126 | 814 | int hdrlen; |
88d0db55 | 815 | |
090ea126 | 816 | if (map) { |
f070facc | 817 | hash_object_file(map, size, type, &real_oid); |
17e65451 | 818 | return oidcmp(oid, &real_oid) ? -1 : 0; |
88d0db55 BC |
819 | } |
820 | ||
ef7b5195 | 821 | st = open_istream(oid, &obj_type, &size, NULL); |
090ea126 NTND |
822 | if (!st) |
823 | return -1; | |
88d0db55 | 824 | |
090ea126 | 825 | /* Generate the header */ |
debca9d2 | 826 | hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(obj_type), size) + 1; |
88d0db55 | 827 | |
090ea126 | 828 | /* Sha1.. */ |
18e2588e | 829 | the_hash_algo->init_fn(&c); |
830 | the_hash_algo->update_fn(&c, hdr, hdrlen); | |
090ea126 NTND |
831 | for (;;) { |
832 | char buf[1024 * 16]; | |
833 | ssize_t readlen = read_istream(st, buf, sizeof(buf)); | |
1f688557 | 834 | |
f54fac53 JK |
835 | if (readlen < 0) { |
836 | close_istream(st); | |
837 | return -1; | |
838 | } | |
090ea126 NTND |
839 | if (!readlen) |
840 | break; | |
18e2588e | 841 | the_hash_algo->update_fn(&c, buf, readlen); |
fa5fc15d | 842 | } |
18e2588e | 843 | the_hash_algo->final_fn(real_oid.hash, &c); |
090ea126 | 844 | close_istream(st); |
17e65451 | 845 | return oidcmp(oid, &real_oid) ? -1 : 0; |
fa5fc15d SP |
846 | } |
847 | ||
1b8ac5ea | 848 | int git_open_cloexec(const char *name, int flags) |
a0788266 | 849 | { |
1e3001a8 JH |
850 | int fd; |
851 | static int o_cloexec = O_CLOEXEC; | |
a0788266 | 852 | |
1e3001a8 JH |
853 | fd = open(name, flags | o_cloexec); |
854 | if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) { | |
cd66ada0 | 855 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ |
1e3001a8 JH |
856 | o_cloexec &= ~O_CLOEXEC; |
857 | fd = open(name, flags | o_cloexec); | |
491a8dec | 858 | } |
491a8dec | 859 | |
9fb9495d | 860 | #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) |
491a8dec | 861 | { |
1e3001a8 | 862 | static int fd_cloexec = FD_CLOEXEC; |
a0788266 | 863 | |
1e3001a8 JH |
864 | if (!o_cloexec && 0 <= fd && fd_cloexec) { |
865 | /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */ | |
9fb9495d EW |
866 | int flags = fcntl(fd, F_GETFD); |
867 | if (fcntl(fd, F_SETFD, flags | fd_cloexec)) | |
1e3001a8 | 868 | fd_cloexec = 0; |
cd66ada0 | 869 | } |
44d1c19e | 870 | } |
a0788266 | 871 | #endif |
1b8ac5ea | 872 | return fd; |
a0788266 JS |
873 | } |
874 | ||
3cf8b462 | 875 | /* |
771e7d57 JK |
876 | * Find "sha1" as a loose object in the local repository or in an alternate. |
877 | * Returns 0 on success, negative on failure. | |
878 | * | |
879 | * The "path" out-parameter will give the path of the object we found (if any). | |
880 | * Note that it may point to static storage and is only valid until another | |
881 | * call to sha1_file_name(), etc. | |
3cf8b462 | 882 | */ |
d2607fa0 SB |
883 | static int stat_sha1_file(struct repository *r, const unsigned char *sha1, |
884 | struct stat *st, const char **path) | |
1f688557 | 885 | { |
052fe5ea | 886 | struct alternate_object_database *alt; |
ea657730 CC |
887 | static struct strbuf buf = STRBUF_INIT; |
888 | ||
889 | strbuf_reset(&buf); | |
d2607fa0 | 890 | sha1_file_name(r, &buf, sha1); |
ea657730 | 891 | *path = buf.buf; |
d079837e | 892 | |
771e7d57 | 893 | if (!lstat(*path, st)) |
052fe5ea | 894 | return 0; |
c7934306 | 895 | |
d2607fa0 | 896 | prepare_alt_odb(r); |
052fe5ea | 897 | errno = ENOENT; |
d2607fa0 | 898 | for (alt = r->objects->alt_odb_list; alt; alt = alt->next) { |
771e7d57 JK |
899 | *path = alt_sha1_path(alt, sha1); |
900 | if (!lstat(*path, st)) | |
052fe5ea | 901 | return 0; |
c7934306 SP |
902 | } |
903 | ||
3cf8b462 SP |
904 | return -1; |
905 | } | |
906 | ||
771e7d57 JK |
907 | /* |
908 | * Like stat_sha1_file(), but actually open the object and return the | |
909 | * descriptor. See the caveats on the "path" parameter above. | |
910 | */ | |
ec7283e5 SB |
911 | static int open_sha1_file(struct repository *r, |
912 | const unsigned char *sha1, const char **path) | |
60bb8b14 | 913 | { |
44d1c19e | 914 | int fd; |
44d1c19e | 915 | struct alternate_object_database *alt; |
d6c8a05b | 916 | int most_interesting_errno; |
ea657730 CC |
917 | static struct strbuf buf = STRBUF_INIT; |
918 | ||
919 | strbuf_reset(&buf); | |
ec7283e5 | 920 | sha1_file_name(r, &buf, sha1); |
ea657730 | 921 | *path = buf.buf; |
60bb8b14 | 922 | |
771e7d57 | 923 | fd = git_open(*path); |
44d1c19e LT |
924 | if (fd >= 0) |
925 | return fd; | |
d6c8a05b | 926 | most_interesting_errno = errno; |
03e79c88 | 927 | |
ec7283e5 SB |
928 | prepare_alt_odb(r); |
929 | for (alt = r->objects->alt_odb_list; alt; alt = alt->next) { | |
771e7d57 JK |
930 | *path = alt_sha1_path(alt, sha1); |
931 | fd = git_open(*path); | |
44d1c19e LT |
932 | if (fd >= 0) |
933 | return fd; | |
d6c8a05b JK |
934 | if (most_interesting_errno == ENOENT) |
935 | most_interesting_errno = errno; | |
03e79c88 | 936 | } |
d6c8a05b | 937 | errno = most_interesting_errno; |
44d1c19e | 938 | return -1; |
1f688557 JH |
939 | } |
940 | ||
f6371f92 JK |
941 | /* |
942 | * Map the loose object at "path" if it is not NULL, or the path found by | |
943 | * searching for a loose object named "sha1". | |
944 | */ | |
1fea63e1 JN |
945 | static void *map_sha1_file_1(struct repository *r, const char *path, |
946 | const unsigned char *sha1, unsigned long *size) | |
27d69a46 | 947 | { |
0fcfd160 | 948 | void *map; |
144bde78 | 949 | int fd; |
84dd81c1 | 950 | |
f6371f92 JK |
951 | if (path) |
952 | fd = git_open(path); | |
8261e1f1 | 953 | else |
1fea63e1 | 954 | fd = open_sha1_file(r, sha1, &path); |
44d1c19e LT |
955 | map = NULL; |
956 | if (fd >= 0) { | |
957 | struct stat st; | |
1f688557 | 958 | |
44d1c19e LT |
959 | if (!fstat(fd, &st)) { |
960 | *size = xsize_t(st.st_size); | |
33e42de0 MM |
961 | if (!*size) { |
962 | /* mmap() is forbidden on empty files */ | |
771e7d57 | 963 | error("object file %s is empty", path); |
33e42de0 MM |
964 | return NULL; |
965 | } | |
44d1c19e | 966 | map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); |
144bde78 | 967 | } |
44d1c19e | 968 | close(fd); |
74e34e1f | 969 | } |
0fcfd160 | 970 | return map; |
74e34e1f NP |
971 | } |
972 | ||
bd27f50c SB |
973 | void *map_sha1_file(struct repository *r, |
974 | const unsigned char *sha1, unsigned long *size) | |
068f85e3 | 975 | { |
bd27f50c | 976 | return map_sha1_file_1(r, NULL, sha1, size); |
068f85e3 | 977 | } |
978 | ||
d21f8426 JH |
979 | static int unpack_sha1_short_header(git_zstream *stream, |
980 | unsigned char *map, unsigned long mapsize, | |
981 | void *buffer, unsigned long bufsiz) | |
47fe3f6e | 982 | { |
c4483576 LT |
983 | /* Get the data stream */ |
984 | memset(stream, 0, sizeof(*stream)); | |
985 | stream->next_in = map; | |
986 | stream->avail_in = mapsize; | |
987 | stream->next_out = buffer; | |
93821bd9 | 988 | stream->avail_out = bufsiz; |
47fe3f6e | 989 | |
39c68542 | 990 | git_inflate_init(stream); |
cc5c54e7 | 991 | return git_inflate(stream, 0); |
9a217f2a JH |
992 | } |
993 | ||
d21f8426 JH |
994 | int unpack_sha1_header(git_zstream *stream, |
995 | unsigned char *map, unsigned long mapsize, | |
996 | void *buffer, unsigned long bufsiz) | |
1f688557 | 997 | { |
d21f8426 JH |
998 | int status = unpack_sha1_short_header(stream, map, mapsize, |
999 | buffer, bufsiz); | |
42873078 | 1000 | |
d21f8426 JH |
1001 | if (status < Z_OK) |
1002 | return status; | |
628522ec | 1003 | |
d21f8426 JH |
1004 | /* Make sure we have the terminating NUL */ |
1005 | if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) | |
1006 | return -1; | |
1f688557 JH |
1007 | return 0; |
1008 | } | |
1009 | ||
46f03448 KN |
1010 | static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map, |
1011 | unsigned long mapsize, void *buffer, | |
1012 | unsigned long bufsiz, struct strbuf *header) | |
d131b7af | 1013 | { |
46f03448 KN |
1014 | int status; |
1015 | ||
d21f8426 JH |
1016 | status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz); |
1017 | if (status < Z_OK) | |
1018 | return -1; | |
d131b7af | 1019 | |
46f03448 KN |
1020 | /* |
1021 | * Check if entire header is unpacked in the first iteration. | |
d131b7af | 1022 | */ |
46f03448 KN |
1023 | if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) |
1024 | return 0; | |
d131b7af | 1025 | |
46f03448 KN |
1026 | /* |
1027 | * buffer[0..bufsiz] was not large enough. Copy the partial | |
1028 | * result out to header, and then append the result of further | |
1029 | * reading the stream. | |
1030 | */ | |
1031 | strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer); | |
1032 | stream->next_out = buffer; | |
1033 | stream->avail_out = bufsiz; | |
d131b7af | 1034 | |
46f03448 KN |
1035 | do { |
1036 | status = git_inflate(stream, 0); | |
1037 | strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer); | |
1038 | if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) | |
1039 | return 0; | |
1040 | stream->next_out = buffer; | |
1041 | stream->avail_out = bufsiz; | |
1042 | } while (status != Z_STREAM_END); | |
1043 | return -1; | |
d131b7af SP |
1044 | } |
1045 | ||
ef49a7a0 | 1046 | static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1) |
95099731 | 1047 | { |
5180cacc | 1048 | int bytes = strlen(buffer) + 1; |
3aee68aa | 1049 | unsigned char *buf = xmallocz(size); |
93821bd9 | 1050 | unsigned long n; |
7efbff75 | 1051 | int status = Z_OK; |
95099731 | 1052 | |
93821bd9 LT |
1053 | n = stream->total_out - bytes; |
1054 | if (n > size) | |
1055 | n = size; | |
1056 | memcpy(buf, (char *) buffer + bytes, n); | |
1057 | bytes = n; | |
456cdf6e LT |
1058 | if (bytes <= size) { |
1059 | /* | |
1060 | * The above condition must be (bytes <= size), not | |
1061 | * (bytes < size). In other words, even though we | |
ccf5ace0 | 1062 | * expect no more output and set avail_out to zero, |
456cdf6e LT |
1063 | * the input zlib stream may have bytes that express |
1064 | * "this concludes the stream", and we *do* want to | |
1065 | * eat that input. | |
1066 | * | |
1067 | * Otherwise we would not be able to test that we | |
1068 | * consumed all the input to reach the expected size; | |
1069 | * we also want to check that zlib tells us that all | |
1070 | * went well with status == Z_STREAM_END at the end. | |
1071 | */ | |
5180cacc LT |
1072 | stream->next_out = buf + bytes; |
1073 | stream->avail_out = size - bytes; | |
7efbff75 | 1074 | while (status == Z_OK) |
39c68542 | 1075 | status = git_inflate(stream, Z_FINISH); |
5180cacc | 1076 | } |
456cdf6e | 1077 | if (status == Z_STREAM_END && !stream->avail_in) { |
39c68542 | 1078 | git_inflate_end(stream); |
7efbff75 | 1079 | return buf; |
95099731 NTND |
1080 | } |
1081 | ||
7efbff75 JH |
1082 | if (status < 0) |
1083 | error("corrupt loose object '%s'", sha1_to_hex(sha1)); | |
1084 | else if (stream->avail_in) | |
1085 | error("garbage at end of loose object '%s'", | |
1086 | sha1_to_hex(sha1)); | |
1087 | free(buf); | |
1088 | return NULL; | |
95099731 NTND |
1089 | } |
1090 | ||
d40d535b | 1091 | /* |
5180cacc LT |
1092 | * We used to just use "sscanf()", but that's actually way |
1093 | * too permissive for what we want to check. So do an anal | |
1094 | * object header parse by hand. | |
d40d535b | 1095 | */ |
46f03448 KN |
1096 | static int parse_sha1_header_extended(const char *hdr, struct object_info *oi, |
1097 | unsigned int flags) | |
1f688557 | 1098 | { |
46f03448 | 1099 | const char *type_buf = hdr; |
5180cacc | 1100 | unsigned long size; |
46f03448 | 1101 | int type, type_len = 0; |
43057304 | 1102 | |
5180cacc | 1103 | /* |
46f03448 | 1104 | * The type can be of any size but is followed by |
21666f1a | 1105 | * a space. |
5180cacc | 1106 | */ |
5180cacc LT |
1107 | for (;;) { |
1108 | char c = *hdr++; | |
d21f8426 JH |
1109 | if (!c) |
1110 | return -1; | |
5180cacc LT |
1111 | if (c == ' ') |
1112 | break; | |
46f03448 | 1113 | type_len++; |
5180cacc | 1114 | } |
1f688557 | 1115 | |
46f03448 | 1116 | type = type_from_string_gently(type_buf, type_len, 1); |
6ca32f47 BW |
1117 | if (oi->type_name) |
1118 | strbuf_add(oi->type_name, type_buf, type_len); | |
46f03448 KN |
1119 | /* |
1120 | * Set type to 0 if its an unknown object and | |
2e3a16b2 | 1121 | * we're obtaining the type using '--allow-unknown-type' |
46f03448 KN |
1122 | * option. |
1123 | */ | |
19fc5e84 | 1124 | if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0)) |
46f03448 KN |
1125 | type = 0; |
1126 | else if (type < 0) | |
1127 | die("invalid object type"); | |
1128 | if (oi->typep) | |
1129 | *oi->typep = type; | |
5180cacc LT |
1130 | |
1131 | /* | |
1132 | * The length must follow immediately, and be in canonical | |
1133 | * decimal format (ie "010" is not valid). | |
1134 | */ | |
1135 | size = *hdr++ - '0'; | |
1136 | if (size > 9) | |
1137 | return -1; | |
1138 | if (size) { | |
1139 | for (;;) { | |
1140 | unsigned long c = *hdr - '0'; | |
1141 | if (c > 9) | |
1142 | break; | |
1143 | hdr++; | |
1144 | size = size * 10 + c; | |
1b1005d1 | 1145 | } |
c01f51cc | 1146 | } |
46f03448 KN |
1147 | |
1148 | if (oi->sizep) | |
1149 | *oi->sizep = size; | |
5180cacc LT |
1150 | |
1151 | /* | |
1152 | * The length must be followed by a zero byte | |
1153 | */ | |
46f03448 | 1154 | return *hdr ? -1 : type; |
1f688557 JH |
1155 | } |
1156 | ||
46f03448 | 1157 | int parse_sha1_header(const char *hdr, unsigned long *sizep) |
bf592c50 | 1158 | { |
27b5c1a0 | 1159 | struct object_info oi = OBJECT_INFO_INIT; |
21666f1a | 1160 | |
46f03448 | 1161 | oi.sizep = sizep; |
1f0c0d36 | 1162 | return parse_sha1_header_extended(hdr, &oi, 0); |
bf592c50 DB |
1163 | } |
1164 | ||
4a7c05f7 JN |
1165 | static int sha1_loose_object_info(struct repository *r, |
1166 | const unsigned char *sha1, | |
1167 | struct object_info *oi, int flags) | |
65c2e0c3 | 1168 | { |
46f03448 KN |
1169 | int status = 0; |
1170 | unsigned long mapsize; | |
65c2e0c3 | 1171 | void *map; |
ef49a7a0 | 1172 | git_zstream stream; |
1af64f73 | 1173 | char hdr[MAX_HEADER_LEN]; |
46f03448 | 1174 | struct strbuf hdrbuf = STRBUF_INIT; |
c84a1f3e | 1175 | unsigned long size_scratch; |
65c2e0c3 | 1176 | |
5d642e75 JK |
1177 | if (oi->delta_base_sha1) |
1178 | hashclr(oi->delta_base_sha1); | |
1179 | ||
052fe5ea JK |
1180 | /* |
1181 | * If we don't care about type or size, then we don't | |
4ef8d1dd JH |
1182 | * need to look inside the object at all. Note that we |
1183 | * do not optimize out the stat call, even if the | |
1184 | * caller doesn't care about the disk-size, since our | |
1185 | * return value implicitly indicates whether the | |
1186 | * object even exists. | |
052fe5ea | 1187 | */ |
6ca32f47 | 1188 | if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) { |
771e7d57 | 1189 | const char *path; |
4ef8d1dd | 1190 | struct stat st; |
4a7c05f7 | 1191 | if (stat_sha1_file(r, sha1, &st, &path) < 0) |
4ef8d1dd JH |
1192 | return -1; |
1193 | if (oi->disk_sizep) | |
23c339c0 | 1194 | *oi->disk_sizep = st.st_size; |
052fe5ea JK |
1195 | return 0; |
1196 | } | |
1197 | ||
4a7c05f7 | 1198 | map = map_sha1_file(r, sha1, &mapsize); |
f0df4ed5 | 1199 | if (!map) |
dbea72a8 | 1200 | return -1; |
c84a1f3e JT |
1201 | |
1202 | if (!oi->sizep) | |
1203 | oi->sizep = &size_scratch; | |
1204 | ||
23c339c0 JK |
1205 | if (oi->disk_sizep) |
1206 | *oi->disk_sizep = mapsize; | |
19fc5e84 | 1207 | if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) { |
46f03448 KN |
1208 | if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0) |
1209 | status = error("unable to unpack %s header with --allow-unknown-type", | |
1210 | sha1_to_hex(sha1)); | |
1211 | } else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) | |
36e4d74a JH |
1212 | status = error("unable to unpack %s header", |
1213 | sha1_to_hex(sha1)); | |
46f03448 KN |
1214 | if (status < 0) |
1215 | ; /* Do nothing */ | |
1216 | else if (hdrbuf.len) { | |
1217 | if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0) | |
1218 | status = error("unable to parse %s header with --allow-unknown-type", | |
1219 | sha1_to_hex(sha1)); | |
1220 | } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0) | |
36e4d74a | 1221 | status = error("unable to parse %s header", sha1_to_hex(sha1)); |
c84a1f3e | 1222 | |
b3ea7dd3 | 1223 | if (status >= 0 && oi->contentp) { |
c84a1f3e JT |
1224 | *oi->contentp = unpack_sha1_rest(&stream, hdr, |
1225 | *oi->sizep, sha1); | |
b3ea7dd3 JK |
1226 | if (!*oi->contentp) { |
1227 | git_inflate_end(&stream); | |
1228 | status = -1; | |
1229 | } | |
1230 | } else | |
c84a1f3e JT |
1231 | git_inflate_end(&stream); |
1232 | ||
65c2e0c3 | 1233 | munmap(map, mapsize); |
46f03448 | 1234 | if (status && oi->typep) |
23c339c0 | 1235 | *oi->typep = status; |
c84a1f3e JT |
1236 | if (oi->sizep == &size_scratch) |
1237 | oi->sizep = NULL; | |
46f03448 | 1238 | strbuf_release(&hdrbuf); |
3ab0fb06 | 1239 | oi->whence = OI_LOOSE; |
93cff9a9 | 1240 | return (status < 0) ? status : 0; |
65c2e0c3 JH |
1241 | } |
1242 | ||
8b4c0103 JT |
1243 | int fetch_if_missing = 1; |
1244 | ||
abef9020 | 1245 | int oid_object_info_extended(const struct object_id *oid, struct object_info *oi, unsigned flags) |
f0df4ed5 | 1246 | { |
cd585e2a | 1247 | static struct object_info blank_oi = OBJECT_INFO_INIT; |
f0df4ed5 | 1248 | struct pack_entry e; |
5b086407 | 1249 | int rtype; |
b383a13c | 1250 | const struct object_id *real = oid; |
8b4c0103 | 1251 | int already_retried = 0; |
f0df4ed5 | 1252 | |
b383a13c | 1253 | if (flags & OBJECT_INFO_LOOKUP_REPLACE) |
1254 | real = lookup_replace_object(oid); | |
f0df4ed5 | 1255 | |
b383a13c | 1256 | if (is_null_oid(real)) |
87b5e236 JK |
1257 | return -1; |
1258 | ||
cd585e2a JT |
1259 | if (!oi) |
1260 | oi = &blank_oi; | |
1261 | ||
dfdd4afc | 1262 | if (!(flags & OBJECT_INFO_SKIP_CACHED)) { |
b383a13c | 1263 | struct cached_object *co = find_cached_object(real->hash); |
dfdd4afc JT |
1264 | if (co) { |
1265 | if (oi->typep) | |
1266 | *(oi->typep) = co->type; | |
1267 | if (oi->sizep) | |
1268 | *(oi->sizep) = co->size; | |
1269 | if (oi->disk_sizep) | |
1270 | *(oi->disk_sizep) = 0; | |
1271 | if (oi->delta_base_sha1) | |
1272 | hashclr(oi->delta_base_sha1); | |
6ca32f47 | 1273 | if (oi->type_name) |
debca9d2 | 1274 | strbuf_addstr(oi->type_name, type_name(co->type)); |
dfdd4afc JT |
1275 | if (oi->contentp) |
1276 | *oi->contentp = xmemdupz(co->buf, co->size); | |
1277 | oi->whence = OI_CACHED; | |
1278 | return 0; | |
1279 | } | |
c4d9986f NTND |
1280 | } |
1281 | ||
8b4c0103 | 1282 | while (1) { |
544443cb | 1283 | if (find_pack_entry(the_repository, real, &e)) |
8b4c0103 JT |
1284 | break; |
1285 | ||
024aa469 TI |
1286 | if (flags & OBJECT_INFO_IGNORE_LOOSE) |
1287 | return -1; | |
1288 | ||
ddd63e64 | 1289 | /* Most likely it's a loose object. */ |
cf0b1793 | 1290 | if (!sha1_loose_object_info(the_repository, real->hash, oi, flags)) |
5b086407 | 1291 | return 0; |
ddd63e64 SG |
1292 | |
1293 | /* Not a loose object; someone else may have just packed it. */ | |
2b7750c9 | 1294 | if (!(flags & OBJECT_INFO_QUICK)) { |
3a1ec60c | 1295 | reprepare_packed_git(the_repository); |
544443cb | 1296 | if (find_pack_entry(the_repository, real, &e)) |
2b7750c9 JT |
1297 | break; |
1298 | } | |
8b4c0103 JT |
1299 | |
1300 | /* Check if it is a missing object */ | |
1301 | if (fetch_if_missing && repository_format_partial_clone && | |
1302 | !already_retried) { | |
1303 | /* | |
1304 | * TODO Investigate haveing fetch_object() return | |
1305 | * TODO error/success and stopping the music here. | |
1306 | */ | |
b383a13c | 1307 | fetch_object(repository_format_partial_clone, real->hash); |
8b4c0103 JT |
1308 | already_retried = 1; |
1309 | continue; | |
dfdd4afc | 1310 | } |
8b4c0103 JT |
1311 | |
1312 | return -1; | |
f0df4ed5 | 1313 | } |
3d77d877 | 1314 | |
cd585e2a JT |
1315 | if (oi == &blank_oi) |
1316 | /* | |
1317 | * We know that the caller doesn't actually need the | |
1318 | * information below, so return early. | |
1319 | */ | |
1320 | return 0; | |
23c339c0 | 1321 | rtype = packed_object_info(e.p, e.offset, oi); |
412916ee | 1322 | if (rtype < 0) { |
b383a13c | 1323 | mark_bad_packed_object(e.p, real->hash); |
1324 | return oid_object_info_extended(real, oi, 0); | |
3ab0fb06 | 1325 | } else if (oi->whence == OI_PACKED) { |
9a490590 JH |
1326 | oi->u.packed.offset = e.offset; |
1327 | oi->u.packed.pack = e.p; | |
1328 | oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA || | |
1329 | rtype == OBJ_OFS_DELTA); | |
3d77d877 NP |
1330 | } |
1331 | ||
5b086407 | 1332 | return 0; |
f0df4ed5 JS |
1333 | } |
1334 | ||
3fc0dca9 | 1335 | /* returns enum object_type or negative */ |
abef9020 | 1336 | int oid_object_info(const struct object_id *oid, unsigned long *sizep) |
9a490590 | 1337 | { |
5b086407 | 1338 | enum object_type type; |
27b5c1a0 | 1339 | struct object_info oi = OBJECT_INFO_INIT; |
9a490590 | 1340 | |
5b086407 | 1341 | oi.typep = &type; |
9a490590 | 1342 | oi.sizep = sizep; |
abef9020 | 1343 | if (oid_object_info_extended(oid, &oi, |
1344 | OBJECT_INFO_LOOKUP_REPLACE) < 0) | |
5b086407 JK |
1345 | return -1; |
1346 | return type; | |
9a490590 JH |
1347 | } |
1348 | ||
f1d8130b JT |
1349 | static void *read_object(const unsigned char *sha1, enum object_type *type, |
1350 | unsigned long *size) | |
1351 | { | |
7984f238 | 1352 | struct object_id oid; |
f1d8130b JT |
1353 | struct object_info oi = OBJECT_INFO_INIT; |
1354 | void *content; | |
1355 | oi.typep = type; | |
1356 | oi.sizep = size; | |
1357 | oi.contentp = &content; | |
1358 | ||
7984f238 | 1359 | hashcpy(oid.hash, sha1); |
1360 | ||
abef9020 | 1361 | if (oid_object_info_extended(&oid, &oi, 0) < 0) |
f1d8130b JT |
1362 | return NULL; |
1363 | return content; | |
1364 | } | |
1365 | ||
829e5c3b PO |
1366 | int pretend_object_file(void *buf, unsigned long len, enum object_type type, |
1367 | struct object_id *oid) | |
d66b37bb JH |
1368 | { |
1369 | struct cached_object *co; | |
1370 | ||
169c9c01 | 1371 | hash_object_file(buf, len, type_name(type), oid); |
829e5c3b | 1372 | if (has_sha1_file(oid->hash) || find_cached_object(oid->hash)) |
d66b37bb | 1373 | return 0; |
c7353967 | 1374 | ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc); |
d66b37bb JH |
1375 | co = &cached_objects[cached_object_nr++]; |
1376 | co->size = len; | |
21666f1a | 1377 | co->type = type; |
efa13f7b JH |
1378 | co->buf = xmalloc(len); |
1379 | memcpy(co->buf, buf, len); | |
829e5c3b | 1380 | hashcpy(co->sha1, oid->hash); |
d66b37bb JH |
1381 | return 0; |
1382 | } | |
1383 | ||
b6c4cecc JH |
1384 | /* |
1385 | * This function dies on corrupt objects; the callers who want to | |
1386 | * deal with them should arrange to call read_object() and give error | |
1387 | * messages themselves. | |
1388 | */ | |
b4f5aca4 | 1389 | void *read_object_file_extended(const struct object_id *oid, |
1390 | enum object_type *type, | |
1391 | unsigned long *size, | |
1392 | int lookup_replace) | |
ac939109 | 1393 | { |
3ba7a065 | 1394 | void *data; |
b6c4cecc | 1395 | const struct packed_git *p; |
771e7d57 JK |
1396 | const char *path; |
1397 | struct stat st; | |
b383a13c | 1398 | const struct object_id *repl = lookup_replace ? lookup_replace_object(oid) |
1399 | : oid; | |
b6c4cecc | 1400 | |
3ba7a065 | 1401 | errno = 0; |
b383a13c | 1402 | data = read_object(repl->hash, type, size); |
4bbf5a26 | 1403 | if (data) |
b6c4cecc | 1404 | return data; |
68095570 | 1405 | |
25f3af3f | 1406 | if (errno && errno != ENOENT) |
b4f5aca4 | 1407 | die_errno("failed to read object %s", oid_to_hex(oid)); |
3ba7a065 | 1408 | |
68095570 | 1409 | /* die if we replaced an object with one that does not exist */ |
b383a13c | 1410 | if (repl != oid) |
68095570 | 1411 | die("replacement %s not found for %s", |
b383a13c | 1412 | oid_to_hex(repl), oid_to_hex(oid)); |
68095570 | 1413 | |
cf0b1793 | 1414 | if (!stat_sha1_file(the_repository, repl->hash, &st, &path)) |
b6c4cecc | 1415 | die("loose object %s (stored in %s) is corrupt", |
b383a13c | 1416 | oid_to_hex(repl), path); |
68095570 | 1417 | |
b383a13c | 1418 | if ((p = has_packed_and_bad(repl->hash)) != NULL) |
b6c4cecc | 1419 | die("packed object %s (stored in %s) is corrupt", |
b383a13c | 1420 | oid_to_hex(repl), p->pack_name); |
f5552aee | 1421 | |
b6c4cecc | 1422 | return NULL; |
ac939109 NP |
1423 | } |
1424 | ||
02f0547e | 1425 | void *read_object_with_reference(const struct object_id *oid, |
21666f1a | 1426 | const char *required_type_name, |
40469ee9 | 1427 | unsigned long *size, |
02f0547e | 1428 | struct object_id *actual_oid_return) |
f4913f91 | 1429 | { |
21666f1a | 1430 | enum object_type type, required_type; |
f4913f91 JH |
1431 | void *buffer; |
1432 | unsigned long isize; | |
02f0547e | 1433 | struct object_id actual_oid; |
f4913f91 | 1434 | |
21666f1a | 1435 | required_type = type_from_string(required_type_name); |
02f0547e | 1436 | oidcpy(&actual_oid, oid); |
40469ee9 JH |
1437 | while (1) { |
1438 | int ref_length = -1; | |
1439 | const char *ref_type = NULL; | |
f4913f91 | 1440 | |
b4f5aca4 | 1441 | buffer = read_object_file(&actual_oid, &type, &isize); |
40469ee9 JH |
1442 | if (!buffer) |
1443 | return NULL; | |
21666f1a | 1444 | if (type == required_type) { |
40469ee9 | 1445 | *size = isize; |
02f0547e | 1446 | if (actual_oid_return) |
1447 | oidcpy(actual_oid_return, &actual_oid); | |
40469ee9 JH |
1448 | return buffer; |
1449 | } | |
1450 | /* Handle references */ | |
21666f1a | 1451 | else if (type == OBJ_COMMIT) |
40469ee9 | 1452 | ref_type = "tree "; |
21666f1a | 1453 | else if (type == OBJ_TAG) |
40469ee9 JH |
1454 | ref_type = "object "; |
1455 | else { | |
1456 | free(buffer); | |
1457 | return NULL; | |
1458 | } | |
1459 | ref_length = strlen(ref_type); | |
f4913f91 | 1460 | |
02f0547e | 1461 | if (ref_length + GIT_SHA1_HEXSZ > isize || |
50974ec9 | 1462 | memcmp(buffer, ref_type, ref_length) || |
02f0547e | 1463 | get_oid_hex((char *) buffer + ref_length, &actual_oid)) { |
40469ee9 JH |
1464 | free(buffer); |
1465 | return NULL; | |
1466 | } | |
1cf58e72 | 1467 | free(buffer); |
40469ee9 | 1468 | /* Now we have the ID of the referred-to object in |
02f0547e | 1469 | * actual_oid. Check again. */ |
f4913f91 | 1470 | } |
f4913f91 JH |
1471 | } |
1472 | ||
a09c985e PO |
1473 | static void write_object_file_prepare(const void *buf, unsigned long len, |
1474 | const char *type, struct object_id *oid, | |
1475 | char *hdr, int *hdrlen) | |
d410c0f5 | 1476 | { |
18e2588e | 1477 | git_hash_ctx c; |
d410c0f5 JH |
1478 | |
1479 | /* Generate the header */ | |
ef1286d3 | 1480 | *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1; |
d410c0f5 JH |
1481 | |
1482 | /* Sha1.. */ | |
18e2588e | 1483 | the_hash_algo->init_fn(&c); |
1484 | the_hash_algo->update_fn(&c, hdr, *hdrlen); | |
1485 | the_hash_algo->update_fn(&c, buf, len); | |
0fd90dab | 1486 | the_hash_algo->final_fn(oid->hash, &c); |
d410c0f5 JH |
1487 | } |
1488 | ||
230f1322 | 1489 | /* |
5a688fe4 | 1490 | * Move the just written object into its final resting place. |
230f1322 | 1491 | */ |
cb5add58 | 1492 | int finalize_object_file(const char *tmpfile, const char *filename) |
230f1322 | 1493 | { |
e32c0a9c | 1494 | int ret = 0; |
5a688fe4 | 1495 | |
348df166 | 1496 | if (object_creation_mode == OBJECT_CREATION_USES_RENAMES) |
be66a6c4 JS |
1497 | goto try_rename; |
1498 | else if (link(tmpfile, filename)) | |
e32c0a9c | 1499 | ret = errno; |
7ebb6fca LT |
1500 | |
1501 | /* | |
1502 | * Coda hack - coda doesn't like cross-directory links, | |
1503 | * so we fall back to a rename, which will mean that it | |
1504 | * won't be able to check collisions, but that's not a | |
1505 | * big deal. | |
1506 | * | |
1507 | * The same holds for FAT formatted media. | |
1508 | * | |
3be1f18e | 1509 | * When this succeeds, we just return. We have nothing |
7ebb6fca LT |
1510 | * left to unlink. |
1511 | */ | |
1512 | if (ret && ret != EEXIST) { | |
be66a6c4 | 1513 | try_rename: |
7ebb6fca | 1514 | if (!rename(tmpfile, filename)) |
3be1f18e | 1515 | goto out; |
9e48b389 | 1516 | ret = errno; |
230f1322 | 1517 | } |
691f1a28 | 1518 | unlink_or_warn(tmpfile); |
230f1322 LT |
1519 | if (ret) { |
1520 | if (ret != EEXIST) { | |
7616c6ca | 1521 | return error_errno("unable to write sha1 filename %s", filename); |
230f1322 LT |
1522 | } |
1523 | /* FIXME!!! Collision check here ? */ | |
1524 | } | |
1525 | ||
3be1f18e | 1526 | out: |
5256b006 | 1527 | if (adjust_shared_perm(filename)) |
5a688fe4 | 1528 | return error("unable to set permission to '%s'", filename); |
230f1322 LT |
1529 | return 0; |
1530 | } | |
1531 | ||
4d548150 LT |
1532 | static int write_buffer(int fd, const void *buf, size_t len) |
1533 | { | |
d34cf19b | 1534 | if (write_in_full(fd, buf, len) < 0) |
7616c6ca | 1535 | return error_errno("file write error"); |
4d548150 LT |
1536 | return 0; |
1537 | } | |
1538 | ||
f070facc PO |
1539 | int hash_object_file(const void *buf, unsigned long len, const char *type, |
1540 | struct object_id *oid) | |
abdc3fc8 | 1541 | { |
1af64f73 | 1542 | char hdr[MAX_HEADER_LEN]; |
ef1286d3 | 1543 | int hdrlen = sizeof(hdr); |
a09c985e | 1544 | write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen); |
abdc3fc8 RS |
1545 | return 0; |
1546 | } | |
1547 | ||
e9039dd3 LT |
1548 | /* Finalize a file on disk, and close it. */ |
1549 | static void close_sha1_file(int fd) | |
1550 | { | |
aafe9fba LT |
1551 | if (fsync_object_files) |
1552 | fsync_or_die(fd, "sha1 file"); | |
e9039dd3 | 1553 | if (close(fd) != 0) |
d824cbba | 1554 | die_errno("error when closing sha1 file"); |
e9039dd3 LT |
1555 | } |
1556 | ||
5723fe7e LT |
1557 | /* Size of directory component, including the ending '/' */ |
1558 | static inline int directory_size(const char *filename) | |
1559 | { | |
1560 | const char *s = strrchr(filename, '/'); | |
1561 | if (!s) | |
1562 | return 0; | |
1563 | return s - filename + 1; | |
1564 | } | |
1565 | ||
1566 | /* | |
1567 | * This creates a temporary file in the same directory as the final | |
1568 | * 'filename' | |
1569 | * | |
1570 | * We want to avoid cross-directory filename renames, because those | |
1571 | * can have problems on various filesystems (FAT, NFS, Coda). | |
1572 | */ | |
d4b3d11a | 1573 | static int create_tmpfile(struct strbuf *tmp, const char *filename) |
5723fe7e LT |
1574 | { |
1575 | int fd, dirlen = directory_size(filename); | |
1576 | ||
d4b3d11a JK |
1577 | strbuf_reset(tmp); |
1578 | strbuf_add(tmp, filename, dirlen); | |
1579 | strbuf_addstr(tmp, "tmp_obj_XXXXXX"); | |
1580 | fd = git_mkstemp_mode(tmp->buf, 0444); | |
cbacbf4e | 1581 | if (fd < 0 && dirlen && errno == ENOENT) { |
d4b3d11a JK |
1582 | /* |
1583 | * Make sure the directory exists; note that the contents | |
1584 | * of the buffer are undefined after mkstemp returns an | |
1585 | * error, so we have to rewrite the whole buffer from | |
1586 | * scratch. | |
1587 | */ | |
1588 | strbuf_reset(tmp); | |
1589 | strbuf_add(tmp, filename, dirlen - 1); | |
1590 | if (mkdir(tmp->buf, 0777) && errno != EEXIST) | |
b2476a60 | 1591 | return -1; |
d4b3d11a | 1592 | if (adjust_shared_perm(tmp->buf)) |
5723fe7e LT |
1593 | return -1; |
1594 | ||
1595 | /* Try again */ | |
d4b3d11a JK |
1596 | strbuf_addstr(tmp, "/tmp_obj_XXXXXX"); |
1597 | fd = git_mkstemp_mode(tmp->buf, 0444); | |
5723fe7e LT |
1598 | } |
1599 | return fd; | |
1600 | } | |
1601 | ||
3fc7281f PO |
1602 | static int write_loose_object(const struct object_id *oid, char *hdr, |
1603 | int hdrlen, const void *buf, unsigned long len, | |
1604 | time_t mtime) | |
0fcfd160 | 1605 | { |
915308b1 | 1606 | int fd, ret; |
9892beba | 1607 | unsigned char compressed[4096]; |
ef49a7a0 | 1608 | git_zstream stream; |
18e2588e | 1609 | git_hash_ctx c; |
3fc7281f | 1610 | struct object_id parano_oid; |
d4b3d11a | 1611 | static struct strbuf tmp_file = STRBUF_INIT; |
ea657730 CC |
1612 | static struct strbuf filename = STRBUF_INIT; |
1613 | ||
1614 | strbuf_reset(&filename); | |
cf0b1793 | 1615 | sha1_file_name(the_repository, &filename, oid->hash); |
a44c9a5e | 1616 | |
ea657730 | 1617 | fd = create_tmpfile(&tmp_file, filename.buf); |
aac17941 | 1618 | if (fd < 0) { |
35243577 | 1619 | if (errno == EACCES) |
82247e9b | 1620 | return error("insufficient permission for adding an object to repository database %s", get_object_directory()); |
916d081b | 1621 | else |
7616c6ca | 1622 | return error_errno("unable to create temporary file"); |
aac17941 LT |
1623 | } |
1624 | ||
0fcfd160 | 1625 | /* Set it up */ |
55bb5c91 | 1626 | git_deflate_init(&stream, zlib_compression_level); |
0fcfd160 | 1627 | stream.next_out = compressed; |
9892beba | 1628 | stream.avail_out = sizeof(compressed); |
18e2588e | 1629 | the_hash_algo->init_fn(&c); |
a44c9a5e LT |
1630 | |
1631 | /* First header.. */ | |
d65a16f6 | 1632 | stream.next_in = (unsigned char *)hdr; |
a44c9a5e | 1633 | stream.avail_in = hdrlen; |
55bb5c91 JH |
1634 | while (git_deflate(&stream, 0) == Z_OK) |
1635 | ; /* nothing */ | |
18e2588e | 1636 | the_hash_algo->update_fn(&c, hdr, hdrlen); |
a44c9a5e LT |
1637 | |
1638 | /* Then the data itself.. */ | |
c00e657d | 1639 | stream.next_in = (void *)buf; |
a44c9a5e | 1640 | stream.avail_in = len; |
9892beba | 1641 | do { |
748af44c | 1642 | unsigned char *in0 = stream.next_in; |
55bb5c91 | 1643 | ret = git_deflate(&stream, Z_FINISH); |
18e2588e | 1644 | the_hash_algo->update_fn(&c, in0, stream.next_in - in0); |
9892beba NP |
1645 | if (write_buffer(fd, compressed, stream.next_out - compressed) < 0) |
1646 | die("unable to write sha1 file"); | |
1647 | stream.next_out = compressed; | |
1648 | stream.avail_out = sizeof(compressed); | |
1649 | } while (ret == Z_OK); | |
1650 | ||
ac54c277 | 1651 | if (ret != Z_STREAM_END) |
3fc7281f PO |
1652 | die("unable to deflate new object %s (%d)", oid_to_hex(oid), |
1653 | ret); | |
55bb5c91 | 1654 | ret = git_deflate_end_gently(&stream); |
ac54c277 | 1655 | if (ret != Z_OK) |
3fc7281f PO |
1656 | die("deflateEnd on object %s failed (%d)", oid_to_hex(oid), |
1657 | ret); | |
18e2588e | 1658 | the_hash_algo->final_fn(parano_oid.hash, &c); |
3fc7281f PO |
1659 | if (oidcmp(oid, ¶no_oid) != 0) |
1660 | die("confused by unstable object source data for %s", | |
1661 | oid_to_hex(oid)); | |
ac54c277 | 1662 | |
e9039dd3 | 1663 | close_sha1_file(fd); |
0fcfd160 | 1664 | |
bbac7311 NP |
1665 | if (mtime) { |
1666 | struct utimbuf utb; | |
1667 | utb.actime = mtime; | |
1668 | utb.modtime = mtime; | |
d4b3d11a | 1669 | if (utime(tmp_file.buf, &utb) < 0) |
7616c6ca | 1670 | warning_errno("failed utime() on %s", tmp_file.buf); |
bbac7311 NP |
1671 | } |
1672 | ||
ea657730 | 1673 | return finalize_object_file(tmp_file.buf, filename.buf); |
0fcfd160 | 1674 | } |
8237b185 | 1675 | |
6862ebbf | 1676 | static int freshen_loose_object(const struct object_id *oid) |
33d4221c | 1677 | { |
6862ebbf | 1678 | return check_and_freshen(oid, 1); |
33d4221c JK |
1679 | } |
1680 | ||
6862ebbf | 1681 | static int freshen_packed_object(const struct object_id *oid) |
33d4221c JK |
1682 | { |
1683 | struct pack_entry e; | |
544443cb | 1684 | if (!find_pack_entry(the_repository, oid, &e)) |
ee1c6c34 JK |
1685 | return 0; |
1686 | if (e.p->freshened) | |
1687 | return 1; | |
1688 | if (!freshen_file(e.p->pack_name)) | |
1689 | return 0; | |
1690 | e.p->freshened = 1; | |
1691 | return 1; | |
33d4221c JK |
1692 | } |
1693 | ||
a09c985e PO |
1694 | int write_object_file(const void *buf, unsigned long len, const char *type, |
1695 | struct object_id *oid) | |
bbac7311 | 1696 | { |
1af64f73 | 1697 | char hdr[MAX_HEADER_LEN]; |
ef1286d3 | 1698 | int hdrlen = sizeof(hdr); |
bbac7311 NP |
1699 | |
1700 | /* Normally if we have it in the pack then we do not bother writing | |
1701 | * it out into .git/objects/??/?{38} file. | |
1702 | */ | |
a09c985e | 1703 | write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen); |
6862ebbf | 1704 | if (freshen_packed_object(oid) || freshen_loose_object(oid)) |
bbac7311 | 1705 | return 0; |
3fc7281f | 1706 | return write_loose_object(oid, hdr, hdrlen, buf, len, 0); |
bbac7311 NP |
1707 | } |
1708 | ||
1752cbbc PO |
1709 | int hash_object_file_literally(const void *buf, unsigned long len, |
1710 | const char *type, struct object_id *oid, | |
1711 | unsigned flags) | |
0c3db67c ES |
1712 | { |
1713 | char *header; | |
1714 | int hdrlen, status = 0; | |
1715 | ||
1716 | /* type string, SP, %lu of the length plus NUL must fit this */ | |
1af64f73 | 1717 | hdrlen = strlen(type) + MAX_HEADER_LEN; |
ef1286d3 | 1718 | header = xmalloc(hdrlen); |
a09c985e | 1719 | write_object_file_prepare(buf, len, type, oid, header, &hdrlen); |
0c3db67c ES |
1720 | |
1721 | if (!(flags & HASH_WRITE_OBJECT)) | |
1722 | goto cleanup; | |
6862ebbf | 1723 | if (freshen_packed_object(oid) || freshen_loose_object(oid)) |
0c3db67c | 1724 | goto cleanup; |
3fc7281f | 1725 | status = write_loose_object(oid, header, hdrlen, buf, len, 0); |
0c3db67c ES |
1726 | |
1727 | cleanup: | |
1728 | free(header); | |
1729 | return status; | |
1730 | } | |
1731 | ||
4bdb70a4 | 1732 | int force_object_loose(const struct object_id *oid, time_t mtime) |
bbac7311 | 1733 | { |
bbac7311 NP |
1734 | void *buf; |
1735 | unsigned long len; | |
1736 | enum object_type type; | |
1af64f73 | 1737 | char hdr[MAX_HEADER_LEN]; |
bbac7311 | 1738 | int hdrlen; |
1fb23e65 | 1739 | int ret; |
bbac7311 | 1740 | |
6862ebbf | 1741 | if (has_loose_object(oid)) |
bbac7311 | 1742 | return 0; |
4bdb70a4 | 1743 | buf = read_object(oid->hash, &type, &len); |
bbac7311 | 1744 | if (!buf) |
4bdb70a4 | 1745 | return error("cannot read sha1_file for %s", oid_to_hex(oid)); |
debca9d2 | 1746 | hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), len) + 1; |
3fc7281f | 1747 | ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime); |
1fb23e65 BS |
1748 | free(buf); |
1749 | ||
1750 | return ret; | |
bbac7311 NP |
1751 | } |
1752 | ||
0eeb077b | 1753 | int has_sha1_file_with_flags(const unsigned char *sha1, int flags) |
8237b185 | 1754 | { |
7984f238 | 1755 | struct object_id oid; |
3e8b7d3c JN |
1756 | if (!startup_info->have_repository) |
1757 | return 0; | |
7984f238 | 1758 | hashcpy(oid.hash, sha1); |
abef9020 | 1759 | return oid_object_info_extended(&oid, NULL, |
1760 | flags | OBJECT_INFO_SKIP_CACHED) >= 0; | |
b419aa25 | 1761 | } |
1762 | ||
1763 | int has_object_file(const struct object_id *oid) | |
1764 | { | |
1765 | return has_sha1_file(oid->hash); | |
8237b185 | 1766 | } |
74400e71 | 1767 | |
5827a035 JK |
1768 | int has_object_file_with_flags(const struct object_id *oid, int flags) |
1769 | { | |
1770 | return has_sha1_file_with_flags(oid->hash, flags); | |
1771 | } | |
1772 | ||
c879daa2 NTND |
1773 | static void check_tree(const void *buf, size_t size) |
1774 | { | |
1775 | struct tree_desc desc; | |
1776 | struct name_entry entry; | |
1777 | ||
1778 | init_tree_desc(&desc, buf, size); | |
1779 | while (tree_entry(&desc, &entry)) | |
1780 | /* do nothing | |
1781 | * tree_entry() will die() on malformed entries */ | |
1782 | ; | |
1783 | } | |
1784 | ||
1785 | static void check_commit(const void *buf, size_t size) | |
1786 | { | |
1787 | struct commit c; | |
1788 | memset(&c, 0, sizeof(c)); | |
1789 | if (parse_commit_buffer(&c, buf, size)) | |
1790 | die("corrupt commit"); | |
1791 | } | |
1792 | ||
1793 | static void check_tag(const void *buf, size_t size) | |
1794 | { | |
1795 | struct tag t; | |
1796 | memset(&t, 0, sizeof(t)); | |
1797 | if (parse_tag_buffer(&t, buf, size)) | |
1798 | die("corrupt tag"); | |
1799 | } | |
1800 | ||
bcd29864 | 1801 | static int index_mem(struct object_id *oid, void *buf, size_t size, |
c4ce46fc JH |
1802 | enum object_type type, |
1803 | const char *path, unsigned flags) | |
e7332f96 | 1804 | { |
6c510bee | 1805 | int ret, re_allocated = 0; |
c4ce46fc | 1806 | int write_object = flags & HASH_WRITE_OBJECT; |
74400e71 | 1807 | |
7672db20 | 1808 | if (!type) |
edaec3fb | 1809 | type = OBJ_BLOB; |
6c510bee LT |
1810 | |
1811 | /* | |
1812 | * Convert blobs to git internal format | |
1813 | */ | |
43df4f86 | 1814 | if ((type == OBJ_BLOB) && path) { |
f285a2d7 | 1815 | struct strbuf nbuf = STRBUF_INIT; |
82b474e0 | 1816 | if (convert_to_git(&the_index, path, buf, size, &nbuf, |
8462ff43 | 1817 | get_conv_flags(flags))) { |
b315c5c0 | 1818 | buf = strbuf_detach(&nbuf, &size); |
6c510bee LT |
1819 | re_allocated = 1; |
1820 | } | |
1821 | } | |
c4ce46fc | 1822 | if (flags & HASH_FORMAT_CHECK) { |
c879daa2 NTND |
1823 | if (type == OBJ_TREE) |
1824 | check_tree(buf, size); | |
1825 | if (type == OBJ_COMMIT) | |
1826 | check_commit(buf, size); | |
1827 | if (type == OBJ_TAG) | |
1828 | check_tag(buf, size); | |
1829 | } | |
6c510bee | 1830 | |
7672db20 | 1831 | if (write_object) |
169c9c01 | 1832 | ret = write_object_file(buf, size, type_name(type), oid); |
abdc3fc8 | 1833 | else |
169c9c01 | 1834 | ret = hash_object_file(buf, size, type_name(type), oid); |
43df4f86 | 1835 | if (re_allocated) |
6c510bee | 1836 | free(buf); |
43df4f86 DP |
1837 | return ret; |
1838 | } | |
1839 | ||
bcd29864 | 1840 | static int index_stream_convert_blob(struct object_id *oid, int fd, |
9035d75a SP |
1841 | const char *path, unsigned flags) |
1842 | { | |
1843 | int ret; | |
1844 | const int write_object = flags & HASH_WRITE_OBJECT; | |
1845 | struct strbuf sbuf = STRBUF_INIT; | |
1846 | ||
1847 | assert(path); | |
1848 | assert(would_convert_to_git_filter_fd(path)); | |
1849 | ||
d6c41c20 | 1850 | convert_to_git_filter_fd(&the_index, path, fd, &sbuf, |
8462ff43 | 1851 | get_conv_flags(flags)); |
9035d75a SP |
1852 | |
1853 | if (write_object) | |
169c9c01 | 1854 | ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), |
a09c985e | 1855 | oid); |
9035d75a | 1856 | else |
169c9c01 | 1857 | ret = hash_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), |
f070facc | 1858 | oid); |
9035d75a SP |
1859 | strbuf_release(&sbuf); |
1860 | return ret; | |
1861 | } | |
1862 | ||
bcd29864 | 1863 | static int index_pipe(struct object_id *oid, int fd, enum object_type type, |
7b41e1e1 JH |
1864 | const char *path, unsigned flags) |
1865 | { | |
1866 | struct strbuf sbuf = STRBUF_INIT; | |
1867 | int ret; | |
1868 | ||
1869 | if (strbuf_read(&sbuf, fd, 4096) >= 0) | |
bcd29864 | 1870 | ret = index_mem(oid, sbuf.buf, sbuf.len, type, path, flags); |
7b41e1e1 JH |
1871 | else |
1872 | ret = -1; | |
1873 | strbuf_release(&sbuf); | |
1874 | return ret; | |
1875 | } | |
1876 | ||
ea68b0ce DP |
1877 | #define SMALL_FILE_SIZE (32*1024) |
1878 | ||
bcd29864 | 1879 | static int index_core(struct object_id *oid, int fd, size_t size, |
7b41e1e1 JH |
1880 | enum object_type type, const char *path, |
1881 | unsigned flags) | |
43df4f86 DP |
1882 | { |
1883 | int ret; | |
43df4f86 | 1884 | |
7b41e1e1 | 1885 | if (!size) { |
bcd29864 | 1886 | ret = index_mem(oid, "", size, type, path, flags); |
ea68b0ce DP |
1887 | } else if (size <= SMALL_FILE_SIZE) { |
1888 | char *buf = xmalloc(size); | |
90dca671 JK |
1889 | ssize_t read_result = read_in_full(fd, buf, size); |
1890 | if (read_result < 0) | |
1891 | ret = error_errno("read error while indexing %s", | |
1892 | path ? path : "<unknown>"); | |
1893 | else if (read_result != size) | |
1894 | ret = error("short read while indexing %s", | |
1895 | path ? path : "<unknown>"); | |
ea68b0ce | 1896 | else |
bcd29864 | 1897 | ret = index_mem(oid, buf, size, type, path, flags); |
ea68b0ce | 1898 | free(buf); |
08bda208 | 1899 | } else { |
43df4f86 | 1900 | void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
bcd29864 | 1901 | ret = index_mem(oid, buf, size, type, path, flags); |
aac17941 | 1902 | munmap(buf, size); |
08bda208 | 1903 | } |
7b41e1e1 JH |
1904 | return ret; |
1905 | } | |
1906 | ||
4dd1fbc7 | 1907 | /* |
568508e7 JH |
1908 | * This creates one packfile per large blob unless bulk-checkin |
1909 | * machinery is "plugged". | |
4dd1fbc7 JH |
1910 | * |
1911 | * This also bypasses the usual "convert-to-git" dance, and that is on | |
1912 | * purpose. We could write a streaming version of the converting | |
1913 | * functions and insert that before feeding the data to fast-import | |
4f22b101 JK |
1914 | * (or equivalent in-core API described above). However, that is |
1915 | * somewhat complicated, as we do not know the size of the filter | |
1916 | * result, which we need to know beforehand when writing a git object. | |
1917 | * Since the primary motivation for trying to stream from the working | |
1918 | * tree file and to avoid mmaping it in core is to deal with large | |
1919 | * binary blobs, they generally do not want to get any conversion, and | |
1920 | * callers should avoid this code path when filters are requested. | |
4dd1fbc7 | 1921 | */ |
7d5e1dc3 | 1922 | static int index_stream(struct object_id *oid, int fd, size_t size, |
4dd1fbc7 JH |
1923 | enum object_type type, const char *path, |
1924 | unsigned flags) | |
1925 | { | |
68ee6dfc | 1926 | return index_bulk_checkin(oid, fd, size, type, path, flags); |
4dd1fbc7 JH |
1927 | } |
1928 | ||
e3506559 | 1929 | int index_fd(struct object_id *oid, int fd, struct stat *st, |
7b41e1e1 JH |
1930 | enum object_type type, const char *path, unsigned flags) |
1931 | { | |
1932 | int ret; | |
7b41e1e1 | 1933 | |
9079ab7c SP |
1934 | /* |
1935 | * Call xsize_t() only when needed to avoid potentially unnecessary | |
1936 | * die() for large files. | |
1937 | */ | |
9035d75a | 1938 | if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path)) |
bcd29864 | 1939 | ret = index_stream_convert_blob(oid, fd, path, flags); |
9035d75a | 1940 | else if (!S_ISREG(st->st_mode)) |
bcd29864 | 1941 | ret = index_pipe(oid, fd, type, path, flags); |
9079ab7c | 1942 | else if (st->st_size <= big_file_threshold || type != OBJ_BLOB || |
82b474e0 | 1943 | (path && would_convert_to_git(&the_index, path))) |
bcd29864 | 1944 | ret = index_core(oid, fd, xsize_t(st->st_size), type, path, |
9079ab7c | 1945 | flags); |
4dd1fbc7 | 1946 | else |
7d5e1dc3 | 1947 | ret = index_stream(oid, fd, xsize_t(st->st_size), type, path, |
9079ab7c | 1948 | flags); |
43df4f86 | 1949 | close(fd); |
aac17941 | 1950 | return ret; |
74400e71 | 1951 | } |
ec1fcc16 | 1952 | |
98e019b0 | 1953 | int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags) |
ec1fcc16 JH |
1954 | { |
1955 | int fd; | |
b760d3aa | 1956 | struct strbuf sb = STRBUF_INIT; |
ea8e0297 | 1957 | int rc = 0; |
ec1fcc16 JH |
1958 | |
1959 | switch (st->st_mode & S_IFMT) { | |
1960 | case S_IFREG: | |
1961 | fd = open(path, O_RDONLY); | |
1962 | if (fd < 0) | |
7616c6ca | 1963 | return error_errno("open(\"%s\")", path); |
e3506559 | 1964 | if (index_fd(oid, fd, st, OBJ_BLOB, path, flags) < 0) |
ec1fcc16 JH |
1965 | return error("%s: failed to insert into database", |
1966 | path); | |
1967 | break; | |
1968 | case S_IFLNK: | |
7616c6ca NTND |
1969 | if (strbuf_readlink(&sb, path, st->st_size)) |
1970 | return error_errno("readlink(\"%s\")", path); | |
c4ce46fc | 1971 | if (!(flags & HASH_WRITE_OBJECT)) |
f070facc | 1972 | hash_object_file(sb.buf, sb.len, blob_type, oid); |
a09c985e | 1973 | else if (write_object_file(sb.buf, sb.len, blob_type, oid)) |
ea8e0297 | 1974 | rc = error("%s: failed to insert into database", path); |
b760d3aa | 1975 | strbuf_release(&sb); |
ec1fcc16 | 1976 | break; |
f35a6d3b | 1977 | case S_IFDIR: |
a98e6101 | 1978 | return resolve_gitlink_ref(path, "HEAD", oid); |
ec1fcc16 JH |
1979 | default: |
1980 | return error("%s: unsupported file type", path); | |
1981 | } | |
ea8e0297 | 1982 | return rc; |
ec1fcc16 | 1983 | } |
a69e5429 JH |
1984 | |
1985 | int read_pack_header(int fd, struct pack_header *header) | |
1986 | { | |
f48ecd38 | 1987 | if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header)) |
c697ad14 HO |
1988 | /* "eof before pack header was fully read" */ |
1989 | return PH_ERROR_EOF; | |
1990 | ||
a69e5429 JH |
1991 | if (header->hdr_signature != htonl(PACK_SIGNATURE)) |
1992 | /* "protocol error (pack signature mismatch detected)" */ | |
1993 | return PH_ERROR_PACK_SIGNATURE; | |
1994 | if (!pack_version_ok(header->hdr_version)) | |
1995 | /* "protocol error (pack version unsupported)" */ | |
1996 | return PH_ERROR_PROTOCOL; | |
1997 | return 0; | |
1998 | } | |
40d52ff7 | 1999 | |
e816caa0 | 2000 | void assert_oid_type(const struct object_id *oid, enum object_type expect) |
40d52ff7 | 2001 | { |
abef9020 | 2002 | enum object_type type = oid_object_info(oid, NULL); |
40d52ff7 | 2003 | if (type < 0) |
e816caa0 | 2004 | die("%s is not a valid object", oid_to_hex(oid)); |
40d52ff7 | 2005 | if (type != expect) |
e816caa0 | 2006 | die("%s is not a valid '%s' object", oid_to_hex(oid), |
debca9d2 | 2007 | type_name(expect)); |
40d52ff7 | 2008 | } |
27e1e22d | 2009 | |
70c49050 | 2010 | int for_each_file_in_obj_subdir(unsigned int subdir_nr, |
cc817ca3 RS |
2011 | struct strbuf *path, |
2012 | each_loose_object_fn obj_cb, | |
2013 | each_loose_cruft_fn cruft_cb, | |
2014 | each_loose_subdir_fn subdir_cb, | |
2015 | void *data) | |
27e1e22d | 2016 | { |
0375f472 RS |
2017 | size_t origlen, baselen; |
2018 | DIR *dir; | |
27e1e22d JK |
2019 | struct dirent *de; |
2020 | int r = 0; | |
62a24c89 | 2021 | struct object_id oid; |
27e1e22d | 2022 | |
70c49050 RS |
2023 | if (subdir_nr > 0xff) |
2024 | BUG("invalid loose object subdirectory: %x", subdir_nr); | |
2025 | ||
0375f472 RS |
2026 | origlen = path->len; |
2027 | strbuf_complete(path, '/'); | |
2028 | strbuf_addf(path, "%02x", subdir_nr); | |
0375f472 RS |
2029 | |
2030 | dir = opendir(path->buf); | |
27e1e22d | 2031 | if (!dir) { |
0375f472 RS |
2032 | if (errno != ENOENT) |
2033 | r = error_errno("unable to open %s", path->buf); | |
2034 | strbuf_setlen(path, origlen); | |
2035 | return r; | |
27e1e22d JK |
2036 | } |
2037 | ||
62a24c89 | 2038 | oid.hash[0] = subdir_nr; |
163ee5e6 DS |
2039 | strbuf_addch(path, '/'); |
2040 | baselen = path->len; | |
62a24c89 | 2041 | |
27e1e22d | 2042 | while ((de = readdir(dir))) { |
163ee5e6 | 2043 | size_t namelen; |
27e1e22d JK |
2044 | if (is_dot_or_dotdot(de->d_name)) |
2045 | continue; | |
2046 | ||
163ee5e6 | 2047 | namelen = strlen(de->d_name); |
27e1e22d | 2048 | strbuf_setlen(path, baselen); |
163ee5e6 DS |
2049 | strbuf_add(path, de->d_name, namelen); |
2050 | if (namelen == GIT_SHA1_HEXSZ - 2 && | |
62a24c89 RS |
2051 | !hex_to_bytes(oid.hash + 1, de->d_name, |
2052 | GIT_SHA1_RAWSZ - 1)) { | |
2053 | if (obj_cb) { | |
2054 | r = obj_cb(&oid, path->buf, data); | |
2055 | if (r) | |
2056 | break; | |
27e1e22d | 2057 | } |
62a24c89 | 2058 | continue; |
27e1e22d JK |
2059 | } |
2060 | ||
2061 | if (cruft_cb) { | |
2062 | r = cruft_cb(de->d_name, path->buf, data); | |
2063 | if (r) | |
2064 | break; | |
2065 | } | |
2066 | } | |
094c7e63 | 2067 | closedir(dir); |
27e1e22d | 2068 | |
163ee5e6 | 2069 | strbuf_setlen(path, baselen - 1); |
27e1e22d JK |
2070 | if (!r && subdir_cb) |
2071 | r = subdir_cb(subdir_nr, path->buf, data); | |
2072 | ||
0375f472 RS |
2073 | strbuf_setlen(path, origlen); |
2074 | ||
27e1e22d JK |
2075 | return r; |
2076 | } | |
2077 | ||
e6f875e0 | 2078 | int for_each_loose_file_in_objdir_buf(struct strbuf *path, |
27e1e22d JK |
2079 | each_loose_object_fn obj_cb, |
2080 | each_loose_cruft_fn cruft_cb, | |
2081 | each_loose_subdir_fn subdir_cb, | |
2082 | void *data) | |
2083 | { | |
27e1e22d JK |
2084 | int r = 0; |
2085 | int i; | |
2086 | ||
27e1e22d | 2087 | for (i = 0; i < 256; i++) { |
e6f875e0 | 2088 | r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb, |
27e1e22d | 2089 | subdir_cb, data); |
27e1e22d JK |
2090 | if (r) |
2091 | break; | |
2092 | } | |
2093 | ||
e6f875e0 JK |
2094 | return r; |
2095 | } | |
2096 | ||
2097 | int for_each_loose_file_in_objdir(const char *path, | |
2098 | each_loose_object_fn obj_cb, | |
2099 | each_loose_cruft_fn cruft_cb, | |
2100 | each_loose_subdir_fn subdir_cb, | |
2101 | void *data) | |
2102 | { | |
2103 | struct strbuf buf = STRBUF_INIT; | |
2104 | int r; | |
2105 | ||
2106 | strbuf_addstr(&buf, path); | |
2107 | r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb, | |
2108 | subdir_cb, data); | |
27e1e22d | 2109 | strbuf_release(&buf); |
e6f875e0 | 2110 | |
27e1e22d JK |
2111 | return r; |
2112 | } | |
660c889e JK |
2113 | |
2114 | struct loose_alt_odb_data { | |
2115 | each_loose_object_fn *cb; | |
2116 | void *data; | |
2117 | }; | |
2118 | ||
2119 | static int loose_from_alt_odb(struct alternate_object_database *alt, | |
2120 | void *vdata) | |
2121 | { | |
2122 | struct loose_alt_odb_data *data = vdata; | |
b0a42642 JM |
2123 | struct strbuf buf = STRBUF_INIT; |
2124 | int r; | |
2125 | ||
597f9134 | 2126 | strbuf_addstr(&buf, alt->path); |
b0a42642 JM |
2127 | r = for_each_loose_file_in_objdir_buf(&buf, |
2128 | data->cb, NULL, NULL, | |
2129 | data->data); | |
2130 | strbuf_release(&buf); | |
2131 | return r; | |
660c889e JK |
2132 | } |
2133 | ||
1385bb7b | 2134 | int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags) |
660c889e JK |
2135 | { |
2136 | struct loose_alt_odb_data alt; | |
2137 | int r; | |
2138 | ||
2139 | r = for_each_loose_file_in_objdir(get_object_directory(), | |
2140 | cb, NULL, NULL, data); | |
2141 | if (r) | |
2142 | return r; | |
2143 | ||
1385bb7b JK |
2144 | if (flags & FOR_EACH_OBJECT_LOCAL_ONLY) |
2145 | return 0; | |
2146 | ||
660c889e JK |
2147 | alt.cb = cb; |
2148 | alt.data = data; | |
2149 | return foreach_alt_odb(loose_from_alt_odb, &alt); | |
2150 | } | |
2151 | ||
f6371f92 JK |
2152 | static int check_stream_sha1(git_zstream *stream, |
2153 | const char *hdr, | |
2154 | unsigned long size, | |
2155 | const char *path, | |
2156 | const unsigned char *expected_sha1) | |
2157 | { | |
18e2588e | 2158 | git_hash_ctx c; |
cd02599c | 2159 | unsigned char real_sha1[GIT_MAX_RAWSZ]; |
f6371f92 JK |
2160 | unsigned char buf[4096]; |
2161 | unsigned long total_read; | |
2162 | int status = Z_OK; | |
2163 | ||
18e2588e | 2164 | the_hash_algo->init_fn(&c); |
2165 | the_hash_algo->update_fn(&c, hdr, stream->total_out); | |
f6371f92 JK |
2166 | |
2167 | /* | |
2168 | * We already read some bytes into hdr, but the ones up to the NUL | |
2169 | * do not count against the object's content size. | |
2170 | */ | |
2171 | total_read = stream->total_out - strlen(hdr) - 1; | |
2172 | ||
2173 | /* | |
2174 | * This size comparison must be "<=" to read the final zlib packets; | |
2175 | * see the comment in unpack_sha1_rest for details. | |
2176 | */ | |
2177 | while (total_read <= size && | |
2178 | (status == Z_OK || status == Z_BUF_ERROR)) { | |
2179 | stream->next_out = buf; | |
2180 | stream->avail_out = sizeof(buf); | |
2181 | if (size - total_read < stream->avail_out) | |
2182 | stream->avail_out = size - total_read; | |
2183 | status = git_inflate(stream, Z_FINISH); | |
18e2588e | 2184 | the_hash_algo->update_fn(&c, buf, stream->next_out - buf); |
f6371f92 JK |
2185 | total_read += stream->next_out - buf; |
2186 | } | |
2187 | git_inflate_end(stream); | |
2188 | ||
2189 | if (status != Z_STREAM_END) { | |
2190 | error("corrupt loose object '%s'", sha1_to_hex(expected_sha1)); | |
2191 | return -1; | |
2192 | } | |
cce044df JK |
2193 | if (stream->avail_in) { |
2194 | error("garbage at end of loose object '%s'", | |
2195 | sha1_to_hex(expected_sha1)); | |
2196 | return -1; | |
2197 | } | |
f6371f92 | 2198 | |
18e2588e | 2199 | the_hash_algo->final_fn(real_sha1, &c); |
f6371f92 JK |
2200 | if (hashcmp(expected_sha1, real_sha1)) { |
2201 | error("sha1 mismatch for %s (expected %s)", path, | |
2202 | sha1_to_hex(expected_sha1)); | |
2203 | return -1; | |
2204 | } | |
2205 | ||
2206 | return 0; | |
2207 | } | |
2208 | ||
2209 | int read_loose_object(const char *path, | |
d61d87bd | 2210 | const struct object_id *expected_oid, |
f6371f92 JK |
2211 | enum object_type *type, |
2212 | unsigned long *size, | |
2213 | void **contents) | |
2214 | { | |
2215 | int ret = -1; | |
f6371f92 JK |
2216 | void *map = NULL; |
2217 | unsigned long mapsize; | |
2218 | git_zstream stream; | |
1af64f73 | 2219 | char hdr[MAX_HEADER_LEN]; |
f6371f92 JK |
2220 | |
2221 | *contents = NULL; | |
2222 | ||
332295d7 | 2223 | map = map_sha1_file_1(the_repository, path, NULL, &mapsize); |
f6371f92 JK |
2224 | if (!map) { |
2225 | error_errno("unable to mmap %s", path); | |
2226 | goto out; | |
2227 | } | |
2228 | ||
2229 | if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) { | |
2230 | error("unable to unpack header of %s", path); | |
2231 | goto out; | |
2232 | } | |
2233 | ||
2234 | *type = parse_sha1_header(hdr, size); | |
2235 | if (*type < 0) { | |
2236 | error("unable to parse header of %s", path); | |
2237 | git_inflate_end(&stream); | |
2238 | goto out; | |
2239 | } | |
2240 | ||
2241 | if (*type == OBJ_BLOB) { | |
d61d87bd | 2242 | if (check_stream_sha1(&stream, hdr, *size, path, expected_oid->hash) < 0) |
f6371f92 JK |
2243 | goto out; |
2244 | } else { | |
d61d87bd | 2245 | *contents = unpack_sha1_rest(&stream, hdr, *size, expected_oid->hash); |
f6371f92 JK |
2246 | if (!*contents) { |
2247 | error("unable to unpack contents of %s", path); | |
2248 | git_inflate_end(&stream); | |
2249 | goto out; | |
2250 | } | |
17e65451 | 2251 | if (check_object_signature(expected_oid, *contents, |
debca9d2 | 2252 | *size, type_name(*type))) { |
f6371f92 | 2253 | error("sha1 mismatch for %s (expected %s)", path, |
d61d87bd | 2254 | oid_to_hex(expected_oid)); |
f6371f92 JK |
2255 | free(*contents); |
2256 | goto out; | |
2257 | } | |
2258 | } | |
2259 | ||
2260 | ret = 0; /* everything checks out */ | |
2261 | ||
2262 | out: | |
2263 | if (map) | |
2264 | munmap(map, mapsize); | |
f6371f92 JK |
2265 | return ret; |
2266 | } |