9 static int max_depth = 10;
10 static unsigned long object_count;
12 static int current_depth;
14 static unsigned long lastdatlen;
15 static unsigned char lastsha1[20];
17 static ssize_t yread(int fd, void *buffer, size_t length)
20 while (ret < length) {
21 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
33 static ssize_t ywrite(int fd, void *buffer, size_t length)
36 while (ret < length) {
37 ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret);
49 static unsigned long encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
54 if (type < OBJ_COMMIT || type > OBJ_DELTA)
55 die("bad type %d", type);
57 c = (type << 4) | (size & 15);
69 static void write_blob (void *dat, unsigned long datlen)
73 unsigned char hdr[64];
74 unsigned long hdrlen, deltalen;
76 if (lastdat && current_depth < max_depth) {
77 delta = diff_delta(lastdat, lastdatlen,
83 memset(&s, 0, sizeof(s));
84 deflateInit(&s, zlib_compression_level);
89 s.avail_in = deltalen;
90 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
91 if (ywrite(packfd, hdr, hdrlen) != hdrlen)
92 die("Can't write object header: %s", strerror(errno));
93 if (ywrite(packfd, lastsha1, sizeof(lastsha1)) != sizeof(lastsha1))
94 die("Can't write object base: %s", strerror(errno));
99 hdrlen = encode_header(OBJ_BLOB, datlen, hdr);
100 if (ywrite(packfd, hdr, hdrlen) != hdrlen)
101 die("Can't write object header: %s", strerror(errno));
104 s.avail_out = deflateBound(&s, s.avail_in);
105 s.next_out = out = xmalloc(s.avail_out);
106 while (deflate(&s, Z_FINISH) == Z_OK)
110 if (ywrite(packfd, out, s.total_out) != s.total_out)
111 die("Failed writing compressed data %s", strerror(errno));
118 static void init_pack_header ()
120 const char* magic = "PACK";
121 unsigned long version = 2;
122 unsigned long zero = 0;
124 version = htonl(version);
126 if (ywrite(packfd, (char*)magic, 4) != 4)
127 die("Can't write pack magic: %s", strerror(errno));
128 if (ywrite(packfd, &version, 4) != 4)
129 die("Can't write pack version: %s", strerror(errno));
130 if (ywrite(packfd, &zero, 4) != 4)
131 die("Can't write 0 object count: %s", strerror(errno));
134 static void fixup_header_footer ()
138 unsigned char sha1[20];
143 if (lseek(packfd, 0, SEEK_SET) != 0)
144 die("Failed seeking to start: %s", strerror(errno));
147 if (yread(packfd, hdr, 8) != 8)
148 die("Failed reading header: %s", strerror(errno));
149 SHA1_Update(&c, hdr, 8);
151 fprintf(stderr, "%lu objects\n", object_count);
152 cnt = htonl(object_count);
153 SHA1_Update(&c, &cnt, 4);
154 if (ywrite(packfd, &cnt, 4) != 4)
155 die("Failed writing object count: %s", strerror(errno));
157 buf = xmalloc(128 * 1024);
159 n = xread(packfd, buf, 128 * 1024);
162 SHA1_Update(&c, buf, n);
166 SHA1_Final(sha1, &c);
167 if (ywrite(packfd, sha1, sizeof(sha1)) != sizeof(sha1))
168 die("Failed writing pack checksum: %s", strerror(errno));
171 int main (int argc, const char **argv)
173 packfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
175 die("Can't create pack file %s: %s", argv[1], strerror(errno));
179 unsigned long datlen;
183 unsigned char sha1[20];
186 if (yread(0, &datlen, 4) != 4)
189 dat = xmalloc(datlen);
190 if (yread(0, dat, datlen) != datlen)
193 hdrlen = sprintf(hdr, "blob %lu", datlen) + 1;
195 SHA1_Update(&c, hdr, hdrlen);
196 SHA1_Update(&c, dat, datlen);
197 SHA1_Final(sha1, &c);
199 write_blob(dat, datlen);
201 printf("%s\n", sha1_to_hex(sha1));
208 memcpy(lastsha1, sha1, sizeof(sha1));
210 fixup_header_footer();