]>
Commit | Line | Data |
---|---|---|
0fcfd160 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | * | |
e5afd444 | 6 | * This handles basic git object files - packing, unpacking, |
0fcfd160 LT |
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" |
bc626927 | 23 | #include "hash-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" |
90c62155 | 33 | #include "object-store.h" |
b14ed5ad | 34 | #include "promisor-remote.h" |
a35e03de | 35 | #include "submodule.h" |
69bbbe48 | 36 | #include "fsck.h" |
e05db0fd | 37 | |
1af64f73 | 38 | /* The maximum size for an object header. */ |
39 | #define MAX_HEADER_LEN 32 | |
40 | ||
e1ccd7e2 | 41 | |
42 | #define EMPTY_TREE_SHA1_BIN_LITERAL \ | |
43 | "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \ | |
44 | "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04" | |
13eeedb5 | 45 | #define EMPTY_TREE_SHA256_BIN_LITERAL \ |
46 | "\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1" \ | |
47 | "\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5" \ | |
48 | "\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc" \ | |
49 | "\x53\x21" | |
e1ccd7e2 | 50 | |
51 | #define EMPTY_BLOB_SHA1_BIN_LITERAL \ | |
52 | "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \ | |
53 | "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91" | |
13eeedb5 | 54 | #define EMPTY_BLOB_SHA256_BIN_LITERAL \ |
55 | "\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2" \ | |
56 | "\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43" \ | |
57 | "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \ | |
58 | "\x18\x13" | |
e1ccd7e2 | 59 | |
e1ccd7e2 | 60 | static const struct object_id empty_tree_oid = { |
5a6dce70 | 61 | .hash = EMPTY_TREE_SHA1_BIN_LITERAL, |
62 | .algo = GIT_HASH_SHA1, | |
8576fde6 | 63 | }; |
e1ccd7e2 | 64 | static const struct object_id empty_blob_oid = { |
5a6dce70 | 65 | .hash = EMPTY_BLOB_SHA1_BIN_LITERAL, |
66 | .algo = GIT_HASH_SHA1, | |
8576fde6 | 67 | }; |
14228447 | 68 | static const struct object_id null_oid_sha1 = { |
69 | .hash = {0}, | |
70 | .algo = GIT_HASH_SHA1, | |
71 | }; | |
13eeedb5 | 72 | static const struct object_id empty_tree_oid_sha256 = { |
5a6dce70 | 73 | .hash = EMPTY_TREE_SHA256_BIN_LITERAL, |
74 | .algo = GIT_HASH_SHA256, | |
13eeedb5 | 75 | }; |
76 | static const struct object_id empty_blob_oid_sha256 = { | |
5a6dce70 | 77 | .hash = EMPTY_BLOB_SHA256_BIN_LITERAL, |
78 | .algo = GIT_HASH_SHA256, | |
13eeedb5 | 79 | }; |
14228447 | 80 | static const struct object_id null_oid_sha256 = { |
81 | .hash = {0}, | |
82 | .algo = GIT_HASH_SHA256, | |
83 | }; | |
88cd621d | 84 | |
ac73cedf | 85 | static void git_hash_sha1_init(git_hash_ctx *ctx) |
f50e766b | 86 | { |
ac73cedf | 87 | git_SHA1_Init(&ctx->sha1); |
f50e766b | 88 | } |
89 | ||
768e30ea | 90 | static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src) |
91 | { | |
92 | git_SHA1_Clone(&dst->sha1, &src->sha1); | |
93 | } | |
94 | ||
ac73cedf | 95 | static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len) |
f50e766b | 96 | { |
ac73cedf | 97 | git_SHA1_Update(&ctx->sha1, data, len); |
f50e766b | 98 | } |
99 | ||
ac73cedf | 100 | static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx) |
f50e766b | 101 | { |
ac73cedf | 102 | git_SHA1_Final(hash, &ctx->sha1); |
f50e766b | 103 | } |
104 | ||
ab795f0d | 105 | static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx) |
106 | { | |
107 | git_SHA1_Final(oid->hash, &ctx->sha1); | |
108 | memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ); | |
5a6dce70 | 109 | oid->algo = GIT_HASH_SHA1; |
ab795f0d | 110 | } |
111 | ||
13eeedb5 | 112 | |
113 | static void git_hash_sha256_init(git_hash_ctx *ctx) | |
114 | { | |
115 | git_SHA256_Init(&ctx->sha256); | |
116 | } | |
117 | ||
768e30ea | 118 | static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src) |
119 | { | |
120 | git_SHA256_Clone(&dst->sha256, &src->sha256); | |
121 | } | |
122 | ||
13eeedb5 | 123 | static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len) |
124 | { | |
125 | git_SHA256_Update(&ctx->sha256, data, len); | |
126 | } | |
127 | ||
128 | static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx) | |
129 | { | |
130 | git_SHA256_Final(hash, &ctx->sha256); | |
131 | } | |
132 | ||
ab795f0d | 133 | static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx) |
134 | { | |
135 | git_SHA256_Final(oid->hash, &ctx->sha256); | |
136 | /* | |
137 | * This currently does nothing, so the compiler should optimize it out, | |
138 | * but keep it in case we extend the hash size again. | |
139 | */ | |
140 | memset(oid->hash + GIT_SHA256_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA256_RAWSZ); | |
5a6dce70 | 141 | oid->algo = GIT_HASH_SHA256; |
ab795f0d | 142 | } |
143 | ||
9eb6cdad | 144 | static void git_hash_unknown_init(git_hash_ctx *ctx UNUSED) |
f50e766b | 145 | { |
1a07e59c | 146 | BUG("trying to init unknown hash"); |
f50e766b | 147 | } |
148 | ||
9eb6cdad JK |
149 | static void git_hash_unknown_clone(git_hash_ctx *dst UNUSED, |
150 | const git_hash_ctx *src UNUSED) | |
768e30ea | 151 | { |
152 | BUG("trying to clone unknown hash"); | |
153 | } | |
154 | ||
9eb6cdad JK |
155 | static void git_hash_unknown_update(git_hash_ctx *ctx UNUSED, |
156 | const void *data UNUSED, | |
157 | size_t len UNUSED) | |
f50e766b | 158 | { |
1a07e59c | 159 | BUG("trying to update unknown hash"); |
f50e766b | 160 | } |
161 | ||
9eb6cdad JK |
162 | static void git_hash_unknown_final(unsigned char *hash UNUSED, |
163 | git_hash_ctx *ctx UNUSED) | |
f50e766b | 164 | { |
1a07e59c | 165 | BUG("trying to finalize unknown hash"); |
f50e766b | 166 | } |
167 | ||
9eb6cdad JK |
168 | static void git_hash_unknown_final_oid(struct object_id *oid UNUSED, |
169 | git_hash_ctx *ctx UNUSED) | |
ab795f0d | 170 | { |
171 | BUG("trying to finalize unknown hash"); | |
172 | } | |
173 | ||
f50e766b | 174 | const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { |
175 | { | |
0cb9872e ÆAB |
176 | .name = NULL, |
177 | .format_id = 0x00000000, | |
178 | .rawsz = 0, | |
179 | .hexsz = 0, | |
180 | .blksz = 0, | |
181 | .init_fn = git_hash_unknown_init, | |
182 | .clone_fn = git_hash_unknown_clone, | |
183 | .update_fn = git_hash_unknown_update, | |
184 | .final_fn = git_hash_unknown_final, | |
185 | .final_oid_fn = git_hash_unknown_final_oid, | |
186 | .empty_tree = NULL, | |
187 | .empty_blob = NULL, | |
188 | .null_oid = NULL, | |
f50e766b | 189 | }, |
190 | { | |
0cb9872e ÆAB |
191 | .name = "sha1", |
192 | .format_id = GIT_SHA1_FORMAT_ID, | |
193 | .rawsz = GIT_SHA1_RAWSZ, | |
194 | .hexsz = GIT_SHA1_HEXSZ, | |
195 | .blksz = GIT_SHA1_BLKSZ, | |
196 | .init_fn = git_hash_sha1_init, | |
197 | .clone_fn = git_hash_sha1_clone, | |
198 | .update_fn = git_hash_sha1_update, | |
199 | .final_fn = git_hash_sha1_final, | |
200 | .final_oid_fn = git_hash_sha1_final_oid, | |
201 | .empty_tree = &empty_tree_oid, | |
202 | .empty_blob = &empty_blob_oid, | |
203 | .null_oid = &null_oid_sha1, | |
f50e766b | 204 | }, |
13eeedb5 | 205 | { |
0cb9872e ÆAB |
206 | .name = "sha256", |
207 | .format_id = GIT_SHA256_FORMAT_ID, | |
208 | .rawsz = GIT_SHA256_RAWSZ, | |
209 | .hexsz = GIT_SHA256_HEXSZ, | |
210 | .blksz = GIT_SHA256_BLKSZ, | |
211 | .init_fn = git_hash_sha256_init, | |
212 | .clone_fn = git_hash_sha256_clone, | |
213 | .update_fn = git_hash_sha256_update, | |
214 | .final_fn = git_hash_sha256_final, | |
215 | .final_oid_fn = git_hash_sha256_final_oid, | |
216 | .empty_tree = &empty_tree_oid_sha256, | |
217 | .empty_blob = &empty_blob_oid_sha256, | |
218 | .null_oid = &null_oid_sha256, | |
13eeedb5 | 219 | } |
f50e766b | 220 | }; |
221 | ||
14228447 | 222 | const struct object_id *null_oid(void) |
223 | { | |
224 | return the_hash_algo->null_oid; | |
225 | } | |
226 | ||
d8a92ced | 227 | const char *empty_tree_oid_hex(void) |
228 | { | |
229 | static char buf[GIT_MAX_HEXSZ + 1]; | |
230 | return oid_to_hex_r(buf, the_hash_algo->empty_tree); | |
231 | } | |
232 | ||
233 | const char *empty_blob_oid_hex(void) | |
234 | { | |
235 | static char buf[GIT_MAX_HEXSZ + 1]; | |
236 | return oid_to_hex_r(buf, the_hash_algo->empty_blob); | |
237 | } | |
238 | ||
2f90b9d9 | 239 | int hash_algo_by_name(const char *name) |
240 | { | |
241 | int i; | |
242 | if (!name) | |
243 | return GIT_HASH_UNKNOWN; | |
244 | for (i = 1; i < GIT_HASH_NALGOS; i++) | |
245 | if (!strcmp(name, hash_algos[i].name)) | |
246 | return i; | |
247 | return GIT_HASH_UNKNOWN; | |
248 | } | |
249 | ||
250 | int hash_algo_by_id(uint32_t format_id) | |
251 | { | |
252 | int i; | |
253 | for (i = 1; i < GIT_HASH_NALGOS; i++) | |
254 | if (format_id == hash_algos[i].format_id) | |
255 | return i; | |
256 | return GIT_HASH_UNKNOWN; | |
257 | } | |
258 | ||
95399788 | 259 | int hash_algo_by_length(int len) |
260 | { | |
261 | int i; | |
262 | for (i = 1; i < GIT_HASH_NALGOS; i++) | |
263 | if (len == hash_algos[i].rawsz) | |
264 | return i; | |
265 | return GIT_HASH_UNKNOWN; | |
266 | } | |
2f90b9d9 | 267 | |
c597ba80 NTND |
268 | /* |
269 | * This is meant to hold a *small* number of objects that you would | |
cb1c8d1d | 270 | * want read_object_file() to be able to return, but yet you do not want |
c597ba80 NTND |
271 | * to write them into the object store (e.g. a browse-only |
272 | * application). | |
273 | */ | |
274 | static struct cached_object { | |
62ba93ea | 275 | struct object_id oid; |
c597ba80 NTND |
276 | enum object_type type; |
277 | void *buf; | |
278 | unsigned long size; | |
279 | } *cached_objects; | |
280 | static int cached_object_nr, cached_object_alloc; | |
281 | ||
282 | static struct cached_object empty_tree = { | |
50103649 ÆAB |
283 | .oid = { |
284 | .hash = EMPTY_TREE_SHA1_BIN_LITERAL, | |
285 | }, | |
286 | .type = OBJ_TREE, | |
287 | .buf = "", | |
c597ba80 NTND |
288 | }; |
289 | ||
62ba93ea | 290 | static struct cached_object *find_cached_object(const struct object_id *oid) |
c597ba80 NTND |
291 | { |
292 | int i; | |
293 | struct cached_object *co = cached_objects; | |
294 | ||
295 | for (i = 0; i < cached_object_nr; i++, co++) { | |
4a7e27e9 | 296 | if (oideq(&co->oid, oid)) |
c597ba80 NTND |
297 | return co; |
298 | } | |
4a7e27e9 | 299 | if (oideq(oid, the_hash_algo->empty_tree)) |
c597ba80 NTND |
300 | return &empty_tree; |
301 | return NULL; | |
302 | } | |
303 | ||
9472935d | 304 | |
8462ff43 | 305 | static int get_conv_flags(unsigned flags) |
9472935d TB |
306 | { |
307 | if (flags & HASH_RENORMALIZE) | |
8462ff43 | 308 | return CONV_EOL_RENORMALIZE; |
9472935d | 309 | else if (flags & HASH_WRITE_OBJECT) |
107642fe | 310 | return global_conv_flags_eol | CONV_WRITE_OBJECT; |
9472935d | 311 | else |
8462ff43 | 312 | return 0; |
9472935d TB |
313 | } |
314 | ||
315 | ||
90a6464b JH |
316 | int mkdir_in_gitdir(const char *path) |
317 | { | |
318 | if (mkdir(path, 0777)) { | |
319 | int saved_errno = errno; | |
320 | struct stat st; | |
321 | struct strbuf sb = STRBUF_INIT; | |
322 | ||
323 | if (errno != EEXIST) | |
324 | return -1; | |
325 | /* | |
326 | * Are we looking at a path in a symlinked worktree | |
327 | * whose original repository does not yet have it? | |
328 | * e.g. .git/rr-cache pointing at its original | |
329 | * repository in which the user hasn't performed any | |
330 | * conflict resolution yet? | |
331 | */ | |
332 | if (lstat(path, &st) || !S_ISLNK(st.st_mode) || | |
333 | strbuf_readlink(&sb, path, st.st_size) || | |
334 | !is_absolute_path(sb.buf) || | |
335 | mkdir(sb.buf, 0777)) { | |
336 | strbuf_release(&sb); | |
337 | errno = saved_errno; | |
338 | return -1; | |
339 | } | |
340 | strbuf_release(&sb); | |
341 | } | |
342 | return adjust_shared_perm(path); | |
343 | } | |
344 | ||
eb3c027e | 345 | static enum scld_error safe_create_leading_directories_1(char *path, int share) |
b2cb9425 | 346 | { |
26c8ae2a | 347 | char *next_component = path + offset_1st_component(path); |
0be0521b | 348 | enum scld_error ret = SCLD_OK; |
67d42212 | 349 | |
0be0521b | 350 | while (ret == SCLD_OK && next_component) { |
f0502332 | 351 | struct stat st; |
0f527403 | 352 | char *slash = next_component, slash_character; |
f0502332 | 353 | |
0f527403 MH |
354 | while (*slash && !is_dir_sep(*slash)) |
355 | slash++; | |
356 | ||
357 | if (!*slash) | |
b2cb9425 | 358 | break; |
bf10cf70 | 359 | |
26c8ae2a | 360 | next_component = slash + 1; |
0f527403 | 361 | while (is_dir_sep(*next_component)) |
bf10cf70 | 362 | next_component++; |
26c8ae2a | 363 | if (!*next_component) |
5f0bdf50 | 364 | break; |
831651fd | 365 | |
0f527403 | 366 | slash_character = *slash; |
831651fd | 367 | *slash = '\0'; |
67d42212 JR |
368 | if (!stat(path, &st)) { |
369 | /* path exists */ | |
204a047f MH |
370 | if (!S_ISDIR(st.st_mode)) { |
371 | errno = ENOTDIR; | |
0be0521b | 372 | ret = SCLD_EXISTS; |
204a047f | 373 | } |
53a39721 | 374 | } else if (mkdir(path, 0777)) { |
928734d9 | 375 | if (errno == EEXIST && |
9e6f885d | 376 | !stat(path, &st) && S_ISDIR(st.st_mode)) |
928734d9 | 377 | ; /* somebody created it since we checked */ |
18d37e86 MH |
378 | else if (errno == ENOENT) |
379 | /* | |
380 | * Either mkdir() failed because | |
381 | * somebody just pruned the containing | |
382 | * directory, or stat() failed because | |
383 | * the file that was in our way was | |
384 | * just removed. Either way, inform | |
385 | * the caller that it might be worth | |
386 | * trying again: | |
387 | */ | |
388 | ret = SCLD_VANISHED; | |
9e6f885d | 389 | else |
0be0521b | 390 | ret = SCLD_FAILED; |
eb3c027e | 391 | } else if (share && adjust_shared_perm(path)) { |
0be0521b | 392 | ret = SCLD_PERMS; |
457f06d6 | 393 | } |
0f527403 | 394 | *slash = slash_character; |
b2cb9425 | 395 | } |
9e6f885d | 396 | return ret; |
b2cb9425 | 397 | } |
723c31fe | 398 | |
eb3c027e MT |
399 | enum scld_error safe_create_leading_directories(char *path) |
400 | { | |
401 | return safe_create_leading_directories_1(path, 1); | |
402 | } | |
403 | ||
404 | enum scld_error safe_create_leading_directories_no_share(char *path) | |
405 | { | |
406 | return safe_create_leading_directories_1(path, 0); | |
407 | } | |
408 | ||
0be0521b | 409 | enum scld_error safe_create_leading_directories_const(const char *path) |
8e21d63b | 410 | { |
02944307 | 411 | int save_errno; |
8e21d63b JK |
412 | /* path points to cache entries, so xstrdup before messing with it */ |
413 | char *buf = xstrdup(path); | |
0be0521b | 414 | enum scld_error result = safe_create_leading_directories(buf); |
02944307 MH |
415 | |
416 | save_errno = errno; | |
8e21d63b | 417 | free(buf); |
02944307 | 418 | errno = save_errno; |
8e21d63b JK |
419 | return result; |
420 | } | |
421 | ||
514c5fdd | 422 | static void fill_loose_path(struct strbuf *buf, const struct object_id *oid) |
ace1534d JH |
423 | { |
424 | int i; | |
94b5e093 | 425 | for (i = 0; i < the_hash_algo->rawsz; i++) { |
ace1534d | 426 | static char hex[] = "0123456789abcdef"; |
514c5fdd | 427 | unsigned int val = oid->hash[i]; |
f7b7774f JK |
428 | strbuf_addch(buf, hex[val >> 4]); |
429 | strbuf_addch(buf, hex[val & 0xf]); | |
afbba2f0 | 430 | if (!i) |
f7b7774f | 431 | strbuf_addch(buf, '/'); |
ace1534d JH |
432 | } |
433 | } | |
434 | ||
f0eaf638 JK |
435 | static const char *odb_loose_path(struct object_directory *odb, |
436 | struct strbuf *buf, | |
514c5fdd | 437 | const struct object_id *oid) |
0fcfd160 | 438 | { |
b69fb867 | 439 | strbuf_reset(buf); |
f0eaf638 | 440 | strbuf_addstr(buf, odb->path); |
34498471 | 441 | strbuf_addch(buf, '/'); |
514c5fdd | 442 | fill_loose_path(buf, oid); |
f3f043a1 | 443 | return buf->buf; |
0fcfd160 LT |
444 | } |
445 | ||
f3f043a1 | 446 | const char *loose_object_path(struct repository *r, struct strbuf *buf, |
514c5fdd | 447 | const struct object_id *oid) |
29ec6af2 | 448 | { |
514c5fdd | 449 | return odb_loose_path(r->objects->odb, buf, oid); |
0fcfd160 LT |
450 | } |
451 | ||
4ea82473 JK |
452 | /* |
453 | * Return non-zero iff the path is usable as an alternate object database. | |
454 | */ | |
13313fc3 SB |
455 | static int alt_odb_usable(struct raw_object_store *o, |
456 | struct strbuf *path, | |
cf2dc1c2 | 457 | const char *normalized_objdir, khiter_t *pos) |
4ea82473 | 458 | { |
cf2dc1c2 | 459 | int r; |
4ea82473 JK |
460 | |
461 | /* Detect cases where alternate disappeared */ | |
462 | if (!is_directory(path->buf)) { | |
259328b7 NTND |
463 | error(_("object directory %s does not exist; " |
464 | "check .git/objects/info/alternates"), | |
4ea82473 JK |
465 | path->buf); |
466 | return 0; | |
467 | } | |
468 | ||
469 | /* | |
470 | * Prevent the common mistake of listing the same | |
471 | * thing twice, or object directory itself. | |
472 | */ | |
cf2dc1c2 EW |
473 | if (!o->odb_by_path) { |
474 | khiter_t p; | |
475 | ||
476 | o->odb_by_path = kh_init_odb_path_map(); | |
477 | assert(!o->odb->next); | |
478 | p = kh_put_odb_path_map(o->odb_by_path, o->odb->path, &r); | |
479 | assert(r == 1); /* never used */ | |
480 | kh_value(o->odb_by_path, p) = o->odb; | |
4ea82473 | 481 | } |
cf2dc1c2 | 482 | if (fspatheq(path->buf, normalized_objdir)) |
4ea82473 | 483 | return 0; |
cf2dc1c2 EW |
484 | *pos = kh_put_odb_path_map(o->odb_by_path, path->buf, &r); |
485 | /* r: 0 = exists, 1 = never used, 2 = deleted */ | |
486 | return r == 0 ? 0 : 1; | |
4ea82473 JK |
487 | } |
488 | ||
ddd5d056 JH |
489 | /* |
490 | * Prepare alternate object database registry. | |
d5a63b99 JH |
491 | * |
492 | * The variable alt_odb_list points at the list of struct | |
263db403 | 493 | * object_directory. The elements on this list come from |
d5a63b99 JH |
494 | * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT |
495 | * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates, | |
1494e038 JH |
496 | * whose contents is similar to that environment variable but can be |
497 | * LF separated. Its base points at a statically allocated buffer that | |
d5a63b99 JH |
498 | * contains "/the/directory/corresponding/to/.git/objects/...", while |
499 | * its name points just after the slash at the end of ".git/objects/" | |
e5afd444 MÅ |
500 | * in the example above, and has enough space to hold all hex characters |
501 | * of the object ID, an extra slash for the first level indirection, and | |
502 | * the terminating NUL. | |
ddd5d056 | 503 | */ |
77f012e8 SB |
504 | static void read_info_alternates(struct repository *r, |
505 | const char *relative_base, | |
506 | int depth); | |
407532f8 | 507 | static int link_alt_odb_entry(struct repository *r, const struct strbuf *entry, |
cfc62fc9 | 508 | const char *relative_base, int depth, const char *normalized_objdir) |
ace1534d | 509 | { |
263db403 | 510 | struct object_directory *ent; |
5bdf0a84 | 511 | struct strbuf pathbuf = STRBUF_INIT; |
199337d6 | 512 | struct strbuf tmp = STRBUF_INIT; |
cf2dc1c2 | 513 | khiter_t pos; |
199337d6 | 514 | int ret = -1; |
d5a63b99 | 515 | |
407532f8 | 516 | if (!is_absolute_path(entry->buf) && relative_base) { |
4ac9006f | 517 | strbuf_realpath(&pathbuf, relative_base, 1); |
5bdf0a84 | 518 | strbuf_addch(&pathbuf, '/'); |
c2f493a4 | 519 | } |
407532f8 | 520 | strbuf_addbuf(&pathbuf, entry); |
c2f493a4 | 521 | |
199337d6 | 522 | if (!strbuf_realpath(&tmp, pathbuf.buf, 0)) { |
259328b7 | 523 | error(_("unable to normalize alternate object path: %s"), |
670c359d | 524 | pathbuf.buf); |
199337d6 | 525 | goto error; |
670c359d | 526 | } |
199337d6 | 527 | strbuf_swap(&pathbuf, &tmp); |
5bdf0a84 HW |
528 | |
529 | /* | |
530 | * The trailing slash after the directory name is given by | |
531 | * this function at the end. Remove duplicates. | |
532 | */ | |
4ea82473 JK |
533 | while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/') |
534 | strbuf_setlen(&pathbuf, pathbuf.len - 1); | |
5bdf0a84 | 535 | |
199337d6 GC |
536 | if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos)) |
537 | goto error; | |
c2f493a4 | 538 | |
ca56dadb | 539 | CALLOC_ARRAY(ent, 1); |
cf2dc1c2 EW |
540 | /* pathbuf.buf is already in r->objects->odb_by_path */ |
541 | ent->path = strbuf_detach(&pathbuf, NULL); | |
c2f493a4 MW |
542 | |
543 | /* add the alternate entry */ | |
f0eaf638 JK |
544 | *r->objects->odb_tail = ent; |
545 | r->objects->odb_tail = &(ent->next); | |
c2f493a4 | 546 | ent->next = NULL; |
cf2dc1c2 EW |
547 | assert(r->objects->odb_by_path); |
548 | kh_value(r->objects->odb_by_path, pos) = ent; | |
c2f493a4 MW |
549 | |
550 | /* recursively add alternates */ | |
cf2dc1c2 | 551 | read_info_alternates(r, ent->path, depth + 1); |
199337d6 GC |
552 | ret = 0; |
553 | error: | |
554 | strbuf_release(&tmp); | |
555 | strbuf_release(&pathbuf); | |
556 | return ret; | |
c2f493a4 MW |
557 | } |
558 | ||
cf3c6352 JK |
559 | static const char *parse_alt_odb_entry(const char *string, |
560 | int sep, | |
561 | struct strbuf *out) | |
562 | { | |
563 | const char *end; | |
564 | ||
565 | strbuf_reset(out); | |
566 | ||
567 | if (*string == '#') { | |
568 | /* comment; consume up to next separator */ | |
569 | end = strchrnul(string, sep); | |
570 | } else if (*string == '"' && !unquote_c_style(out, string, &end)) { | |
571 | /* | |
572 | * quoted path; unquote_c_style has copied the | |
573 | * data for us and set "end". Broken quoting (e.g., | |
574 | * an entry that doesn't end with a quote) falls | |
575 | * back to the unquoted case below. | |
576 | */ | |
577 | } else { | |
578 | /* normal, unquoted path */ | |
579 | end = strchrnul(string, sep); | |
580 | strbuf_add(out, string, end - string); | |
581 | } | |
582 | ||
583 | if (*end) | |
584 | end++; | |
585 | return end; | |
586 | } | |
587 | ||
77f012e8 SB |
588 | static void link_alt_odb_entries(struct repository *r, const char *alt, |
589 | int sep, const char *relative_base, int depth) | |
c2f493a4 | 590 | { |
539e7506 | 591 | struct strbuf objdirbuf = STRBUF_INIT; |
cf3c6352 | 592 | struct strbuf entry = STRBUF_INIT; |
c2f493a4 | 593 | |
f28e3668 JK |
594 | if (!alt || !*alt) |
595 | return; | |
596 | ||
c2f493a4 | 597 | if (depth > 5) { |
259328b7 | 598 | error(_("%s: ignoring alternate object stores, nesting too deep"), |
c2f493a4 MW |
599 | relative_base); |
600 | return; | |
601 | } | |
602 | ||
199337d6 | 603 | strbuf_realpath(&objdirbuf, r->objects->odb->path, 1); |
539e7506 | 604 | |
cf3c6352 JK |
605 | while (*alt) { |
606 | alt = parse_alt_odb_entry(alt, sep, &entry); | |
607 | if (!entry.len) | |
9577e7e3 | 608 | continue; |
407532f8 | 609 | link_alt_odb_entry(r, &entry, |
cfc62fc9 | 610 | relative_base, depth, objdirbuf.buf); |
9577e7e3 | 611 | } |
cf3c6352 | 612 | strbuf_release(&entry); |
539e7506 | 613 | strbuf_release(&objdirbuf); |
d5a63b99 JH |
614 | } |
615 | ||
77f012e8 SB |
616 | static void read_info_alternates(struct repository *r, |
617 | const char *relative_base, | |
618 | int depth) | |
d5a63b99 | 619 | { |
5015f01c | 620 | char *path; |
dc732bd5 | 621 | struct strbuf buf = STRBUF_INIT; |
d5a63b99 | 622 | |
5015f01c | 623 | path = xstrfmt("%s/info/alternates", relative_base); |
dc732bd5 | 624 | if (strbuf_read_file(&buf, path, 1024) < 0) { |
f0f7bebe | 625 | warn_on_fopen_errors(path); |
dc732bd5 | 626 | free(path); |
9a217f2a | 627 | return; |
ace1534d | 628 | } |
d5a63b99 | 629 | |
77f012e8 | 630 | link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth); |
dc732bd5 JK |
631 | strbuf_release(&buf); |
632 | free(path); | |
ace1534d JH |
633 | } |
634 | ||
bef70b22 DB |
635 | void add_to_alternates_file(const char *reference) |
636 | { | |
f132a127 | 637 | struct lock_file lock = LOCK_INIT; |
77b9b1d1 JK |
638 | char *alts = git_pathdup("objects/info/alternates"); |
639 | FILE *in, *out; | |
f132a127 | 640 | int found = 0; |
77b9b1d1 | 641 | |
f132a127 MÅ |
642 | hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR); |
643 | out = fdopen_lock_file(&lock, "w"); | |
77b9b1d1 | 644 | if (!out) |
259328b7 | 645 | die_errno(_("unable to fdopen alternates lockfile")); |
77b9b1d1 JK |
646 | |
647 | in = fopen(alts, "r"); | |
648 | if (in) { | |
649 | struct strbuf line = STRBUF_INIT; | |
77b9b1d1 | 650 | |
3f163962 | 651 | while (strbuf_getline(&line, in) != EOF) { |
77b9b1d1 JK |
652 | if (!strcmp(reference, line.buf)) { |
653 | found = 1; | |
654 | break; | |
655 | } | |
656 | fprintf_or_die(out, "%s\n", line.buf); | |
657 | } | |
658 | ||
659 | strbuf_release(&line); | |
660 | fclose(in); | |
77b9b1d1 JK |
661 | } |
662 | else if (errno != ENOENT) | |
259328b7 | 663 | die_errno(_("unable to read alternates file")); |
77b9b1d1 | 664 | |
f132a127 MÅ |
665 | if (found) { |
666 | rollback_lock_file(&lock); | |
667 | } else { | |
77b9b1d1 | 668 | fprintf_or_die(out, "%s\n", reference); |
f132a127 | 669 | if (commit_lock_file(&lock)) |
259328b7 | 670 | die_errno(_("unable to move new alternates file into place")); |
f0eaf638 | 671 | if (the_repository->objects->loaded_alternates) |
93d8d1e2 SB |
672 | link_alt_odb_entries(the_repository, reference, |
673 | '\n', NULL, 0); | |
77b9b1d1 JK |
674 | } |
675 | free(alts); | |
bef70b22 DB |
676 | } |
677 | ||
a5b34d21 JK |
678 | void add_to_alternates_memory(const char *reference) |
679 | { | |
680 | /* | |
681 | * Make sure alternates are initialized, or else our entry may be | |
682 | * overwritten when they are. | |
683 | */ | |
0b209034 | 684 | prepare_alt_odb(the_repository); |
a5b34d21 | 685 | |
93d8d1e2 SB |
686 | link_alt_odb_entries(the_repository, reference, |
687 | '\n', NULL, 0); | |
a5b34d21 JK |
688 | } |
689 | ||
b3cecf49 NS |
690 | struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy) |
691 | { | |
692 | struct object_directory *new_odb; | |
693 | ||
694 | /* | |
695 | * Make sure alternates are initialized, or else our entry may be | |
696 | * overwritten when they are. | |
697 | */ | |
698 | prepare_alt_odb(the_repository); | |
699 | ||
700 | /* | |
701 | * Make a new primary odb and link the old primary ODB in as an | |
702 | * alternate | |
703 | */ | |
704 | new_odb = xcalloc(1, sizeof(*new_odb)); | |
705 | new_odb->path = xstrdup(dir); | |
ecd81dfc NS |
706 | |
707 | /* | |
708 | * Disable ref updates while a temporary odb is active, since | |
709 | * the objects in the database may roll back. | |
710 | */ | |
711 | new_odb->disable_ref_updates = 1; | |
b3cecf49 NS |
712 | new_odb->will_destroy = will_destroy; |
713 | new_odb->next = the_repository->objects->odb; | |
714 | the_repository->objects->odb = new_odb; | |
715 | return new_odb->next; | |
716 | } | |
717 | ||
718 | void restore_primary_odb(struct object_directory *restore_odb, const char *old_path) | |
719 | { | |
720 | struct object_directory *cur_odb = the_repository->objects->odb; | |
721 | ||
722 | if (strcmp(old_path, cur_odb->path)) | |
723 | BUG("expected %s as primary object store; found %s", | |
724 | old_path, cur_odb->path); | |
725 | ||
726 | if (cur_odb->next != restore_odb) | |
727 | BUG("we expect the old primary object store to be the first alternate"); | |
728 | ||
729 | the_repository->objects->odb = restore_odb; | |
730 | free_object_directory(cur_odb); | |
731 | } | |
732 | ||
9eeea7d2 SB |
733 | /* |
734 | * Compute the exact path an alternate is at and returns it. In case of | |
735 | * error NULL is returned and the human readable error is added to `err` | |
efde7b72 | 736 | * `path` may be relative and should point to $GIT_DIR. |
9eeea7d2 SB |
737 | * `err` must not be null. |
738 | */ | |
739 | char *compute_alternate_path(const char *path, struct strbuf *err) | |
740 | { | |
741 | char *ref_git = NULL; | |
4530a85b | 742 | const char *repo; |
9eeea7d2 SB |
743 | int seen_error = 0; |
744 | ||
4530a85b AM |
745 | ref_git = real_pathdup(path, 0); |
746 | if (!ref_git) { | |
9eeea7d2 SB |
747 | seen_error = 1; |
748 | strbuf_addf(err, _("path '%s' does not exist"), path); | |
749 | goto out; | |
4530a85b | 750 | } |
9eeea7d2 SB |
751 | |
752 | repo = read_gitfile(ref_git); | |
753 | if (!repo) | |
754 | repo = read_gitfile(mkpath("%s/.git", ref_git)); | |
755 | if (repo) { | |
756 | free(ref_git); | |
757 | ref_git = xstrdup(repo); | |
758 | } | |
759 | ||
760 | if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) { | |
761 | char *ref_git_git = mkpathdup("%s/.git", ref_git); | |
762 | free(ref_git); | |
763 | ref_git = ref_git_git; | |
764 | } else if (!is_directory(mkpath("%s/objects", ref_git))) { | |
765 | struct strbuf sb = STRBUF_INIT; | |
766 | seen_error = 1; | |
767 | if (get_common_dir(&sb, ref_git)) { | |
768 | strbuf_addf(err, | |
769 | _("reference repository '%s' as a linked " | |
770 | "checkout is not supported yet."), | |
771 | path); | |
772 | goto out; | |
773 | } | |
774 | ||
775 | strbuf_addf(err, _("reference repository '%s' is not a " | |
776 | "local repository."), path); | |
777 | goto out; | |
778 | } | |
779 | ||
780 | if (!access(mkpath("%s/shallow", ref_git), F_OK)) { | |
781 | strbuf_addf(err, _("reference repository '%s' is shallow"), | |
782 | path); | |
783 | seen_error = 1; | |
784 | goto out; | |
785 | } | |
786 | ||
787 | if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) { | |
788 | strbuf_addf(err, | |
789 | _("reference repository '%s' is grafted"), | |
790 | path); | |
791 | seen_error = 1; | |
792 | goto out; | |
793 | } | |
794 | ||
795 | out: | |
796 | if (seen_error) { | |
6a83d902 | 797 | FREE_AND_NULL(ref_git); |
9eeea7d2 SB |
798 | } |
799 | ||
800 | return ref_git; | |
801 | } | |
802 | ||
f57a7396 TB |
803 | struct object_directory *find_odb(struct repository *r, const char *obj_dir) |
804 | { | |
805 | struct object_directory *odb; | |
806 | char *obj_dir_real = real_pathdup(obj_dir, 1); | |
807 | struct strbuf odb_path_real = STRBUF_INIT; | |
808 | ||
809 | prepare_alt_odb(r); | |
810 | for (odb = r->objects->odb; odb; odb = odb->next) { | |
811 | strbuf_realpath(&odb_path_real, odb->path, 1); | |
812 | if (!strcmp(obj_dir_real, odb_path_real.buf)) | |
813 | break; | |
814 | } | |
815 | ||
816 | free(obj_dir_real); | |
817 | strbuf_release(&odb_path_real); | |
818 | ||
819 | if (!odb) | |
820 | die(_("could not find object directory matching %s"), obj_dir); | |
821 | return odb; | |
822 | } | |
823 | ||
709dfa69 JK |
824 | static void fill_alternate_refs_command(struct child_process *cmd, |
825 | const char *repo_path) | |
826 | { | |
827 | const char *value; | |
828 | ||
829 | if (!git_config_get_value("core.alternateRefsCommand", &value)) { | |
830 | cmd->use_shell = 1; | |
831 | ||
c972bf4c JK |
832 | strvec_push(&cmd->args, value); |
833 | strvec_push(&cmd->args, repo_path); | |
709dfa69 JK |
834 | } else { |
835 | cmd->git_cmd = 1; | |
836 | ||
c972bf4c JK |
837 | strvec_pushf(&cmd->args, "--git-dir=%s", repo_path); |
838 | strvec_push(&cmd->args, "for-each-ref"); | |
839 | strvec_push(&cmd->args, "--format=%(objectname)"); | |
709dfa69 JK |
840 | |
841 | if (!git_config_get_value("core.alternateRefsPrefixes", &value)) { | |
c972bf4c JK |
842 | strvec_push(&cmd->args, "--"); |
843 | strvec_split(&cmd->args, value); | |
709dfa69 JK |
844 | } |
845 | } | |
846 | ||
29fda24d | 847 | strvec_pushv(&cmd->env, (const char **)local_repo_env); |
709dfa69 JK |
848 | cmd->out = -1; |
849 | } | |
850 | ||
851 | static void read_alternate_refs(const char *path, | |
852 | alternate_ref_fn *cb, | |
853 | void *data) | |
854 | { | |
855 | struct child_process cmd = CHILD_PROCESS_INIT; | |
856 | struct strbuf line = STRBUF_INIT; | |
857 | FILE *fh; | |
858 | ||
859 | fill_alternate_refs_command(&cmd, path); | |
860 | ||
861 | if (start_command(&cmd)) | |
862 | return; | |
863 | ||
864 | fh = xfdopen(cmd.out, "r"); | |
865 | while (strbuf_getline_lf(&line, fh) != EOF) { | |
866 | struct object_id oid; | |
867 | const char *p; | |
868 | ||
869 | if (parse_oid_hex(line.buf, &oid, &p) || *p) { | |
870 | warning(_("invalid line while parsing alternate refs: %s"), | |
871 | line.buf); | |
872 | break; | |
873 | } | |
874 | ||
875 | cb(&oid, data); | |
876 | } | |
877 | ||
878 | fclose(fh); | |
879 | finish_command(&cmd); | |
86ad3ea5 | 880 | strbuf_release(&line); |
709dfa69 JK |
881 | } |
882 | ||
883 | struct alternate_refs_data { | |
884 | alternate_ref_fn *fn; | |
885 | void *data; | |
886 | }; | |
887 | ||
888 | static int refs_from_alternate_cb(struct object_directory *e, | |
889 | void *data) | |
890 | { | |
891 | struct strbuf path = STRBUF_INIT; | |
892 | size_t base_len; | |
893 | struct alternate_refs_data *cb = data; | |
894 | ||
895 | if (!strbuf_realpath(&path, e->path, 0)) | |
896 | goto out; | |
897 | if (!strbuf_strip_suffix(&path, "/objects")) | |
898 | goto out; | |
899 | base_len = path.len; | |
900 | ||
901 | /* Is this a git repository with refs? */ | |
902 | strbuf_addstr(&path, "/refs"); | |
903 | if (!is_directory(path.buf)) | |
904 | goto out; | |
905 | strbuf_setlen(&path, base_len); | |
906 | ||
907 | read_alternate_refs(path.buf, cb->fn, cb->data); | |
908 | ||
909 | out: | |
910 | strbuf_release(&path); | |
911 | return 0; | |
912 | } | |
913 | ||
914 | void for_each_alternate_ref(alternate_ref_fn fn, void *data) | |
915 | { | |
916 | struct alternate_refs_data cb; | |
917 | cb.fn = fn; | |
918 | cb.data = data; | |
919 | foreach_alt_odb(refs_from_alternate_cb, &cb); | |
920 | } | |
921 | ||
fe1b2268 | 922 | int foreach_alt_odb(alt_odb_fn fn, void *cb) |
d79796bc | 923 | { |
263db403 | 924 | struct object_directory *ent; |
fe1b2268 | 925 | int r = 0; |
d79796bc | 926 | |
0b209034 | 927 | prepare_alt_odb(the_repository); |
f0eaf638 | 928 | for (ent = the_repository->objects->odb->next; ent; ent = ent->next) { |
fe1b2268 JK |
929 | r = fn(ent, cb); |
930 | if (r) | |
931 | break; | |
932 | } | |
933 | return r; | |
d79796bc JH |
934 | } |
935 | ||
13068bf0 | 936 | void prepare_alt_odb(struct repository *r) |
c2f493a4 | 937 | { |
f0eaf638 | 938 | if (r->objects->loaded_alternates) |
7dc24aa5 SP |
939 | return; |
940 | ||
13068bf0 | 941 | link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0); |
c2f493a4 | 942 | |
f0eaf638 JK |
943 | read_info_alternates(r, r->objects->odb->path, 0); |
944 | r->objects->loaded_alternates = 1; | |
c2f493a4 MW |
945 | } |
946 | ||
d85cd187 PS |
947 | int has_alt_odb(struct repository *r) |
948 | { | |
949 | prepare_alt_odb(r); | |
950 | return !!r->objects->odb->next; | |
951 | } | |
952 | ||
3096b2ec | 953 | /* Returns 1 if we have successfully freshened the file, 0 otherwise. */ |
33d4221c | 954 | static int freshen_file(const char *fn) |
ace1534d | 955 | { |
312cd761 | 956 | return !utime(fn, NULL); |
0f4dc14a | 957 | } |
ace1534d | 958 | |
3096b2ec JK |
959 | /* |
960 | * All of the check_and_freshen functions return 1 if the file exists and was | |
961 | * freshened (if freshening was requested), 0 otherwise. If they return | |
962 | * 0, you should not assume that it is safe to skip a write of the object (it | |
963 | * either does not exist on disk, or has a stale mtime and may be subject to | |
964 | * pruning). | |
965 | */ | |
6a5e6f5e | 966 | int check_and_freshen_file(const char *fn, int freshen) |
33d4221c JK |
967 | { |
968 | if (access(fn, F_OK)) | |
969 | return 0; | |
3096b2ec | 970 | if (freshen && !freshen_file(fn)) |
33d4221c JK |
971 | return 0; |
972 | return 1; | |
973 | } | |
974 | ||
f0eaf638 JK |
975 | static int check_and_freshen_odb(struct object_directory *odb, |
976 | const struct object_id *oid, | |
977 | int freshen) | |
33d4221c | 978 | { |
f0eaf638 | 979 | static struct strbuf path = STRBUF_INIT; |
514c5fdd | 980 | odb_loose_path(odb, &path, oid); |
f0eaf638 JK |
981 | return check_and_freshen_file(path.buf, freshen); |
982 | } | |
ea657730 | 983 | |
f0eaf638 JK |
984 | static int check_and_freshen_local(const struct object_id *oid, int freshen) |
985 | { | |
986 | return check_and_freshen_odb(the_repository->objects->odb, oid, freshen); | |
33d4221c JK |
987 | } |
988 | ||
6862ebbf | 989 | static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen) |
0f4dc14a | 990 | { |
263db403 | 991 | struct object_directory *odb; |
f3f043a1 | 992 | |
0b209034 | 993 | prepare_alt_odb(the_repository); |
f0eaf638 JK |
994 | for (odb = the_repository->objects->odb->next; odb; odb = odb->next) { |
995 | if (check_and_freshen_odb(odb, oid, freshen)) | |
c529d75a | 996 | return 1; |
ace1534d | 997 | } |
c529d75a | 998 | return 0; |
ace1534d JH |
999 | } |
1000 | ||
6862ebbf | 1001 | static int check_and_freshen(const struct object_id *oid, int freshen) |
33d4221c | 1002 | { |
6862ebbf | 1003 | return check_and_freshen_local(oid, freshen) || |
1004 | check_and_freshen_nonlocal(oid, freshen); | |
33d4221c JK |
1005 | } |
1006 | ||
6862ebbf | 1007 | int has_loose_object_nonlocal(const struct object_id *oid) |
33d4221c | 1008 | { |
6862ebbf | 1009 | return check_and_freshen_nonlocal(oid, 0); |
33d4221c JK |
1010 | } |
1011 | ||
b7573536 | 1012 | int has_loose_object(const struct object_id *oid) |
0f4dc14a | 1013 | { |
6862ebbf | 1014 | return check_and_freshen(oid, 0); |
0f4dc14a BC |
1015 | } |
1016 | ||
02710228 SP |
1017 | static void mmap_limit_check(size_t length) |
1018 | { | |
1019 | static size_t limit = 0; | |
1020 | if (!limit) { | |
1021 | limit = git_env_ulong("GIT_MMAP_LIMIT", 0); | |
1022 | if (!limit) | |
1023 | limit = SIZE_MAX; | |
1024 | } | |
1025 | if (length > limit) | |
259328b7 | 1026 | die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX), |
02710228 SP |
1027 | (uintmax_t)length, (uintmax_t)limit); |
1028 | } | |
1029 | ||
1570856b JK |
1030 | void *xmmap_gently(void *start, size_t length, |
1031 | int prot, int flags, int fd, off_t offset) | |
58ecbd5e | 1032 | { |
02710228 SP |
1033 | void *ret; |
1034 | ||
1035 | mmap_limit_check(length); | |
1036 | ret = mmap(start, length, prot, flags, fd, offset); | |
9827d4c1 JK |
1037 | if (ret == MAP_FAILED && !length) |
1038 | ret = NULL; | |
58ecbd5e JN |
1039 | return ret; |
1040 | } | |
1041 | ||
dc059294 EW |
1042 | const char *mmap_os_err(void) |
1043 | { | |
1044 | static const char blank[] = ""; | |
1045 | #if defined(__linux__) | |
1046 | if (errno == ENOMEM) { | |
1047 | /* this continues an existing error message: */ | |
1048 | static const char enomem[] = | |
1049 | ", check sys.vm.max_map_count and/or RLIMIT_DATA"; | |
1050 | return enomem; | |
1051 | } | |
1052 | #endif /* OS-specific bits */ | |
1053 | return blank; | |
1054 | } | |
1055 | ||
1570856b JK |
1056 | void *xmmap(void *start, size_t length, |
1057 | int prot, int flags, int fd, off_t offset) | |
1058 | { | |
1059 | void *ret = xmmap_gently(start, length, prot, flags, fd, offset); | |
1060 | if (ret == MAP_FAILED) | |
dc059294 | 1061 | die_errno(_("mmap failed%s"), mmap_os_err()); |
1570856b JK |
1062 | return ret; |
1063 | } | |
1064 | ||
b04cdea4 ÆAB |
1065 | static int format_object_header_literally(char *str, size_t size, |
1066 | const char *type, size_t objsize) | |
1067 | { | |
1068 | return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1; | |
1069 | } | |
1070 | ||
1071 | int format_object_header(char *str, size_t size, enum object_type type, | |
1072 | size_t objsize) | |
1073 | { | |
1074 | const char *name = type_name(type); | |
1075 | ||
1076 | if (!name) | |
1077 | BUG("could not get a type name for 'enum object_type' value %d", type); | |
1078 | ||
1079 | return format_object_header_literally(str, size, name, objsize); | |
1080 | } | |
1081 | ||
b98d1885 | 1082 | int check_object_signature(struct repository *r, const struct object_id *oid, |
44439c1c ÆAB |
1083 | void *buf, unsigned long size, |
1084 | enum object_type type) | |
88d0db55 | 1085 | { |
0f156dbb ÆAB |
1086 | struct object_id real_oid; |
1087 | ||
1088 | hash_object_file(r->hash_algo, buf, size, type, &real_oid); | |
1089 | ||
1090 | return !oideq(oid, &real_oid) ? -1 : 0; | |
1091 | } | |
1092 | ||
1093 | int stream_object_signature(struct repository *r, const struct object_id *oid) | |
1094 | { | |
1095 | struct object_id real_oid; | |
1096 | unsigned long size; | |
090ea126 NTND |
1097 | enum object_type obj_type; |
1098 | struct git_istream *st; | |
18e2588e | 1099 | git_hash_ctx c; |
1af64f73 | 1100 | char hdr[MAX_HEADER_LEN]; |
090ea126 | 1101 | int hdrlen; |
88d0db55 | 1102 | |
b98d1885 | 1103 | st = open_istream(r, oid, &obj_type, &size, NULL); |
090ea126 NTND |
1104 | if (!st) |
1105 | return -1; | |
88d0db55 | 1106 | |
090ea126 | 1107 | /* Generate the header */ |
b04cdea4 | 1108 | hdrlen = format_object_header(hdr, sizeof(hdr), obj_type, size); |
88d0db55 | 1109 | |
090ea126 | 1110 | /* Sha1.. */ |
b98d1885 MT |
1111 | r->hash_algo->init_fn(&c); |
1112 | r->hash_algo->update_fn(&c, hdr, hdrlen); | |
090ea126 NTND |
1113 | for (;;) { |
1114 | char buf[1024 * 16]; | |
1115 | ssize_t readlen = read_istream(st, buf, sizeof(buf)); | |
1f688557 | 1116 | |
f54fac53 JK |
1117 | if (readlen < 0) { |
1118 | close_istream(st); | |
1119 | return -1; | |
1120 | } | |
090ea126 NTND |
1121 | if (!readlen) |
1122 | break; | |
b98d1885 | 1123 | r->hash_algo->update_fn(&c, buf, readlen); |
fa5fc15d | 1124 | } |
0f156dbb | 1125 | r->hash_algo->final_oid_fn(&real_oid, &c); |
090ea126 | 1126 | close_istream(st); |
0f156dbb | 1127 | return !oideq(oid, &real_oid) ? -1 : 0; |
fa5fc15d SP |
1128 | } |
1129 | ||
1b8ac5ea | 1130 | int git_open_cloexec(const char *name, int flags) |
a0788266 | 1131 | { |
1e3001a8 JH |
1132 | int fd; |
1133 | static int o_cloexec = O_CLOEXEC; | |
a0788266 | 1134 | |
1e3001a8 JH |
1135 | fd = open(name, flags | o_cloexec); |
1136 | if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) { | |
cd66ada0 | 1137 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ |
1e3001a8 JH |
1138 | o_cloexec &= ~O_CLOEXEC; |
1139 | fd = open(name, flags | o_cloexec); | |
491a8dec | 1140 | } |
491a8dec | 1141 | |
9fb9495d | 1142 | #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) |
491a8dec | 1143 | { |
1e3001a8 | 1144 | static int fd_cloexec = FD_CLOEXEC; |
a0788266 | 1145 | |
1e3001a8 JH |
1146 | if (!o_cloexec && 0 <= fd && fd_cloexec) { |
1147 | /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */ | |
9fb9495d EW |
1148 | int flags = fcntl(fd, F_GETFD); |
1149 | if (fcntl(fd, F_SETFD, flags | fd_cloexec)) | |
1e3001a8 | 1150 | fd_cloexec = 0; |
cd66ada0 | 1151 | } |
44d1c19e | 1152 | } |
a0788266 | 1153 | #endif |
1b8ac5ea | 1154 | return fd; |
a0788266 JS |
1155 | } |
1156 | ||
3cf8b462 | 1157 | /* |
514c5fdd | 1158 | * Find "oid" as a loose object in the local repository or in an alternate. |
771e7d57 JK |
1159 | * Returns 0 on success, negative on failure. |
1160 | * | |
1161 | * The "path" out-parameter will give the path of the object we found (if any). | |
1162 | * Note that it may point to static storage and is only valid until another | |
514c5fdd | 1163 | * call to stat_loose_object(). |
3cf8b462 | 1164 | */ |
514c5fdd JK |
1165 | static int stat_loose_object(struct repository *r, const struct object_id *oid, |
1166 | struct stat *st, const char **path) | |
1f688557 | 1167 | { |
263db403 | 1168 | struct object_directory *odb; |
ea657730 CC |
1169 | static struct strbuf buf = STRBUF_INIT; |
1170 | ||
d2607fa0 | 1171 | prepare_alt_odb(r); |
f0eaf638 | 1172 | for (odb = r->objects->odb; odb; odb = odb->next) { |
514c5fdd | 1173 | *path = odb_loose_path(odb, &buf, oid); |
771e7d57 | 1174 | if (!lstat(*path, st)) |
052fe5ea | 1175 | return 0; |
c7934306 SP |
1176 | } |
1177 | ||
3cf8b462 SP |
1178 | return -1; |
1179 | } | |
1180 | ||
771e7d57 | 1181 | /* |
514c5fdd | 1182 | * Like stat_loose_object(), but actually open the object and return the |
771e7d57 JK |
1183 | * descriptor. See the caveats on the "path" parameter above. |
1184 | */ | |
514c5fdd JK |
1185 | static int open_loose_object(struct repository *r, |
1186 | const struct object_id *oid, const char **path) | |
60bb8b14 | 1187 | { |
44d1c19e | 1188 | int fd; |
263db403 | 1189 | struct object_directory *odb; |
f0eaf638 | 1190 | int most_interesting_errno = ENOENT; |
ea657730 CC |
1191 | static struct strbuf buf = STRBUF_INIT; |
1192 | ||
ec7283e5 | 1193 | prepare_alt_odb(r); |
f0eaf638 | 1194 | for (odb = r->objects->odb; odb; odb = odb->next) { |
514c5fdd | 1195 | *path = odb_loose_path(odb, &buf, oid); |
771e7d57 | 1196 | fd = git_open(*path); |
44d1c19e LT |
1197 | if (fd >= 0) |
1198 | return fd; | |
f0eaf638 | 1199 | |
d6c8a05b JK |
1200 | if (most_interesting_errno == ENOENT) |
1201 | most_interesting_errno = errno; | |
03e79c88 | 1202 | } |
d6c8a05b | 1203 | errno = most_interesting_errno; |
44d1c19e | 1204 | return -1; |
1f688557 JH |
1205 | } |
1206 | ||
61c7711c | 1207 | static int quick_has_loose(struct repository *r, |
d7a24573 | 1208 | const struct object_id *oid) |
61c7711c | 1209 | { |
61c7711c JK |
1210 | struct object_directory *odb; |
1211 | ||
61c7711c JK |
1212 | prepare_alt_odb(r); |
1213 | for (odb = r->objects->odb; odb; odb = odb->next) { | |
92d8ed8a | 1214 | if (oidtree_contains(odb_loose_cache(odb, oid), oid)) |
61c7711c JK |
1215 | return 1; |
1216 | } | |
1217 | return 0; | |
1218 | } | |
1219 | ||
f6371f92 | 1220 | /* |
ae285ac4 JT |
1221 | * Map and close the given loose object fd. The path argument is used for |
1222 | * error reporting. | |
f6371f92 | 1223 | */ |
ae285ac4 | 1224 | static void *map_fd(int fd, const char *path, unsigned long *size) |
27d69a46 | 1225 | { |
ae285ac4 JT |
1226 | void *map = NULL; |
1227 | struct stat st; | |
1f688557 | 1228 | |
ae285ac4 JT |
1229 | if (!fstat(fd, &st)) { |
1230 | *size = xsize_t(st.st_size); | |
1231 | if (!*size) { | |
1232 | /* mmap() is forbidden on empty files */ | |
1233 | error(_("object file %s is empty"), path); | |
1234 | close(fd); | |
1235 | return NULL; | |
144bde78 | 1236 | } |
ae285ac4 | 1237 | map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); |
74e34e1f | 1238 | } |
ae285ac4 | 1239 | close(fd); |
0fcfd160 | 1240 | return map; |
74e34e1f NP |
1241 | } |
1242 | ||
514c5fdd JK |
1243 | void *map_loose_object(struct repository *r, |
1244 | const struct object_id *oid, | |
1245 | unsigned long *size) | |
068f85e3 | 1246 | { |
ae285ac4 JT |
1247 | const char *p; |
1248 | int fd = open_loose_object(r, oid, &p); | |
1249 | ||
1250 | if (fd < 0) | |
1251 | return NULL; | |
1252 | return map_fd(fd, p, size); | |
068f85e3 | 1253 | } |
1254 | ||
3b6a8db3 ÆAB |
1255 | enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, |
1256 | unsigned char *map, | |
1257 | unsigned long mapsize, | |
1258 | void *buffer, | |
1259 | unsigned long bufsiz, | |
1260 | struct strbuf *header) | |
47fe3f6e | 1261 | { |
01cab976 | 1262 | int status; |
31877c9a | 1263 | |
c4483576 LT |
1264 | /* Get the data stream */ |
1265 | memset(stream, 0, sizeof(*stream)); | |
1266 | stream->next_in = map; | |
1267 | stream->avail_in = mapsize; | |
1268 | stream->next_out = buffer; | |
93821bd9 | 1269 | stream->avail_out = bufsiz; |
47fe3f6e | 1270 | |
39c68542 | 1271 | git_inflate_init(stream); |
31877c9a | 1272 | obj_read_unlock(); |
01cab976 | 1273 | status = git_inflate(stream, 0); |
31877c9a | 1274 | obj_read_lock(); |
d21f8426 | 1275 | if (status < Z_OK) |
3b6a8db3 | 1276 | return ULHR_BAD; |
d131b7af | 1277 | |
46f03448 KN |
1278 | /* |
1279 | * Check if entire header is unpacked in the first iteration. | |
d131b7af | 1280 | */ |
46f03448 | 1281 | if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) |
3b6a8db3 | 1282 | return ULHR_OK; |
d131b7af | 1283 | |
01cab976 ÆAB |
1284 | /* |
1285 | * We have a header longer than MAX_HEADER_LEN. The "header" | |
1286 | * here is only non-NULL when we run "cat-file | |
1287 | * --allow-unknown-type". | |
1288 | */ | |
1289 | if (!header) | |
5848fb11 | 1290 | return ULHR_TOO_LONG; |
d131b7af | 1291 | |
46f03448 KN |
1292 | /* |
1293 | * buffer[0..bufsiz] was not large enough. Copy the partial | |
1294 | * result out to header, and then append the result of further | |
1295 | * reading the stream. | |
1296 | */ | |
1297 | strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer); | |
1298 | stream->next_out = buffer; | |
1299 | stream->avail_out = bufsiz; | |
d131b7af | 1300 | |
46f03448 | 1301 | do { |
31877c9a | 1302 | obj_read_unlock(); |
46f03448 | 1303 | status = git_inflate(stream, 0); |
31877c9a | 1304 | obj_read_lock(); |
46f03448 KN |
1305 | strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer); |
1306 | if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) | |
1307 | return 0; | |
1308 | stream->next_out = buffer; | |
1309 | stream->avail_out = bufsiz; | |
1310 | } while (status != Z_STREAM_END); | |
5848fb11 | 1311 | return ULHR_TOO_LONG; |
d131b7af SP |
1312 | } |
1313 | ||
00a7760e JK |
1314 | static void *unpack_loose_rest(git_zstream *stream, |
1315 | void *buffer, unsigned long size, | |
1316 | const struct object_id *oid) | |
95099731 | 1317 | { |
5180cacc | 1318 | int bytes = strlen(buffer) + 1; |
3aee68aa | 1319 | unsigned char *buf = xmallocz(size); |
93821bd9 | 1320 | unsigned long n; |
7efbff75 | 1321 | int status = Z_OK; |
95099731 | 1322 | |
93821bd9 LT |
1323 | n = stream->total_out - bytes; |
1324 | if (n > size) | |
1325 | n = size; | |
1326 | memcpy(buf, (char *) buffer + bytes, n); | |
1327 | bytes = n; | |
456cdf6e LT |
1328 | if (bytes <= size) { |
1329 | /* | |
1330 | * The above condition must be (bytes <= size), not | |
1331 | * (bytes < size). In other words, even though we | |
ccf5ace0 | 1332 | * expect no more output and set avail_out to zero, |
456cdf6e LT |
1333 | * the input zlib stream may have bytes that express |
1334 | * "this concludes the stream", and we *do* want to | |
1335 | * eat that input. | |
1336 | * | |
1337 | * Otherwise we would not be able to test that we | |
1338 | * consumed all the input to reach the expected size; | |
1339 | * we also want to check that zlib tells us that all | |
1340 | * went well with status == Z_STREAM_END at the end. | |
1341 | */ | |
5180cacc LT |
1342 | stream->next_out = buf + bytes; |
1343 | stream->avail_out = size - bytes; | |
31877c9a MT |
1344 | while (status == Z_OK) { |
1345 | obj_read_unlock(); | |
39c68542 | 1346 | status = git_inflate(stream, Z_FINISH); |
31877c9a MT |
1347 | obj_read_lock(); |
1348 | } | |
5180cacc | 1349 | } |
456cdf6e | 1350 | if (status == Z_STREAM_END && !stream->avail_in) { |
39c68542 | 1351 | git_inflate_end(stream); |
7efbff75 | 1352 | return buf; |
95099731 NTND |
1353 | } |
1354 | ||
7efbff75 | 1355 | if (status < 0) |
00a7760e | 1356 | error(_("corrupt loose object '%s'"), oid_to_hex(oid)); |
7efbff75 | 1357 | else if (stream->avail_in) |
259328b7 | 1358 | error(_("garbage at end of loose object '%s'"), |
00a7760e | 1359 | oid_to_hex(oid)); |
7efbff75 JH |
1360 | free(buf); |
1361 | return NULL; | |
95099731 NTND |
1362 | } |
1363 | ||
d40d535b | 1364 | /* |
5180cacc LT |
1365 | * We used to just use "sscanf()", but that's actually way |
1366 | * too permissive for what we want to check. So do an anal | |
1367 | * object header parse by hand. | |
d40d535b | 1368 | */ |
dccb32bf | 1369 | int parse_loose_header(const char *hdr, struct object_info *oi) |
1f688557 | 1370 | { |
46f03448 | 1371 | const char *type_buf = hdr; |
d6a09e79 | 1372 | size_t size; |
46f03448 | 1373 | int type, type_len = 0; |
43057304 | 1374 | |
5180cacc | 1375 | /* |
46f03448 | 1376 | * The type can be of any size but is followed by |
21666f1a | 1377 | * a space. |
5180cacc | 1378 | */ |
5180cacc LT |
1379 | for (;;) { |
1380 | char c = *hdr++; | |
d21f8426 JH |
1381 | if (!c) |
1382 | return -1; | |
5180cacc LT |
1383 | if (c == ' ') |
1384 | break; | |
46f03448 | 1385 | type_len++; |
5180cacc | 1386 | } |
1f688557 | 1387 | |
46f03448 | 1388 | type = type_from_string_gently(type_buf, type_len, 1); |
6ca32f47 BW |
1389 | if (oi->type_name) |
1390 | strbuf_add(oi->type_name, type_buf, type_len); | |
46f03448 KN |
1391 | if (oi->typep) |
1392 | *oi->typep = type; | |
5180cacc LT |
1393 | |
1394 | /* | |
1395 | * The length must follow immediately, and be in canonical | |
1396 | * decimal format (ie "010" is not valid). | |
1397 | */ | |
1398 | size = *hdr++ - '0'; | |
1399 | if (size > 9) | |
1400 | return -1; | |
1401 | if (size) { | |
1402 | for (;;) { | |
1403 | unsigned long c = *hdr - '0'; | |
1404 | if (c > 9) | |
1405 | break; | |
1406 | hdr++; | |
d6a09e79 | 1407 | size = st_add(st_mult(size, 10), c); |
1b1005d1 | 1408 | } |
c01f51cc | 1409 | } |
46f03448 KN |
1410 | |
1411 | if (oi->sizep) | |
d6a09e79 | 1412 | *oi->sizep = cast_size_t_to_ulong(size); |
5180cacc LT |
1413 | |
1414 | /* | |
1415 | * The length must be followed by a zero byte | |
1416 | */ | |
dccb32bf ÆAB |
1417 | if (*hdr) |
1418 | return -1; | |
21666f1a | 1419 | |
dccb32bf ÆAB |
1420 | /* |
1421 | * The format is valid, but the type may still be bogus. The | |
1422 | * Caller needs to check its oi->typep. | |
1423 | */ | |
1424 | return 0; | |
bf592c50 DB |
1425 | } |
1426 | ||
514c5fdd JK |
1427 | static int loose_object_info(struct repository *r, |
1428 | const struct object_id *oid, | |
1429 | struct object_info *oi, int flags) | |
65c2e0c3 | 1430 | { |
46f03448 | 1431 | int status = 0; |
9e59b38c | 1432 | int fd; |
46f03448 | 1433 | unsigned long mapsize; |
9e59b38c | 1434 | const char *path; |
65c2e0c3 | 1435 | void *map; |
ef49a7a0 | 1436 | git_zstream stream; |
1af64f73 | 1437 | char hdr[MAX_HEADER_LEN]; |
46f03448 | 1438 | struct strbuf hdrbuf = STRBUF_INIT; |
c84a1f3e | 1439 | unsigned long size_scratch; |
dccb32bf | 1440 | enum object_type type_scratch; |
01cab976 | 1441 | int allow_unknown = flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE; |
65c2e0c3 | 1442 | |
b99b6bcc JK |
1443 | if (oi->delta_base_oid) |
1444 | oidclr(oi->delta_base_oid); | |
5d642e75 | 1445 | |
052fe5ea JK |
1446 | /* |
1447 | * If we don't care about type or size, then we don't | |
4ef8d1dd JH |
1448 | * need to look inside the object at all. Note that we |
1449 | * do not optimize out the stat call, even if the | |
1450 | * caller doesn't care about the disk-size, since our | |
1451 | * return value implicitly indicates whether the | |
1452 | * object even exists. | |
052fe5ea | 1453 | */ |
6ca32f47 | 1454 | if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) { |
4ef8d1dd | 1455 | struct stat st; |
61c7711c | 1456 | if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK)) |
d7a24573 | 1457 | return quick_has_loose(r, oid) ? 0 : -1; |
514c5fdd | 1458 | if (stat_loose_object(r, oid, &st, &path) < 0) |
4ef8d1dd JH |
1459 | return -1; |
1460 | if (oi->disk_sizep) | |
23c339c0 | 1461 | *oi->disk_sizep = st.st_size; |
052fe5ea JK |
1462 | return 0; |
1463 | } | |
1464 | ||
9e59b38c JT |
1465 | fd = open_loose_object(r, oid, &path); |
1466 | if (fd < 0) { | |
1467 | if (errno != ENOENT) | |
1468 | error_errno(_("unable to open loose object %s"), oid_to_hex(oid)); | |
1469 | return -1; | |
1470 | } | |
1471 | map = map_fd(fd, path, &mapsize); | |
f0df4ed5 | 1472 | if (!map) |
dbea72a8 | 1473 | return -1; |
c84a1f3e JT |
1474 | |
1475 | if (!oi->sizep) | |
1476 | oi->sizep = &size_scratch; | |
dccb32bf ÆAB |
1477 | if (!oi->typep) |
1478 | oi->typep = &type_scratch; | |
c84a1f3e | 1479 | |
23c339c0 JK |
1480 | if (oi->disk_sizep) |
1481 | *oi->disk_sizep = mapsize; | |
01cab976 | 1482 | |
3b6a8db3 ÆAB |
1483 | switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr), |
1484 | allow_unknown ? &hdrbuf : NULL)) { | |
1485 | case ULHR_OK: | |
dccb32bf ÆAB |
1486 | if (parse_loose_header(hdrbuf.len ? hdrbuf.buf : hdr, oi) < 0) |
1487 | status = error(_("unable to parse %s header"), oid_to_hex(oid)); | |
1488 | else if (!allow_unknown && *oi->typep < 0) | |
1489 | die(_("invalid object type")); | |
1490 | ||
1491 | if (!oi->contentp) | |
1492 | break; | |
1493 | *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid); | |
1494 | if (*oi->contentp) | |
1495 | goto cleanup; | |
1496 | ||
1497 | status = -1; | |
3b6a8db3 ÆAB |
1498 | break; |
1499 | case ULHR_BAD: | |
259328b7 | 1500 | status = error(_("unable to unpack %s header"), |
514c5fdd | 1501 | oid_to_hex(oid)); |
3b6a8db3 | 1502 | break; |
5848fb11 ÆAB |
1503 | case ULHR_TOO_LONG: |
1504 | status = error(_("header for %s too long, exceeds %d bytes"), | |
1505 | oid_to_hex(oid), MAX_HEADER_LEN); | |
1506 | break; | |
3b6a8db3 | 1507 | } |
c84a1f3e | 1508 | |
9e59b38c JT |
1509 | if (status && (flags & OBJECT_INFO_DIE_IF_CORRUPT)) |
1510 | die(_("loose object %s (stored in %s) is corrupt"), | |
1511 | oid_to_hex(oid), path); | |
1512 | ||
dccb32bf ÆAB |
1513 | git_inflate_end(&stream); |
1514 | cleanup: | |
65c2e0c3 | 1515 | munmap(map, mapsize); |
c84a1f3e JT |
1516 | if (oi->sizep == &size_scratch) |
1517 | oi->sizep = NULL; | |
46f03448 | 1518 | strbuf_release(&hdrbuf); |
dccb32bf ÆAB |
1519 | if (oi->typep == &type_scratch) |
1520 | oi->typep = NULL; | |
3ab0fb06 | 1521 | oi->whence = OI_LOOSE; |
dccb32bf | 1522 | return status; |
65c2e0c3 JH |
1523 | } |
1524 | ||
31877c9a MT |
1525 | int obj_read_use_lock = 0; |
1526 | pthread_mutex_t obj_read_mutex; | |
1527 | ||
1528 | void enable_obj_read_lock(void) | |
1529 | { | |
1530 | if (obj_read_use_lock) | |
1531 | return; | |
1532 | ||
1533 | obj_read_use_lock = 1; | |
1534 | init_recursive_mutex(&obj_read_mutex); | |
1535 | } | |
1536 | ||
1537 | void disable_obj_read_lock(void) | |
1538 | { | |
1539 | if (!obj_read_use_lock) | |
1540 | return; | |
1541 | ||
1542 | obj_read_use_lock = 0; | |
1543 | pthread_mutex_destroy(&obj_read_mutex); | |
1544 | } | |
1545 | ||
8b4c0103 JT |
1546 | int fetch_if_missing = 1; |
1547 | ||
31877c9a MT |
1548 | static int do_oid_object_info_extended(struct repository *r, |
1549 | const struct object_id *oid, | |
1550 | struct object_info *oi, unsigned flags) | |
f0df4ed5 | 1551 | { |
cd585e2a | 1552 | static struct object_info blank_oi = OBJECT_INFO_INIT; |
9c8a294a | 1553 | struct cached_object *co; |
f0df4ed5 | 1554 | struct pack_entry e; |
5b086407 | 1555 | int rtype; |
b383a13c | 1556 | const struct object_id *real = oid; |
8b4c0103 | 1557 | int already_retried = 0; |
f0df4ed5 | 1558 | |
31877c9a | 1559 | |
b383a13c | 1560 | if (flags & OBJECT_INFO_LOOKUP_REPLACE) |
9d98354f | 1561 | real = lookup_replace_object(r, oid); |
f0df4ed5 | 1562 | |
b383a13c | 1563 | if (is_null_oid(real)) |
87b5e236 JK |
1564 | return -1; |
1565 | ||
cd585e2a JT |
1566 | if (!oi) |
1567 | oi = &blank_oi; | |
1568 | ||
9c8a294a JT |
1569 | co = find_cached_object(real); |
1570 | if (co) { | |
1571 | if (oi->typep) | |
1572 | *(oi->typep) = co->type; | |
1573 | if (oi->sizep) | |
1574 | *(oi->sizep) = co->size; | |
1575 | if (oi->disk_sizep) | |
1576 | *(oi->disk_sizep) = 0; | |
b99b6bcc JK |
1577 | if (oi->delta_base_oid) |
1578 | oidclr(oi->delta_base_oid); | |
9c8a294a JT |
1579 | if (oi->type_name) |
1580 | strbuf_addstr(oi->type_name, type_name(co->type)); | |
1581 | if (oi->contentp) | |
1582 | *oi->contentp = xmemdupz(co->buf, co->size); | |
1583 | oi->whence = OI_CACHED; | |
1584 | return 0; | |
c4d9986f NTND |
1585 | } |
1586 | ||
8b4c0103 | 1587 | while (1) { |
42c8ce1c | 1588 | if (find_pack_entry(r, real, &e)) |
8b4c0103 JT |
1589 | break; |
1590 | ||
ddd63e64 | 1591 | /* Most likely it's a loose object. */ |
514c5fdd | 1592 | if (!loose_object_info(r, real, oi, flags)) |
5b086407 | 1593 | return 0; |
ddd63e64 SG |
1594 | |
1595 | /* Not a loose object; someone else may have just packed it. */ | |
2b7750c9 | 1596 | if (!(flags & OBJECT_INFO_QUICK)) { |
9d98354f | 1597 | reprepare_packed_git(r); |
42c8ce1c | 1598 | if (find_pack_entry(r, real, &e)) |
2b7750c9 JT |
1599 | break; |
1600 | } | |
8b4c0103 | 1601 | |
eef71904 JT |
1602 | /* |
1603 | * If r is the_repository, this might be an attempt at | |
1604 | * accessing a submodule object as if it were in the_repository | |
1605 | * (having called add_submodule_odb() on that submodule's ODB). | |
1606 | * If any such ODBs exist, register them and try again. | |
1607 | */ | |
1608 | if (r == the_repository && | |
1609 | register_all_submodule_odb_as_alternates()) | |
a35e03de JT |
1610 | /* We added some alternates; retry */ |
1611 | continue; | |
1612 | ||
8b4c0103 | 1613 | /* Check if it is a missing object */ |
ef830cc4 JT |
1614 | if (fetch_if_missing && repo_has_promisor_remote(r) && |
1615 | !already_retried && | |
31f5256c | 1616 | !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) { |
b14ed5ad | 1617 | promisor_remote_get_direct(r, real, 1); |
8b4c0103 JT |
1618 | already_retried = 1; |
1619 | continue; | |
dfdd4afc | 1620 | } |
8b4c0103 | 1621 | |
9e59b38c JT |
1622 | if (flags & OBJECT_INFO_DIE_IF_CORRUPT) { |
1623 | const struct packed_git *p; | |
1624 | if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid)) | |
1625 | die(_("replacement %s not found for %s"), | |
1626 | oid_to_hex(real), oid_to_hex(oid)); | |
1627 | if ((p = has_packed_and_bad(r, real))) | |
1628 | die(_("packed object %s (stored in %s) is corrupt"), | |
1629 | oid_to_hex(real), p->pack_name); | |
1630 | } | |
8b4c0103 | 1631 | return -1; |
f0df4ed5 | 1632 | } |
3d77d877 | 1633 | |
cd585e2a JT |
1634 | if (oi == &blank_oi) |
1635 | /* | |
1636 | * We know that the caller doesn't actually need the | |
1637 | * information below, so return early. | |
1638 | */ | |
1639 | return 0; | |
9d98354f | 1640 | rtype = packed_object_info(r, e.p, e.offset, oi); |
412916ee | 1641 | if (rtype < 0) { |
751530de | 1642 | mark_bad_packed_object(e.p, real); |
31877c9a | 1643 | return do_oid_object_info_extended(r, real, oi, 0); |
3ab0fb06 | 1644 | } else if (oi->whence == OI_PACKED) { |
9a490590 JH |
1645 | oi->u.packed.offset = e.offset; |
1646 | oi->u.packed.pack = e.p; | |
1647 | oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA || | |
1648 | rtype == OBJ_OFS_DELTA); | |
3d77d877 NP |
1649 | } |
1650 | ||
5b086407 | 1651 | return 0; |
f0df4ed5 JS |
1652 | } |
1653 | ||
31877c9a MT |
1654 | int oid_object_info_extended(struct repository *r, const struct object_id *oid, |
1655 | struct object_info *oi, unsigned flags) | |
1656 | { | |
1657 | int ret; | |
1658 | obj_read_lock(); | |
1659 | ret = do_oid_object_info_extended(r, oid, oi, flags); | |
1660 | obj_read_unlock(); | |
1661 | return ret; | |
1662 | } | |
1663 | ||
1664 | ||
3fc0dca9 | 1665 | /* returns enum object_type or negative */ |
9d98354f SB |
1666 | int oid_object_info(struct repository *r, |
1667 | const struct object_id *oid, | |
1668 | unsigned long *sizep) | |
9a490590 | 1669 | { |
5b086407 | 1670 | enum object_type type; |
27b5c1a0 | 1671 | struct object_info oi = OBJECT_INFO_INIT; |
9a490590 | 1672 | |
5b086407 | 1673 | oi.typep = &type; |
9a490590 | 1674 | oi.sizep = sizep; |
9d98354f SB |
1675 | if (oid_object_info_extended(r, oid, &oi, |
1676 | OBJECT_INFO_LOOKUP_REPLACE) < 0) | |
5b086407 JK |
1677 | return -1; |
1678 | return type; | |
9a490590 JH |
1679 | } |
1680 | ||
829e5c3b PO |
1681 | int pretend_object_file(void *buf, unsigned long len, enum object_type type, |
1682 | struct object_id *oid) | |
d66b37bb JH |
1683 | { |
1684 | struct cached_object *co; | |
1685 | ||
44439c1c | 1686 | hash_object_file(the_hash_algo, buf, len, type, oid); |
a64d2aae JT |
1687 | if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) || |
1688 | find_cached_object(oid)) | |
d66b37bb | 1689 | return 0; |
c7353967 | 1690 | ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc); |
d66b37bb JH |
1691 | co = &cached_objects[cached_object_nr++]; |
1692 | co->size = len; | |
21666f1a | 1693 | co->type = type; |
efa13f7b JH |
1694 | co->buf = xmalloc(len); |
1695 | memcpy(co->buf, buf, len); | |
62ba93ea | 1696 | oidcpy(&co->oid, oid); |
d66b37bb JH |
1697 | return 0; |
1698 | } | |
1699 | ||
b6c4cecc JH |
1700 | /* |
1701 | * This function dies on corrupt objects; the callers who want to | |
b25562e6 JK |
1702 | * deal with them should arrange to call oid_object_info_extended() and give |
1703 | * error messages themselves. | |
b6c4cecc | 1704 | */ |
0ba05cf2 JK |
1705 | void *repo_read_object_file(struct repository *r, |
1706 | const struct object_id *oid, | |
1707 | enum object_type *type, | |
1708 | unsigned long *size) | |
ac939109 | 1709 | { |
b25562e6 | 1710 | struct object_info oi = OBJECT_INFO_INIT; |
7be13f5f | 1711 | unsigned flags = OBJECT_INFO_DIE_IF_CORRUPT | OBJECT_INFO_LOOKUP_REPLACE; |
3ba7a065 | 1712 | void *data; |
b6c4cecc | 1713 | |
b25562e6 JK |
1714 | oi.typep = type; |
1715 | oi.sizep = size; | |
1716 | oi.contentp = &data; | |
b25562e6 | 1717 | if (oid_object_info_extended(r, oid, &oi, flags)) |
15b63689 | 1718 | return NULL; |
68095570 | 1719 | |
b25562e6 | 1720 | return data; |
ac939109 NP |
1721 | } |
1722 | ||
d3b4705a NTND |
1723 | void *read_object_with_reference(struct repository *r, |
1724 | const struct object_id *oid, | |
6aea6bae | 1725 | enum object_type required_type, |
40469ee9 | 1726 | unsigned long *size, |
02f0547e | 1727 | struct object_id *actual_oid_return) |
f4913f91 | 1728 | { |
6aea6bae | 1729 | enum object_type type; |
f4913f91 JH |
1730 | void *buffer; |
1731 | unsigned long isize; | |
02f0547e | 1732 | struct object_id actual_oid; |
f4913f91 | 1733 | |
02f0547e | 1734 | oidcpy(&actual_oid, oid); |
40469ee9 JH |
1735 | while (1) { |
1736 | int ref_length = -1; | |
1737 | const char *ref_type = NULL; | |
f4913f91 | 1738 | |
d3b4705a | 1739 | buffer = repo_read_object_file(r, &actual_oid, &type, &isize); |
40469ee9 JH |
1740 | if (!buffer) |
1741 | return NULL; | |
21666f1a | 1742 | if (type == required_type) { |
40469ee9 | 1743 | *size = isize; |
02f0547e | 1744 | if (actual_oid_return) |
1745 | oidcpy(actual_oid_return, &actual_oid); | |
40469ee9 JH |
1746 | return buffer; |
1747 | } | |
1748 | /* Handle references */ | |
21666f1a | 1749 | else if (type == OBJ_COMMIT) |
40469ee9 | 1750 | ref_type = "tree "; |
21666f1a | 1751 | else if (type == OBJ_TAG) |
40469ee9 JH |
1752 | ref_type = "object "; |
1753 | else { | |
1754 | free(buffer); | |
1755 | return NULL; | |
1756 | } | |
1757 | ref_length = strlen(ref_type); | |
f4913f91 | 1758 | |
94b5e093 | 1759 | if (ref_length + the_hash_algo->hexsz > isize || |
50974ec9 | 1760 | memcmp(buffer, ref_type, ref_length) || |
02f0547e | 1761 | get_oid_hex((char *) buffer + ref_length, &actual_oid)) { |
40469ee9 JH |
1762 | free(buffer); |
1763 | return NULL; | |
1764 | } | |
1cf58e72 | 1765 | free(buffer); |
40469ee9 | 1766 | /* Now we have the ID of the referred-to object in |
02f0547e | 1767 | * actual_oid. Check again. */ |
f4913f91 | 1768 | } |
f4913f91 JH |
1769 | } |
1770 | ||
2bbb28a3 ÆAB |
1771 | static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c, |
1772 | const void *buf, unsigned long len, | |
1773 | struct object_id *oid, | |
1774 | char *hdr, int *hdrlen) | |
1775 | { | |
1776 | algo->init_fn(c); | |
1777 | algo->update_fn(c, hdr, *hdrlen); | |
1778 | algo->update_fn(c, buf, len); | |
1779 | algo->final_oid_fn(oid, c); | |
1780 | } | |
1781 | ||
7ad5c44d MT |
1782 | static void write_object_file_prepare(const struct git_hash_algo *algo, |
1783 | const void *buf, unsigned long len, | |
2bbb28a3 | 1784 | enum object_type type, struct object_id *oid, |
a09c985e | 1785 | char *hdr, int *hdrlen) |
d410c0f5 | 1786 | { |
18e2588e | 1787 | git_hash_ctx c; |
d410c0f5 JH |
1788 | |
1789 | /* Generate the header */ | |
2bbb28a3 | 1790 | *hdrlen = format_object_header(hdr, *hdrlen, type, len); |
d410c0f5 JH |
1791 | |
1792 | /* Sha1.. */ | |
2bbb28a3 ÆAB |
1793 | hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); |
1794 | } | |
1795 | ||
1796 | static void write_object_file_prepare_literally(const struct git_hash_algo *algo, | |
1797 | const void *buf, unsigned long len, | |
1798 | const char *type, struct object_id *oid, | |
1799 | char *hdr, int *hdrlen) | |
1800 | { | |
1801 | git_hash_ctx c; | |
1802 | ||
1803 | *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); | |
1804 | hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); | |
d410c0f5 JH |
1805 | } |
1806 | ||
230f1322 | 1807 | /* |
5a688fe4 | 1808 | * Move the just written object into its final resting place. |
230f1322 | 1809 | */ |
cb5add58 | 1810 | int finalize_object_file(const char *tmpfile, const char *filename) |
230f1322 | 1811 | { |
e32c0a9c | 1812 | int ret = 0; |
5a688fe4 | 1813 | |
348df166 | 1814 | if (object_creation_mode == OBJECT_CREATION_USES_RENAMES) |
be66a6c4 JS |
1815 | goto try_rename; |
1816 | else if (link(tmpfile, filename)) | |
e32c0a9c | 1817 | ret = errno; |
7ebb6fca LT |
1818 | |
1819 | /* | |
1820 | * Coda hack - coda doesn't like cross-directory links, | |
1821 | * so we fall back to a rename, which will mean that it | |
1822 | * won't be able to check collisions, but that's not a | |
1823 | * big deal. | |
1824 | * | |
1825 | * The same holds for FAT formatted media. | |
1826 | * | |
3be1f18e | 1827 | * When this succeeds, we just return. We have nothing |
7ebb6fca LT |
1828 | * left to unlink. |
1829 | */ | |
1830 | if (ret && ret != EEXIST) { | |
be66a6c4 | 1831 | try_rename: |
7ebb6fca | 1832 | if (!rename(tmpfile, filename)) |
3be1f18e | 1833 | goto out; |
9e48b389 | 1834 | ret = errno; |
230f1322 | 1835 | } |
691f1a28 | 1836 | unlink_or_warn(tmpfile); |
230f1322 LT |
1837 | if (ret) { |
1838 | if (ret != EEXIST) { | |
2c319886 | 1839 | return error_errno(_("unable to write file %s"), filename); |
230f1322 LT |
1840 | } |
1841 | /* FIXME!!! Collision check here ? */ | |
1842 | } | |
1843 | ||
3be1f18e | 1844 | out: |
5256b006 | 1845 | if (adjust_shared_perm(filename)) |
259328b7 | 1846 | return error(_("unable to set permission to '%s'"), filename); |
230f1322 LT |
1847 | return 0; |
1848 | } | |
1849 | ||
44439c1c ÆAB |
1850 | static void hash_object_file_literally(const struct git_hash_algo *algo, |
1851 | const void *buf, unsigned long len, | |
1852 | const char *type, struct object_id *oid) | |
abdc3fc8 | 1853 | { |
1af64f73 | 1854 | char hdr[MAX_HEADER_LEN]; |
ef1286d3 | 1855 | int hdrlen = sizeof(hdr); |
44439c1c | 1856 | |
2bbb28a3 | 1857 | write_object_file_prepare_literally(algo, buf, len, type, oid, hdr, &hdrlen); |
abdc3fc8 RS |
1858 | } |
1859 | ||
44439c1c ÆAB |
1860 | void hash_object_file(const struct git_hash_algo *algo, const void *buf, |
1861 | unsigned long len, enum object_type type, | |
1862 | struct object_id *oid) | |
1863 | { | |
1864 | hash_object_file_literally(algo, buf, len, type_name(type), oid); | |
abdc3fc8 RS |
1865 | } |
1866 | ||
e9039dd3 | 1867 | /* Finalize a file on disk, and close it. */ |
c4e707f8 | 1868 | static void close_loose_object(int fd, const char *filename) |
e9039dd3 | 1869 | { |
020406ea NS |
1870 | if (the_repository->objects->odb->will_destroy) |
1871 | goto out; | |
b3cecf49 | 1872 | |
c0f4752e | 1873 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
83937e95 | 1874 | fsync_loose_object_bulk_checkin(fd, filename); |
c0f4752e | 1875 | else if (fsync_object_files > 0) |
c4e707f8 | 1876 | fsync_or_die(fd, filename); |
020406ea NS |
1877 | else |
1878 | fsync_component_or_die(FSYNC_COMPONENT_LOOSE_OBJECT, fd, | |
c4e707f8 | 1879 | filename); |
020406ea NS |
1880 | |
1881 | out: | |
e9039dd3 | 1882 | if (close(fd) != 0) |
76011357 | 1883 | die_errno(_("error when closing loose object file")); |
e9039dd3 LT |
1884 | } |
1885 | ||
5723fe7e LT |
1886 | /* Size of directory component, including the ending '/' */ |
1887 | static inline int directory_size(const char *filename) | |
1888 | { | |
1889 | const char *s = strrchr(filename, '/'); | |
1890 | if (!s) | |
1891 | return 0; | |
1892 | return s - filename + 1; | |
1893 | } | |
1894 | ||
1895 | /* | |
1896 | * This creates a temporary file in the same directory as the final | |
1897 | * 'filename' | |
1898 | * | |
1899 | * We want to avoid cross-directory filename renames, because those | |
1900 | * can have problems on various filesystems (FAT, NFS, Coda). | |
1901 | */ | |
d4b3d11a | 1902 | static int create_tmpfile(struct strbuf *tmp, const char *filename) |
5723fe7e LT |
1903 | { |
1904 | int fd, dirlen = directory_size(filename); | |
1905 | ||
d4b3d11a JK |
1906 | strbuf_reset(tmp); |
1907 | strbuf_add(tmp, filename, dirlen); | |
1908 | strbuf_addstr(tmp, "tmp_obj_XXXXXX"); | |
1909 | fd = git_mkstemp_mode(tmp->buf, 0444); | |
cbacbf4e | 1910 | if (fd < 0 && dirlen && errno == ENOENT) { |
d4b3d11a JK |
1911 | /* |
1912 | * Make sure the directory exists; note that the contents | |
1913 | * of the buffer are undefined after mkstemp returns an | |
1914 | * error, so we have to rewrite the whole buffer from | |
1915 | * scratch. | |
1916 | */ | |
1917 | strbuf_reset(tmp); | |
1918 | strbuf_add(tmp, filename, dirlen - 1); | |
1919 | if (mkdir(tmp->buf, 0777) && errno != EEXIST) | |
b2476a60 | 1920 | return -1; |
d4b3d11a | 1921 | if (adjust_shared_perm(tmp->buf)) |
5723fe7e LT |
1922 | return -1; |
1923 | ||
1924 | /* Try again */ | |
d4b3d11a JK |
1925 | strbuf_addstr(tmp, "/tmp_obj_XXXXXX"); |
1926 | fd = git_mkstemp_mode(tmp->buf, 0444); | |
5723fe7e LT |
1927 | } |
1928 | return fd; | |
1929 | } | |
1930 | ||
97a9db6f HX |
1931 | /** |
1932 | * Common steps for loose object writers to start writing loose | |
1933 | * objects: | |
1934 | * | |
1935 | * - Create tmpfile for the loose object. | |
1936 | * - Setup zlib stream for compression. | |
1937 | * - Start to feed header to zlib stream. | |
1938 | * | |
1939 | * Returns a "fd", which should later be provided to | |
1940 | * end_loose_object_common(). | |
1941 | */ | |
1942 | static int start_loose_object_common(struct strbuf *tmp_file, | |
1943 | const char *filename, unsigned flags, | |
1944 | git_zstream *stream, | |
1945 | unsigned char *buf, size_t buflen, | |
1946 | git_hash_ctx *c, | |
1947 | char *hdr, int hdrlen) | |
1948 | { | |
1949 | int fd; | |
1950 | ||
1951 | fd = create_tmpfile(tmp_file, filename); | |
1952 | if (fd < 0) { | |
1953 | if (flags & HASH_SILENT) | |
1954 | return -1; | |
1955 | else if (errno == EACCES) | |
1956 | return error(_("insufficient permission for adding " | |
1957 | "an object to repository database %s"), | |
1958 | get_object_directory()); | |
1959 | else | |
1960 | return error_errno( | |
1961 | _("unable to create temporary file")); | |
1962 | } | |
1963 | ||
1964 | /* Setup zlib stream for compression */ | |
1965 | git_deflate_init(stream, zlib_compression_level); | |
1966 | stream->next_out = buf; | |
1967 | stream->avail_out = buflen; | |
1968 | the_hash_algo->init_fn(c); | |
1969 | ||
1970 | /* Start to feed header to zlib stream */ | |
1971 | stream->next_in = (unsigned char *)hdr; | |
1972 | stream->avail_in = hdrlen; | |
1973 | while (git_deflate(stream, 0) == Z_OK) | |
1974 | ; /* nothing */ | |
1975 | the_hash_algo->update_fn(c, hdr, hdrlen); | |
1976 | ||
1977 | return fd; | |
1978 | } | |
1979 | ||
21e7d881 ÆAB |
1980 | /** |
1981 | * Common steps for the inner git_deflate() loop for writing loose | |
1982 | * objects. Returns what git_deflate() returns. | |
1983 | */ | |
1984 | static int write_loose_object_common(git_hash_ctx *c, | |
1985 | git_zstream *stream, const int flush, | |
1986 | unsigned char *in0, const int fd, | |
1987 | unsigned char *compressed, | |
1988 | const size_t compressed_len) | |
1989 | { | |
1990 | int ret; | |
1991 | ||
1992 | ret = git_deflate(stream, flush ? Z_FINISH : 0); | |
1993 | the_hash_algo->update_fn(c, in0, stream->next_in - in0); | |
d422d061 RS |
1994 | if (write_in_full(fd, compressed, stream->next_out - compressed) < 0) |
1995 | die_errno(_("unable to write loose object file")); | |
21e7d881 ÆAB |
1996 | stream->next_out = compressed; |
1997 | stream->avail_out = compressed_len; | |
1998 | ||
1999 | return ret; | |
2000 | } | |
2001 | ||
97a9db6f HX |
2002 | /** |
2003 | * Common steps for loose object writers to end writing loose objects: | |
2004 | * | |
2005 | * - End the compression of zlib stream. | |
2006 | * - Get the calculated oid to "oid". | |
2007 | */ | |
2008 | static int end_loose_object_common(git_hash_ctx *c, git_zstream *stream, | |
2009 | struct object_id *oid) | |
2010 | { | |
2011 | int ret; | |
2012 | ||
2013 | ret = git_deflate_end_gently(stream); | |
2014 | if (ret != Z_OK) | |
2015 | return ret; | |
2016 | the_hash_algo->final_oid_fn(oid, c); | |
2017 | ||
2018 | return Z_OK; | |
2019 | } | |
2020 | ||
3fc7281f PO |
2021 | static int write_loose_object(const struct object_id *oid, char *hdr, |
2022 | int hdrlen, const void *buf, unsigned long len, | |
4ef91a2d | 2023 | time_t mtime, unsigned flags) |
0fcfd160 | 2024 | { |
915308b1 | 2025 | int fd, ret; |
9892beba | 2026 | unsigned char compressed[4096]; |
ef49a7a0 | 2027 | git_zstream stream; |
18e2588e | 2028 | git_hash_ctx c; |
3fc7281f | 2029 | struct object_id parano_oid; |
d4b3d11a | 2030 | static struct strbuf tmp_file = STRBUF_INIT; |
ea657730 CC |
2031 | static struct strbuf filename = STRBUF_INIT; |
2032 | ||
c0f4752e NS |
2033 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
2034 | prepare_loose_object_bulk_checkin(); | |
2035 | ||
514c5fdd | 2036 | loose_object_path(the_repository, &filename, oid); |
a44c9a5e | 2037 | |
97a9db6f HX |
2038 | fd = start_loose_object_common(&tmp_file, filename.buf, flags, |
2039 | &stream, compressed, sizeof(compressed), | |
2040 | &c, hdr, hdrlen); | |
2041 | if (fd < 0) | |
2042 | return -1; | |
a44c9a5e LT |
2043 | |
2044 | /* Then the data itself.. */ | |
c00e657d | 2045 | stream.next_in = (void *)buf; |
a44c9a5e | 2046 | stream.avail_in = len; |
9892beba | 2047 | do { |
748af44c | 2048 | unsigned char *in0 = stream.next_in; |
21e7d881 ÆAB |
2049 | |
2050 | ret = write_loose_object_common(&c, &stream, 1, in0, fd, | |
2051 | compressed, sizeof(compressed)); | |
9892beba NP |
2052 | } while (ret == Z_OK); |
2053 | ||
ac54c277 | 2054 | if (ret != Z_STREAM_END) |
259328b7 | 2055 | die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid), |
3fc7281f | 2056 | ret); |
97a9db6f | 2057 | ret = end_loose_object_common(&c, &stream, ¶no_oid); |
ac54c277 | 2058 | if (ret != Z_OK) |
259328b7 | 2059 | die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid), |
3fc7281f | 2060 | ret); |
9001dc2a | 2061 | if (!oideq(oid, ¶no_oid)) |
259328b7 | 2062 | die(_("confused by unstable object source data for %s"), |
3fc7281f | 2063 | oid_to_hex(oid)); |
ac54c277 | 2064 | |
c4e707f8 | 2065 | close_loose_object(fd, tmp_file.buf); |
0fcfd160 | 2066 | |
bbac7311 NP |
2067 | if (mtime) { |
2068 | struct utimbuf utb; | |
2069 | utb.actime = mtime; | |
2070 | utb.modtime = mtime; | |
4ef91a2d ÆAB |
2071 | if (utime(tmp_file.buf, &utb) < 0 && |
2072 | !(flags & HASH_SILENT)) | |
259328b7 | 2073 | warning_errno(_("failed utime() on %s"), tmp_file.buf); |
bbac7311 NP |
2074 | } |
2075 | ||
ea657730 | 2076 | return finalize_object_file(tmp_file.buf, filename.buf); |
0fcfd160 | 2077 | } |
8237b185 | 2078 | |
6862ebbf | 2079 | static int freshen_loose_object(const struct object_id *oid) |
33d4221c | 2080 | { |
6862ebbf | 2081 | return check_and_freshen(oid, 1); |
33d4221c JK |
2082 | } |
2083 | ||
6862ebbf | 2084 | static int freshen_packed_object(const struct object_id *oid) |
33d4221c JK |
2085 | { |
2086 | struct pack_entry e; | |
544443cb | 2087 | if (!find_pack_entry(the_repository, oid, &e)) |
ee1c6c34 | 2088 | return 0; |
a6131642 TB |
2089 | if (e.p->is_cruft) |
2090 | return 0; | |
ee1c6c34 JK |
2091 | if (e.p->freshened) |
2092 | return 1; | |
2093 | if (!freshen_file(e.p->pack_name)) | |
2094 | return 0; | |
2095 | e.p->freshened = 1; | |
2096 | return 1; | |
33d4221c JK |
2097 | } |
2098 | ||
2b6070ac HX |
2099 | int stream_loose_object(struct input_stream *in_stream, size_t len, |
2100 | struct object_id *oid) | |
2101 | { | |
2102 | int fd, ret, err = 0, flush = 0; | |
2103 | unsigned char compressed[4096]; | |
2104 | git_zstream stream; | |
2105 | git_hash_ctx c; | |
2106 | struct strbuf tmp_file = STRBUF_INIT; | |
2107 | struct strbuf filename = STRBUF_INIT; | |
2108 | int dirlen; | |
2109 | char hdr[MAX_HEADER_LEN]; | |
2110 | int hdrlen; | |
2111 | ||
2112 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) | |
2113 | prepare_loose_object_bulk_checkin(); | |
2114 | ||
2115 | /* Since oid is not determined, save tmp file to odb path. */ | |
2116 | strbuf_addf(&filename, "%s/", get_object_directory()); | |
2117 | hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len); | |
2118 | ||
2119 | /* | |
2120 | * Common steps for write_loose_object and stream_loose_object to | |
2121 | * start writing loose objects: | |
2122 | * | |
2123 | * - Create tmpfile for the loose object. | |
2124 | * - Setup zlib stream for compression. | |
2125 | * - Start to feed header to zlib stream. | |
2126 | */ | |
2127 | fd = start_loose_object_common(&tmp_file, filename.buf, 0, | |
2128 | &stream, compressed, sizeof(compressed), | |
2129 | &c, hdr, hdrlen); | |
2130 | if (fd < 0) { | |
2131 | err = -1; | |
2132 | goto cleanup; | |
2133 | } | |
2134 | ||
2135 | /* Then the data itself.. */ | |
2136 | do { | |
2137 | unsigned char *in0 = stream.next_in; | |
2138 | ||
2139 | if (!stream.avail_in && !in_stream->is_finished) { | |
2140 | const void *in = in_stream->read(in_stream, &stream.avail_in); | |
2141 | stream.next_in = (void *)in; | |
2142 | in0 = (unsigned char *)in; | |
2143 | /* All data has been read. */ | |
2144 | if (in_stream->is_finished) | |
2145 | flush = 1; | |
2146 | } | |
2147 | ret = write_loose_object_common(&c, &stream, flush, in0, fd, | |
2148 | compressed, sizeof(compressed)); | |
2149 | /* | |
2150 | * Unlike write_loose_object(), we do not have the entire | |
2151 | * buffer. If we get Z_BUF_ERROR due to too few input bytes, | |
2152 | * then we'll replenish them in the next input_stream->read() | |
2153 | * call when we loop. | |
2154 | */ | |
2155 | } while (ret == Z_OK || ret == Z_BUF_ERROR); | |
2156 | ||
2157 | if (stream.total_in != len + hdrlen) | |
2158 | die(_("write stream object %ld != %"PRIuMAX), stream.total_in, | |
2159 | (uintmax_t)len + hdrlen); | |
2160 | ||
2161 | /* | |
2162 | * Common steps for write_loose_object and stream_loose_object to | |
2163 | * end writing loose oject: | |
2164 | * | |
2165 | * - End the compression of zlib stream. | |
2166 | * - Get the calculated oid. | |
2167 | */ | |
2168 | if (ret != Z_STREAM_END) | |
2169 | die(_("unable to stream deflate new object (%d)"), ret); | |
2170 | ret = end_loose_object_common(&c, &stream, oid); | |
2171 | if (ret != Z_OK) | |
2172 | die(_("deflateEnd on stream object failed (%d)"), ret); | |
2173 | close_loose_object(fd, tmp_file.buf); | |
2174 | ||
2175 | if (freshen_packed_object(oid) || freshen_loose_object(oid)) { | |
2176 | unlink_or_warn(tmp_file.buf); | |
2177 | goto cleanup; | |
2178 | } | |
2179 | ||
2180 | loose_object_path(the_repository, &filename, oid); | |
2181 | ||
2182 | /* We finally know the object path, and create the missing dir. */ | |
2183 | dirlen = directory_size(filename.buf); | |
2184 | if (dirlen) { | |
2185 | struct strbuf dir = STRBUF_INIT; | |
2186 | strbuf_add(&dir, filename.buf, dirlen); | |
2187 | ||
2188 | if (mkdir_in_gitdir(dir.buf) && errno != EEXIST) { | |
2189 | err = error_errno(_("unable to create directory %s"), dir.buf); | |
2190 | strbuf_release(&dir); | |
2191 | goto cleanup; | |
2192 | } | |
2193 | strbuf_release(&dir); | |
2194 | } | |
2195 | ||
2196 | err = finalize_object_file(tmp_file.buf, filename.buf); | |
2197 | cleanup: | |
2198 | strbuf_release(&tmp_file); | |
2199 | strbuf_release(&filename); | |
2200 | return err; | |
2201 | } | |
2202 | ||
4ef91a2d | 2203 | int write_object_file_flags(const void *buf, unsigned long len, |
c80d226a | 2204 | enum object_type type, struct object_id *oid, |
4ef91a2d | 2205 | unsigned flags) |
bbac7311 | 2206 | { |
1af64f73 | 2207 | char hdr[MAX_HEADER_LEN]; |
ef1286d3 | 2208 | int hdrlen = sizeof(hdr); |
bbac7311 NP |
2209 | |
2210 | /* Normally if we have it in the pack then we do not bother writing | |
2211 | * it out into .git/objects/??/?{38} file. | |
2212 | */ | |
7ad5c44d MT |
2213 | write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr, |
2214 | &hdrlen); | |
6862ebbf | 2215 | if (freshen_packed_object(oid) || freshen_loose_object(oid)) |
bbac7311 | 2216 | return 0; |
4ef91a2d | 2217 | return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags); |
bbac7311 NP |
2218 | } |
2219 | ||
0ff7b4f9 ÆAB |
2220 | int write_object_file_literally(const void *buf, unsigned long len, |
2221 | const char *type, struct object_id *oid, | |
2222 | unsigned flags) | |
0c3db67c ES |
2223 | { |
2224 | char *header; | |
2225 | int hdrlen, status = 0; | |
2226 | ||
2227 | /* type string, SP, %lu of the length plus NUL must fit this */ | |
1af64f73 | 2228 | hdrlen = strlen(type) + MAX_HEADER_LEN; |
ef1286d3 | 2229 | header = xmalloc(hdrlen); |
2bbb28a3 ÆAB |
2230 | write_object_file_prepare_literally(the_hash_algo, buf, len, type, |
2231 | oid, header, &hdrlen); | |
0c3db67c ES |
2232 | |
2233 | if (!(flags & HASH_WRITE_OBJECT)) | |
2234 | goto cleanup; | |
6862ebbf | 2235 | if (freshen_packed_object(oid) || freshen_loose_object(oid)) |
0c3db67c | 2236 | goto cleanup; |
4ef91a2d | 2237 | status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0); |
0c3db67c ES |
2238 | |
2239 | cleanup: | |
2240 | free(header); | |
2241 | return status; | |
2242 | } | |
2243 | ||
4bdb70a4 | 2244 | int force_object_loose(const struct object_id *oid, time_t mtime) |
bbac7311 | 2245 | { |
bbac7311 NP |
2246 | void *buf; |
2247 | unsigned long len; | |
b25562e6 | 2248 | struct object_info oi = OBJECT_INFO_INIT; |
bbac7311 | 2249 | enum object_type type; |
1af64f73 | 2250 | char hdr[MAX_HEADER_LEN]; |
bbac7311 | 2251 | int hdrlen; |
1fb23e65 | 2252 | int ret; |
bbac7311 | 2253 | |
6862ebbf | 2254 | if (has_loose_object(oid)) |
bbac7311 | 2255 | return 0; |
b25562e6 JK |
2256 | oi.typep = &type; |
2257 | oi.sizep = &len; | |
2258 | oi.contentp = &buf; | |
2259 | if (oid_object_info_extended(the_repository, oid, &oi, 0)) | |
2c319886 | 2260 | return error(_("cannot read object for %s"), oid_to_hex(oid)); |
b04cdea4 | 2261 | hdrlen = format_object_header(hdr, sizeof(hdr), type, len); |
4ef91a2d | 2262 | ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0); |
1fb23e65 BS |
2263 | free(buf); |
2264 | ||
2265 | return ret; | |
bbac7311 NP |
2266 | } |
2267 | ||
1d8d9cb6 JT |
2268 | int has_object(struct repository *r, const struct object_id *oid, |
2269 | unsigned flags) | |
2270 | { | |
2271 | int quick = !(flags & HAS_OBJECT_RECHECK_PACKED); | |
2272 | unsigned object_info_flags = OBJECT_INFO_SKIP_FETCH_OBJECT | | |
2273 | (quick ? OBJECT_INFO_QUICK : 0); | |
2274 | ||
2275 | if (!startup_info->have_repository) | |
2276 | return 0; | |
2277 | return oid_object_info_extended(r, oid, NULL, object_info_flags) >= 0; | |
2278 | } | |
2279 | ||
cba595ab JH |
2280 | int repo_has_object_file_with_flags(struct repository *r, |
2281 | const struct object_id *oid, int flags) | |
8237b185 | 2282 | { |
3e8b7d3c JN |
2283 | if (!startup_info->have_repository) |
2284 | return 0; | |
9c8a294a | 2285 | return oid_object_info_extended(r, oid, NULL, flags) >= 0; |
b419aa25 | 2286 | } |
2287 | ||
9b45f499 SB |
2288 | int repo_has_object_file(struct repository *r, |
2289 | const struct object_id *oid) | |
b419aa25 | 2290 | { |
cba595ab | 2291 | return repo_has_object_file_with_flags(r, oid, 0); |
5827a035 JK |
2292 | } |
2293 | ||
69bbbe48 JK |
2294 | /* |
2295 | * We can't use the normal fsck_error_function() for index_mem(), | |
2296 | * because we don't yet have a valid oid for it to report. Instead, | |
2297 | * report the minimal fsck error here, and rely on the caller to | |
2298 | * give more context. | |
2299 | */ | |
2300 | static int hash_format_check_report(struct fsck_options *opts, | |
2301 | const struct object_id *oid, | |
2302 | enum object_type object_type, | |
2303 | enum fsck_msg_type msg_type, | |
2304 | enum fsck_msg_id msg_id, | |
2305 | const char *message) | |
2306 | { | |
2307 | error(_("object fails fsck: %s"), message); | |
2308 | return 1; | |
c879daa2 NTND |
2309 | } |
2310 | ||
58bf2a4c NTND |
2311 | static int index_mem(struct index_state *istate, |
2312 | struct object_id *oid, void *buf, size_t size, | |
c4ce46fc JH |
2313 | enum object_type type, |
2314 | const char *path, unsigned flags) | |
e7332f96 | 2315 | { |
63e05f90 | 2316 | int ret = 0; |
bbea0dde | 2317 | int re_allocated = 0; |
c4ce46fc | 2318 | int write_object = flags & HASH_WRITE_OBJECT; |
74400e71 | 2319 | |
7672db20 | 2320 | if (!type) |
edaec3fb | 2321 | type = OBJ_BLOB; |
6c510bee LT |
2322 | |
2323 | /* | |
2324 | * Convert blobs to git internal format | |
2325 | */ | |
43df4f86 | 2326 | if ((type == OBJ_BLOB) && path) { |
f285a2d7 | 2327 | struct strbuf nbuf = STRBUF_INIT; |
58bf2a4c | 2328 | if (convert_to_git(istate, path, buf, size, &nbuf, |
8462ff43 | 2329 | get_conv_flags(flags))) { |
b315c5c0 | 2330 | buf = strbuf_detach(&nbuf, &size); |
6c510bee LT |
2331 | re_allocated = 1; |
2332 | } | |
2333 | } | |
c4ce46fc | 2334 | if (flags & HASH_FORMAT_CHECK) { |
69bbbe48 JK |
2335 | struct fsck_options opts = FSCK_OPTIONS_DEFAULT; |
2336 | ||
2337 | opts.strict = 1; | |
2338 | opts.error_func = hash_format_check_report; | |
2339 | if (fsck_buffer(null_oid(), type, buf, size, &opts)) | |
2340 | die(_("refusing to create malformed object")); | |
2341 | fsck_finish(&opts); | |
c879daa2 | 2342 | } |
6c510bee | 2343 | |
7672db20 | 2344 | if (write_object) |
c80d226a | 2345 | ret = write_object_file(buf, size, type, oid); |
abdc3fc8 | 2346 | else |
44439c1c | 2347 | hash_object_file(the_hash_algo, buf, size, type, oid); |
43df4f86 | 2348 | if (re_allocated) |
6c510bee | 2349 | free(buf); |
43df4f86 DP |
2350 | return ret; |
2351 | } | |
2352 | ||
58bf2a4c NTND |
2353 | static int index_stream_convert_blob(struct index_state *istate, |
2354 | struct object_id *oid, | |
2355 | int fd, | |
2356 | const char *path, | |
2357 | unsigned flags) | |
9035d75a | 2358 | { |
63e05f90 | 2359 | int ret = 0; |
9035d75a SP |
2360 | const int write_object = flags & HASH_WRITE_OBJECT; |
2361 | struct strbuf sbuf = STRBUF_INIT; | |
2362 | ||
2363 | assert(path); | |
58bf2a4c | 2364 | assert(would_convert_to_git_filter_fd(istate, path)); |
9035d75a | 2365 | |
58bf2a4c | 2366 | convert_to_git_filter_fd(istate, path, fd, &sbuf, |
8462ff43 | 2367 | get_conv_flags(flags)); |
9035d75a SP |
2368 | |
2369 | if (write_object) | |
c80d226a | 2370 | ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB, |
a09c985e | 2371 | oid); |
9035d75a | 2372 | else |
44439c1c ÆAB |
2373 | hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB, |
2374 | oid); | |
9035d75a SP |
2375 | strbuf_release(&sbuf); |
2376 | return ret; | |
2377 | } | |
2378 | ||
58bf2a4c NTND |
2379 | static int index_pipe(struct index_state *istate, struct object_id *oid, |
2380 | int fd, enum object_type type, | |
7b41e1e1 JH |
2381 | const char *path, unsigned flags) |
2382 | { | |
2383 | struct strbuf sbuf = STRBUF_INIT; | |
2384 | int ret; | |
2385 | ||
2386 | if (strbuf_read(&sbuf, fd, 4096) >= 0) | |
58bf2a4c | 2387 | ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags); |
7b41e1e1 JH |
2388 | else |
2389 | ret = -1; | |
2390 | strbuf_release(&sbuf); | |
2391 | return ret; | |
2392 | } | |
2393 | ||
ea68b0ce DP |
2394 | #define SMALL_FILE_SIZE (32*1024) |
2395 | ||
58bf2a4c NTND |
2396 | static int index_core(struct index_state *istate, |
2397 | struct object_id *oid, int fd, size_t size, | |
7b41e1e1 JH |
2398 | enum object_type type, const char *path, |
2399 | unsigned flags) | |
43df4f86 DP |
2400 | { |
2401 | int ret; | |
43df4f86 | 2402 | |
7b41e1e1 | 2403 | if (!size) { |
58bf2a4c | 2404 | ret = index_mem(istate, oid, "", size, type, path, flags); |
ea68b0ce DP |
2405 | } else if (size <= SMALL_FILE_SIZE) { |
2406 | char *buf = xmalloc(size); | |
90dca671 JK |
2407 | ssize_t read_result = read_in_full(fd, buf, size); |
2408 | if (read_result < 0) | |
259328b7 | 2409 | ret = error_errno(_("read error while indexing %s"), |
90dca671 JK |
2410 | path ? path : "<unknown>"); |
2411 | else if (read_result != size) | |
259328b7 | 2412 | ret = error(_("short read while indexing %s"), |
90dca671 | 2413 | path ? path : "<unknown>"); |
ea68b0ce | 2414 | else |
58bf2a4c | 2415 | ret = index_mem(istate, oid, buf, size, type, path, flags); |
ea68b0ce | 2416 | free(buf); |
08bda208 | 2417 | } else { |
43df4f86 | 2418 | void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
58bf2a4c | 2419 | ret = index_mem(istate, oid, buf, size, type, path, flags); |
aac17941 | 2420 | munmap(buf, size); |
08bda208 | 2421 | } |
7b41e1e1 JH |
2422 | return ret; |
2423 | } | |
2424 | ||
4dd1fbc7 | 2425 | /* |
568508e7 JH |
2426 | * This creates one packfile per large blob unless bulk-checkin |
2427 | * machinery is "plugged". | |
4dd1fbc7 JH |
2428 | * |
2429 | * This also bypasses the usual "convert-to-git" dance, and that is on | |
2430 | * purpose. We could write a streaming version of the converting | |
2431 | * functions and insert that before feeding the data to fast-import | |
4f22b101 JK |
2432 | * (or equivalent in-core API described above). However, that is |
2433 | * somewhat complicated, as we do not know the size of the filter | |
2434 | * result, which we need to know beforehand when writing a git object. | |
2435 | * Since the primary motivation for trying to stream from the working | |
2436 | * tree file and to avoid mmaping it in core is to deal with large | |
2437 | * binary blobs, they generally do not want to get any conversion, and | |
2438 | * callers should avoid this code path when filters are requested. | |
4dd1fbc7 | 2439 | */ |
7d5e1dc3 | 2440 | static int index_stream(struct object_id *oid, int fd, size_t size, |
4dd1fbc7 JH |
2441 | enum object_type type, const char *path, |
2442 | unsigned flags) | |
2443 | { | |
68ee6dfc | 2444 | return index_bulk_checkin(oid, fd, size, type, path, flags); |
4dd1fbc7 JH |
2445 | } |
2446 | ||
58bf2a4c NTND |
2447 | int index_fd(struct index_state *istate, struct object_id *oid, |
2448 | int fd, struct stat *st, | |
7b41e1e1 JH |
2449 | enum object_type type, const char *path, unsigned flags) |
2450 | { | |
2451 | int ret; | |
7b41e1e1 | 2452 | |
9079ab7c SP |
2453 | /* |
2454 | * Call xsize_t() only when needed to avoid potentially unnecessary | |
2455 | * die() for large files. | |
2456 | */ | |
58bf2a4c NTND |
2457 | if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path)) |
2458 | ret = index_stream_convert_blob(istate, oid, fd, path, flags); | |
9035d75a | 2459 | else if (!S_ISREG(st->st_mode)) |
58bf2a4c | 2460 | ret = index_pipe(istate, oid, fd, type, path, flags); |
9079ab7c | 2461 | else if (st->st_size <= big_file_threshold || type != OBJ_BLOB || |
58bf2a4c NTND |
2462 | (path && would_convert_to_git(istate, path))) |
2463 | ret = index_core(istate, oid, fd, xsize_t(st->st_size), | |
2464 | type, path, flags); | |
4dd1fbc7 | 2465 | else |
7d5e1dc3 | 2466 | ret = index_stream(oid, fd, xsize_t(st->st_size), type, path, |
9079ab7c | 2467 | flags); |
43df4f86 | 2468 | close(fd); |
aac17941 | 2469 | return ret; |
74400e71 | 2470 | } |
ec1fcc16 | 2471 | |
58bf2a4c NTND |
2472 | int index_path(struct index_state *istate, struct object_id *oid, |
2473 | const char *path, struct stat *st, unsigned flags) | |
ec1fcc16 JH |
2474 | { |
2475 | int fd; | |
b760d3aa | 2476 | struct strbuf sb = STRBUF_INIT; |
ea8e0297 | 2477 | int rc = 0; |
ec1fcc16 JH |
2478 | |
2479 | switch (st->st_mode & S_IFMT) { | |
2480 | case S_IFREG: | |
2481 | fd = open(path, O_RDONLY); | |
2482 | if (fd < 0) | |
7616c6ca | 2483 | return error_errno("open(\"%s\")", path); |
58bf2a4c | 2484 | if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0) |
259328b7 | 2485 | return error(_("%s: failed to insert into database"), |
ec1fcc16 JH |
2486 | path); |
2487 | break; | |
2488 | case S_IFLNK: | |
7616c6ca NTND |
2489 | if (strbuf_readlink(&sb, path, st->st_size)) |
2490 | return error_errno("readlink(\"%s\")", path); | |
c4ce46fc | 2491 | if (!(flags & HASH_WRITE_OBJECT)) |
2dcde20e | 2492 | hash_object_file(the_hash_algo, sb.buf, sb.len, |
44439c1c | 2493 | OBJ_BLOB, oid); |
c80d226a | 2494 | else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid)) |
259328b7 | 2495 | rc = error(_("%s: failed to insert into database"), path); |
b760d3aa | 2496 | strbuf_release(&sb); |
ec1fcc16 | 2497 | break; |
f35a6d3b | 2498 | case S_IFDIR: |
a98e6101 | 2499 | return resolve_gitlink_ref(path, "HEAD", oid); |
ec1fcc16 | 2500 | default: |
259328b7 | 2501 | return error(_("%s: unsupported file type"), path); |
ec1fcc16 | 2502 | } |
ea8e0297 | 2503 | return rc; |
ec1fcc16 | 2504 | } |
a69e5429 JH |
2505 | |
2506 | int read_pack_header(int fd, struct pack_header *header) | |
2507 | { | |
f48ecd38 | 2508 | if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header)) |
c697ad14 HO |
2509 | /* "eof before pack header was fully read" */ |
2510 | return PH_ERROR_EOF; | |
2511 | ||
a69e5429 JH |
2512 | if (header->hdr_signature != htonl(PACK_SIGNATURE)) |
2513 | /* "protocol error (pack signature mismatch detected)" */ | |
2514 | return PH_ERROR_PACK_SIGNATURE; | |
2515 | if (!pack_version_ok(header->hdr_version)) | |
2516 | /* "protocol error (pack version unsupported)" */ | |
2517 | return PH_ERROR_PROTOCOL; | |
2518 | return 0; | |
2519 | } | |
40d52ff7 | 2520 | |
e816caa0 | 2521 | void assert_oid_type(const struct object_id *oid, enum object_type expect) |
40d52ff7 | 2522 | { |
0df8e965 | 2523 | enum object_type type = oid_object_info(the_repository, oid, NULL); |
40d52ff7 | 2524 | if (type < 0) |
259328b7 | 2525 | die(_("%s is not a valid object"), oid_to_hex(oid)); |
40d52ff7 | 2526 | if (type != expect) |
259328b7 | 2527 | die(_("%s is not a valid '%s' object"), oid_to_hex(oid), |
debca9d2 | 2528 | type_name(expect)); |
40d52ff7 | 2529 | } |
27e1e22d | 2530 | |
70c49050 | 2531 | int for_each_file_in_obj_subdir(unsigned int subdir_nr, |
cc817ca3 RS |
2532 | struct strbuf *path, |
2533 | each_loose_object_fn obj_cb, | |
2534 | each_loose_cruft_fn cruft_cb, | |
2535 | each_loose_subdir_fn subdir_cb, | |
2536 | void *data) | |
27e1e22d | 2537 | { |
0375f472 RS |
2538 | size_t origlen, baselen; |
2539 | DIR *dir; | |
27e1e22d JK |
2540 | struct dirent *de; |
2541 | int r = 0; | |
62a24c89 | 2542 | struct object_id oid; |
27e1e22d | 2543 | |
70c49050 RS |
2544 | if (subdir_nr > 0xff) |
2545 | BUG("invalid loose object subdirectory: %x", subdir_nr); | |
2546 | ||
0375f472 RS |
2547 | origlen = path->len; |
2548 | strbuf_complete(path, '/'); | |
2549 | strbuf_addf(path, "%02x", subdir_nr); | |
0375f472 RS |
2550 | |
2551 | dir = opendir(path->buf); | |
27e1e22d | 2552 | if (!dir) { |
0375f472 | 2553 | if (errno != ENOENT) |
259328b7 | 2554 | r = error_errno(_("unable to open %s"), path->buf); |
0375f472 RS |
2555 | strbuf_setlen(path, origlen); |
2556 | return r; | |
27e1e22d JK |
2557 | } |
2558 | ||
62a24c89 | 2559 | oid.hash[0] = subdir_nr; |
163ee5e6 DS |
2560 | strbuf_addch(path, '/'); |
2561 | baselen = path->len; | |
62a24c89 | 2562 | |
b548f0f1 | 2563 | while ((de = readdir_skip_dot_and_dotdot(dir))) { |
163ee5e6 | 2564 | size_t namelen; |
27e1e22d | 2565 | |
163ee5e6 | 2566 | namelen = strlen(de->d_name); |
27e1e22d | 2567 | strbuf_setlen(path, baselen); |
163ee5e6 | 2568 | strbuf_add(path, de->d_name, namelen); |
94b5e093 | 2569 | if (namelen == the_hash_algo->hexsz - 2 && |
62a24c89 | 2570 | !hex_to_bytes(oid.hash + 1, de->d_name, |
94b5e093 | 2571 | the_hash_algo->rawsz - 1)) { |
5a6dce70 | 2572 | oid_set_algo(&oid, the_hash_algo); |
62a24c89 RS |
2573 | if (obj_cb) { |
2574 | r = obj_cb(&oid, path->buf, data); | |
2575 | if (r) | |
2576 | break; | |
27e1e22d | 2577 | } |
62a24c89 | 2578 | continue; |
27e1e22d JK |
2579 | } |
2580 | ||
2581 | if (cruft_cb) { | |
2582 | r = cruft_cb(de->d_name, path->buf, data); | |
2583 | if (r) | |
2584 | break; | |
2585 | } | |
2586 | } | |
094c7e63 | 2587 | closedir(dir); |
27e1e22d | 2588 | |
163ee5e6 | 2589 | strbuf_setlen(path, baselen - 1); |
27e1e22d JK |
2590 | if (!r && subdir_cb) |
2591 | r = subdir_cb(subdir_nr, path->buf, data); | |
2592 | ||
0375f472 RS |
2593 | strbuf_setlen(path, origlen); |
2594 | ||
27e1e22d JK |
2595 | return r; |
2596 | } | |
2597 | ||
e6f875e0 | 2598 | int for_each_loose_file_in_objdir_buf(struct strbuf *path, |
27e1e22d JK |
2599 | each_loose_object_fn obj_cb, |
2600 | each_loose_cruft_fn cruft_cb, | |
2601 | each_loose_subdir_fn subdir_cb, | |
2602 | void *data) | |
2603 | { | |
27e1e22d JK |
2604 | int r = 0; |
2605 | int i; | |
2606 | ||
27e1e22d | 2607 | for (i = 0; i < 256; i++) { |
e6f875e0 | 2608 | r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb, |
27e1e22d | 2609 | subdir_cb, data); |
27e1e22d JK |
2610 | if (r) |
2611 | break; | |
2612 | } | |
2613 | ||
e6f875e0 JK |
2614 | return r; |
2615 | } | |
2616 | ||
2617 | int for_each_loose_file_in_objdir(const char *path, | |
2618 | each_loose_object_fn obj_cb, | |
2619 | each_loose_cruft_fn cruft_cb, | |
2620 | each_loose_subdir_fn subdir_cb, | |
2621 | void *data) | |
2622 | { | |
2623 | struct strbuf buf = STRBUF_INIT; | |
2624 | int r; | |
2625 | ||
2626 | strbuf_addstr(&buf, path); | |
2627 | r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb, | |
2628 | subdir_cb, data); | |
27e1e22d | 2629 | strbuf_release(&buf); |
e6f875e0 | 2630 | |
27e1e22d JK |
2631 | return r; |
2632 | } | |
660c889e | 2633 | |
a7ff6f5a JK |
2634 | int for_each_loose_object(each_loose_object_fn cb, void *data, |
2635 | enum for_each_object_flags flags) | |
660c889e | 2636 | { |
f0eaf638 | 2637 | struct object_directory *odb; |
b0a42642 | 2638 | |
f0eaf638 JK |
2639 | prepare_alt_odb(the_repository); |
2640 | for (odb = the_repository->objects->odb; odb; odb = odb->next) { | |
2641 | int r = for_each_loose_file_in_objdir(odb->path, cb, NULL, | |
2642 | NULL, data); | |
2643 | if (r) | |
2644 | return r; | |
660c889e | 2645 | |
f0eaf638 JK |
2646 | if (flags & FOR_EACH_OBJECT_LOCAL_ONLY) |
2647 | break; | |
2648 | } | |
1385bb7b | 2649 | |
f0eaf638 | 2650 | return 0; |
660c889e JK |
2651 | } |
2652 | ||
3a2e0824 JK |
2653 | static int append_loose_object(const struct object_id *oid, const char *path, |
2654 | void *data) | |
660c889e | 2655 | { |
92d8ed8a | 2656 | oidtree_insert(data, oid); |
3a2e0824 JK |
2657 | return 0; |
2658 | } | |
660c889e | 2659 | |
92d8ed8a | 2660 | struct oidtree *odb_loose_cache(struct object_directory *odb, |
0000d654 RS |
2661 | const struct object_id *oid) |
2662 | { | |
2663 | int subdir_nr = oid->hash[0]; | |
3a2e0824 | 2664 | struct strbuf buf = STRBUF_INIT; |
33f379ee EW |
2665 | size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]); |
2666 | size_t word_index = subdir_nr / word_bits; | |
26de1fc0 | 2667 | size_t mask = (size_t)1u << (subdir_nr % word_bits); |
33f379ee | 2668 | uint32_t *bitmap; |
660c889e | 2669 | |
3a2e0824 | 2670 | if (subdir_nr < 0 || |
33f379ee | 2671 | subdir_nr >= bitsizeof(odb->loose_objects_subdir_seen)) |
3a2e0824 | 2672 | BUG("subdir_nr out of range"); |
1385bb7b | 2673 | |
33f379ee EW |
2674 | bitmap = &odb->loose_objects_subdir_seen[word_index]; |
2675 | if (*bitmap & mask) | |
92d8ed8a EW |
2676 | return odb->loose_objects_cache; |
2677 | if (!odb->loose_objects_cache) { | |
2678 | ALLOC_ARRAY(odb->loose_objects_cache, 1); | |
2679 | oidtree_init(odb->loose_objects_cache); | |
2680 | } | |
3a2e0824 JK |
2681 | strbuf_addstr(&buf, odb->path); |
2682 | for_each_file_in_obj_subdir(subdir_nr, &buf, | |
2683 | append_loose_object, | |
2684 | NULL, NULL, | |
92d8ed8a | 2685 | odb->loose_objects_cache); |
33f379ee | 2686 | *bitmap |= mask; |
7317aa71 | 2687 | strbuf_release(&buf); |
92d8ed8a | 2688 | return odb->loose_objects_cache; |
660c889e JK |
2689 | } |
2690 | ||
d4e19e51 RS |
2691 | void odb_clear_loose_cache(struct object_directory *odb) |
2692 | { | |
92d8ed8a EW |
2693 | oidtree_clear(odb->loose_objects_cache); |
2694 | FREE_AND_NULL(odb->loose_objects_cache); | |
d4e19e51 RS |
2695 | memset(&odb->loose_objects_subdir_seen, 0, |
2696 | sizeof(odb->loose_objects_subdir_seen)); | |
2697 | } | |
2698 | ||
00a7760e JK |
2699 | static int check_stream_oid(git_zstream *stream, |
2700 | const char *hdr, | |
2701 | unsigned long size, | |
2702 | const char *path, | |
2703 | const struct object_id *expected_oid) | |
f6371f92 | 2704 | { |
18e2588e | 2705 | git_hash_ctx c; |
00a7760e | 2706 | struct object_id real_oid; |
f6371f92 JK |
2707 | unsigned char buf[4096]; |
2708 | unsigned long total_read; | |
2709 | int status = Z_OK; | |
2710 | ||
18e2588e | 2711 | the_hash_algo->init_fn(&c); |
2712 | the_hash_algo->update_fn(&c, hdr, stream->total_out); | |
f6371f92 JK |
2713 | |
2714 | /* | |
2715 | * We already read some bytes into hdr, but the ones up to the NUL | |
2716 | * do not count against the object's content size. | |
2717 | */ | |
2718 | total_read = stream->total_out - strlen(hdr) - 1; | |
2719 | ||
2720 | /* | |
2721 | * This size comparison must be "<=" to read the final zlib packets; | |
00a7760e | 2722 | * see the comment in unpack_loose_rest for details. |
f6371f92 JK |
2723 | */ |
2724 | while (total_read <= size && | |
18ad13e5 JH |
2725 | (status == Z_OK || |
2726 | (status == Z_BUF_ERROR && !stream->avail_out))) { | |
f6371f92 JK |
2727 | stream->next_out = buf; |
2728 | stream->avail_out = sizeof(buf); | |
2729 | if (size - total_read < stream->avail_out) | |
2730 | stream->avail_out = size - total_read; | |
2731 | status = git_inflate(stream, Z_FINISH); | |
18e2588e | 2732 | the_hash_algo->update_fn(&c, buf, stream->next_out - buf); |
f6371f92 JK |
2733 | total_read += stream->next_out - buf; |
2734 | } | |
2735 | git_inflate_end(stream); | |
2736 | ||
2737 | if (status != Z_STREAM_END) { | |
00a7760e | 2738 | error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid)); |
f6371f92 JK |
2739 | return -1; |
2740 | } | |
cce044df | 2741 | if (stream->avail_in) { |
259328b7 | 2742 | error(_("garbage at end of loose object '%s'"), |
00a7760e | 2743 | oid_to_hex(expected_oid)); |
cce044df JK |
2744 | return -1; |
2745 | } | |
f6371f92 | 2746 | |
5951bf46 | 2747 | the_hash_algo->final_oid_fn(&real_oid, &c); |
00a7760e | 2748 | if (!oideq(expected_oid, &real_oid)) { |
01f8d594 | 2749 | error(_("hash mismatch for %s (expected %s)"), path, |
00a7760e | 2750 | oid_to_hex(expected_oid)); |
f6371f92 JK |
2751 | return -1; |
2752 | } | |
2753 | ||
2754 | return 0; | |
2755 | } | |
2756 | ||
2757 | int read_loose_object(const char *path, | |
d61d87bd | 2758 | const struct object_id *expected_oid, |
96e41f58 | 2759 | struct object_id *real_oid, |
31deb28f ÆAB |
2760 | void **contents, |
2761 | struct object_info *oi) | |
f6371f92 JK |
2762 | { |
2763 | int ret = -1; | |
ae285ac4 | 2764 | int fd; |
f6371f92 JK |
2765 | void *map = NULL; |
2766 | unsigned long mapsize; | |
2767 | git_zstream stream; | |
1af64f73 | 2768 | char hdr[MAX_HEADER_LEN]; |
31deb28f | 2769 | unsigned long *size = oi->sizep; |
f6371f92 | 2770 | |
ae285ac4 JT |
2771 | fd = git_open(path); |
2772 | if (fd >= 0) | |
2773 | map = map_fd(fd, path, &mapsize); | |
f6371f92 | 2774 | if (!map) { |
259328b7 | 2775 | error_errno(_("unable to mmap %s"), path); |
f6371f92 JK |
2776 | goto out; |
2777 | } | |
2778 | ||
8a50571a DS |
2779 | if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr), |
2780 | NULL) != ULHR_OK) { | |
259328b7 | 2781 | error(_("unable to unpack header of %s"), path); |
f6371f92 JK |
2782 | goto out; |
2783 | } | |
2784 | ||
31deb28f | 2785 | if (parse_loose_header(hdr, oi) < 0) { |
259328b7 | 2786 | error(_("unable to parse header of %s"), path); |
f6371f92 JK |
2787 | git_inflate_end(&stream); |
2788 | goto out; | |
2789 | } | |
2790 | ||
31deb28f | 2791 | if (*oi->typep == OBJ_BLOB && *size > big_file_threshold) { |
00a7760e | 2792 | if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0) |
f6371f92 JK |
2793 | goto out; |
2794 | } else { | |
00a7760e | 2795 | *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid); |
f6371f92 | 2796 | if (!*contents) { |
259328b7 | 2797 | error(_("unable to unpack contents of %s"), path); |
f6371f92 JK |
2798 | git_inflate_end(&stream); |
2799 | goto out; | |
2800 | } | |
44439c1c | 2801 | hash_object_file_literally(the_repository->hash_algo, |
16235e3b | 2802 | *contents, *size, |
44439c1c | 2803 | oi->type_name->buf, real_oid); |
0f156dbb | 2804 | if (!oideq(expected_oid, real_oid)) |
f6371f92 | 2805 | goto out; |
f6371f92 JK |
2806 | } |
2807 | ||
2808 | ret = 0; /* everything checks out */ | |
2809 | ||
2810 | out: | |
2811 | if (map) | |
2812 | munmap(map, mapsize); | |
f6371f92 JK |
2813 | return ret; |
2814 | } |