]>
Commit | Line | Data |
---|---|---|
0fcfd160 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | * | |
e5afd444 | 6 | * This handles basic git object files - packing, unpacking, |
0fcfd160 LT |
7 | * creation etc. |
8 | */ | |
e7da9385 PS |
9 | |
10 | #define USE_THE_REPOSITORY_VARIABLE | |
41f43b82 | 11 | #define DISABLE_SIGN_COMPARE_WARNINGS |
e7da9385 | 12 | |
5e3f94df | 13 | #include "git-compat-util.h" |
d9f517d0 | 14 | #include "bulk-checkin.h" |
73359a9b | 15 | #include "convert.h" |
68cd492a | 16 | #include "dir.h" |
32a8f510 | 17 | #include "environment.h" |
d9f517d0 | 18 | #include "fsck.h" |
f394e093 | 19 | #include "gettext.h" |
41771fa4 | 20 | #include "hex.h" |
d9f517d0 PS |
21 | #include "loose.h" |
22 | #include "object-file-convert.h" | |
87bed179 | 23 | #include "object-file.h" |
90c62155 | 24 | #include "object-store.h" |
e3d2f20e | 25 | #include "oidtree.h" |
d9f517d0 PS |
26 | #include "pack.h" |
27 | #include "packfile.h" | |
c339932b | 28 | #include "path.h" |
e38da487 | 29 | #include "setup.h" |
d9f517d0 | 30 | #include "streaming.h" |
e05db0fd | 31 | |
1af64f73 | 32 | /* The maximum size for an object header. */ |
33 | #define MAX_HEADER_LEN 32 | |
34 | ||
8462ff43 | 35 | static int get_conv_flags(unsigned flags) |
9472935d | 36 | { |
70c0f9db | 37 | if (flags & INDEX_RENORMALIZE) |
8462ff43 | 38 | return CONV_EOL_RENORMALIZE; |
70c0f9db | 39 | else if (flags & INDEX_WRITE_OBJECT) |
107642fe | 40 | return global_conv_flags_eol | CONV_WRITE_OBJECT; |
9472935d | 41 | else |
8462ff43 | 42 | return 0; |
9472935d TB |
43 | } |
44 | ||
514c5fdd | 45 | static void fill_loose_path(struct strbuf *buf, const struct object_id *oid) |
ace1534d JH |
46 | { |
47 | int i; | |
94b5e093 | 48 | for (i = 0; i < the_hash_algo->rawsz; i++) { |
ace1534d | 49 | static char hex[] = "0123456789abcdef"; |
514c5fdd | 50 | unsigned int val = oid->hash[i]; |
f7b7774f JK |
51 | strbuf_addch(buf, hex[val >> 4]); |
52 | strbuf_addch(buf, hex[val & 0xf]); | |
afbba2f0 | 53 | if (!i) |
f7b7774f | 54 | strbuf_addch(buf, '/'); |
ace1534d JH |
55 | } |
56 | } | |
57 | ||
d9f517d0 PS |
58 | const char *odb_loose_path(struct object_directory *odb, |
59 | struct strbuf *buf, | |
60 | const struct object_id *oid) | |
0fcfd160 | 61 | { |
b69fb867 | 62 | strbuf_reset(buf); |
f0eaf638 | 63 | strbuf_addstr(buf, odb->path); |
34498471 | 64 | strbuf_addch(buf, '/'); |
514c5fdd | 65 | fill_loose_path(buf, oid); |
f3f043a1 | 66 | return buf->buf; |
0fcfd160 LT |
67 | } |
68 | ||
3096b2ec | 69 | /* Returns 1 if we have successfully freshened the file, 0 otherwise. */ |
33d4221c | 70 | static int freshen_file(const char *fn) |
ace1534d | 71 | { |
312cd761 | 72 | return !utime(fn, NULL); |
0f4dc14a | 73 | } |
ace1534d | 74 | |
3096b2ec JK |
75 | /* |
76 | * All of the check_and_freshen functions return 1 if the file exists and was | |
77 | * freshened (if freshening was requested), 0 otherwise. If they return | |
78 | * 0, you should not assume that it is safe to skip a write of the object (it | |
79 | * either does not exist on disk, or has a stale mtime and may be subject to | |
80 | * pruning). | |
81 | */ | |
6a5e6f5e | 82 | int check_and_freshen_file(const char *fn, int freshen) |
33d4221c JK |
83 | { |
84 | if (access(fn, F_OK)) | |
85 | return 0; | |
3096b2ec | 86 | if (freshen && !freshen_file(fn)) |
33d4221c JK |
87 | return 0; |
88 | return 1; | |
89 | } | |
90 | ||
f0eaf638 JK |
91 | static int check_and_freshen_odb(struct object_directory *odb, |
92 | const struct object_id *oid, | |
93 | int freshen) | |
33d4221c | 94 | { |
f0eaf638 | 95 | static struct strbuf path = STRBUF_INIT; |
514c5fdd | 96 | odb_loose_path(odb, &path, oid); |
f0eaf638 JK |
97 | return check_and_freshen_file(path.buf, freshen); |
98 | } | |
ea657730 | 99 | |
f0eaf638 JK |
100 | static int check_and_freshen_local(const struct object_id *oid, int freshen) |
101 | { | |
102 | return check_and_freshen_odb(the_repository->objects->odb, oid, freshen); | |
33d4221c JK |
103 | } |
104 | ||
6862ebbf | 105 | static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen) |
0f4dc14a | 106 | { |
263db403 | 107 | struct object_directory *odb; |
f3f043a1 | 108 | |
0b209034 | 109 | prepare_alt_odb(the_repository); |
f0eaf638 JK |
110 | for (odb = the_repository->objects->odb->next; odb; odb = odb->next) { |
111 | if (check_and_freshen_odb(odb, oid, freshen)) | |
c529d75a | 112 | return 1; |
ace1534d | 113 | } |
c529d75a | 114 | return 0; |
ace1534d JH |
115 | } |
116 | ||
6862ebbf | 117 | static int check_and_freshen(const struct object_id *oid, int freshen) |
33d4221c | 118 | { |
6862ebbf | 119 | return check_and_freshen_local(oid, freshen) || |
120 | check_and_freshen_nonlocal(oid, freshen); | |
33d4221c JK |
121 | } |
122 | ||
6862ebbf | 123 | int has_loose_object_nonlocal(const struct object_id *oid) |
33d4221c | 124 | { |
6862ebbf | 125 | return check_and_freshen_nonlocal(oid, 0); |
33d4221c JK |
126 | } |
127 | ||
b7573536 | 128 | int has_loose_object(const struct object_id *oid) |
0f4dc14a | 129 | { |
6862ebbf | 130 | return check_and_freshen(oid, 0); |
0f4dc14a BC |
131 | } |
132 | ||
b04cdea4 ÆAB |
133 | int format_object_header(char *str, size_t size, enum object_type type, |
134 | size_t objsize) | |
135 | { | |
136 | const char *name = type_name(type); | |
137 | ||
138 | if (!name) | |
139 | BUG("could not get a type name for 'enum object_type' value %d", type); | |
140 | ||
141f8c8c | 141 | return xsnprintf(str, size, "%s %"PRIuMAX, name, (uintmax_t)objsize) + 1; |
b04cdea4 ÆAB |
142 | } |
143 | ||
b98d1885 | 144 | int check_object_signature(struct repository *r, const struct object_id *oid, |
44439c1c ÆAB |
145 | void *buf, unsigned long size, |
146 | enum object_type type) | |
88d0db55 | 147 | { |
8d691757 EB |
148 | const struct git_hash_algo *algo = |
149 | oid->algo ? &hash_algos[oid->algo] : r->hash_algo; | |
0f156dbb ÆAB |
150 | struct object_id real_oid; |
151 | ||
8d691757 | 152 | hash_object_file(algo, buf, size, type, &real_oid); |
0f156dbb ÆAB |
153 | |
154 | return !oideq(oid, &real_oid) ? -1 : 0; | |
155 | } | |
156 | ||
157 | int stream_object_signature(struct repository *r, const struct object_id *oid) | |
158 | { | |
159 | struct object_id real_oid; | |
160 | unsigned long size; | |
090ea126 NTND |
161 | enum object_type obj_type; |
162 | struct git_istream *st; | |
7346e340 | 163 | struct git_hash_ctx c; |
1af64f73 | 164 | char hdr[MAX_HEADER_LEN]; |
090ea126 | 165 | int hdrlen; |
88d0db55 | 166 | |
b98d1885 | 167 | st = open_istream(r, oid, &obj_type, &size, NULL); |
090ea126 NTND |
168 | if (!st) |
169 | return -1; | |
88d0db55 | 170 | |
090ea126 | 171 | /* Generate the header */ |
b04cdea4 | 172 | hdrlen = format_object_header(hdr, sizeof(hdr), obj_type, size); |
88d0db55 | 173 | |
090ea126 | 174 | /* Sha1.. */ |
b98d1885 | 175 | r->hash_algo->init_fn(&c); |
0578f1e6 | 176 | git_hash_update(&c, hdr, hdrlen); |
090ea126 NTND |
177 | for (;;) { |
178 | char buf[1024 * 16]; | |
179 | ssize_t readlen = read_istream(st, buf, sizeof(buf)); | |
1f688557 | 180 | |
f54fac53 JK |
181 | if (readlen < 0) { |
182 | close_istream(st); | |
183 | return -1; | |
184 | } | |
090ea126 NTND |
185 | if (!readlen) |
186 | break; | |
0578f1e6 | 187 | git_hash_update(&c, buf, readlen); |
fa5fc15d | 188 | } |
0578f1e6 | 189 | git_hash_final_oid(&real_oid, &c); |
090ea126 | 190 | close_istream(st); |
0f156dbb | 191 | return !oideq(oid, &real_oid) ? -1 : 0; |
fa5fc15d SP |
192 | } |
193 | ||
3cf8b462 | 194 | /* |
514c5fdd | 195 | * Find "oid" as a loose object in the local repository or in an alternate. |
771e7d57 JK |
196 | * Returns 0 on success, negative on failure. |
197 | * | |
198 | * The "path" out-parameter will give the path of the object we found (if any). | |
199 | * Note that it may point to static storage and is only valid until another | |
514c5fdd | 200 | * call to stat_loose_object(). |
3cf8b462 | 201 | */ |
514c5fdd JK |
202 | static int stat_loose_object(struct repository *r, const struct object_id *oid, |
203 | struct stat *st, const char **path) | |
1f688557 | 204 | { |
263db403 | 205 | struct object_directory *odb; |
ea657730 CC |
206 | static struct strbuf buf = STRBUF_INIT; |
207 | ||
d2607fa0 | 208 | prepare_alt_odb(r); |
f0eaf638 | 209 | for (odb = r->objects->odb; odb; odb = odb->next) { |
514c5fdd | 210 | *path = odb_loose_path(odb, &buf, oid); |
771e7d57 | 211 | if (!lstat(*path, st)) |
052fe5ea | 212 | return 0; |
c7934306 SP |
213 | } |
214 | ||
3cf8b462 SP |
215 | return -1; |
216 | } | |
217 | ||
771e7d57 | 218 | /* |
514c5fdd | 219 | * Like stat_loose_object(), but actually open the object and return the |
771e7d57 JK |
220 | * descriptor. See the caveats on the "path" parameter above. |
221 | */ | |
514c5fdd JK |
222 | static int open_loose_object(struct repository *r, |
223 | const struct object_id *oid, const char **path) | |
60bb8b14 | 224 | { |
44d1c19e | 225 | int fd; |
263db403 | 226 | struct object_directory *odb; |
f0eaf638 | 227 | int most_interesting_errno = ENOENT; |
ea657730 CC |
228 | static struct strbuf buf = STRBUF_INIT; |
229 | ||
ec7283e5 | 230 | prepare_alt_odb(r); |
f0eaf638 | 231 | for (odb = r->objects->odb; odb; odb = odb->next) { |
514c5fdd | 232 | *path = odb_loose_path(odb, &buf, oid); |
771e7d57 | 233 | fd = git_open(*path); |
44d1c19e LT |
234 | if (fd >= 0) |
235 | return fd; | |
f0eaf638 | 236 | |
d6c8a05b JK |
237 | if (most_interesting_errno == ENOENT) |
238 | most_interesting_errno = errno; | |
03e79c88 | 239 | } |
d6c8a05b | 240 | errno = most_interesting_errno; |
44d1c19e | 241 | return -1; |
1f688557 JH |
242 | } |
243 | ||
61c7711c | 244 | static int quick_has_loose(struct repository *r, |
d7a24573 | 245 | const struct object_id *oid) |
61c7711c | 246 | { |
61c7711c JK |
247 | struct object_directory *odb; |
248 | ||
61c7711c JK |
249 | prepare_alt_odb(r); |
250 | for (odb = r->objects->odb; odb; odb = odb->next) { | |
92d8ed8a | 251 | if (oidtree_contains(odb_loose_cache(odb, oid), oid)) |
61c7711c JK |
252 | return 1; |
253 | } | |
254 | return 0; | |
255 | } | |
256 | ||
f6371f92 | 257 | /* |
ae285ac4 JT |
258 | * Map and close the given loose object fd. The path argument is used for |
259 | * error reporting. | |
f6371f92 | 260 | */ |
ae285ac4 | 261 | static void *map_fd(int fd, const char *path, unsigned long *size) |
27d69a46 | 262 | { |
ae285ac4 JT |
263 | void *map = NULL; |
264 | struct stat st; | |
1f688557 | 265 | |
ae285ac4 JT |
266 | if (!fstat(fd, &st)) { |
267 | *size = xsize_t(st.st_size); | |
268 | if (!*size) { | |
269 | /* mmap() is forbidden on empty files */ | |
270 | error(_("object file %s is empty"), path); | |
271 | close(fd); | |
272 | return NULL; | |
144bde78 | 273 | } |
ae285ac4 | 274 | map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); |
74e34e1f | 275 | } |
ae285ac4 | 276 | close(fd); |
0fcfd160 | 277 | return map; |
74e34e1f NP |
278 | } |
279 | ||
514c5fdd JK |
280 | void *map_loose_object(struct repository *r, |
281 | const struct object_id *oid, | |
282 | unsigned long *size) | |
068f85e3 | 283 | { |
ae285ac4 JT |
284 | const char *p; |
285 | int fd = open_loose_object(r, oid, &p); | |
286 | ||
287 | if (fd < 0) | |
288 | return NULL; | |
289 | return map_fd(fd, p, size); | |
068f85e3 | 290 | } |
291 | ||
3b6a8db3 ÆAB |
292 | enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, |
293 | unsigned char *map, | |
294 | unsigned long mapsize, | |
295 | void *buffer, | |
ae24b032 | 296 | unsigned long bufsiz) |
47fe3f6e | 297 | { |
01cab976 | 298 | int status; |
31877c9a | 299 | |
c4483576 LT |
300 | /* Get the data stream */ |
301 | memset(stream, 0, sizeof(*stream)); | |
302 | stream->next_in = map; | |
303 | stream->avail_in = mapsize; | |
304 | stream->next_out = buffer; | |
93821bd9 | 305 | stream->avail_out = bufsiz; |
47fe3f6e | 306 | |
39c68542 | 307 | git_inflate_init(stream); |
31877c9a | 308 | obj_read_unlock(); |
01cab976 | 309 | status = git_inflate(stream, 0); |
31877c9a | 310 | obj_read_lock(); |
67a6b1ae | 311 | if (status != Z_OK && status != Z_STREAM_END) |
3b6a8db3 | 312 | return ULHR_BAD; |
d131b7af | 313 | |
46f03448 KN |
314 | /* |
315 | * Check if entire header is unpacked in the first iteration. | |
d131b7af | 316 | */ |
46f03448 | 317 | if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) |
3b6a8db3 | 318 | return ULHR_OK; |
d131b7af | 319 | |
01cab976 | 320 | /* |
ae24b032 | 321 | * We have a header longer than MAX_HEADER_LEN. |
01cab976 | 322 | */ |
ae24b032 | 323 | return ULHR_TOO_LONG; |
d131b7af SP |
324 | } |
325 | ||
00a7760e JK |
326 | static void *unpack_loose_rest(git_zstream *stream, |
327 | void *buffer, unsigned long size, | |
328 | const struct object_id *oid) | |
95099731 | 329 | { |
5180cacc | 330 | int bytes = strlen(buffer) + 1; |
3aee68aa | 331 | unsigned char *buf = xmallocz(size); |
93821bd9 | 332 | unsigned long n; |
7efbff75 | 333 | int status = Z_OK; |
95099731 | 334 | |
93821bd9 LT |
335 | n = stream->total_out - bytes; |
336 | if (n > size) | |
337 | n = size; | |
338 | memcpy(buf, (char *) buffer + bytes, n); | |
339 | bytes = n; | |
456cdf6e LT |
340 | if (bytes <= size) { |
341 | /* | |
342 | * The above condition must be (bytes <= size), not | |
343 | * (bytes < size). In other words, even though we | |
ccf5ace0 | 344 | * expect no more output and set avail_out to zero, |
456cdf6e LT |
345 | * the input zlib stream may have bytes that express |
346 | * "this concludes the stream", and we *do* want to | |
347 | * eat that input. | |
348 | * | |
349 | * Otherwise we would not be able to test that we | |
350 | * consumed all the input to reach the expected size; | |
351 | * we also want to check that zlib tells us that all | |
352 | * went well with status == Z_STREAM_END at the end. | |
353 | */ | |
5180cacc LT |
354 | stream->next_out = buf + bytes; |
355 | stream->avail_out = size - bytes; | |
31877c9a MT |
356 | while (status == Z_OK) { |
357 | obj_read_unlock(); | |
39c68542 | 358 | status = git_inflate(stream, Z_FINISH); |
31877c9a MT |
359 | obj_read_lock(); |
360 | } | |
5180cacc | 361 | } |
95099731 | 362 | |
1cb2f293 | 363 | if (status != Z_STREAM_END) { |
00a7760e | 364 | error(_("corrupt loose object '%s'"), oid_to_hex(oid)); |
1cb2f293 JK |
365 | FREE_AND_NULL(buf); |
366 | } else if (stream->avail_in) { | |
259328b7 | 367 | error(_("garbage at end of loose object '%s'"), |
00a7760e | 368 | oid_to_hex(oid)); |
1cb2f293 JK |
369 | FREE_AND_NULL(buf); |
370 | } | |
547f719d | 371 | |
1cb2f293 | 372 | return buf; |
95099731 NTND |
373 | } |
374 | ||
d40d535b | 375 | /* |
5180cacc LT |
376 | * We used to just use "sscanf()", but that's actually way |
377 | * too permissive for what we want to check. So do an anal | |
378 | * object header parse by hand. | |
d40d535b | 379 | */ |
dccb32bf | 380 | int parse_loose_header(const char *hdr, struct object_info *oi) |
1f688557 | 381 | { |
46f03448 | 382 | const char *type_buf = hdr; |
d6a09e79 | 383 | size_t size; |
46f03448 | 384 | int type, type_len = 0; |
43057304 | 385 | |
5180cacc | 386 | /* |
46f03448 | 387 | * The type can be of any size but is followed by |
21666f1a | 388 | * a space. |
5180cacc | 389 | */ |
5180cacc LT |
390 | for (;;) { |
391 | char c = *hdr++; | |
d21f8426 JH |
392 | if (!c) |
393 | return -1; | |
5180cacc LT |
394 | if (c == ' ') |
395 | break; | |
46f03448 | 396 | type_len++; |
5180cacc | 397 | } |
1f688557 | 398 | |
46f03448 | 399 | type = type_from_string_gently(type_buf, type_len, 1); |
46f03448 KN |
400 | if (oi->typep) |
401 | *oi->typep = type; | |
5180cacc LT |
402 | |
403 | /* | |
404 | * The length must follow immediately, and be in canonical | |
405 | * decimal format (ie "010" is not valid). | |
406 | */ | |
407 | size = *hdr++ - '0'; | |
408 | if (size > 9) | |
409 | return -1; | |
410 | if (size) { | |
411 | for (;;) { | |
412 | unsigned long c = *hdr - '0'; | |
413 | if (c > 9) | |
414 | break; | |
415 | hdr++; | |
d6a09e79 | 416 | size = st_add(st_mult(size, 10), c); |
1b1005d1 | 417 | } |
c01f51cc | 418 | } |
46f03448 KN |
419 | |
420 | if (oi->sizep) | |
d6a09e79 | 421 | *oi->sizep = cast_size_t_to_ulong(size); |
5180cacc LT |
422 | |
423 | /* | |
424 | * The length must be followed by a zero byte | |
425 | */ | |
dccb32bf ÆAB |
426 | if (*hdr) |
427 | return -1; | |
21666f1a | 428 | |
dccb32bf ÆAB |
429 | /* |
430 | * The format is valid, but the type may still be bogus. The | |
431 | * Caller needs to check its oi->typep. | |
432 | */ | |
433 | return 0; | |
bf592c50 DB |
434 | } |
435 | ||
d9f517d0 PS |
436 | int loose_object_info(struct repository *r, |
437 | const struct object_id *oid, | |
438 | struct object_info *oi, int flags) | |
65c2e0c3 | 439 | { |
46f03448 | 440 | int status = 0; |
9e59b38c | 441 | int fd; |
46f03448 | 442 | unsigned long mapsize; |
9e59b38c | 443 | const char *path; |
65c2e0c3 | 444 | void *map; |
ef49a7a0 | 445 | git_zstream stream; |
1af64f73 | 446 | char hdr[MAX_HEADER_LEN]; |
c84a1f3e | 447 | unsigned long size_scratch; |
dccb32bf | 448 | enum object_type type_scratch; |
65c2e0c3 | 449 | |
b99b6bcc | 450 | if (oi->delta_base_oid) |
9da95bda | 451 | oidclr(oi->delta_base_oid, the_repository->hash_algo); |
5d642e75 | 452 | |
052fe5ea JK |
453 | /* |
454 | * If we don't care about type or size, then we don't | |
4ef8d1dd JH |
455 | * need to look inside the object at all. Note that we |
456 | * do not optimize out the stat call, even if the | |
457 | * caller doesn't care about the disk-size, since our | |
458 | * return value implicitly indicates whether the | |
459 | * object even exists. | |
052fe5ea | 460 | */ |
d2956385 | 461 | if (!oi->typep && !oi->sizep && !oi->contentp) { |
4ef8d1dd | 462 | struct stat st; |
61c7711c | 463 | if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK)) |
d7a24573 | 464 | return quick_has_loose(r, oid) ? 0 : -1; |
514c5fdd | 465 | if (stat_loose_object(r, oid, &st, &path) < 0) |
4ef8d1dd JH |
466 | return -1; |
467 | if (oi->disk_sizep) | |
23c339c0 | 468 | *oi->disk_sizep = st.st_size; |
052fe5ea JK |
469 | return 0; |
470 | } | |
471 | ||
9e59b38c JT |
472 | fd = open_loose_object(r, oid, &path); |
473 | if (fd < 0) { | |
474 | if (errno != ENOENT) | |
475 | error_errno(_("unable to open loose object %s"), oid_to_hex(oid)); | |
476 | return -1; | |
477 | } | |
478 | map = map_fd(fd, path, &mapsize); | |
f0df4ed5 | 479 | if (!map) |
dbea72a8 | 480 | return -1; |
c84a1f3e JT |
481 | |
482 | if (!oi->sizep) | |
483 | oi->sizep = &size_scratch; | |
dccb32bf ÆAB |
484 | if (!oi->typep) |
485 | oi->typep = &type_scratch; | |
c84a1f3e | 486 | |
23c339c0 JK |
487 | if (oi->disk_sizep) |
488 | *oi->disk_sizep = mapsize; | |
01cab976 | 489 | |
ae24b032 | 490 | switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr))) { |
3b6a8db3 | 491 | case ULHR_OK: |
ae24b032 | 492 | if (parse_loose_header(hdr, oi) < 0) |
dccb32bf | 493 | status = error(_("unable to parse %s header"), oid_to_hex(oid)); |
ae24b032 | 494 | else if (*oi->typep < 0) |
dccb32bf ÆAB |
495 | die(_("invalid object type")); |
496 | ||
497 | if (!oi->contentp) | |
498 | break; | |
499 | *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid); | |
500 | if (*oi->contentp) | |
501 | goto cleanup; | |
502 | ||
503 | status = -1; | |
3b6a8db3 ÆAB |
504 | break; |
505 | case ULHR_BAD: | |
259328b7 | 506 | status = error(_("unable to unpack %s header"), |
514c5fdd | 507 | oid_to_hex(oid)); |
3b6a8db3 | 508 | break; |
5848fb11 ÆAB |
509 | case ULHR_TOO_LONG: |
510 | status = error(_("header for %s too long, exceeds %d bytes"), | |
511 | oid_to_hex(oid), MAX_HEADER_LEN); | |
512 | break; | |
3b6a8db3 | 513 | } |
c84a1f3e | 514 | |
9e59b38c JT |
515 | if (status && (flags & OBJECT_INFO_DIE_IF_CORRUPT)) |
516 | die(_("loose object %s (stored in %s) is corrupt"), | |
517 | oid_to_hex(oid), path); | |
518 | ||
dccb32bf | 519 | cleanup: |
84b5c1a0 | 520 | git_inflate_end(&stream); |
65c2e0c3 | 521 | munmap(map, mapsize); |
c84a1f3e JT |
522 | if (oi->sizep == &size_scratch) |
523 | oi->sizep = NULL; | |
dccb32bf ÆAB |
524 | if (oi->typep == &type_scratch) |
525 | oi->typep = NULL; | |
3ab0fb06 | 526 | oi->whence = OI_LOOSE; |
dccb32bf | 527 | return status; |
65c2e0c3 JH |
528 | } |
529 | ||
7346e340 | 530 | static void hash_object_body(const struct git_hash_algo *algo, struct git_hash_ctx *c, |
2bbb28a3 ÆAB |
531 | const void *buf, unsigned long len, |
532 | struct object_id *oid, | |
533 | char *hdr, int *hdrlen) | |
534 | { | |
535 | algo->init_fn(c); | |
0578f1e6 PS |
536 | git_hash_update(c, hdr, *hdrlen); |
537 | git_hash_update(c, buf, len); | |
538 | git_hash_final_oid(oid, c); | |
2bbb28a3 ÆAB |
539 | } |
540 | ||
7ad5c44d MT |
541 | static void write_object_file_prepare(const struct git_hash_algo *algo, |
542 | const void *buf, unsigned long len, | |
2bbb28a3 | 543 | enum object_type type, struct object_id *oid, |
a09c985e | 544 | char *hdr, int *hdrlen) |
d410c0f5 | 545 | { |
7346e340 | 546 | struct git_hash_ctx c; |
d410c0f5 JH |
547 | |
548 | /* Generate the header */ | |
2bbb28a3 | 549 | *hdrlen = format_object_header(hdr, *hdrlen, type, len); |
d410c0f5 JH |
550 | |
551 | /* Sha1.. */ | |
2bbb28a3 ÆAB |
552 | hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); |
553 | } | |
554 | ||
d7fcbe2c PS |
555 | #define CHECK_COLLISION_DEST_VANISHED -2 |
556 | ||
c1acf1a3 | 557 | static int check_collision(const char *source, const char *dest) |
b1b8dfde | 558 | { |
c1acf1a3 PS |
559 | char buf_source[4096], buf_dest[4096]; |
560 | int fd_source = -1, fd_dest = -1; | |
b1b8dfde TB |
561 | int ret = 0; |
562 | ||
c1acf1a3 PS |
563 | fd_source = open(source, O_RDONLY); |
564 | if (fd_source < 0) { | |
cfae50e4 | 565 | ret = error_errno(_("unable to open %s"), source); |
b1b8dfde TB |
566 | goto out; |
567 | } | |
568 | ||
c1acf1a3 PS |
569 | fd_dest = open(dest, O_RDONLY); |
570 | if (fd_dest < 0) { | |
0ad3d656 | 571 | if (errno != ENOENT) |
c1acf1a3 | 572 | ret = error_errno(_("unable to open %s"), dest); |
d7fcbe2c PS |
573 | else |
574 | ret = CHECK_COLLISION_DEST_VANISHED; | |
b1b8dfde TB |
575 | goto out; |
576 | } | |
577 | ||
578 | while (1) { | |
579 | ssize_t sz_a, sz_b; | |
580 | ||
c1acf1a3 | 581 | sz_a = read_in_full(fd_source, buf_source, sizeof(buf_source)); |
b1b8dfde | 582 | if (sz_a < 0) { |
c1acf1a3 | 583 | ret = error_errno(_("unable to read %s"), source); |
b1b8dfde TB |
584 | goto out; |
585 | } | |
586 | ||
c1acf1a3 | 587 | sz_b = read_in_full(fd_dest, buf_dest, sizeof(buf_dest)); |
b1b8dfde | 588 | if (sz_b < 0) { |
c1acf1a3 | 589 | ret = error_errno(_("unable to read %s"), dest); |
b1b8dfde TB |
590 | goto out; |
591 | } | |
592 | ||
c1acf1a3 | 593 | if (sz_a != sz_b || memcmp(buf_source, buf_dest, sz_a)) { |
b1b8dfde | 594 | ret = error(_("files '%s' and '%s' differ in contents"), |
c1acf1a3 | 595 | source, dest); |
b1b8dfde TB |
596 | goto out; |
597 | } | |
598 | ||
c1acf1a3 | 599 | if (sz_a < sizeof(buf_source)) |
b1b8dfde TB |
600 | break; |
601 | } | |
602 | ||
603 | out: | |
c1acf1a3 PS |
604 | if (fd_source > -1) |
605 | close(fd_source); | |
606 | if (fd_dest > -1) | |
607 | close(fd_dest); | |
b1b8dfde TB |
608 | return ret; |
609 | } | |
610 | ||
230f1322 | 611 | /* |
5a688fe4 | 612 | * Move the just written object into its final resting place. |
230f1322 | 613 | */ |
cb5add58 | 614 | int finalize_object_file(const char *tmpfile, const char *filename) |
230f1322 | 615 | { |
b1b8dfde TB |
616 | return finalize_object_file_flags(tmpfile, filename, 0); |
617 | } | |
618 | ||
619 | int finalize_object_file_flags(const char *tmpfile, const char *filename, | |
620 | enum finalize_object_file_flags flags) | |
230f1322 | 621 | { |
d7fcbe2c PS |
622 | unsigned retries = 0; |
623 | int ret; | |
624 | ||
625 | retry: | |
626 | ret = 0; | |
5a688fe4 | 627 | |
348df166 | 628 | if (object_creation_mode == OBJECT_CREATION_USES_RENAMES) |
be66a6c4 JS |
629 | goto try_rename; |
630 | else if (link(tmpfile, filename)) | |
e32c0a9c | 631 | ret = errno; |
9ca7c2c1 TB |
632 | else |
633 | unlink_or_warn(tmpfile); | |
7ebb6fca LT |
634 | |
635 | /* | |
636 | * Coda hack - coda doesn't like cross-directory links, | |
637 | * so we fall back to a rename, which will mean that it | |
638 | * won't be able to check collisions, but that's not a | |
639 | * big deal. | |
640 | * | |
641 | * The same holds for FAT formatted media. | |
642 | * | |
3be1f18e | 643 | * When this succeeds, we just return. We have nothing |
7ebb6fca LT |
644 | * left to unlink. |
645 | */ | |
646 | if (ret && ret != EEXIST) { | |
d7fcbe2c PS |
647 | struct stat st; |
648 | ||
be66a6c4 | 649 | try_rename: |
d1b44bb7 TB |
650 | if (!stat(filename, &st)) |
651 | ret = EEXIST; | |
652 | else if (!rename(tmpfile, filename)) | |
3be1f18e | 653 | goto out; |
d1b44bb7 TB |
654 | else |
655 | ret = errno; | |
230f1322 | 656 | } |
230f1322 LT |
657 | if (ret) { |
658 | if (ret != EEXIST) { | |
9ca7c2c1 TB |
659 | int saved_errno = errno; |
660 | unlink_or_warn(tmpfile); | |
661 | errno = saved_errno; | |
2c319886 | 662 | return error_errno(_("unable to write file %s"), filename); |
230f1322 | 663 | } |
d7fcbe2c PS |
664 | if (!(flags & FOF_SKIP_COLLISION_CHECK)) { |
665 | ret = check_collision(tmpfile, filename); | |
666 | if (ret == CHECK_COLLISION_DEST_VANISHED) { | |
667 | if (retries++ > 5) | |
668 | return error(_("unable to write repeatedly vanishing file %s"), | |
669 | filename); | |
670 | goto retry; | |
671 | } | |
672 | else if (ret) | |
b1b8dfde | 673 | return -1; |
d7fcbe2c | 674 | } |
9ca7c2c1 | 675 | unlink_or_warn(tmpfile); |
230f1322 LT |
676 | } |
677 | ||
3be1f18e | 678 | out: |
028f6186 | 679 | if (adjust_shared_perm(the_repository, filename)) |
259328b7 | 680 | return error(_("unable to set permission to '%s'"), filename); |
230f1322 LT |
681 | return 0; |
682 | } | |
683 | ||
44439c1c ÆAB |
684 | void hash_object_file(const struct git_hash_algo *algo, const void *buf, |
685 | unsigned long len, enum object_type type, | |
686 | struct object_id *oid) | |
687 | { | |
141f8c8c JK |
688 | char hdr[MAX_HEADER_LEN]; |
689 | int hdrlen = sizeof(hdr); | |
690 | ||
691 | write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); | |
abdc3fc8 RS |
692 | } |
693 | ||
e9039dd3 | 694 | /* Finalize a file on disk, and close it. */ |
c4e707f8 | 695 | static void close_loose_object(int fd, const char *filename) |
e9039dd3 | 696 | { |
020406ea NS |
697 | if (the_repository->objects->odb->will_destroy) |
698 | goto out; | |
b3cecf49 | 699 | |
c0f4752e | 700 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
83937e95 | 701 | fsync_loose_object_bulk_checkin(fd, filename); |
c0f4752e | 702 | else if (fsync_object_files > 0) |
c4e707f8 | 703 | fsync_or_die(fd, filename); |
020406ea NS |
704 | else |
705 | fsync_component_or_die(FSYNC_COMPONENT_LOOSE_OBJECT, fd, | |
c4e707f8 | 706 | filename); |
020406ea NS |
707 | |
708 | out: | |
e9039dd3 | 709 | if (close(fd) != 0) |
76011357 | 710 | die_errno(_("error when closing loose object file")); |
e9039dd3 LT |
711 | } |
712 | ||
5723fe7e LT |
713 | /* Size of directory component, including the ending '/' */ |
714 | static inline int directory_size(const char *filename) | |
715 | { | |
716 | const char *s = strrchr(filename, '/'); | |
717 | if (!s) | |
718 | return 0; | |
719 | return s - filename + 1; | |
720 | } | |
721 | ||
722 | /* | |
723 | * This creates a temporary file in the same directory as the final | |
724 | * 'filename' | |
725 | * | |
726 | * We want to avoid cross-directory filename renames, because those | |
727 | * can have problems on various filesystems (FAT, NFS, Coda). | |
728 | */ | |
d4b3d11a | 729 | static int create_tmpfile(struct strbuf *tmp, const char *filename) |
5723fe7e LT |
730 | { |
731 | int fd, dirlen = directory_size(filename); | |
732 | ||
d4b3d11a JK |
733 | strbuf_reset(tmp); |
734 | strbuf_add(tmp, filename, dirlen); | |
735 | strbuf_addstr(tmp, "tmp_obj_XXXXXX"); | |
736 | fd = git_mkstemp_mode(tmp->buf, 0444); | |
cbacbf4e | 737 | if (fd < 0 && dirlen && errno == ENOENT) { |
d4b3d11a JK |
738 | /* |
739 | * Make sure the directory exists; note that the contents | |
740 | * of the buffer are undefined after mkstemp returns an | |
741 | * error, so we have to rewrite the whole buffer from | |
742 | * scratch. | |
743 | */ | |
744 | strbuf_reset(tmp); | |
745 | strbuf_add(tmp, filename, dirlen - 1); | |
746 | if (mkdir(tmp->buf, 0777) && errno != EEXIST) | |
b2476a60 | 747 | return -1; |
028f6186 | 748 | if (adjust_shared_perm(the_repository, tmp->buf)) |
5723fe7e LT |
749 | return -1; |
750 | ||
751 | /* Try again */ | |
d4b3d11a JK |
752 | strbuf_addstr(tmp, "/tmp_obj_XXXXXX"); |
753 | fd = git_mkstemp_mode(tmp->buf, 0444); | |
5723fe7e LT |
754 | } |
755 | return fd; | |
756 | } | |
757 | ||
97a9db6f HX |
758 | /** |
759 | * Common steps for loose object writers to start writing loose | |
760 | * objects: | |
761 | * | |
762 | * - Create tmpfile for the loose object. | |
763 | * - Setup zlib stream for compression. | |
764 | * - Start to feed header to zlib stream. | |
765 | * | |
766 | * Returns a "fd", which should later be provided to | |
767 | * end_loose_object_common(). | |
768 | */ | |
769 | static int start_loose_object_common(struct strbuf *tmp_file, | |
770 | const char *filename, unsigned flags, | |
771 | git_zstream *stream, | |
772 | unsigned char *buf, size_t buflen, | |
7346e340 | 773 | struct git_hash_ctx *c, struct git_hash_ctx *compat_c, |
97a9db6f HX |
774 | char *hdr, int hdrlen) |
775 | { | |
63a6745a EB |
776 | struct repository *repo = the_repository; |
777 | const struct git_hash_algo *algo = repo->hash_algo; | |
778 | const struct git_hash_algo *compat = repo->compat_hash_algo; | |
97a9db6f HX |
779 | int fd; |
780 | ||
781 | fd = create_tmpfile(tmp_file, filename); | |
782 | if (fd < 0) { | |
70c0f9db | 783 | if (flags & WRITE_OBJECT_FILE_SILENT) |
97a9db6f HX |
784 | return -1; |
785 | else if (errno == EACCES) | |
786 | return error(_("insufficient permission for adding " | |
787 | "an object to repository database %s"), | |
a3673f48 | 788 | repo_get_object_directory(the_repository)); |
97a9db6f HX |
789 | else |
790 | return error_errno( | |
791 | _("unable to create temporary file")); | |
792 | } | |
793 | ||
794 | /* Setup zlib stream for compression */ | |
795 | git_deflate_init(stream, zlib_compression_level); | |
796 | stream->next_out = buf; | |
797 | stream->avail_out = buflen; | |
63a6745a EB |
798 | algo->init_fn(c); |
799 | if (compat && compat_c) | |
800 | compat->init_fn(compat_c); | |
97a9db6f HX |
801 | |
802 | /* Start to feed header to zlib stream */ | |
803 | stream->next_in = (unsigned char *)hdr; | |
804 | stream->avail_in = hdrlen; | |
805 | while (git_deflate(stream, 0) == Z_OK) | |
806 | ; /* nothing */ | |
0578f1e6 | 807 | git_hash_update(c, hdr, hdrlen); |
63a6745a | 808 | if (compat && compat_c) |
0578f1e6 | 809 | git_hash_update(compat_c, hdr, hdrlen); |
97a9db6f HX |
810 | |
811 | return fd; | |
812 | } | |
813 | ||
21e7d881 ÆAB |
814 | /** |
815 | * Common steps for the inner git_deflate() loop for writing loose | |
816 | * objects. Returns what git_deflate() returns. | |
817 | */ | |
7346e340 | 818 | static int write_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c, |
21e7d881 ÆAB |
819 | git_zstream *stream, const int flush, |
820 | unsigned char *in0, const int fd, | |
821 | unsigned char *compressed, | |
822 | const size_t compressed_len) | |
823 | { | |
63a6745a | 824 | struct repository *repo = the_repository; |
63a6745a | 825 | const struct git_hash_algo *compat = repo->compat_hash_algo; |
21e7d881 ÆAB |
826 | int ret; |
827 | ||
828 | ret = git_deflate(stream, flush ? Z_FINISH : 0); | |
0578f1e6 | 829 | git_hash_update(c, in0, stream->next_in - in0); |
63a6745a | 830 | if (compat && compat_c) |
0578f1e6 | 831 | git_hash_update(compat_c, in0, stream->next_in - in0); |
d422d061 RS |
832 | if (write_in_full(fd, compressed, stream->next_out - compressed) < 0) |
833 | die_errno(_("unable to write loose object file")); | |
21e7d881 ÆAB |
834 | stream->next_out = compressed; |
835 | stream->avail_out = compressed_len; | |
836 | ||
837 | return ret; | |
838 | } | |
839 | ||
97a9db6f HX |
840 | /** |
841 | * Common steps for loose object writers to end writing loose objects: | |
842 | * | |
843 | * - End the compression of zlib stream. | |
844 | * - Get the calculated oid to "oid". | |
845 | */ | |
7346e340 | 846 | static int end_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c, |
63a6745a EB |
847 | git_zstream *stream, struct object_id *oid, |
848 | struct object_id *compat_oid) | |
97a9db6f | 849 | { |
63a6745a | 850 | struct repository *repo = the_repository; |
63a6745a | 851 | const struct git_hash_algo *compat = repo->compat_hash_algo; |
97a9db6f HX |
852 | int ret; |
853 | ||
854 | ret = git_deflate_end_gently(stream); | |
855 | if (ret != Z_OK) | |
856 | return ret; | |
0578f1e6 | 857 | git_hash_final_oid(oid, c); |
63a6745a | 858 | if (compat && compat_c) |
0578f1e6 | 859 | git_hash_final_oid(compat_oid, compat_c); |
97a9db6f HX |
860 | |
861 | return Z_OK; | |
862 | } | |
863 | ||
3fc7281f PO |
864 | static int write_loose_object(const struct object_id *oid, char *hdr, |
865 | int hdrlen, const void *buf, unsigned long len, | |
4ef91a2d | 866 | time_t mtime, unsigned flags) |
0fcfd160 | 867 | { |
915308b1 | 868 | int fd, ret; |
9892beba | 869 | unsigned char compressed[4096]; |
ef49a7a0 | 870 | git_zstream stream; |
7346e340 | 871 | struct git_hash_ctx c; |
3fc7281f | 872 | struct object_id parano_oid; |
d4b3d11a | 873 | static struct strbuf tmp_file = STRBUF_INIT; |
ea657730 CC |
874 | static struct strbuf filename = STRBUF_INIT; |
875 | ||
c0f4752e NS |
876 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
877 | prepare_loose_object_bulk_checkin(); | |
878 | ||
56ef85e8 | 879 | odb_loose_path(the_repository->objects->odb, &filename, oid); |
a44c9a5e | 880 | |
97a9db6f HX |
881 | fd = start_loose_object_common(&tmp_file, filename.buf, flags, |
882 | &stream, compressed, sizeof(compressed), | |
63a6745a | 883 | &c, NULL, hdr, hdrlen); |
97a9db6f HX |
884 | if (fd < 0) |
885 | return -1; | |
a44c9a5e LT |
886 | |
887 | /* Then the data itself.. */ | |
c00e657d | 888 | stream.next_in = (void *)buf; |
a44c9a5e | 889 | stream.avail_in = len; |
9892beba | 890 | do { |
748af44c | 891 | unsigned char *in0 = stream.next_in; |
21e7d881 | 892 | |
63a6745a | 893 | ret = write_loose_object_common(&c, NULL, &stream, 1, in0, fd, |
21e7d881 | 894 | compressed, sizeof(compressed)); |
9892beba NP |
895 | } while (ret == Z_OK); |
896 | ||
ac54c277 | 897 | if (ret != Z_STREAM_END) |
259328b7 | 898 | die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid), |
3fc7281f | 899 | ret); |
63a6745a | 900 | ret = end_loose_object_common(&c, NULL, &stream, ¶no_oid, NULL); |
ac54c277 | 901 | if (ret != Z_OK) |
259328b7 | 902 | die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid), |
3fc7281f | 903 | ret); |
9001dc2a | 904 | if (!oideq(oid, ¶no_oid)) |
259328b7 | 905 | die(_("confused by unstable object source data for %s"), |
3fc7281f | 906 | oid_to_hex(oid)); |
ac54c277 | 907 | |
c4e707f8 | 908 | close_loose_object(fd, tmp_file.buf); |
0fcfd160 | 909 | |
bbac7311 NP |
910 | if (mtime) { |
911 | struct utimbuf utb; | |
912 | utb.actime = mtime; | |
913 | utb.modtime = mtime; | |
4ef91a2d | 914 | if (utime(tmp_file.buf, &utb) < 0 && |
70c0f9db | 915 | !(flags & WRITE_OBJECT_FILE_SILENT)) |
259328b7 | 916 | warning_errno(_("failed utime() on %s"), tmp_file.buf); |
bbac7311 NP |
917 | } |
918 | ||
b1b8dfde TB |
919 | return finalize_object_file_flags(tmp_file.buf, filename.buf, |
920 | FOF_SKIP_COLLISION_CHECK); | |
0fcfd160 | 921 | } |
8237b185 | 922 | |
6862ebbf | 923 | static int freshen_loose_object(const struct object_id *oid) |
33d4221c | 924 | { |
6862ebbf | 925 | return check_and_freshen(oid, 1); |
33d4221c JK |
926 | } |
927 | ||
6862ebbf | 928 | static int freshen_packed_object(const struct object_id *oid) |
33d4221c JK |
929 | { |
930 | struct pack_entry e; | |
544443cb | 931 | if (!find_pack_entry(the_repository, oid, &e)) |
ee1c6c34 | 932 | return 0; |
a6131642 TB |
933 | if (e.p->is_cruft) |
934 | return 0; | |
ee1c6c34 JK |
935 | if (e.p->freshened) |
936 | return 1; | |
937 | if (!freshen_file(e.p->pack_name)) | |
938 | return 0; | |
939 | e.p->freshened = 1; | |
940 | return 1; | |
33d4221c JK |
941 | } |
942 | ||
2b6070ac HX |
943 | int stream_loose_object(struct input_stream *in_stream, size_t len, |
944 | struct object_id *oid) | |
945 | { | |
63a6745a EB |
946 | const struct git_hash_algo *compat = the_repository->compat_hash_algo; |
947 | struct object_id compat_oid; | |
2b6070ac HX |
948 | int fd, ret, err = 0, flush = 0; |
949 | unsigned char compressed[4096]; | |
950 | git_zstream stream; | |
7346e340 | 951 | struct git_hash_ctx c, compat_c; |
2b6070ac HX |
952 | struct strbuf tmp_file = STRBUF_INIT; |
953 | struct strbuf filename = STRBUF_INIT; | |
954 | int dirlen; | |
955 | char hdr[MAX_HEADER_LEN]; | |
956 | int hdrlen; | |
957 | ||
958 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) | |
959 | prepare_loose_object_bulk_checkin(); | |
960 | ||
961 | /* Since oid is not determined, save tmp file to odb path. */ | |
a3673f48 | 962 | strbuf_addf(&filename, "%s/", repo_get_object_directory(the_repository)); |
2b6070ac HX |
963 | hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len); |
964 | ||
965 | /* | |
966 | * Common steps for write_loose_object and stream_loose_object to | |
967 | * start writing loose objects: | |
968 | * | |
969 | * - Create tmpfile for the loose object. | |
970 | * - Setup zlib stream for compression. | |
971 | * - Start to feed header to zlib stream. | |
972 | */ | |
973 | fd = start_loose_object_common(&tmp_file, filename.buf, 0, | |
974 | &stream, compressed, sizeof(compressed), | |
63a6745a | 975 | &c, &compat_c, hdr, hdrlen); |
2b6070ac HX |
976 | if (fd < 0) { |
977 | err = -1; | |
978 | goto cleanup; | |
979 | } | |
980 | ||
981 | /* Then the data itself.. */ | |
982 | do { | |
983 | unsigned char *in0 = stream.next_in; | |
984 | ||
985 | if (!stream.avail_in && !in_stream->is_finished) { | |
986 | const void *in = in_stream->read(in_stream, &stream.avail_in); | |
987 | stream.next_in = (void *)in; | |
988 | in0 = (unsigned char *)in; | |
989 | /* All data has been read. */ | |
990 | if (in_stream->is_finished) | |
991 | flush = 1; | |
992 | } | |
63a6745a | 993 | ret = write_loose_object_common(&c, &compat_c, &stream, flush, in0, fd, |
2b6070ac HX |
994 | compressed, sizeof(compressed)); |
995 | /* | |
996 | * Unlike write_loose_object(), we do not have the entire | |
997 | * buffer. If we get Z_BUF_ERROR due to too few input bytes, | |
998 | * then we'll replenish them in the next input_stream->read() | |
999 | * call when we loop. | |
1000 | */ | |
1001 | } while (ret == Z_OK || ret == Z_BUF_ERROR); | |
1002 | ||
1003 | if (stream.total_in != len + hdrlen) | |
1004 | die(_("write stream object %ld != %"PRIuMAX), stream.total_in, | |
1005 | (uintmax_t)len + hdrlen); | |
1006 | ||
1007 | /* | |
1008 | * Common steps for write_loose_object and stream_loose_object to | |
28012b91 | 1009 | * end writing loose object: |
2b6070ac HX |
1010 | * |
1011 | * - End the compression of zlib stream. | |
1012 | * - Get the calculated oid. | |
1013 | */ | |
1014 | if (ret != Z_STREAM_END) | |
1015 | die(_("unable to stream deflate new object (%d)"), ret); | |
63a6745a | 1016 | ret = end_loose_object_common(&c, &compat_c, &stream, oid, &compat_oid); |
2b6070ac HX |
1017 | if (ret != Z_OK) |
1018 | die(_("deflateEnd on stream object failed (%d)"), ret); | |
1019 | close_loose_object(fd, tmp_file.buf); | |
1020 | ||
1021 | if (freshen_packed_object(oid) || freshen_loose_object(oid)) { | |
1022 | unlink_or_warn(tmp_file.buf); | |
1023 | goto cleanup; | |
1024 | } | |
1025 | ||
56ef85e8 | 1026 | odb_loose_path(the_repository->objects->odb, &filename, oid); |
2b6070ac HX |
1027 | |
1028 | /* We finally know the object path, and create the missing dir. */ | |
1029 | dirlen = directory_size(filename.buf); | |
1030 | if (dirlen) { | |
1031 | struct strbuf dir = STRBUF_INIT; | |
1032 | strbuf_add(&dir, filename.buf, dirlen); | |
1033 | ||
d1fa670d PS |
1034 | if (safe_create_dir_in_gitdir(the_repository, dir.buf) && |
1035 | errno != EEXIST) { | |
2b6070ac HX |
1036 | err = error_errno(_("unable to create directory %s"), dir.buf); |
1037 | strbuf_release(&dir); | |
1038 | goto cleanup; | |
1039 | } | |
1040 | strbuf_release(&dir); | |
1041 | } | |
1042 | ||
b1b8dfde TB |
1043 | err = finalize_object_file_flags(tmp_file.buf, filename.buf, |
1044 | FOF_SKIP_COLLISION_CHECK); | |
63a6745a EB |
1045 | if (!err && compat) |
1046 | err = repo_add_loose_object_map(the_repository, oid, &compat_oid); | |
2b6070ac HX |
1047 | cleanup: |
1048 | strbuf_release(&tmp_file); | |
1049 | strbuf_release(&filename); | |
1050 | return err; | |
1051 | } | |
1052 | ||
4ef91a2d | 1053 | int write_object_file_flags(const void *buf, unsigned long len, |
c80d226a | 1054 | enum object_type type, struct object_id *oid, |
c2538492 | 1055 | struct object_id *compat_oid_in, unsigned flags) |
bbac7311 | 1056 | { |
63a6745a EB |
1057 | struct repository *repo = the_repository; |
1058 | const struct git_hash_algo *algo = repo->hash_algo; | |
1059 | const struct git_hash_algo *compat = repo->compat_hash_algo; | |
1060 | struct object_id compat_oid; | |
1af64f73 | 1061 | char hdr[MAX_HEADER_LEN]; |
ef1286d3 | 1062 | int hdrlen = sizeof(hdr); |
bbac7311 | 1063 | |
63a6745a EB |
1064 | /* Generate compat_oid */ |
1065 | if (compat) { | |
c2538492 EB |
1066 | if (compat_oid_in) |
1067 | oidcpy(&compat_oid, compat_oid_in); | |
1068 | else if (type == OBJ_BLOB) | |
63a6745a EB |
1069 | hash_object_file(compat, buf, len, type, &compat_oid); |
1070 | else { | |
1071 | struct strbuf converted = STRBUF_INIT; | |
f6e174b2 | 1072 | convert_object_file(the_repository, &converted, algo, compat, |
63a6745a EB |
1073 | buf, len, type, 0); |
1074 | hash_object_file(compat, converted.buf, converted.len, | |
1075 | type, &compat_oid); | |
1076 | strbuf_release(&converted); | |
1077 | } | |
1078 | } | |
1079 | ||
bbac7311 NP |
1080 | /* Normally if we have it in the pack then we do not bother writing |
1081 | * it out into .git/objects/??/?{38} file. | |
1082 | */ | |
63a6745a | 1083 | write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); |
6862ebbf | 1084 | if (freshen_packed_object(oid) || freshen_loose_object(oid)) |
bbac7311 | 1085 | return 0; |
63a6745a EB |
1086 | if (write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags)) |
1087 | return -1; | |
1088 | if (compat) | |
1089 | return repo_add_loose_object_map(repo, oid, &compat_oid); | |
1090 | return 0; | |
bbac7311 NP |
1091 | } |
1092 | ||
4bdb70a4 | 1093 | int force_object_loose(const struct object_id *oid, time_t mtime) |
bbac7311 | 1094 | { |
63a6745a EB |
1095 | struct repository *repo = the_repository; |
1096 | const struct git_hash_algo *compat = repo->compat_hash_algo; | |
bbac7311 NP |
1097 | void *buf; |
1098 | unsigned long len; | |
b25562e6 | 1099 | struct object_info oi = OBJECT_INFO_INIT; |
63a6745a | 1100 | struct object_id compat_oid; |
bbac7311 | 1101 | enum object_type type; |
1af64f73 | 1102 | char hdr[MAX_HEADER_LEN]; |
bbac7311 | 1103 | int hdrlen; |
1fb23e65 | 1104 | int ret; |
bbac7311 | 1105 | |
6862ebbf | 1106 | if (has_loose_object(oid)) |
bbac7311 | 1107 | return 0; |
b25562e6 JK |
1108 | oi.typep = &type; |
1109 | oi.sizep = &len; | |
1110 | oi.contentp = &buf; | |
1111 | if (oid_object_info_extended(the_repository, oid, &oi, 0)) | |
2c319886 | 1112 | return error(_("cannot read object for %s"), oid_to_hex(oid)); |
63a6745a EB |
1113 | if (compat) { |
1114 | if (repo_oid_to_algop(repo, oid, compat, &compat_oid)) | |
1115 | return error(_("cannot map object %s to %s"), | |
1116 | oid_to_hex(oid), compat->name); | |
1117 | } | |
b04cdea4 | 1118 | hdrlen = format_object_header(hdr, sizeof(hdr), type, len); |
4ef91a2d | 1119 | ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0); |
63a6745a EB |
1120 | if (!ret && compat) |
1121 | ret = repo_add_loose_object_map(the_repository, oid, &compat_oid); | |
1fb23e65 BS |
1122 | free(buf); |
1123 | ||
1124 | return ret; | |
bbac7311 NP |
1125 | } |
1126 | ||
69bbbe48 JK |
1127 | /* |
1128 | * We can't use the normal fsck_error_function() for index_mem(), | |
1129 | * because we don't yet have a valid oid for it to report. Instead, | |
1130 | * report the minimal fsck error here, and rely on the caller to | |
1131 | * give more context. | |
1132 | */ | |
0b4e9013 | 1133 | static int hash_format_check_report(struct fsck_options *opts UNUSED, |
0ec5dfe8 | 1134 | void *fsck_report UNUSED, |
1135 | enum fsck_msg_type msg_type UNUSED, | |
1136 | enum fsck_msg_id msg_id UNUSED, | |
1137 | const char *message) | |
69bbbe48 JK |
1138 | { |
1139 | error(_("object fails fsck: %s"), message); | |
1140 | return 1; | |
c879daa2 NTND |
1141 | } |
1142 | ||
58bf2a4c | 1143 | static int index_mem(struct index_state *istate, |
9f03e481 PS |
1144 | struct object_id *oid, |
1145 | const void *buf, size_t size, | |
c4ce46fc JH |
1146 | enum object_type type, |
1147 | const char *path, unsigned flags) | |
e7332f96 | 1148 | { |
9f03e481 | 1149 | struct strbuf nbuf = STRBUF_INIT; |
63e05f90 | 1150 | int ret = 0; |
70c0f9db | 1151 | int write_object = flags & INDEX_WRITE_OBJECT; |
74400e71 | 1152 | |
7672db20 | 1153 | if (!type) |
edaec3fb | 1154 | type = OBJ_BLOB; |
6c510bee LT |
1155 | |
1156 | /* | |
1157 | * Convert blobs to git internal format | |
1158 | */ | |
43df4f86 | 1159 | if ((type == OBJ_BLOB) && path) { |
58bf2a4c | 1160 | if (convert_to_git(istate, path, buf, size, &nbuf, |
8462ff43 | 1161 | get_conv_flags(flags))) { |
9f03e481 PS |
1162 | buf = nbuf.buf; |
1163 | size = nbuf.len; | |
6c510bee LT |
1164 | } |
1165 | } | |
70c0f9db | 1166 | if (flags & INDEX_FORMAT_CHECK) { |
69bbbe48 JK |
1167 | struct fsck_options opts = FSCK_OPTIONS_DEFAULT; |
1168 | ||
1169 | opts.strict = 1; | |
1170 | opts.error_func = hash_format_check_report; | |
7d70b29c | 1171 | if (fsck_buffer(null_oid(the_hash_algo), type, buf, size, &opts)) |
69bbbe48 JK |
1172 | die(_("refusing to create malformed object")); |
1173 | fsck_finish(&opts); | |
c879daa2 | 1174 | } |
6c510bee | 1175 | |
7672db20 | 1176 | if (write_object) |
c80d226a | 1177 | ret = write_object_file(buf, size, type, oid); |
abdc3fc8 | 1178 | else |
44439c1c | 1179 | hash_object_file(the_hash_algo, buf, size, type, oid); |
9f03e481 PS |
1180 | |
1181 | strbuf_release(&nbuf); | |
43df4f86 DP |
1182 | return ret; |
1183 | } | |
1184 | ||
58bf2a4c NTND |
1185 | static int index_stream_convert_blob(struct index_state *istate, |
1186 | struct object_id *oid, | |
1187 | int fd, | |
1188 | const char *path, | |
1189 | unsigned flags) | |
9035d75a | 1190 | { |
63e05f90 | 1191 | int ret = 0; |
70c0f9db | 1192 | const int write_object = flags & INDEX_WRITE_OBJECT; |
9035d75a SP |
1193 | struct strbuf sbuf = STRBUF_INIT; |
1194 | ||
1195 | assert(path); | |
5633aa3a | 1196 | ASSERT(would_convert_to_git_filter_fd(istate, path)); |
9035d75a | 1197 | |
58bf2a4c | 1198 | convert_to_git_filter_fd(istate, path, fd, &sbuf, |
8462ff43 | 1199 | get_conv_flags(flags)); |
9035d75a SP |
1200 | |
1201 | if (write_object) | |
c80d226a | 1202 | ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB, |
a09c985e | 1203 | oid); |
9035d75a | 1204 | else |
44439c1c ÆAB |
1205 | hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB, |
1206 | oid); | |
9035d75a SP |
1207 | strbuf_release(&sbuf); |
1208 | return ret; | |
1209 | } | |
1210 | ||
58bf2a4c NTND |
1211 | static int index_pipe(struct index_state *istate, struct object_id *oid, |
1212 | int fd, enum object_type type, | |
7b41e1e1 JH |
1213 | const char *path, unsigned flags) |
1214 | { | |
1215 | struct strbuf sbuf = STRBUF_INIT; | |
1216 | int ret; | |
1217 | ||
1218 | if (strbuf_read(&sbuf, fd, 4096) >= 0) | |
58bf2a4c | 1219 | ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags); |
7b41e1e1 JH |
1220 | else |
1221 | ret = -1; | |
1222 | strbuf_release(&sbuf); | |
1223 | return ret; | |
1224 | } | |
1225 | ||
ea68b0ce DP |
1226 | #define SMALL_FILE_SIZE (32*1024) |
1227 | ||
58bf2a4c NTND |
1228 | static int index_core(struct index_state *istate, |
1229 | struct object_id *oid, int fd, size_t size, | |
7b41e1e1 JH |
1230 | enum object_type type, const char *path, |
1231 | unsigned flags) | |
43df4f86 DP |
1232 | { |
1233 | int ret; | |
43df4f86 | 1234 | |
7b41e1e1 | 1235 | if (!size) { |
58bf2a4c | 1236 | ret = index_mem(istate, oid, "", size, type, path, flags); |
ea68b0ce DP |
1237 | } else if (size <= SMALL_FILE_SIZE) { |
1238 | char *buf = xmalloc(size); | |
90dca671 JK |
1239 | ssize_t read_result = read_in_full(fd, buf, size); |
1240 | if (read_result < 0) | |
259328b7 | 1241 | ret = error_errno(_("read error while indexing %s"), |
90dca671 JK |
1242 | path ? path : "<unknown>"); |
1243 | else if (read_result != size) | |
259328b7 | 1244 | ret = error(_("short read while indexing %s"), |
90dca671 | 1245 | path ? path : "<unknown>"); |
ea68b0ce | 1246 | else |
58bf2a4c | 1247 | ret = index_mem(istate, oid, buf, size, type, path, flags); |
ea68b0ce | 1248 | free(buf); |
08bda208 | 1249 | } else { |
43df4f86 | 1250 | void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
58bf2a4c | 1251 | ret = index_mem(istate, oid, buf, size, type, path, flags); |
aac17941 | 1252 | munmap(buf, size); |
08bda208 | 1253 | } |
7b41e1e1 JH |
1254 | return ret; |
1255 | } | |
1256 | ||
58bf2a4c NTND |
1257 | int index_fd(struct index_state *istate, struct object_id *oid, |
1258 | int fd, struct stat *st, | |
7b41e1e1 JH |
1259 | enum object_type type, const char *path, unsigned flags) |
1260 | { | |
1261 | int ret; | |
7b41e1e1 | 1262 | |
9079ab7c SP |
1263 | /* |
1264 | * Call xsize_t() only when needed to avoid potentially unnecessary | |
1265 | * die() for large files. | |
1266 | */ | |
58bf2a4c NTND |
1267 | if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path)) |
1268 | ret = index_stream_convert_blob(istate, oid, fd, path, flags); | |
9035d75a | 1269 | else if (!S_ISREG(st->st_mode)) |
58bf2a4c | 1270 | ret = index_pipe(istate, oid, fd, type, path, flags); |
7835ee75 PS |
1271 | else if (st->st_size <= repo_settings_get_big_file_threshold(the_repository) || |
1272 | type != OBJ_BLOB || | |
58bf2a4c NTND |
1273 | (path && would_convert_to_git(istate, path))) |
1274 | ret = index_core(istate, oid, fd, xsize_t(st->st_size), | |
1275 | type, path, flags); | |
4dd1fbc7 | 1276 | else |
8a54ebd5 PS |
1277 | ret = index_blob_bulk_checkin(oid, fd, xsize_t(st->st_size), path, |
1278 | flags); | |
43df4f86 | 1279 | close(fd); |
aac17941 | 1280 | return ret; |
74400e71 | 1281 | } |
ec1fcc16 | 1282 | |
58bf2a4c NTND |
1283 | int index_path(struct index_state *istate, struct object_id *oid, |
1284 | const char *path, struct stat *st, unsigned flags) | |
ec1fcc16 JH |
1285 | { |
1286 | int fd; | |
b760d3aa | 1287 | struct strbuf sb = STRBUF_INIT; |
ea8e0297 | 1288 | int rc = 0; |
ec1fcc16 JH |
1289 | |
1290 | switch (st->st_mode & S_IFMT) { | |
1291 | case S_IFREG: | |
1292 | fd = open(path, O_RDONLY); | |
1293 | if (fd < 0) | |
7616c6ca | 1294 | return error_errno("open(\"%s\")", path); |
58bf2a4c | 1295 | if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0) |
259328b7 | 1296 | return error(_("%s: failed to insert into database"), |
ec1fcc16 JH |
1297 | path); |
1298 | break; | |
1299 | case S_IFLNK: | |
7616c6ca NTND |
1300 | if (strbuf_readlink(&sb, path, st->st_size)) |
1301 | return error_errno("readlink(\"%s\")", path); | |
70c0f9db | 1302 | if (!(flags & INDEX_WRITE_OBJECT)) |
2dcde20e | 1303 | hash_object_file(the_hash_algo, sb.buf, sb.len, |
44439c1c | 1304 | OBJ_BLOB, oid); |
c80d226a | 1305 | else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid)) |
259328b7 | 1306 | rc = error(_("%s: failed to insert into database"), path); |
b760d3aa | 1307 | strbuf_release(&sb); |
ec1fcc16 | 1308 | break; |
f35a6d3b | 1309 | case S_IFDIR: |
e19488a6 | 1310 | return repo_resolve_gitlink_ref(the_repository, path, "HEAD", oid); |
ec1fcc16 | 1311 | default: |
259328b7 | 1312 | return error(_("%s: unsupported file type"), path); |
ec1fcc16 | 1313 | } |
ea8e0297 | 1314 | return rc; |
ec1fcc16 | 1315 | } |
a69e5429 JH |
1316 | |
1317 | int read_pack_header(int fd, struct pack_header *header) | |
1318 | { | |
f48ecd38 | 1319 | if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header)) |
c697ad14 HO |
1320 | /* "eof before pack header was fully read" */ |
1321 | return PH_ERROR_EOF; | |
1322 | ||
a69e5429 JH |
1323 | if (header->hdr_signature != htonl(PACK_SIGNATURE)) |
1324 | /* "protocol error (pack signature mismatch detected)" */ | |
1325 | return PH_ERROR_PACK_SIGNATURE; | |
1326 | if (!pack_version_ok(header->hdr_version)) | |
1327 | /* "protocol error (pack version unsupported)" */ | |
1328 | return PH_ERROR_PROTOCOL; | |
1329 | return 0; | |
1330 | } | |
40d52ff7 | 1331 | |
70c49050 | 1332 | int for_each_file_in_obj_subdir(unsigned int subdir_nr, |
cc817ca3 RS |
1333 | struct strbuf *path, |
1334 | each_loose_object_fn obj_cb, | |
1335 | each_loose_cruft_fn cruft_cb, | |
1336 | each_loose_subdir_fn subdir_cb, | |
1337 | void *data) | |
27e1e22d | 1338 | { |
0375f472 RS |
1339 | size_t origlen, baselen; |
1340 | DIR *dir; | |
27e1e22d JK |
1341 | struct dirent *de; |
1342 | int r = 0; | |
62a24c89 | 1343 | struct object_id oid; |
27e1e22d | 1344 | |
70c49050 RS |
1345 | if (subdir_nr > 0xff) |
1346 | BUG("invalid loose object subdirectory: %x", subdir_nr); | |
1347 | ||
0375f472 RS |
1348 | origlen = path->len; |
1349 | strbuf_complete(path, '/'); | |
1350 | strbuf_addf(path, "%02x", subdir_nr); | |
0375f472 RS |
1351 | |
1352 | dir = opendir(path->buf); | |
27e1e22d | 1353 | if (!dir) { |
0375f472 | 1354 | if (errno != ENOENT) |
259328b7 | 1355 | r = error_errno(_("unable to open %s"), path->buf); |
0375f472 RS |
1356 | strbuf_setlen(path, origlen); |
1357 | return r; | |
27e1e22d JK |
1358 | } |
1359 | ||
62a24c89 | 1360 | oid.hash[0] = subdir_nr; |
163ee5e6 DS |
1361 | strbuf_addch(path, '/'); |
1362 | baselen = path->len; | |
62a24c89 | 1363 | |
b548f0f1 | 1364 | while ((de = readdir_skip_dot_and_dotdot(dir))) { |
163ee5e6 | 1365 | size_t namelen; |
27e1e22d | 1366 | |
163ee5e6 | 1367 | namelen = strlen(de->d_name); |
27e1e22d | 1368 | strbuf_setlen(path, baselen); |
163ee5e6 | 1369 | strbuf_add(path, de->d_name, namelen); |
94b5e093 | 1370 | if (namelen == the_hash_algo->hexsz - 2 && |
62a24c89 | 1371 | !hex_to_bytes(oid.hash + 1, de->d_name, |
94b5e093 | 1372 | the_hash_algo->rawsz - 1)) { |
5a6dce70 | 1373 | oid_set_algo(&oid, the_hash_algo); |
c98d762e PS |
1374 | memset(oid.hash + the_hash_algo->rawsz, 0, |
1375 | GIT_MAX_RAWSZ - the_hash_algo->rawsz); | |
62a24c89 RS |
1376 | if (obj_cb) { |
1377 | r = obj_cb(&oid, path->buf, data); | |
1378 | if (r) | |
1379 | break; | |
27e1e22d | 1380 | } |
62a24c89 | 1381 | continue; |
27e1e22d JK |
1382 | } |
1383 | ||
1384 | if (cruft_cb) { | |
1385 | r = cruft_cb(de->d_name, path->buf, data); | |
1386 | if (r) | |
1387 | break; | |
1388 | } | |
1389 | } | |
094c7e63 | 1390 | closedir(dir); |
27e1e22d | 1391 | |
163ee5e6 | 1392 | strbuf_setlen(path, baselen - 1); |
27e1e22d JK |
1393 | if (!r && subdir_cb) |
1394 | r = subdir_cb(subdir_nr, path->buf, data); | |
1395 | ||
0375f472 RS |
1396 | strbuf_setlen(path, origlen); |
1397 | ||
27e1e22d JK |
1398 | return r; |
1399 | } | |
1400 | ||
e6f875e0 | 1401 | int for_each_loose_file_in_objdir_buf(struct strbuf *path, |
27e1e22d JK |
1402 | each_loose_object_fn obj_cb, |
1403 | each_loose_cruft_fn cruft_cb, | |
1404 | each_loose_subdir_fn subdir_cb, | |
1405 | void *data) | |
1406 | { | |
27e1e22d JK |
1407 | int r = 0; |
1408 | int i; | |
1409 | ||
27e1e22d | 1410 | for (i = 0; i < 256; i++) { |
e6f875e0 | 1411 | r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb, |
27e1e22d | 1412 | subdir_cb, data); |
27e1e22d JK |
1413 | if (r) |
1414 | break; | |
1415 | } | |
1416 | ||
e6f875e0 JK |
1417 | return r; |
1418 | } | |
1419 | ||
1420 | int for_each_loose_file_in_objdir(const char *path, | |
1421 | each_loose_object_fn obj_cb, | |
1422 | each_loose_cruft_fn cruft_cb, | |
1423 | each_loose_subdir_fn subdir_cb, | |
1424 | void *data) | |
1425 | { | |
1426 | struct strbuf buf = STRBUF_INIT; | |
1427 | int r; | |
1428 | ||
1429 | strbuf_addstr(&buf, path); | |
1430 | r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb, | |
1431 | subdir_cb, data); | |
27e1e22d | 1432 | strbuf_release(&buf); |
e6f875e0 | 1433 | |
27e1e22d JK |
1434 | return r; |
1435 | } | |
660c889e | 1436 | |
a7ff6f5a JK |
1437 | int for_each_loose_object(each_loose_object_fn cb, void *data, |
1438 | enum for_each_object_flags flags) | |
660c889e | 1439 | { |
f0eaf638 | 1440 | struct object_directory *odb; |
b0a42642 | 1441 | |
f0eaf638 JK |
1442 | prepare_alt_odb(the_repository); |
1443 | for (odb = the_repository->objects->odb; odb; odb = odb->next) { | |
1444 | int r = for_each_loose_file_in_objdir(odb->path, cb, NULL, | |
1445 | NULL, data); | |
1446 | if (r) | |
1447 | return r; | |
660c889e | 1448 | |
f0eaf638 JK |
1449 | if (flags & FOR_EACH_OBJECT_LOCAL_ONLY) |
1450 | break; | |
1451 | } | |
1385bb7b | 1452 | |
f0eaf638 | 1453 | return 0; |
660c889e JK |
1454 | } |
1455 | ||
be252d33 JK |
1456 | static int append_loose_object(const struct object_id *oid, |
1457 | const char *path UNUSED, | |
3a2e0824 | 1458 | void *data) |
660c889e | 1459 | { |
92d8ed8a | 1460 | oidtree_insert(data, oid); |
3a2e0824 JK |
1461 | return 0; |
1462 | } | |
660c889e | 1463 | |
92d8ed8a | 1464 | struct oidtree *odb_loose_cache(struct object_directory *odb, |
0000d654 RS |
1465 | const struct object_id *oid) |
1466 | { | |
1467 | int subdir_nr = oid->hash[0]; | |
3a2e0824 | 1468 | struct strbuf buf = STRBUF_INIT; |
33f379ee EW |
1469 | size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]); |
1470 | size_t word_index = subdir_nr / word_bits; | |
26de1fc0 | 1471 | size_t mask = (size_t)1u << (subdir_nr % word_bits); |
33f379ee | 1472 | uint32_t *bitmap; |
660c889e | 1473 | |
3a2e0824 | 1474 | if (subdir_nr < 0 || |
33f379ee | 1475 | subdir_nr >= bitsizeof(odb->loose_objects_subdir_seen)) |
3a2e0824 | 1476 | BUG("subdir_nr out of range"); |
1385bb7b | 1477 | |
33f379ee EW |
1478 | bitmap = &odb->loose_objects_subdir_seen[word_index]; |
1479 | if (*bitmap & mask) | |
92d8ed8a EW |
1480 | return odb->loose_objects_cache; |
1481 | if (!odb->loose_objects_cache) { | |
1482 | ALLOC_ARRAY(odb->loose_objects_cache, 1); | |
1483 | oidtree_init(odb->loose_objects_cache); | |
1484 | } | |
3a2e0824 JK |
1485 | strbuf_addstr(&buf, odb->path); |
1486 | for_each_file_in_obj_subdir(subdir_nr, &buf, | |
1487 | append_loose_object, | |
1488 | NULL, NULL, | |
92d8ed8a | 1489 | odb->loose_objects_cache); |
33f379ee | 1490 | *bitmap |= mask; |
7317aa71 | 1491 | strbuf_release(&buf); |
92d8ed8a | 1492 | return odb->loose_objects_cache; |
660c889e JK |
1493 | } |
1494 | ||
d4e19e51 RS |
1495 | void odb_clear_loose_cache(struct object_directory *odb) |
1496 | { | |
92d8ed8a EW |
1497 | oidtree_clear(odb->loose_objects_cache); |
1498 | FREE_AND_NULL(odb->loose_objects_cache); | |
d4e19e51 RS |
1499 | memset(&odb->loose_objects_subdir_seen, 0, |
1500 | sizeof(odb->loose_objects_subdir_seen)); | |
1501 | } | |
1502 | ||
00a7760e JK |
1503 | static int check_stream_oid(git_zstream *stream, |
1504 | const char *hdr, | |
1505 | unsigned long size, | |
1506 | const char *path, | |
1507 | const struct object_id *expected_oid) | |
f6371f92 | 1508 | { |
7346e340 | 1509 | struct git_hash_ctx c; |
00a7760e | 1510 | struct object_id real_oid; |
f6371f92 JK |
1511 | unsigned char buf[4096]; |
1512 | unsigned long total_read; | |
1513 | int status = Z_OK; | |
1514 | ||
18e2588e | 1515 | the_hash_algo->init_fn(&c); |
0578f1e6 | 1516 | git_hash_update(&c, hdr, stream->total_out); |
f6371f92 JK |
1517 | |
1518 | /* | |
1519 | * We already read some bytes into hdr, but the ones up to the NUL | |
1520 | * do not count against the object's content size. | |
1521 | */ | |
1522 | total_read = stream->total_out - strlen(hdr) - 1; | |
1523 | ||
1524 | /* | |
1525 | * This size comparison must be "<=" to read the final zlib packets; | |
00a7760e | 1526 | * see the comment in unpack_loose_rest for details. |
f6371f92 JK |
1527 | */ |
1528 | while (total_read <= size && | |
18ad13e5 JH |
1529 | (status == Z_OK || |
1530 | (status == Z_BUF_ERROR && !stream->avail_out))) { | |
f6371f92 JK |
1531 | stream->next_out = buf; |
1532 | stream->avail_out = sizeof(buf); | |
1533 | if (size - total_read < stream->avail_out) | |
1534 | stream->avail_out = size - total_read; | |
1535 | status = git_inflate(stream, Z_FINISH); | |
0578f1e6 | 1536 | git_hash_update(&c, buf, stream->next_out - buf); |
f6371f92 JK |
1537 | total_read += stream->next_out - buf; |
1538 | } | |
f6371f92 JK |
1539 | |
1540 | if (status != Z_STREAM_END) { | |
00a7760e | 1541 | error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid)); |
f6371f92 JK |
1542 | return -1; |
1543 | } | |
cce044df | 1544 | if (stream->avail_in) { |
259328b7 | 1545 | error(_("garbage at end of loose object '%s'"), |
00a7760e | 1546 | oid_to_hex(expected_oid)); |
cce044df JK |
1547 | return -1; |
1548 | } | |
f6371f92 | 1549 | |
0578f1e6 | 1550 | git_hash_final_oid(&real_oid, &c); |
00a7760e | 1551 | if (!oideq(expected_oid, &real_oid)) { |
01f8d594 | 1552 | error(_("hash mismatch for %s (expected %s)"), path, |
00a7760e | 1553 | oid_to_hex(expected_oid)); |
f6371f92 JK |
1554 | return -1; |
1555 | } | |
1556 | ||
1557 | return 0; | |
1558 | } | |
1559 | ||
1560 | int read_loose_object(const char *path, | |
d61d87bd | 1561 | const struct object_id *expected_oid, |
96e41f58 | 1562 | struct object_id *real_oid, |
31deb28f ÆAB |
1563 | void **contents, |
1564 | struct object_info *oi) | |
f6371f92 JK |
1565 | { |
1566 | int ret = -1; | |
ae285ac4 | 1567 | int fd; |
f6371f92 JK |
1568 | void *map = NULL; |
1569 | unsigned long mapsize; | |
1570 | git_zstream stream; | |
1af64f73 | 1571 | char hdr[MAX_HEADER_LEN]; |
31deb28f | 1572 | unsigned long *size = oi->sizep; |
f6371f92 | 1573 | |
ae285ac4 JT |
1574 | fd = git_open(path); |
1575 | if (fd >= 0) | |
1576 | map = map_fd(fd, path, &mapsize); | |
f6371f92 | 1577 | if (!map) { |
259328b7 | 1578 | error_errno(_("unable to mmap %s"), path); |
f6371f92 JK |
1579 | goto out; |
1580 | } | |
1581 | ||
ae24b032 | 1582 | if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) != ULHR_OK) { |
259328b7 | 1583 | error(_("unable to unpack header of %s"), path); |
84b5c1a0 | 1584 | goto out_inflate; |
f6371f92 JK |
1585 | } |
1586 | ||
31deb28f | 1587 | if (parse_loose_header(hdr, oi) < 0) { |
259328b7 | 1588 | error(_("unable to parse header of %s"), path); |
84b5c1a0 | 1589 | goto out_inflate; |
f6371f92 JK |
1590 | } |
1591 | ||
4ae0e942 JK |
1592 | if (*oi->typep < 0) { |
1593 | error(_("unable to parse type from header '%s' of %s"), | |
1594 | hdr, path); | |
1595 | goto out_inflate; | |
1596 | } | |
1597 | ||
7835ee75 PS |
1598 | if (*oi->typep == OBJ_BLOB && |
1599 | *size > repo_settings_get_big_file_threshold(the_repository)) { | |
00a7760e | 1600 | if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0) |
84b5c1a0 | 1601 | goto out_inflate; |
f6371f92 | 1602 | } else { |
00a7760e | 1603 | *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid); |
f6371f92 | 1604 | if (!*contents) { |
259328b7 | 1605 | error(_("unable to unpack contents of %s"), path); |
84b5c1a0 | 1606 | goto out_inflate; |
f6371f92 | 1607 | } |
4ae0e942 JK |
1608 | hash_object_file(the_repository->hash_algo, |
1609 | *contents, *size, | |
1610 | *oi->typep, real_oid); | |
0f156dbb | 1611 | if (!oideq(expected_oid, real_oid)) |
84b5c1a0 | 1612 | goto out_inflate; |
f6371f92 JK |
1613 | } |
1614 | ||
1615 | ret = 0; /* everything checks out */ | |
1616 | ||
84b5c1a0 JK |
1617 | out_inflate: |
1618 | git_inflate_end(&stream); | |
f6371f92 JK |
1619 | out: |
1620 | if (map) | |
1621 | munmap(map, mapsize); | |
f6371f92 JK |
1622 | return ret; |
1623 | } |