Code

index-pack: --verify
[git.git] / pack-write.c
1 #include "cache.h"
2 #include "pack.h"
3 #include "csum-file.h"
5 void reset_pack_idx_option(struct pack_idx_option *opts)
6 {
7         memset(opts, 0, sizeof(*opts));
8         opts->version = 2;
9         opts->off32_limit = 0x7fffffff;
10 }
12 static int sha1_compare(const void *_a, const void *_b)
13 {
14         struct pack_idx_entry *a = *(struct pack_idx_entry **)_a;
15         struct pack_idx_entry *b = *(struct pack_idx_entry **)_b;
16         return hashcmp(a->sha1, b->sha1);
17 }
19 /*
20  * On entry *sha1 contains the pack content SHA1 hash, on exit it is
21  * the SHA1 hash of sorted object names. The objects array passed in
22  * will be sorted by SHA1 on exit.
23  */
24 const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
25                            int nr_objects, const struct pack_idx_option *opts,
26                            unsigned char *sha1)
27 {
28         struct sha1file *f;
29         struct pack_idx_entry **sorted_by_sha, **list, **last;
30         off_t last_obj_offset = 0;
31         uint32_t array[256];
32         int i, fd;
33         git_SHA_CTX ctx;
34         uint32_t index_version;
36         if (nr_objects) {
37                 sorted_by_sha = objects;
38                 list = sorted_by_sha;
39                 last = sorted_by_sha + nr_objects;
40                 for (i = 0; i < nr_objects; ++i) {
41                         if (objects[i]->offset > last_obj_offset)
42                                 last_obj_offset = objects[i]->offset;
43                 }
44                 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
45                       sha1_compare);
46         }
47         else
48                 sorted_by_sha = list = last = NULL;
50         if (opts->flags & WRITE_IDX_VERIFY) {
51                 assert(index_name);
52                 f = sha1fd_check(index_name);
53         } else {
54                 if (!index_name) {
55                         static char tmpfile[PATH_MAX];
56                         fd = odb_mkstemp(tmpfile, sizeof(tmpfile), "pack/tmp_idx_XXXXXX");
57                         index_name = xstrdup(tmpfile);
58                 } else {
59                         unlink(index_name);
60                         fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
61                 }
62                 if (fd < 0)
63                         die_errno("unable to create '%s'", index_name);
64                 f = sha1fd(fd, index_name);
65         }
67         /* if last object's offset is >= 2^31 we should use index V2 */
68         index_version = (last_obj_offset >> 31) ? 2 : opts->version;
70         /* index versions 2 and above need a header */
71         if (index_version >= 2) {
72                 struct pack_idx_header hdr;
73                 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
74                 hdr.idx_version = htonl(index_version);
75                 sha1write(f, &hdr, sizeof(hdr));
76         }
78         /*
79          * Write the first-level table (the list is sorted,
80          * but we use a 256-entry lookup to be able to avoid
81          * having to do eight extra binary search iterations).
82          */
83         for (i = 0; i < 256; i++) {
84                 struct pack_idx_entry **next = list;
85                 while (next < last) {
86                         struct pack_idx_entry *obj = *next;
87                         if (obj->sha1[0] != i)
88                                 break;
89                         next++;
90                 }
91                 array[i] = htonl(next - sorted_by_sha);
92                 list = next;
93         }
94         sha1write(f, array, 256 * 4);
96         /* compute the SHA1 hash of sorted object names. */
97         git_SHA1_Init(&ctx);
99         /*
100          * Write the actual SHA1 entries..
101          */
102         list = sorted_by_sha;
103         for (i = 0; i < nr_objects; i++) {
104                 struct pack_idx_entry *obj = *list++;
105                 if (index_version < 2) {
106                         uint32_t offset = htonl(obj->offset);
107                         sha1write(f, &offset, 4);
108                 }
109                 sha1write(f, obj->sha1, 20);
110                 git_SHA1_Update(&ctx, obj->sha1, 20);
111         }
113         if (index_version >= 2) {
114                 unsigned int nr_large_offset = 0;
116                 /* write the crc32 table */
117                 list = sorted_by_sha;
118                 for (i = 0; i < nr_objects; i++) {
119                         struct pack_idx_entry *obj = *list++;
120                         uint32_t crc32_val = htonl(obj->crc32);
121                         sha1write(f, &crc32_val, 4);
122                 }
124                 /* write the 32-bit offset table */
125                 list = sorted_by_sha;
126                 for (i = 0; i < nr_objects; i++) {
127                         struct pack_idx_entry *obj = *list++;
128                         uint32_t offset = (obj->offset <= opts->off32_limit) ?
129                                 obj->offset : (0x80000000 | nr_large_offset++);
130                         offset = htonl(offset);
131                         sha1write(f, &offset, 4);
132                 }
134                 /* write the large offset table */
135                 list = sorted_by_sha;
136                 while (nr_large_offset) {
137                         struct pack_idx_entry *obj = *list++;
138                         uint64_t offset = obj->offset;
139                         if (offset > opts->off32_limit) {
140                                 uint32_t split[2];
141                                 split[0] = htonl(offset >> 32);
142                                 split[1] = htonl(offset & 0xffffffff);
143                                 sha1write(f, split, 8);
144                                 nr_large_offset--;
145                         }
146                 }
147         }
149         sha1write(f, sha1, 20);
150         sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
151                             ? CSUM_CLOSE : CSUM_FSYNC));
152         git_SHA1_Final(sha1, &ctx);
153         return index_name;
156 /*
157  * Update pack header with object_count and compute new SHA1 for pack data
158  * associated to pack_fd, and write that SHA1 at the end.  That new SHA1
159  * is also returned in new_pack_sha1.
160  *
161  * If partial_pack_sha1 is non null, then the SHA1 of the existing pack
162  * (without the header update) is computed and validated against the
163  * one provided in partial_pack_sha1.  The validation is performed at
164  * partial_pack_offset bytes in the pack file.  The SHA1 of the remaining
165  * data (i.e. from partial_pack_offset to the end) is then computed and
166  * returned in partial_pack_sha1.
167  *
168  * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and
169  * partial_pack_sha1 can refer to the same buffer if the caller is not
170  * interested in the resulting SHA1 of pack data above partial_pack_offset.
171  */
172 void fixup_pack_header_footer(int pack_fd,
173                          unsigned char *new_pack_sha1,
174                          const char *pack_name,
175                          uint32_t object_count,
176                          unsigned char *partial_pack_sha1,
177                          off_t partial_pack_offset)
179         int aligned_sz, buf_sz = 8 * 1024;
180         git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
181         struct pack_header hdr;
182         char *buf;
184         git_SHA1_Init(&old_sha1_ctx);
185         git_SHA1_Init(&new_sha1_ctx);
187         if (lseek(pack_fd, 0, SEEK_SET) != 0)
188                 die_errno("Failed seeking to start of '%s'", pack_name);
189         if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
190                 die_errno("Unable to reread header of '%s'", pack_name);
191         if (lseek(pack_fd, 0, SEEK_SET) != 0)
192                 die_errno("Failed seeking to start of '%s'", pack_name);
193         git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
194         hdr.hdr_entries = htonl(object_count);
195         git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
196         write_or_die(pack_fd, &hdr, sizeof(hdr));
197         partial_pack_offset -= sizeof(hdr);
199         buf = xmalloc(buf_sz);
200         aligned_sz = buf_sz - sizeof(hdr);
201         for (;;) {
202                 ssize_t m, n;
203                 m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
204                         partial_pack_offset : aligned_sz;
205                 n = xread(pack_fd, buf, m);
206                 if (!n)
207                         break;
208                 if (n < 0)
209                         die_errno("Failed to checksum '%s'", pack_name);
210                 git_SHA1_Update(&new_sha1_ctx, buf, n);
212                 aligned_sz -= n;
213                 if (!aligned_sz)
214                         aligned_sz = buf_sz;
216                 if (!partial_pack_sha1)
217                         continue;
219                 git_SHA1_Update(&old_sha1_ctx, buf, n);
220                 partial_pack_offset -= n;
221                 if (partial_pack_offset == 0) {
222                         unsigned char sha1[20];
223                         git_SHA1_Final(sha1, &old_sha1_ctx);
224                         if (hashcmp(sha1, partial_pack_sha1) != 0)
225                                 die("Unexpected checksum for %s "
226                                     "(disk corruption?)", pack_name);
227                         /*
228                          * Now let's compute the SHA1 of the remainder of the
229                          * pack, which also means making partial_pack_offset
230                          * big enough not to matter anymore.
231                          */
232                         git_SHA1_Init(&old_sha1_ctx);
233                         partial_pack_offset = ~partial_pack_offset;
234                         partial_pack_offset -= MSB(partial_pack_offset, 1);
235                 }
236         }
237         free(buf);
239         if (partial_pack_sha1)
240                 git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
241         git_SHA1_Final(new_pack_sha1, &new_sha1_ctx);
242         write_or_die(pack_fd, new_pack_sha1, 20);
243         fsync_or_die(pack_fd, pack_name);
246 char *index_pack_lockfile(int ip_out)
248         char packname[46];
250         /*
251          * The first thing we expect from index-pack's output
252          * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
253          * %40s is the newly created pack SHA1 name.  In the "keep"
254          * case, we need it to remove the corresponding .keep file
255          * later on.  If we don't get that then tough luck with it.
256          */
257         if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
258             memcmp(packname, "keep\t", 5) == 0) {
259                 char path[PATH_MAX];
260                 packname[45] = 0;
261                 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
262                          get_object_directory(), packname + 5);
263                 return xstrdup(path);
264         }
265         return NULL;
268 /*
269  * The per-object header is a pretty dense thing, which is
270  *  - first byte: low four bits are "size", then three bits of "type",
271  *    and the high bit is "size continues".
272  *  - each byte afterwards: low seven bits are size continuation,
273  *    with the high bit being "size continues"
274  */
275 int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
277         int n = 1;
278         unsigned char c;
280         if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
281                 die("bad type %d", type);
283         c = (type << 4) | (size & 15);
284         size >>= 4;
285         while (size) {
286                 *hdr++ = c | 0x80;
287                 c = size & 0x7f;
288                 size >>= 7;
289                 n++;
290         }
291         *hdr = c;
292         return n;