csum-file: fix missing buf pointer update
[git/dscho.git] / unpack-objects.c
bloba792b92ece0770e5c2305b26d70eb26ee51d26c7
1 #include "cache.h"
2 #include "object.h"
3 #include "delta.h"
5 static int dry_run;
6 static int nr_entries;
7 static const char *base_name;
8 static const char unpack_usage[] = "git-unpack-objects basename";
10 struct pack_entry {
11 unsigned int offset; /* network byte order */
12 unsigned char sha1[20];
15 static void *pack_base;
16 static unsigned long pack_size;
17 static void *index_base;
18 static unsigned long index_size;
20 static struct pack_entry **pack_list;
22 static void *map_file(const char *suffix, unsigned long *sizep)
24 static char pathname[PATH_MAX];
25 unsigned long len;
26 int fd;
27 struct stat st;
28 void *map;
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;
47 static int sort_by_offset(const void *_a, const void *_b)
49 struct pack_entry *a = *(struct pack_entry **)_a;
50 struct pack_entry *b = *(struct pack_entry **)_b;
51 unsigned int o1, o2;
53 o1 = ntohl(a->offset);
54 o2 = ntohl(b->offset);
55 return o1 < o2 ? -1 : 1;
58 static int check_index(void)
60 unsigned int *array = index_base;
61 unsigned int nr;
62 int i;
64 if (index_size < 4*256 + 20)
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;
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 of the packfile
78 * - 20-byte SHA1 file checksum
80 if (index_size != 4*256 + nr * 24 + 20 + 20)
81 return error("wrong index file size");
83 nr_entries = nr;
84 pack_list = xmalloc(nr * sizeof(struct pack_entry *));
85 for (i = 0; i < nr; i++)
86 pack_list[i] = index_base + 4*256 + i*24;
88 qsort(pack_list, nr, sizeof(*pack_list), sort_by_offset);
90 printf("%d entries\n", nr);
91 return 0;
94 static int unpack_non_delta_entry(struct pack_entry *entry,
95 int kind,
96 unsigned char *data,
97 unsigned long size,
98 unsigned long left)
100 int st;
101 z_stream stream;
102 char *buffer;
103 unsigned char sha1[20];
104 char *type_s;
106 printf("%s %c %lu\n", sha1_to_hex(entry->sha1), kind, size);
107 if (dry_run)
108 return 0;
110 buffer = xmalloc(size + 1);
111 buffer[size] = 0;
112 memset(&stream, 0, sizeof(stream));
113 stream.next_in = data;
114 stream.avail_in = left;
115 stream.next_out = buffer;
116 stream.avail_out = size;
118 inflateInit(&stream);
119 st = inflate(&stream, Z_FINISH);
120 inflateEnd(&stream);
121 if ((st != Z_STREAM_END) || stream.total_out != size)
122 goto err_finish;
123 switch (kind) {
124 case 'C': type_s = "commit"; break;
125 case 'T': type_s = "tree"; break;
126 case 'B': type_s = "blob"; break;
127 default: goto err_finish;
129 if (write_sha1_file(buffer, size, type_s, sha1) < 0)
130 die("failed to write %s (%s)",
131 sha1_to_hex(entry->sha1), type_s);
132 printf("%s %s\n", sha1_to_hex(sha1), type_s);
133 if (memcmp(sha1, entry->sha1, 20))
134 die("resulting %s have wrong SHA1", type_s);
136 finish:
137 st = 0;
138 free(buffer);
139 return st;
140 err_finish:
141 st = -1;
142 goto finish;
145 static int find_pack_entry(unsigned char *sha1, struct pack_entry **ent)
147 int *level1_ofs = index_base;
148 int hi = ntohl(level1_ofs[*sha1]);
149 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
150 void *index = index_base + 4*256;
152 do {
153 int mi = (lo + hi) / 2;
154 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
155 if (!cmp) {
156 *ent = index + 24 * mi;
157 return 1;
159 if (cmp > 0)
160 hi = mi;
161 else
162 lo = mi+1;
163 } while (lo < hi);
164 return 0;
167 /* forward declaration for a mutually recursive function */
168 static void unpack_entry(struct pack_entry *);
170 static int unpack_delta_entry(struct pack_entry *entry,
171 unsigned char *base_sha1,
172 unsigned long delta_size,
173 unsigned long left)
175 void *data, *delta_data, *result, *base;
176 unsigned long data_size, result_size, base_size;
177 z_stream stream;
178 int st;
179 char type[20];
180 unsigned char sha1[20];
182 if (left < 20)
183 die("truncated pack file");
184 data = base_sha1 + 20;
185 data_size = left - 20;
186 printf("%s D %lu", sha1_to_hex(entry->sha1), delta_size);
187 printf(" %s\n", sha1_to_hex(base_sha1));
189 if (dry_run)
190 return 0;
192 /* pack+5 is the base sha1, unless we have it, we need to
193 * unpack it first.
195 if (!has_sha1_file(base_sha1)) {
196 struct pack_entry *base;
197 if (!find_pack_entry(base_sha1, &base))
198 die("cannot find delta-pack base object");
199 unpack_entry(base);
201 delta_data = xmalloc(delta_size);
203 memset(&stream, 0, sizeof(stream));
205 stream.next_in = data;
206 stream.avail_in = data_size;
207 stream.next_out = delta_data;
208 stream.avail_out = delta_size;
210 inflateInit(&stream);
211 st = inflate(&stream, Z_FINISH);
212 inflateEnd(&stream);
213 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
214 die("delta data unpack failed");
216 base = read_sha1_file(base_sha1, type, &base_size);
217 if (!base)
218 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
219 result = patch_delta(base, base_size,
220 delta_data, delta_size,
221 &result_size);
222 if (!result)
223 die("failed to apply delta");
224 free(delta_data);
226 if (write_sha1_file(result, result_size, type, sha1) < 0)
227 die("failed to write %s (%s)",
228 sha1_to_hex(entry->sha1), type);
229 free(result);
230 printf("%s %s\n", sha1_to_hex(sha1), type);
231 if (memcmp(sha1, entry->sha1, 20))
232 die("resulting %s have wrong SHA1", type);
233 return 0;
236 static void unpack_entry(struct pack_entry *entry)
238 unsigned long offset, size, left;
239 unsigned char *pack;
241 /* Have we done this one already due to deltas based on it? */
242 if (lookup_object(entry->sha1))
243 return;
245 offset = ntohl(entry->offset);
246 if (offset > pack_size - 5)
247 die("object offset outside of pack file");
248 pack = pack_base + offset;
249 size = (pack[1] << 24) + (pack[2] << 16) + (pack[3] << 8) + pack[4];
250 left = pack_size - offset - 5;
251 switch (*pack) {
252 case 'C': case 'T': case 'B':
253 unpack_non_delta_entry(entry, *pack, pack+5, size, left);
254 break;
255 case 'D':
256 unpack_delta_entry(entry, pack+5, size, left);
257 break;
258 default:
259 die("corrupted pack file");
264 * We unpack from the end, older files first. Now, usually
265 * there are deltas etc, so we'll not actually write the
266 * objects in that order, but we might as well try..
268 static void unpack_all(void)
270 int i = nr_entries;
272 while (--i >= 0) {
273 struct pack_entry *entry = pack_list[i];
274 unpack_entry(entry);
278 int main(int argc, char **argv)
280 int i;
282 for (i = 1 ; i < argc; i++) {
283 const char *arg = argv[i];
285 if (*arg == '-') {
286 if (!strcmp(arg, "-n")) {
287 dry_run = 1;
288 continue;
290 usage(unpack_usage);
292 if (base_name)
293 usage(unpack_usage);
294 base_name = arg;
296 if (!base_name)
297 usage(unpack_usage);
298 index_base = map_file("idx", &index_size);
299 pack_base = map_file("pack", &pack_size);
300 if (check_index() < 0)
301 die("bad index file");
302 unpack_all();
303 return 0;