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