| 1 | #include "git-compat-util.h" |
| 2 | #include "abspath.h" |
| 3 | #include "chdir-notify.h" |
| 4 | #include "gettext.h" |
| 5 | #include "hex.h" |
| 6 | #include "loose.h" |
| 7 | #include "object-file.h" |
| 8 | #include "object-file-convert.h" |
| 9 | #include "odb.h" |
| 10 | #include "odb/source-files.h" |
| 11 | #include "odb/source-loose.h" |
| 12 | #include "odb/streaming.h" |
| 13 | #include "oidtree.h" |
| 14 | #include "path.h" |
| 15 | #include "repository.h" |
| 16 | #include "strbuf.h" |
| 17 | #include "tempfile.h" |
| 18 | #include "write-or-die.h" |
| 19 | |
| 20 | static int append_loose_object(const struct object_id *oid, |
| 21 | const char *path UNUSED, |
| 22 | void *data) |
| 23 | { |
| 24 | oidtree_insert(data, oid, NULL); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | static struct oidtree *odb_source_loose_cache(struct odb_source_loose *loose, |
| 29 | const struct object_id *oid) |
| 30 | { |
| 31 | int subdir_nr = oid->hash[0]; |
| 32 | struct strbuf buf = STRBUF_INIT; |
| 33 | size_t word_bits = bitsizeof(loose->subdir_seen[0]); |
| 34 | size_t word_index = subdir_nr / word_bits; |
| 35 | size_t mask = (size_t)1u << (subdir_nr % word_bits); |
| 36 | uint32_t *bitmap; |
| 37 | |
| 38 | if (subdir_nr < 0 || |
| 39 | (size_t) subdir_nr >= bitsizeof(loose->subdir_seen)) |
| 40 | BUG("subdir_nr out of range"); |
| 41 | |
| 42 | bitmap = &loose->subdir_seen[word_index]; |
| 43 | if (*bitmap & mask) |
| 44 | return loose->cache; |
| 45 | if (!loose->cache) { |
| 46 | ALLOC_ARRAY(loose->cache, 1); |
| 47 | oidtree_init(loose->cache); |
| 48 | } |
| 49 | strbuf_addstr(&buf, loose->base.path); |
| 50 | for_each_file_in_obj_subdir(subdir_nr, &buf, |
| 51 | loose->base.odb->repo->hash_algo, |
| 52 | append_loose_object, |
| 53 | NULL, NULL, |
| 54 | loose->cache); |
| 55 | *bitmap |= mask; |
| 56 | strbuf_release(&buf); |
| 57 | return loose->cache; |
| 58 | } |
| 59 | |
| 60 | static int quick_has_loose(struct odb_source_loose *loose, |
| 61 | const struct object_id *oid) |
| 62 | { |
| 63 | return !!oidtree_contains(odb_source_loose_cache(loose, oid), oid); |
| 64 | } |
| 65 | |
| 66 | static int read_object_info_from_path(struct odb_source_loose *loose, |
| 67 | const char *path, |
| 68 | const struct object_id *oid, |
| 69 | struct object_info *oi, |
| 70 | enum object_info_flags flags) |
| 71 | { |
| 72 | int ret; |
| 73 | int fd; |
| 74 | unsigned long mapsize; |
| 75 | void *map = NULL; |
| 76 | git_zstream stream, *stream_to_end = NULL; |
| 77 | char hdr[MAX_HEADER_LEN]; |
| 78 | size_t size_scratch; |
| 79 | enum object_type type_scratch; |
| 80 | struct stat st; |
| 81 | |
| 82 | /* |
| 83 | * If we don't care about type or size, then we don't |
| 84 | * need to look inside the object at all. Note that we |
| 85 | * do not optimize out the stat call, even if the |
| 86 | * caller doesn't care about the disk-size, since our |
| 87 | * return value implicitly indicates whether the |
| 88 | * object even exists. |
| 89 | */ |
| 90 | if (!oi || (!oi->typep && !oi->sizep && !oi->contentp)) { |
| 91 | struct stat st; |
| 92 | |
| 93 | if ((!oi || (!oi->disk_sizep && !oi->mtimep)) && (flags & OBJECT_INFO_QUICK)) { |
| 94 | ret = quick_has_loose(loose, oid) ? 0 : -1; |
| 95 | goto out; |
| 96 | } |
| 97 | |
| 98 | if (lstat(path, &st) < 0) { |
| 99 | ret = -1; |
| 100 | goto out; |
| 101 | } |
| 102 | |
| 103 | if (oi) { |
| 104 | if (oi->disk_sizep) |
| 105 | *oi->disk_sizep = st.st_size; |
| 106 | if (oi->mtimep) |
| 107 | *oi->mtimep = st.st_mtime; |
| 108 | } |
| 109 | |
| 110 | ret = 0; |
| 111 | goto out; |
| 112 | } |
| 113 | |
| 114 | fd = git_open(path); |
| 115 | if (fd < 0) { |
| 116 | if (errno != ENOENT) |
| 117 | error_errno(_("unable to open loose object %s"), oid_to_hex(oid)); |
| 118 | ret = -1; |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | if (fstat(fd, &st)) { |
| 123 | close(fd); |
| 124 | ret = -1; |
| 125 | goto out; |
| 126 | } |
| 127 | |
| 128 | mapsize = xsize_t(st.st_size); |
| 129 | if (!mapsize) { |
| 130 | close(fd); |
| 131 | ret = error(_("object file %s is empty"), path); |
| 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | map = xmmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 136 | close(fd); |
| 137 | if (!map) { |
| 138 | ret = -1; |
| 139 | goto out; |
| 140 | } |
| 141 | |
| 142 | if (oi->disk_sizep) |
| 143 | *oi->disk_sizep = mapsize; |
| 144 | if (oi->mtimep) |
| 145 | *oi->mtimep = st.st_mtime; |
| 146 | |
| 147 | stream_to_end = &stream; |
| 148 | |
| 149 | switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr))) { |
| 150 | case ULHR_OK: |
| 151 | if (!oi->sizep) |
| 152 | oi->sizep = &size_scratch; |
| 153 | if (!oi->typep) |
| 154 | oi->typep = &type_scratch; |
| 155 | |
| 156 | if (parse_loose_header(hdr, oi) < 0) { |
| 157 | ret = error(_("unable to parse %s header"), oid_to_hex(oid)); |
| 158 | goto corrupt; |
| 159 | } |
| 160 | |
| 161 | if (*oi->typep < 0) |
| 162 | die(_("invalid object type")); |
| 163 | |
| 164 | if (oi->contentp) { |
| 165 | *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid); |
| 166 | if (!*oi->contentp) { |
| 167 | ret = -1; |
| 168 | goto corrupt; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | break; |
| 173 | case ULHR_BAD: |
| 174 | ret = error(_("unable to unpack %s header"), |
| 175 | oid_to_hex(oid)); |
| 176 | goto corrupt; |
| 177 | case ULHR_TOO_LONG: |
| 178 | ret = error(_("header for %s too long, exceeds %d bytes"), |
| 179 | oid_to_hex(oid), MAX_HEADER_LEN); |
| 180 | goto corrupt; |
| 181 | } |
| 182 | |
| 183 | ret = 0; |
| 184 | |
| 185 | corrupt: |
| 186 | if (ret && (flags & OBJECT_INFO_DIE_IF_CORRUPT)) |
| 187 | die(_("loose object %s (stored in %s) is corrupt"), |
| 188 | oid_to_hex(oid), path); |
| 189 | |
| 190 | out: |
| 191 | if (stream_to_end) |
| 192 | git_inflate_end(stream_to_end); |
| 193 | if (map) |
| 194 | munmap(map, mapsize); |
| 195 | if (oi) { |
| 196 | if (oi->sizep == &size_scratch) |
| 197 | oi->sizep = NULL; |
| 198 | if (oi->typep == &type_scratch) |
| 199 | oi->typep = NULL; |
| 200 | if (oi->delta_base_oid) |
| 201 | oidclr(oi->delta_base_oid, loose->base.odb->repo->hash_algo); |
| 202 | if (oi->source_infop && !ret) |
| 203 | oi->source_infop->source = &loose->base; |
| 204 | } |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static int odb_source_loose_read_object_info(struct odb_source *source, |
| 210 | const struct object_id *oid, |
| 211 | struct object_info *oi, |
| 212 | enum object_info_flags flags) |
| 213 | { |
| 214 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 215 | static struct strbuf buf = STRBUF_INIT; |
| 216 | |
| 217 | /* |
| 218 | * The second read shouldn't cause new loose objects to show up, unless |
| 219 | * there was a race condition with a secondary process. We don't care |
| 220 | * about this case though, so we simply skip reading loose objects a |
| 221 | * second time. |
| 222 | */ |
| 223 | if (flags & OBJECT_INFO_SECOND_READ) |
| 224 | return -1; |
| 225 | |
| 226 | odb_loose_path(loose, &buf, oid); |
| 227 | return read_object_info_from_path(loose, buf.buf, oid, oi, flags); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Find "oid" as a loose object in given source, open the object and return its |
| 232 | * file descriptor. Returns the file descriptor on success, negative on failure. |
| 233 | * |
| 234 | * The "path" out-parameter will give the path of the object we found (if any). |
| 235 | * Note that it may point to static storage and is only valid until another |
| 236 | * call to open_loose_object(). |
| 237 | */ |
| 238 | static int open_loose_object(struct odb_source_loose *loose, |
| 239 | const struct object_id *oid, const char **path) |
| 240 | { |
| 241 | static struct strbuf buf = STRBUF_INIT; |
| 242 | int fd; |
| 243 | |
| 244 | *path = odb_loose_path(loose, &buf, oid); |
| 245 | fd = git_open(*path); |
| 246 | if (fd >= 0) |
| 247 | return fd; |
| 248 | |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | static void *odb_source_loose_map_object(struct odb_source_loose *loose, |
| 253 | const struct object_id *oid, |
| 254 | unsigned long *size) |
| 255 | { |
| 256 | const char *p; |
| 257 | int fd = open_loose_object(loose, oid, &p); |
| 258 | void *map = NULL; |
| 259 | struct stat st; |
| 260 | |
| 261 | if (fd < 0) |
| 262 | return NULL; |
| 263 | |
| 264 | if (!fstat(fd, &st)) { |
| 265 | *size = xsize_t(st.st_size); |
| 266 | if (!*size) { |
| 267 | /* mmap() is forbidden on empty files */ |
| 268 | error(_("object file %s is empty"), p); |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 273 | } |
| 274 | |
| 275 | out: |
| 276 | close(fd); |
| 277 | return map; |
| 278 | } |
| 279 | |
| 280 | struct odb_loose_read_stream { |
| 281 | struct odb_read_stream base; |
| 282 | git_zstream z; |
| 283 | enum { |
| 284 | ODB_LOOSE_READ_STREAM_INUSE, |
| 285 | ODB_LOOSE_READ_STREAM_DONE, |
| 286 | ODB_LOOSE_READ_STREAM_ERROR, |
| 287 | } z_state; |
| 288 | void *mapped; |
| 289 | unsigned long mapsize; |
| 290 | char hdr[32]; |
| 291 | int hdr_avail; |
| 292 | int hdr_used; |
| 293 | }; |
| 294 | |
| 295 | static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t sz) |
| 296 | { |
| 297 | struct odb_loose_read_stream *st = |
| 298 | container_of(_st, struct odb_loose_read_stream, base); |
| 299 | size_t total_read = 0; |
| 300 | |
| 301 | switch (st->z_state) { |
| 302 | case ODB_LOOSE_READ_STREAM_DONE: |
| 303 | return 0; |
| 304 | case ODB_LOOSE_READ_STREAM_ERROR: |
| 305 | return -1; |
| 306 | default: |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | if (st->hdr_used < st->hdr_avail) { |
| 311 | size_t to_copy = st->hdr_avail - st->hdr_used; |
| 312 | if (sz < to_copy) |
| 313 | to_copy = sz; |
| 314 | memcpy(buf, st->hdr + st->hdr_used, to_copy); |
| 315 | st->hdr_used += to_copy; |
| 316 | total_read += to_copy; |
| 317 | } |
| 318 | |
| 319 | while (total_read < sz) { |
| 320 | int status; |
| 321 | |
| 322 | st->z.next_out = (unsigned char *)buf + total_read; |
| 323 | st->z.avail_out = sz - total_read; |
| 324 | status = git_inflate(&st->z, Z_FINISH); |
| 325 | |
| 326 | total_read = st->z.next_out - (unsigned char *)buf; |
| 327 | |
| 328 | if (status == Z_STREAM_END) { |
| 329 | git_inflate_end(&st->z); |
| 330 | st->z_state = ODB_LOOSE_READ_STREAM_DONE; |
| 331 | break; |
| 332 | } |
| 333 | if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) { |
| 334 | git_inflate_end(&st->z); |
| 335 | st->z_state = ODB_LOOSE_READ_STREAM_ERROR; |
| 336 | return -1; |
| 337 | } |
| 338 | } |
| 339 | return total_read; |
| 340 | } |
| 341 | |
| 342 | static int close_istream_loose(struct odb_read_stream *_st) |
| 343 | { |
| 344 | struct odb_loose_read_stream *st = |
| 345 | container_of(_st, struct odb_loose_read_stream, base); |
| 346 | |
| 347 | if (st->z_state == ODB_LOOSE_READ_STREAM_INUSE) |
| 348 | git_inflate_end(&st->z); |
| 349 | munmap(st->mapped, st->mapsize); |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int odb_source_loose_read_object_stream(struct odb_read_stream **out, |
| 354 | struct odb_source *source, |
| 355 | const struct object_id *oid) |
| 356 | { |
| 357 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 358 | struct object_info oi = OBJECT_INFO_INIT; |
| 359 | struct odb_loose_read_stream *st; |
| 360 | unsigned long mapsize; |
| 361 | void *mapped; |
| 362 | |
| 363 | mapped = odb_source_loose_map_object(loose, oid, &mapsize); |
| 364 | if (!mapped) |
| 365 | return -1; |
| 366 | |
| 367 | /* |
| 368 | * Note: we must allocate this structure early even though we may still |
| 369 | * fail. This is because we need to initialize the zlib stream, and it |
| 370 | * is not possible to copy the stream around after the fact because it |
| 371 | * has self-referencing pointers. |
| 372 | */ |
| 373 | CALLOC_ARRAY(st, 1); |
| 374 | |
| 375 | switch (unpack_loose_header(&st->z, mapped, mapsize, st->hdr, |
| 376 | sizeof(st->hdr))) { |
| 377 | case ULHR_OK: |
| 378 | break; |
| 379 | case ULHR_BAD: |
| 380 | case ULHR_TOO_LONG: |
| 381 | goto error; |
| 382 | } |
| 383 | |
| 384 | oi.sizep = &st->base.size; |
| 385 | oi.typep = &st->base.type; |
| 386 | |
| 387 | if (parse_loose_header(st->hdr, &oi) < 0 || st->base.type < 0) |
| 388 | goto error; |
| 389 | |
| 390 | st->mapped = mapped; |
| 391 | st->mapsize = mapsize; |
| 392 | st->hdr_used = strlen(st->hdr) + 1; |
| 393 | st->hdr_avail = st->z.total_out; |
| 394 | st->z_state = ODB_LOOSE_READ_STREAM_INUSE; |
| 395 | st->base.close = close_istream_loose; |
| 396 | st->base.read = read_istream_loose; |
| 397 | |
| 398 | *out = &st->base; |
| 399 | |
| 400 | return 0; |
| 401 | error: |
| 402 | git_inflate_end(&st->z); |
| 403 | munmap(mapped, mapsize); |
| 404 | free(st); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | struct for_each_object_wrapper_data { |
| 409 | struct odb_source_loose *loose; |
| 410 | const struct object_info *request; |
| 411 | odb_for_each_object_cb cb; |
| 412 | void *cb_data; |
| 413 | }; |
| 414 | |
| 415 | static int for_each_object_wrapper_cb(const struct object_id *oid, |
| 416 | const char *path, |
| 417 | void *cb_data) |
| 418 | { |
| 419 | struct for_each_object_wrapper_data *data = cb_data; |
| 420 | |
| 421 | if (data->request) { |
| 422 | struct object_info oi = *data->request; |
| 423 | |
| 424 | if (read_object_info_from_path(data->loose, path, oid, &oi, 0) < 0) |
| 425 | return -1; |
| 426 | |
| 427 | return data->cb(oid, &oi, data->cb_data); |
| 428 | } else { |
| 429 | return data->cb(oid, NULL, data->cb_data); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid, |
| 434 | void *node_data UNUSED, |
| 435 | void *cb_data) |
| 436 | { |
| 437 | struct for_each_object_wrapper_data *data = cb_data; |
| 438 | if (data->request) { |
| 439 | struct object_info oi = *data->request; |
| 440 | |
| 441 | if (odb_source_read_object_info(&data->loose->base, |
| 442 | oid, &oi, 0) < 0) |
| 443 | return -1; |
| 444 | |
| 445 | return data->cb(oid, &oi, data->cb_data); |
| 446 | } else { |
| 447 | return data->cb(oid, NULL, data->cb_data); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static int odb_source_loose_for_each_object(struct odb_source *source, |
| 452 | const struct object_info *request, |
| 453 | odb_for_each_object_cb cb, |
| 454 | void *cb_data, |
| 455 | const struct odb_for_each_object_options *opts) |
| 456 | { |
| 457 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 458 | struct for_each_object_wrapper_data data = { |
| 459 | .loose = loose, |
| 460 | .request = request, |
| 461 | .cb = cb, |
| 462 | .cb_data = cb_data, |
| 463 | }; |
| 464 | |
| 465 | /* There are no loose promisor objects, so we can return immediately. */ |
| 466 | if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) |
| 467 | return 0; |
| 468 | if ((opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !source->local) |
| 469 | return 0; |
| 470 | |
| 471 | if (opts->prefix) |
| 472 | return oidtree_each(odb_source_loose_cache(loose, opts->prefix), |
| 473 | opts->prefix, opts->prefix_hex_len, |
| 474 | for_each_prefixed_object_wrapper_cb, &data); |
| 475 | |
| 476 | return for_each_loose_file_in_source(source, for_each_object_wrapper_cb, |
| 477 | NULL, NULL, &data); |
| 478 | } |
| 479 | |
| 480 | struct find_abbrev_len_data { |
| 481 | const struct object_id *oid; |
| 482 | unsigned len; |
| 483 | }; |
| 484 | |
| 485 | static int find_abbrev_len_cb(const struct object_id *oid, |
| 486 | struct object_info *oi UNUSED, |
| 487 | void *cb_data) |
| 488 | { |
| 489 | struct find_abbrev_len_data *data = cb_data; |
| 490 | unsigned len = oid_common_prefix_hexlen(oid, data->oid); |
| 491 | if (len != hash_algos[oid->algo].hexsz && len >= data->len) |
| 492 | data->len = len + 1; |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int odb_source_loose_find_abbrev_len(struct odb_source *source, |
| 497 | const struct object_id *oid, |
| 498 | unsigned min_len, |
| 499 | unsigned *out) |
| 500 | { |
| 501 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 502 | struct odb_for_each_object_options opts = { |
| 503 | .prefix = oid, |
| 504 | .prefix_hex_len = min_len, |
| 505 | }; |
| 506 | struct find_abbrev_len_data data = { |
| 507 | .oid = oid, |
| 508 | .len = min_len, |
| 509 | }; |
| 510 | int ret; |
| 511 | |
| 512 | ret = odb_source_for_each_object(&loose->base, NULL, find_abbrev_len_cb, |
| 513 | &data, &opts); |
| 514 | *out = data.len; |
| 515 | |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | static int count_loose_object(const struct object_id *oid UNUSED, |
| 520 | struct object_info *oi UNUSED, |
| 521 | void *payload) |
| 522 | { |
| 523 | unsigned long *count = payload; |
| 524 | (*count)++; |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int odb_source_loose_count_objects(struct odb_source *source, |
| 529 | enum odb_count_objects_flags flags, |
| 530 | unsigned long *out) |
| 531 | { |
| 532 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 533 | const unsigned hexsz = source->odb->repo->hash_algo->hexsz - 2; |
| 534 | char *path = NULL; |
| 535 | DIR *dir = NULL; |
| 536 | int ret; |
| 537 | |
| 538 | if (flags & ODB_COUNT_OBJECTS_APPROXIMATE) { |
| 539 | unsigned long count = 0; |
| 540 | struct dirent *ent; |
| 541 | |
| 542 | path = xstrfmt("%s/17", source->path); |
| 543 | |
| 544 | dir = opendir(path); |
| 545 | if (!dir) { |
| 546 | if (errno == ENOENT) { |
| 547 | *out = 0; |
| 548 | ret = 0; |
| 549 | goto out; |
| 550 | } |
| 551 | |
| 552 | ret = error_errno("cannot open object shard '%s'", path); |
| 553 | goto out; |
| 554 | } |
| 555 | |
| 556 | while ((ent = readdir(dir)) != NULL) { |
| 557 | if (strspn(ent->d_name, "0123456789abcdef") != hexsz || |
| 558 | ent->d_name[hexsz] != '\0') |
| 559 | continue; |
| 560 | count++; |
| 561 | } |
| 562 | |
| 563 | *out = count * 256; |
| 564 | ret = 0; |
| 565 | } else { |
| 566 | struct odb_for_each_object_options opts = { 0 }; |
| 567 | *out = 0; |
| 568 | ret = odb_source_for_each_object(&loose->base, NULL, count_loose_object, |
| 569 | out, &opts); |
| 570 | } |
| 571 | |
| 572 | out: |
| 573 | if (dir) |
| 574 | closedir(dir); |
| 575 | free(path); |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | static int odb_source_loose_freshen_object(struct odb_source *source, |
| 580 | const struct object_id *oid, |
| 581 | const time_t *mtime) |
| 582 | { |
| 583 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 584 | static struct strbuf path = STRBUF_INIT; |
| 585 | odb_loose_path(loose, &path, oid); |
| 586 | return !!check_and_freshen_file(path.buf, 1, mtime); |
| 587 | } |
| 588 | |
| 589 | /* Finalize a file on disk, and close it. */ |
| 590 | static void close_loose_object(struct odb_source_loose *loose, |
| 591 | int fd, const char *filename) |
| 592 | { |
| 593 | if (loose->base.will_destroy) |
| 594 | goto out; |
| 595 | |
| 596 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
| 597 | odb_transaction_files_fsync(loose->base.odb->transaction, fd, filename); |
| 598 | else if (fsync_object_files > 0) |
| 599 | fsync_or_die(fd, filename); |
| 600 | else |
| 601 | fsync_component_or_die(FSYNC_COMPONENT_LOOSE_OBJECT, fd, |
| 602 | filename); |
| 603 | |
| 604 | out: |
| 605 | if (close(fd) != 0) |
| 606 | die_errno(_("error when closing loose object file")); |
| 607 | } |
| 608 | |
| 609 | /* Size of directory component, including the ending '/' */ |
| 610 | static inline int directory_size(const char *filename) |
| 611 | { |
| 612 | const char *s = strrchr(filename, '/'); |
| 613 | if (!s) |
| 614 | return 0; |
| 615 | return s - filename + 1; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * This creates a temporary file in the same directory as the final |
| 620 | * 'filename' |
| 621 | * |
| 622 | * We want to avoid cross-directory filename renames, because those |
| 623 | * can have problems on various filesystems (FAT, NFS, Coda). |
| 624 | */ |
| 625 | static int create_tmpfile(struct repository *repo, |
| 626 | struct strbuf *tmp, const char *filename) |
| 627 | { |
| 628 | int fd, dirlen = directory_size(filename); |
| 629 | |
| 630 | strbuf_reset(tmp); |
| 631 | strbuf_add(tmp, filename, dirlen); |
| 632 | strbuf_addstr(tmp, "tmp_obj_XXXXXX"); |
| 633 | fd = git_mkstemp_mode(tmp->buf, 0444); |
| 634 | if (fd < 0 && dirlen && errno == ENOENT) { |
| 635 | /* |
| 636 | * Make sure the directory exists; note that the contents |
| 637 | * of the buffer are undefined after mkstemp returns an |
| 638 | * error, so we have to rewrite the whole buffer from |
| 639 | * scratch. |
| 640 | */ |
| 641 | strbuf_reset(tmp); |
| 642 | strbuf_add(tmp, filename, dirlen - 1); |
| 643 | if (mkdir(tmp->buf, 0777) && errno != EEXIST) |
| 644 | return -1; |
| 645 | if (adjust_shared_perm(repo, tmp->buf)) |
| 646 | return -1; |
| 647 | |
| 648 | /* Try again */ |
| 649 | strbuf_addstr(tmp, "/tmp_obj_XXXXXX"); |
| 650 | fd = git_mkstemp_mode(tmp->buf, 0444); |
| 651 | } |
| 652 | return fd; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Common steps for loose object writers to start writing loose |
| 657 | * objects: |
| 658 | * |
| 659 | * - Create tmpfile for the loose object. |
| 660 | * - Setup zlib stream for compression. |
| 661 | * - Start to feed header to zlib stream. |
| 662 | * |
| 663 | * Returns a "fd", which should later be provided to |
| 664 | * end_loose_object_common(). |
| 665 | */ |
| 666 | static int start_loose_object_common(struct odb_source_loose *loose, |
| 667 | struct strbuf *tmp_file, |
| 668 | const char *filename, unsigned flags, |
| 669 | git_zstream *stream, |
| 670 | unsigned char *buf, size_t buflen, |
| 671 | struct git_hash_ctx *c, struct git_hash_ctx *compat_c, |
| 672 | char *hdr, int hdrlen) |
| 673 | { |
| 674 | const struct git_hash_algo *algo = loose->base.odb->repo->hash_algo; |
| 675 | const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; |
| 676 | int fd; |
| 677 | struct repo_config_values *cfg = repo_config_values(loose->base.odb->repo); |
| 678 | |
| 679 | fd = create_tmpfile(loose->base.odb->repo, tmp_file, filename); |
| 680 | if (fd < 0) { |
| 681 | if (flags & ODB_WRITE_OBJECT_SILENT) |
| 682 | return -1; |
| 683 | else if (errno == EACCES) |
| 684 | return error(_("insufficient permission for adding " |
| 685 | "an object to repository database %s"), |
| 686 | loose->base.path); |
| 687 | else |
| 688 | return error_errno( |
| 689 | _("unable to create temporary file")); |
| 690 | } |
| 691 | |
| 692 | /* Setup zlib stream for compression */ |
| 693 | git_deflate_init(stream, cfg->zlib_compression_level); |
| 694 | stream->next_out = buf; |
| 695 | stream->avail_out = buflen; |
| 696 | git_hash_init(c, algo); |
| 697 | if (compat && compat_c) |
| 698 | git_hash_init(compat_c, compat); |
| 699 | |
| 700 | /* Start to feed header to zlib stream */ |
| 701 | stream->next_in = (unsigned char *)hdr; |
| 702 | stream->avail_in = hdrlen; |
| 703 | while (git_deflate(stream, 0) == Z_OK) |
| 704 | ; /* nothing */ |
| 705 | git_hash_update(c, hdr, hdrlen); |
| 706 | if (compat && compat_c) |
| 707 | git_hash_update(compat_c, hdr, hdrlen); |
| 708 | |
| 709 | return fd; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Common steps for the inner git_deflate() loop for writing loose |
| 714 | * objects. Returns what git_deflate() returns. |
| 715 | */ |
| 716 | static int write_loose_object_common(struct odb_source_loose *loose, |
| 717 | struct git_hash_ctx *c, struct git_hash_ctx *compat_c, |
| 718 | git_zstream *stream, const int flush, |
| 719 | unsigned char *in0, const int fd, |
| 720 | unsigned char *compressed, |
| 721 | const size_t compressed_len) |
| 722 | { |
| 723 | const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; |
| 724 | int ret; |
| 725 | |
| 726 | ret = git_deflate(stream, flush ? Z_FINISH : 0); |
| 727 | git_hash_update(c, in0, stream->next_in - in0); |
| 728 | if (compat && compat_c) |
| 729 | git_hash_update(compat_c, in0, stream->next_in - in0); |
| 730 | if (write_in_full(fd, compressed, stream->next_out - compressed) < 0) |
| 731 | die_errno(_("unable to write loose object file")); |
| 732 | stream->next_out = compressed; |
| 733 | stream->avail_out = compressed_len; |
| 734 | |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Common steps for loose object writers to end writing loose objects: |
| 740 | * |
| 741 | * - End the compression of zlib stream. |
| 742 | * - Get the calculated oid to "oid". |
| 743 | */ |
| 744 | static int end_loose_object_common(struct odb_source_loose *loose, |
| 745 | struct git_hash_ctx *c, struct git_hash_ctx *compat_c, |
| 746 | git_zstream *stream, struct object_id *oid, |
| 747 | struct object_id *compat_oid) |
| 748 | { |
| 749 | const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; |
| 750 | int ret; |
| 751 | |
| 752 | ret = git_deflate_end_gently(stream); |
| 753 | if (ret != Z_OK) |
| 754 | return ret; |
| 755 | git_hash_final_oid(oid, c); |
| 756 | if (compat && compat_c) |
| 757 | git_hash_final_oid(compat_oid, compat_c); |
| 758 | |
| 759 | return Z_OK; |
| 760 | } |
| 761 | |
| 762 | static int write_loose_object(struct odb_source_loose *loose, |
| 763 | const struct object_id *oid, char *hdr, |
| 764 | int hdrlen, const void *buf, unsigned long len, |
| 765 | const time_t *mtime, unsigned flags) |
| 766 | { |
| 767 | int fd, ret; |
| 768 | unsigned char compressed[4096]; |
| 769 | git_zstream stream; |
| 770 | struct git_hash_ctx c; |
| 771 | struct object_id parano_oid; |
| 772 | static struct strbuf tmp_file = STRBUF_INIT; |
| 773 | static struct strbuf filename = STRBUF_INIT; |
| 774 | |
| 775 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
| 776 | odb_transaction_files_prepare(loose->base.odb->transaction); |
| 777 | |
| 778 | odb_loose_path(loose, &filename, oid); |
| 779 | |
| 780 | fd = start_loose_object_common(loose, &tmp_file, filename.buf, flags, |
| 781 | &stream, compressed, sizeof(compressed), |
| 782 | &c, NULL, hdr, hdrlen); |
| 783 | if (fd < 0) |
| 784 | return -1; |
| 785 | |
| 786 | /* Then the data itself.. */ |
| 787 | stream.next_in = (void *)buf; |
| 788 | stream.avail_in = len; |
| 789 | do { |
| 790 | unsigned char *in0 = stream.next_in; |
| 791 | |
| 792 | ret = write_loose_object_common(loose, &c, NULL, &stream, 1, in0, fd, |
| 793 | compressed, sizeof(compressed)); |
| 794 | } while (ret == Z_OK); |
| 795 | |
| 796 | if (ret != Z_STREAM_END) |
| 797 | die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid), |
| 798 | ret); |
| 799 | ret = end_loose_object_common(loose, &c, NULL, &stream, ¶no_oid, NULL); |
| 800 | if (ret != Z_OK) |
| 801 | die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid), |
| 802 | ret); |
| 803 | if (!oideq(oid, ¶no_oid)) |
| 804 | die(_("confused by unstable object source data for %s"), |
| 805 | oid_to_hex(oid)); |
| 806 | |
| 807 | close_loose_object(loose, fd, tmp_file.buf); |
| 808 | |
| 809 | if (mtime) { |
| 810 | struct utimbuf utb = { |
| 811 | .actime = *mtime, |
| 812 | .modtime = *mtime, |
| 813 | }; |
| 814 | |
| 815 | if (utime(tmp_file.buf, &utb) < 0 && |
| 816 | !(flags & ODB_WRITE_OBJECT_SILENT)) |
| 817 | warning_errno(_("failed utime() on %s"), tmp_file.buf); |
| 818 | } |
| 819 | |
| 820 | return finalize_object_file_flags(loose->base.odb->repo, tmp_file.buf, filename.buf, |
| 821 | FOF_SKIP_COLLISION_CHECK); |
| 822 | } |
| 823 | |
| 824 | static int odb_source_loose_write_object(struct odb_source *source, |
| 825 | const void *buf, size_t len, |
| 826 | enum object_type type, |
| 827 | const struct object_id *oid, |
| 828 | const struct object_id *compat_oid, |
| 829 | const time_t *mtime, |
| 830 | enum odb_write_object_flags flags) |
| 831 | { |
| 832 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 833 | char hdr[MAX_HEADER_LEN]; |
| 834 | int hdrlen; |
| 835 | |
| 836 | hdrlen = format_object_header(hdr, sizeof(hdr), type, len); |
| 837 | |
| 838 | if (write_loose_object(loose, oid, hdr, hdrlen, buf, len, mtime, flags)) |
| 839 | return -1; |
| 840 | |
| 841 | if (compat_oid) |
| 842 | return repo_add_loose_object_map(loose, oid, compat_oid); |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | static int odb_source_loose_write_object_stream(struct odb_source *source, |
| 848 | struct odb_write_stream *in_stream, |
| 849 | size_t len, |
| 850 | struct object_id *oid) |
| 851 | { |
| 852 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 853 | const struct git_hash_algo *compat = loose->base.odb->repo->compat_hash_algo; |
| 854 | struct object_id compat_oid; |
| 855 | int fd, ret, err = 0, flush = 0; |
| 856 | unsigned char compressed[4096]; |
| 857 | git_zstream stream; |
| 858 | struct git_hash_ctx c, compat_c; |
| 859 | struct strbuf tmp_file = STRBUF_INIT; |
| 860 | struct strbuf filename = STRBUF_INIT; |
| 861 | unsigned char buf[8192]; |
| 862 | int dirlen; |
| 863 | char hdr[MAX_HEADER_LEN]; |
| 864 | int hdrlen; |
| 865 | |
| 866 | if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT)) |
| 867 | odb_transaction_files_prepare(loose->base.odb->transaction); |
| 868 | |
| 869 | /* Since oid is not determined, save tmp file to odb path. */ |
| 870 | strbuf_addf(&filename, "%s/", loose->base.path); |
| 871 | hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len); |
| 872 | |
| 873 | /* |
| 874 | * Common steps for write_loose_object and stream_loose_object to |
| 875 | * start writing loose objects: |
| 876 | * |
| 877 | * - Create tmpfile for the loose object. |
| 878 | * - Setup zlib stream for compression. |
| 879 | * - Start to feed header to zlib stream. |
| 880 | */ |
| 881 | fd = start_loose_object_common(loose, &tmp_file, filename.buf, 0, |
| 882 | &stream, compressed, sizeof(compressed), |
| 883 | &c, &compat_c, hdr, hdrlen); |
| 884 | if (fd < 0) { |
| 885 | err = -1; |
| 886 | goto cleanup; |
| 887 | } |
| 888 | |
| 889 | /* Then the data itself.. */ |
| 890 | do { |
| 891 | unsigned char *in0 = stream.next_in; |
| 892 | |
| 893 | if (!stream.avail_in && !in_stream->is_finished) { |
| 894 | ssize_t read_len = odb_write_stream_read(in_stream, buf, |
| 895 | sizeof(buf)); |
| 896 | if (read_len < 0) { |
| 897 | close(fd); |
| 898 | err = -1; |
| 899 | goto cleanup; |
| 900 | } |
| 901 | |
| 902 | stream.avail_in = read_len; |
| 903 | stream.next_in = buf; |
| 904 | in0 = buf; |
| 905 | /* All data has been read. */ |
| 906 | if (in_stream->is_finished) |
| 907 | flush = 1; |
| 908 | } |
| 909 | ret = write_loose_object_common(loose, &c, &compat_c, &stream, flush, in0, fd, |
| 910 | compressed, sizeof(compressed)); |
| 911 | /* |
| 912 | * Unlike write_loose_object(), we do not have the entire |
| 913 | * buffer. If we get Z_BUF_ERROR due to too few input bytes, |
| 914 | * then we'll replenish them in the next input_stream->read() |
| 915 | * call when we loop. |
| 916 | */ |
| 917 | } while (ret == Z_OK || ret == Z_BUF_ERROR); |
| 918 | |
| 919 | if (stream.total_in != len + hdrlen) |
| 920 | die(_("write stream object %"PRIuMAX" != %"PRIuMAX), (uintmax_t)stream.total_in, |
| 921 | (uintmax_t)len + hdrlen); |
| 922 | |
| 923 | /* |
| 924 | * Common steps for write_loose_object and stream_loose_object to |
| 925 | * end writing loose object: |
| 926 | * |
| 927 | * - End the compression of zlib stream. |
| 928 | * - Get the calculated oid. |
| 929 | */ |
| 930 | if (ret != Z_STREAM_END) |
| 931 | die(_("unable to stream deflate new object (%d)"), ret); |
| 932 | ret = end_loose_object_common(loose, &c, &compat_c, &stream, oid, &compat_oid); |
| 933 | if (ret != Z_OK) |
| 934 | die(_("deflateEnd on stream object failed (%d)"), ret); |
| 935 | close_loose_object(loose, fd, tmp_file.buf); |
| 936 | |
| 937 | if (odb_freshen_object(loose->base.odb, oid)) { |
| 938 | unlink_or_warn(tmp_file.buf); |
| 939 | goto cleanup; |
| 940 | } |
| 941 | odb_loose_path(loose, &filename, oid); |
| 942 | |
| 943 | /* We finally know the object path, and create the missing dir. */ |
| 944 | dirlen = directory_size(filename.buf); |
| 945 | if (dirlen) { |
| 946 | struct strbuf dir = STRBUF_INIT; |
| 947 | strbuf_add(&dir, filename.buf, dirlen); |
| 948 | |
| 949 | if (safe_create_dir_in_gitdir(loose->base.odb->repo, dir.buf) && |
| 950 | errno != EEXIST) { |
| 951 | err = error_errno(_("unable to create directory %s"), dir.buf); |
| 952 | strbuf_release(&dir); |
| 953 | goto cleanup; |
| 954 | } |
| 955 | strbuf_release(&dir); |
| 956 | } |
| 957 | |
| 958 | err = finalize_object_file_flags(loose->base.odb->repo, tmp_file.buf, filename.buf, |
| 959 | FOF_SKIP_COLLISION_CHECK); |
| 960 | if (!err && compat) |
| 961 | err = repo_add_loose_object_map(loose, oid, &compat_oid); |
| 962 | cleanup: |
| 963 | strbuf_release(&tmp_file); |
| 964 | strbuf_release(&filename); |
| 965 | return err; |
| 966 | } |
| 967 | |
| 968 | static int odb_source_loose_begin_transaction(struct odb_source *source UNUSED, |
| 969 | struct odb_transaction **out UNUSED, |
| 970 | enum odb_transaction_flags flags UNUSED) |
| 971 | { |
| 972 | /* TODO: this is a known omission that we'll want to address eventually. */ |
| 973 | return error("loose source does not support transactions"); |
| 974 | } |
| 975 | |
| 976 | static int odb_source_loose_read_alternates(struct odb_source *source UNUSED, |
| 977 | struct strvec *out UNUSED) |
| 978 | { |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int odb_source_loose_write_alternate(struct odb_source *source UNUSED, |
| 983 | const char *alternate UNUSED) |
| 984 | { |
| 985 | return error("loose source does not support alternates"); |
| 986 | } |
| 987 | |
| 988 | static void odb_source_loose_clear_cache(struct odb_source_loose *loose) |
| 989 | { |
| 990 | oidtree_clear(loose->cache); |
| 991 | FREE_AND_NULL(loose->cache); |
| 992 | memset(&loose->subdir_seen, 0, |
| 993 | sizeof(loose->subdir_seen)); |
| 994 | } |
| 995 | |
| 996 | static void odb_source_loose_prepare(struct odb_source *source, |
| 997 | enum odb_prepare_flags flags) |
| 998 | { |
| 999 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 1000 | if (flags & ODB_PREPARE_FLUSH_CACHES) |
| 1001 | odb_source_loose_clear_cache(loose); |
| 1002 | } |
| 1003 | |
| 1004 | static void odb_source_loose_close(struct odb_source *source UNUSED) |
| 1005 | { |
| 1006 | /* Nothing to do. */ |
| 1007 | } |
| 1008 | |
| 1009 | static void odb_source_loose_reparent(const char *name UNUSED, |
| 1010 | const char *old_cwd, |
| 1011 | const char *new_cwd, |
| 1012 | void *cb_data) |
| 1013 | { |
| 1014 | struct odb_source_loose *loose = cb_data; |
| 1015 | char *path = reparent_relative_path(old_cwd, new_cwd, |
| 1016 | loose->base.path); |
| 1017 | free(loose->base.path); |
| 1018 | loose->base.path = path; |
| 1019 | } |
| 1020 | |
| 1021 | static void odb_source_loose_free(struct odb_source *source) |
| 1022 | { |
| 1023 | struct odb_source_loose *loose = odb_source_loose_downcast(source); |
| 1024 | odb_source_loose_clear_cache(loose); |
| 1025 | loose_object_map_clear(&loose->map); |
| 1026 | chdir_notify_unregister(NULL, odb_source_loose_reparent, loose); |
| 1027 | odb_source_release(&loose->base); |
| 1028 | free(loose); |
| 1029 | } |
| 1030 | |
| 1031 | struct odb_source_loose *odb_source_loose_new(struct object_database *odb, |
| 1032 | const char *path, |
| 1033 | bool local) |
| 1034 | { |
| 1035 | struct odb_source_loose *loose; |
| 1036 | |
| 1037 | CALLOC_ARRAY(loose, 1); |
| 1038 | odb_source_init(&loose->base, odb, ODB_SOURCE_LOOSE, path, local); |
| 1039 | |
| 1040 | loose->base.free = odb_source_loose_free; |
| 1041 | loose->base.close = odb_source_loose_close; |
| 1042 | loose->base.prepare = odb_source_loose_prepare; |
| 1043 | loose->base.read_object_info = odb_source_loose_read_object_info; |
| 1044 | loose->base.read_object_stream = odb_source_loose_read_object_stream; |
| 1045 | loose->base.for_each_object = odb_source_loose_for_each_object; |
| 1046 | loose->base.find_abbrev_len = odb_source_loose_find_abbrev_len; |
| 1047 | loose->base.count_objects = odb_source_loose_count_objects; |
| 1048 | loose->base.freshen_object = odb_source_loose_freshen_object; |
| 1049 | loose->base.write_object = odb_source_loose_write_object; |
| 1050 | loose->base.write_object_stream = odb_source_loose_write_object_stream; |
| 1051 | loose->base.begin_transaction = odb_source_loose_begin_transaction; |
| 1052 | loose->base.read_alternates = odb_source_loose_read_alternates; |
| 1053 | loose->base.write_alternate = odb_source_loose_write_alternate; |
| 1054 | |
| 1055 | if (!is_absolute_path(loose->base.path)) |
| 1056 | chdir_notify_register(NULL, odb_source_loose_reparent, loose); |
| 1057 | |
| 1058 | loose_object_map_load(loose); |
| 1059 | |
| 1060 | return loose; |
| 1061 | } |