Don't special-case a zero-sized compression.
[git.git] / unpack-objects.c
1 #include "cache.h"
2 #include "object.h"
3 #include "delta.h"
4 #include "pack.h"
5
6 static int dry_run;
7 static const char unpack_usage[] = "git-unpack-objects < pack-file";
8
9 /* We always read in 4kB chunks. */
10 static unsigned char buffer[4096];
11 static unsigned long offset, len, eof;
12 static SHA_CTX ctx;
13
14 /*
15  * Make sure at least "min" bytes are available in the buffer, and
16  * return the pointer to the buffer.
17  */
18 static void * fill(int min)
19 {
20         if (min <= len)
21                 return buffer + offset;
22         if (eof)
23                 die("unable to fill input");
24         if (min > sizeof(buffer))
25                 die("cannot fill %d bytes", min);
26         if (offset) {
27                 SHA1_Update(&ctx, buffer, offset);
28                 memcpy(buffer, buffer + offset, len);
29                 offset = 0;
30         }
31         do {
32                 int ret = read(0, buffer + len, sizeof(buffer) - len);
33                 if (ret <= 0) {
34                         if (!ret)
35                                 die("early EOF");
36                         if (errno == EAGAIN || errno == EINTR)
37                                 continue;
38                         die("read error on input: %s", strerror(errno));
39                 }
40                 len += ret;
41         } while (len < min);
42         return buffer;
43 }
44
45 static void use(int bytes)
46 {
47         if (bytes > len)
48                 die("used more bytes than were available");
49         len -= bytes;
50         offset += bytes;
51 }
52
53 static void *get_data(unsigned long size)
54 {
55         z_stream stream;
56         void *buf = xmalloc(size);
57
58         memset(&stream, 0, sizeof(stream));
59
60         stream.next_out = buf;
61         stream.avail_out = size;
62         stream.next_in = fill(1);
63         stream.avail_in = len;
64         inflateInit(&stream);
65
66         for (;;) {
67                 int ret = inflate(&stream, 0);
68                 use(len - stream.avail_in);
69                 if (stream.total_out == size && ret == Z_STREAM_END)
70                         break;
71                 if (ret != Z_OK)
72                         die("inflate returned %d\n", ret);
73                 stream.next_in = fill(1);
74                 stream.avail_in = len;
75         }
76         return buf;
77 }
78
79 struct delta_info {
80         unsigned char base_sha1[20];
81         unsigned long size;
82         void *delta;
83         struct delta_info *next;
84 };
85
86 static struct delta_info *delta_list;
87
88 static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
89 {
90         struct delta_info *info = xmalloc(sizeof(*info));
91
92         memcpy(info->base_sha1, base_sha1, 20);
93         info->size = size;
94         info->delta = delta;
95         info->next = delta_list;
96         delta_list = info;
97 }
98
99 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
100
101 static void write_object(void *buf, unsigned long size, const char *type)
102 {
103         unsigned char sha1[20];
104         if (write_sha1_file(buf, size, type, sha1) < 0)
105                 die("failed to write object");
106         added_object(sha1, type, buf, size);
107 }
108
109 static int resolve_delta(const char *type,
110         void *base, unsigned long base_size, 
111         void *delta, unsigned long delta_size)
112 {
113         void *result;
114         unsigned long result_size;
115
116         result = patch_delta(base, base_size,
117                              delta, delta_size,
118                              &result_size);
119         if (!result)
120                 die("failed to apply delta");
121         free(delta);
122         write_object(result, result_size, type);
123         free(result);
124         return 0;
125 }
126
127 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
128 {
129         struct delta_info **p = &delta_list;
130         struct delta_info *info;
131
132         while ((info = *p) != NULL) {
133                 if (!memcmp(info->base_sha1, sha1, 20)) {
134                         *p = info->next;
135                         p = &delta_list;
136                         resolve_delta(type, data, size, info->delta, info->size);
137                         free(info);
138                         continue;
139                 }
140                 p = &info->next;
141         }
142 }
143
144 static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
145 {
146         void *buf = get_data(size);
147         const char *type;
148
149         switch (kind) {
150         case OBJ_COMMIT: type = "commit"; break;
151         case OBJ_TREE:   type = "tree"; break;
152         case OBJ_BLOB:   type = "blob"; break;
153         case OBJ_TAG:    type = "tag"; break;
154         default: die("bad type %d", kind);
155         }
156         if (!dry_run)
157                 write_object(buf, size, type);
158         free(buf);
159         return 0;
160 }
161
162 static int unpack_delta_entry(unsigned long delta_size)
163 {
164         void *delta_data, *base;
165         unsigned long base_size;
166         char type[20];
167         unsigned char base_sha1[20];
168
169         memcpy(base_sha1, fill(20), 20);
170         use(20);
171
172         delta_data = get_data(delta_size);
173         if (dry_run) {
174                 free(delta_data);
175                 return 0;
176         }
177
178         if (!has_sha1_file(base_sha1)) {
179                 add_delta_to_list(base_sha1, delta_data, delta_size);
180                 return 0;
181         }
182         base = read_sha1_file(base_sha1, type, &base_size);
183         if (!base)
184                 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
185         return resolve_delta(type, base, base_size, delta_data, delta_size);
186 }
187
188 static void unpack_one(void)
189 {
190         unsigned shift;
191         unsigned char *pack, c;
192         unsigned long size;
193         enum object_type type;
194
195         pack = fill(1);
196         c = *pack;
197         use(1);
198         type = (c >> 4) & 7;
199         size = (c & 15);
200         shift = 4;
201         while (c & 0x80) {
202                 pack = fill(1);
203                 c = *pack++;
204                 use(1);
205                 size += (c & 0x7f) << shift;
206                 shift += 7;
207         }
208         switch (type) {
209         case OBJ_COMMIT:
210         case OBJ_TREE:
211         case OBJ_BLOB:
212         case OBJ_TAG:
213                 unpack_non_delta_entry(type, size);
214                 return;
215         case OBJ_DELTA:
216                 unpack_delta_entry(size);
217                 return;
218         default:
219                 die("bad object type %d", type);
220         }
221 }
222
223 /*
224  * We unpack from the end, older files first. Now, usually
225  * there are deltas etc, so we'll not actually write the
226  * objects in that order, but we might as well try..
227  */
228 static void unpack_all(void)
229 {
230         int i;
231         struct pack_header *hdr = fill(sizeof(struct pack_header));
232         unsigned version = ntohl(hdr->hdr_version);
233         unsigned nr_objects = ntohl(hdr->hdr_entries);
234
235         if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
236                 die("bad pack file");
237         if (version != PACK_VERSION)
238                 die("unable to handle pack file version %d", version);
239         fprintf(stderr, "Unpacking %d objects\n", nr_objects);
240
241         use(sizeof(struct pack_header));
242         for (i = 0; i < nr_objects; i++)
243                 unpack_one();
244         if (delta_list)
245                 die("unresolved deltas left after unpacking");
246 }
247
248 int main(int argc, char **argv)
249 {
250         int i;
251         unsigned char sha1[20];
252
253         for (i = 1 ; i < argc; i++) {
254                 const char *arg = argv[i];
255
256                 if (*arg == '-') {
257                         if (!strcmp(arg, "-n")) {
258                                 dry_run = 1;
259                                 continue;
260                         }
261                         usage(unpack_usage);
262                 }
263
264                 /* We don't take any non-flag arguments now.. Maybe some day */
265                 usage(unpack_usage);
266         }
267         SHA1_Init(&ctx);
268         unpack_all();
269         SHA1_Update(&ctx, buffer, offset);
270         SHA1_Final(sha1, &ctx);
271         if (memcmp(fill(20), sha1, 20))
272                 die("final sha1 did not match");
273         use(20);
274
275         /* Write the last part of the buffer to stdout */
276         while (len) {
277                 int ret = write(1, buffer + offset, len);
278                 if (!ret)
279                         break;
280                 if (ret < 0) {
281                         if (errno == EAGAIN || errno == EINTR)
282                                 continue;
283                         break;
284                 }
285                 len -= ret;
286                 offset += ret;
287         }
288
289         /* All done */
290         return 0;
291 }