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