Merge branch 'jc/submittingpatches'
[git] / pack-write.c
1 #include "cache.h"
2 #include "pack.h"
3 #include "csum-file.h"
4
5 uint32_t pack_idx_default_version = 1;
6 uint32_t pack_idx_off32_limit = 0x7fffffff;
7
8 static int sha1_compare(const void *_a, const void *_b)
9 {
10         struct pack_idx_entry *a = *(struct pack_idx_entry **)_a;
11         struct pack_idx_entry *b = *(struct pack_idx_entry **)_b;
12         return hashcmp(a->sha1, b->sha1);
13 }
14
15 /*
16  * On entry *sha1 contains the pack content SHA1 hash, on exit it is
17  * the SHA1 hash of sorted object names. The objects array passed in
18  * will be sorted by SHA1 on exit.
19  */
20 char *write_idx_file(char *index_name, struct pack_idx_entry **objects,
21                      int nr_objects, unsigned char *sha1)
22 {
23         struct sha1file *f;
24         struct pack_idx_entry **sorted_by_sha, **list, **last;
25         off_t last_obj_offset = 0;
26         uint32_t array[256];
27         int i, fd;
28         SHA_CTX ctx;
29         uint32_t index_version;
30
31         if (nr_objects) {
32                 sorted_by_sha = objects;
33                 list = sorted_by_sha;
34                 last = sorted_by_sha + nr_objects;
35                 for (i = 0; i < nr_objects; ++i) {
36                         if (objects[i]->offset > last_obj_offset)
37                                 last_obj_offset = objects[i]->offset;
38                 }
39                 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
40                       sha1_compare);
41         }
42         else
43                 sorted_by_sha = list = last = NULL;
44
45         if (!index_name) {
46                 static char tmpfile[PATH_MAX];
47                 snprintf(tmpfile, sizeof(tmpfile),
48                          "%s/tmp_idx_XXXXXX", get_object_directory());
49                 fd = xmkstemp(tmpfile);
50                 index_name = xstrdup(tmpfile);
51         } else {
52                 unlink(index_name);
53                 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
54         }
55         if (fd < 0)
56                 die("unable to create %s: %s", index_name, strerror(errno));
57         f = sha1fd(fd, index_name);
58
59         /* if last object's offset is >= 2^31 we should use index V2 */
60         index_version = (last_obj_offset >> 31) ? 2 : pack_idx_default_version;
61
62         /* index versions 2 and above need a header */
63         if (index_version >= 2) {
64                 struct pack_idx_header hdr;
65                 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
66                 hdr.idx_version = htonl(index_version);
67                 sha1write(f, &hdr, sizeof(hdr));
68         }
69
70         /*
71          * Write the first-level table (the list is sorted,
72          * but we use a 256-entry lookup to be able to avoid
73          * having to do eight extra binary search iterations).
74          */
75         for (i = 0; i < 256; i++) {
76                 struct pack_idx_entry **next = list;
77                 while (next < last) {
78                         struct pack_idx_entry *obj = *next;
79                         if (obj->sha1[0] != i)
80                                 break;
81                         next++;
82                 }
83                 array[i] = htonl(next - sorted_by_sha);
84                 list = next;
85         }
86         sha1write(f, array, 256 * 4);
87
88         /* compute the SHA1 hash of sorted object names. */
89         SHA1_Init(&ctx);
90
91         /*
92          * Write the actual SHA1 entries..
93          */
94         list = sorted_by_sha;
95         for (i = 0; i < nr_objects; i++) {
96                 struct pack_idx_entry *obj = *list++;
97                 if (index_version < 2) {
98                         uint32_t offset = htonl(obj->offset);
99                         sha1write(f, &offset, 4);
100                 }
101                 sha1write(f, obj->sha1, 20);
102                 SHA1_Update(&ctx, obj->sha1, 20);
103         }
104
105         if (index_version >= 2) {
106                 unsigned int nr_large_offset = 0;
107
108                 /* write the crc32 table */
109                 list = sorted_by_sha;
110                 for (i = 0; i < nr_objects; i++) {
111                         struct pack_idx_entry *obj = *list++;
112                         uint32_t crc32_val = htonl(obj->crc32);
113                         sha1write(f, &crc32_val, 4);
114                 }
115
116                 /* write the 32-bit offset table */
117                 list = sorted_by_sha;
118                 for (i = 0; i < nr_objects; i++) {
119                         struct pack_idx_entry *obj = *list++;
120                         uint32_t offset = (obj->offset <= pack_idx_off32_limit) ?
121                                 obj->offset : (0x80000000 | nr_large_offset++);
122                         offset = htonl(offset);
123                         sha1write(f, &offset, 4);
124                 }
125
126                 /* write the large offset table */
127                 list = sorted_by_sha;
128                 while (nr_large_offset) {
129                         struct pack_idx_entry *obj = *list++;
130                         uint64_t offset = obj->offset;
131                         if (offset > pack_idx_off32_limit) {
132                                 uint32_t split[2];
133                                 split[0] = htonl(offset >> 32);
134                                 split[1] = htonl(offset & 0xffffffff);
135                                 sha1write(f, split, 8);
136                                 nr_large_offset--;
137                         }
138                 }
139         }
140
141         sha1write(f, sha1, 20);
142         sha1close(f, NULL, 1);
143         SHA1_Final(sha1, &ctx);
144         return index_name;
145 }
146
147 void fixup_pack_header_footer(int pack_fd,
148                          unsigned char *pack_file_sha1,
149                          const char *pack_name,
150                          uint32_t object_count)
151 {
152         static const int buf_sz = 128 * 1024;
153         SHA_CTX c;
154         struct pack_header hdr;
155         char *buf;
156
157         if (lseek(pack_fd, 0, SEEK_SET) != 0)
158                 die("Failed seeking to start: %s", strerror(errno));
159         if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
160                 die("Unable to reread header of %s: %s", pack_name, strerror(errno));
161         if (lseek(pack_fd, 0, SEEK_SET) != 0)
162                 die("Failed seeking to start: %s", strerror(errno));
163         hdr.hdr_entries = htonl(object_count);
164         write_or_die(pack_fd, &hdr, sizeof(hdr));
165
166         SHA1_Init(&c);
167         SHA1_Update(&c, &hdr, sizeof(hdr));
168
169         buf = xmalloc(buf_sz);
170         for (;;) {
171                 ssize_t n = xread(pack_fd, buf, buf_sz);
172                 if (!n)
173                         break;
174                 if (n < 0)
175                         die("Failed to checksum %s: %s", pack_name, strerror(errno));
176                 SHA1_Update(&c, buf, n);
177         }
178         free(buf);
179
180         SHA1_Final(pack_file_sha1, &c);
181         write_or_die(pack_fd, pack_file_sha1, 20);
182 }
183
184 char *index_pack_lockfile(int ip_out)
185 {
186         int len, s;
187         char packname[46];
188
189         /*
190          * The first thing we expects from index-pack's output
191          * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
192          * %40s is the newly created pack SHA1 name.  In the "keep"
193          * case, we need it to remove the corresponding .keep file
194          * later on.  If we don't get that then tough luck with it.
195          */
196         for (len = 0;
197                  len < 46 && (s = xread(ip_out, packname+len, 46-len)) > 0;
198                  len += s);
199         if (len == 46 && packname[45] == '\n' &&
200                 memcmp(packname, "keep\t", 5) == 0) {
201                 char path[PATH_MAX];
202                 packname[45] = 0;
203                 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
204                          get_object_directory(), packname + 5);
205                 return xstrdup(path);
206         }
207         return NULL;
208 }