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