]> git.ipfire.org Git - thirdparty/git.git/blame - unpack-objects.c
git-pack-objects: write the pack files with a SHA1 csum
[thirdparty/git.git] / unpack-objects.c
CommitLineData
bad50dc8 1#include "cache.h"
74536958 2#include "object.h"
8ee378a0 3#include "delta.h"
bad50dc8 4
74536958 5static int dry_run;
bad50dc8
LT
6static int nr_entries;
7static const char *base_name;
8static const char unpack_usage[] = "git-unpack-objects basename";
9
10struct pack_entry {
8ee378a0 11 unsigned int offset; /* network byte order */
bad50dc8
LT
12 unsigned char sha1[20];
13};
14
74536958
LT
15static void *pack_base;
16static unsigned long pack_size;
8ee378a0
JH
17static void *index_base;
18static unsigned long index_size;
74536958 19
bad50dc8
LT
20static struct pack_entry **pack_list;
21
22static void *map_file(const char *suffix, unsigned long *sizep)
23{
24 static char pathname[PATH_MAX];
25 unsigned long len;
26 int fd;
27 struct stat st;
28 void *map;
29
30 len = snprintf(pathname, PATH_MAX, "%s.%s", base_name, suffix);
31 if (len >= PATH_MAX)
32 die("bad pack base-name");
33 fd = open(pathname, O_RDONLY);
34 if (fd < 0 || fstat(fd, &st))
35 die("unable to open '%s'", pathname);
36 len = st.st_size;
37 if (!len)
38 die("bad pack file '%s'", pathname);
39 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
40 if (-1 == (int)(long)map)
41 die("unable to mmap '%s'", pathname);
42 close(fd);
43 *sizep = len;
44 return map;
45}
46
47static int sort_by_offset(const void *_a, const void *_b)
48{
49 struct pack_entry *a = *(struct pack_entry **)_a;
50 struct pack_entry *b = *(struct pack_entry **)_b;
51 unsigned int o1, o2;
52
53 o1 = ntohl(a->offset);
54 o2 = ntohl(b->offset);
55 return o1 < o2 ? -1 : 1;
56}
57
8ee378a0 58static int check_index(void)
bad50dc8 59{
8ee378a0 60 unsigned int *array = index_base;
bad50dc8
LT
61 unsigned int nr;
62 int i;
63
c38138cd 64 if (index_size < 4*256 + 20)
bad50dc8
LT
65 return error("index file too small");
66 nr = 0;
67 for (i = 0; i < 256; i++) {
68 unsigned int n = ntohl(array[i]);
69 if (n < nr)
70 return error("non-monotonic index");
71 nr = n;
72 }
c38138cd
LT
73 /*
74 * Total size:
75 * - 256 index entries 4 bytes each
76 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
77 * - 20-byte SHA1 file checksum
78 */
79 if (index_size != 4*256 + nr * 24 + 20)
bad50dc8 80 return error("wrong index file size");
bad50dc8
LT
81
82 nr_entries = nr;
83 pack_list = xmalloc(nr * sizeof(struct pack_entry *));
84 for (i = 0; i < nr; i++)
8ee378a0 85 pack_list[i] = index_base + 4*256 + i*24;
bad50dc8
LT
86
87 qsort(pack_list, nr, sizeof(*pack_list), sort_by_offset);
88
89 printf("%d entries\n", nr);
90 return 0;
91}
92
8ee378a0 93static int unpack_non_delta_entry(struct pack_entry *entry,
c4fb06c0
LT
94 int kind,
95 unsigned char *data,
96 unsigned long size,
97 unsigned long left)
74536958 98{
c4fb06c0 99 int st;
8ee378a0
JH
100 z_stream stream;
101 char *buffer;
102 unsigned char sha1[20];
103 char *type_s;
8ee378a0 104
8ee378a0 105 printf("%s %c %lu\n", sha1_to_hex(entry->sha1), kind, size);
c4fb06c0
LT
106 if (dry_run)
107 return 0;
8ee378a0
JH
108
109 buffer = xmalloc(size + 1);
110 buffer[size] = 0;
111 memset(&stream, 0, sizeof(stream));
c4fb06c0
LT
112 stream.next_in = data;
113 stream.avail_in = left;
8ee378a0
JH
114 stream.next_out = buffer;
115 stream.avail_out = size;
116
117 inflateInit(&stream);
118 st = inflate(&stream, Z_FINISH);
119 inflateEnd(&stream);
120 if ((st != Z_STREAM_END) || stream.total_out != size)
121 goto err_finish;
122 switch (kind) {
123 case 'C': type_s = "commit"; break;
124 case 'T': type_s = "tree"; break;
125 case 'B': type_s = "blob"; break;
126 default: goto err_finish;
127 }
128 if (write_sha1_file(buffer, size, type_s, sha1) < 0)
129 die("failed to write %s (%s)",
130 sha1_to_hex(entry->sha1), type_s);
131 printf("%s %s\n", sha1_to_hex(sha1), type_s);
132 if (memcmp(sha1, entry->sha1, 20))
133 die("resulting %s have wrong SHA1", type_s);
134
135 finish:
136 st = 0;
137 free(buffer);
138 return st;
139 err_finish:
140 st = -1;
141 goto finish;
142}
143
144static int find_pack_entry(unsigned char *sha1, struct pack_entry **ent)
145{
146 int *level1_ofs = index_base;
147 int hi = ntohl(level1_ofs[*sha1]);
148 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
149 void *index = index_base + 4*256;
150
151 do {
152 int mi = (lo + hi) / 2;
153 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
154 if (!cmp) {
155 *ent = index + 24 * mi;
156 return 1;
157 }
c4fb06c0 158 if (cmp > 0)
8ee378a0
JH
159 hi = mi;
160 else
c4fb06c0 161 lo = mi+1;
8ee378a0
JH
162 } while (lo < hi);
163 return 0;
164}
165
166/* forward declaration for a mutually recursive function */
167static void unpack_entry(struct pack_entry *);
168
c4fb06c0
LT
169static int unpack_delta_entry(struct pack_entry *entry,
170 unsigned char *base_sha1,
171 unsigned long delta_size,
172 unsigned long left)
8ee378a0 173{
c4fb06c0
LT
174 void *data, *delta_data, *result, *base;
175 unsigned long data_size, result_size, base_size;
8ee378a0
JH
176 z_stream stream;
177 int st;
178 char type[20];
179 unsigned char sha1[20];
180
c4fb06c0
LT
181 if (left < 20)
182 die("truncated pack file");
183 data = base_sha1 + 20;
184 data_size = left - 20;
185 printf("%s D %lu", sha1_to_hex(entry->sha1), delta_size);
186 printf(" %s\n", sha1_to_hex(base_sha1));
187
188 if (dry_run)
189 return 0;
8ee378a0 190
c4fb06c0 191 /* pack+5 is the base sha1, unless we have it, we need to
8ee378a0
JH
192 * unpack it first.
193 */
c4fb06c0 194 if (!has_sha1_file(base_sha1)) {
8ee378a0 195 struct pack_entry *base;
c4fb06c0 196 if (!find_pack_entry(base_sha1, &base))
8ee378a0
JH
197 die("cannot find delta-pack base object");
198 unpack_entry(base);
199 }
c4fb06c0 200 delta_data = xmalloc(delta_size);
8ee378a0
JH
201
202 memset(&stream, 0, sizeof(stream));
203
c4fb06c0
LT
204 stream.next_in = data;
205 stream.avail_in = data_size;
8ee378a0 206 stream.next_out = delta_data;
c4fb06c0 207 stream.avail_out = delta_size;
8ee378a0
JH
208
209 inflateInit(&stream);
c4fb06c0 210 st = inflate(&stream, Z_FINISH);
8ee378a0 211 inflateEnd(&stream);
c4fb06c0
LT
212 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
213 die("delta data unpack failed");
8ee378a0 214
c4fb06c0 215 base = read_sha1_file(base_sha1, type, &base_size);
8ee378a0 216 if (!base)
c4fb06c0 217 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
8ee378a0
JH
218 result = patch_delta(base, base_size,
219 delta_data, delta_size,
220 &result_size);
221 if (!result)
222 die("failed to apply delta");
223 free(delta_data);
224
225 if (write_sha1_file(result, result_size, type, sha1) < 0)
226 die("failed to write %s (%s)",
227 sha1_to_hex(entry->sha1), type);
228 free(result);
229 printf("%s %s\n", sha1_to_hex(sha1), type);
230 if (memcmp(sha1, entry->sha1, 20))
231 die("resulting %s have wrong SHA1", type);
232 return 0;
233}
234
235static void unpack_entry(struct pack_entry *entry)
236{
c4fb06c0 237 unsigned long offset, size, left;
74536958
LT
238 unsigned char *pack;
239
240 /* Have we done this one already due to deltas based on it? */
241 if (lookup_object(entry->sha1))
242 return;
243
244 offset = ntohl(entry->offset);
245 if (offset > pack_size - 5)
246 die("object offset outside of pack file");
247 pack = pack_base + offset;
c4fb06c0
LT
248 size = (pack[1] << 24) + (pack[2] << 16) + (pack[3] << 8) + pack[4];
249 left = pack_size - offset - 5;
74536958
LT
250 switch (*pack) {
251 case 'C': case 'T': case 'B':
c4fb06c0 252 unpack_non_delta_entry(entry, *pack, pack+5, size, left);
74536958
LT
253 break;
254 case 'D':
c4fb06c0 255 unpack_delta_entry(entry, pack+5, size, left);
74536958
LT
256 break;
257 default:
258 die("corrupted pack file");
259 }
260}
261
262/*
263 * We unpack from the end, older files first. Now, usually
264 * there are deltas etc, so we'll not actually write the
265 * objects in that order, but we might as well try..
266 */
267static void unpack_all(void)
268{
269 int i = nr_entries;
270
271 while (--i >= 0) {
272 struct pack_entry *entry = pack_list[i];
273 unpack_entry(entry);
274 }
275}
276
bad50dc8
LT
277int main(int argc, char **argv)
278{
279 int i;
bad50dc8
LT
280
281 for (i = 1 ; i < argc; i++) {
282 const char *arg = argv[i];
283
284 if (*arg == '-') {
74536958
LT
285 if (!strcmp(arg, "-n")) {
286 dry_run = 1;
287 continue;
288 }
bad50dc8
LT
289 usage(unpack_usage);
290 }
291 if (base_name)
292 usage(unpack_usage);
293 base_name = arg;
294 }
295 if (!base_name)
296 usage(unpack_usage);
8ee378a0 297 index_base = map_file("idx", &index_size);
74536958 298 pack_base = map_file("pack", &pack_size);
8ee378a0 299 if (check_index() < 0)
bad50dc8 300 die("bad index file");
74536958 301 unpack_all();
bad50dc8
LT
302 return 0;
303}