* The *sha1 contains the pack content SHA1 hash. * The objects array passed in will be sorted by SHA1 on exit. */
| 55 | * The objects array passed in will be sorted by SHA1 on exit. |
| 56 | */ |
| 57 | const char *write_idx_file(struct repository *repo, |
| 58 | const char *index_name, struct pack_idx_entry **objects, |
| 59 | int nr_objects, const struct pack_idx_option *opts, |
| 60 | const unsigned char *sha1) |
| 61 | { |
| 62 | struct hashfile *f; |
| 63 | struct pack_idx_entry **sorted_by_sha, **list, **last; |
| 64 | off_t last_obj_offset = 0; |
| 65 | int i, fd; |
| 66 | uint32_t index_version; |
| 67 | |
| 68 | if (nr_objects) { |
| 69 | sorted_by_sha = objects; |
| 70 | list = sorted_by_sha; |
| 71 | last = sorted_by_sha + nr_objects; |
| 72 | for (i = 0; i < nr_objects; ++i) { |
| 73 | if (objects[i]->offset > last_obj_offset) |
| 74 | last_obj_offset = objects[i]->offset; |
| 75 | } |
| 76 | QSORT(sorted_by_sha, nr_objects, sha1_compare); |
| 77 | } |
| 78 | else |
| 79 | sorted_by_sha = list = last = NULL; |
| 80 | |
| 81 | if (opts->flags & WRITE_IDX_VERIFY) { |
| 82 | assert(index_name); |
| 83 | f = hashfd_check(repo->hash_algo, index_name); |
| 84 | } else { |
| 85 | if (!index_name) { |
| 86 | struct strbuf tmp_file = STRBUF_INIT; |
| 87 | fd = odb_mkstemp(repo->objects, &tmp_file, |
| 88 | "pack/tmp_idx_XXXXXX"); |
| 89 | index_name = strbuf_detach(&tmp_file, NULL); |
| 90 | } else { |
| 91 | unlink(index_name); |
| 92 | fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); |
| 93 | } |
| 94 | f = hashfd(repo->hash_algo, fd, index_name); |
| 95 | } |
| 96 | |
| 97 | /* if last object's offset is >= 2^31 we should use index V2 */ |
| 98 | index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version; |
| 99 | |
| 100 | /* index versions 2 and above need a header */ |
| 101 | if (index_version >= 2) { |
| 102 | struct pack_idx_header hdr; |
| 103 | hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); |
| 104 | hdr.idx_version = htonl(index_version); |
| 105 | hashwrite(f, &hdr, sizeof(hdr)); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Write the first-level table (the list is sorted, |
| 110 | * but we use a 256-entry lookup to be able to avoid |
| 111 | * having to do eight extra binary search iterations). |
| 112 | */ |
| 113 | for (i = 0; i < 256; i++) { |
| 114 | struct pack_idx_entry **next = list; |
no test coverage detected