@matheusbsbs / matheus-bagliano / commits / 8b0eca7c7b
Fork 0

Create pack-write.c for common pack writing code

Include a generalized fixup_pack_header_footer() in this new file. Needed by git-repack --max-pack-size feature in a later patchset. [sp: Moved close(pack_fd) to callers, to support index-pack, and changed name to better indicate it is for packfiles.] Signed-off-by: Dana L. How <danahow@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Dana L. How committed May 2, 2007 at 12:13 UTC 8b0eca7c7b73d0dc5d49f400a878d9b781ec4bec
4 files changed +45 -39
Makefile
+2 -2
@@ -301,8 +301,8 @@ LIB_OBJS = \
301 interpolate.o \
302 lockfile.o \
303 patch-ids.o \
304 - object.o pack-check.o patch-delta.o path.o pkt-line.o sideband.o \
305 - reachable.o reflog-walk.o \
304 + object.o pack-check.o pack-write.o patch-delta.o path.o pkt-line.o \
305 + sideband.o reachable.o reflog-walk.o \
306 quote.o read-cache.o refs.o run-command.o dir.o object-refs.o \
307 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
308 tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
fast-import.c
+3 -37
@@ -651,42 +651,6 @@ static void start_packfile(void)
651 all_packs[pack_id] = p;
652 }
653
654 -static void fixup_header_footer(void)
655 -{
656 - static const int buf_sz = 128 * 1024;
657 - int pack_fd = pack_data->pack_fd;
658 - SHA_CTX c;
659 - struct pack_header hdr;
660 - char *buf;
661 -
662 - if (lseek(pack_fd, 0, SEEK_SET) != 0)
663 - die("Failed seeking to start: %s", strerror(errno));
664 - if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
665 - die("Unable to reread header of %s", pack_data->pack_name);
666 - if (lseek(pack_fd, 0, SEEK_SET) != 0)
667 - die("Failed seeking to start: %s", strerror(errno));
668 - hdr.hdr_entries = htonl(object_count);
669 - write_or_die(pack_fd, &hdr, sizeof(hdr));
670 -
671 - SHA1_Init(&c);
672 - SHA1_Update(&c, &hdr, sizeof(hdr));
673 -
674 - buf = xmalloc(buf_sz);
675 - for (;;) {
676 - ssize_t n = xread(pack_fd, buf, buf_sz);
677 - if (!n)
678 - break;
679 - if (n < 0)
680 - die("Failed to checksum %s", pack_data->pack_name);
681 - SHA1_Update(&c, buf, n);
682 - }
683 - free(buf);
684 -
685 - SHA1_Final(pack_data->sha1, &c);
686 - write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
687 - close(pack_fd);
688 -}
689 -
654 static int oecmp (const void *a_, const void *b_)
655 {
656 struct object_entry *a = *((struct object_entry**)a_);
@@ -802,7 +766,9 @@ static void end_packfile(void)
766 struct branch *b;
767 struct tag *t;
768
805 - fixup_header_footer();
769 + fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
770 + pack_data->pack_name, object_count);
771 + close(pack_data->pack_fd);
772 idx_name = keep_pack(create_index());
773
774 /* Register the packfile with core git's machinary. */
pack-write.c new
+39
@@ -0,0 +1,39 @@
1 +#include "cache.h"
2 +#include "pack.h"
3 +
4 +void fixup_pack_header_footer(int pack_fd,
5 + unsigned char *pack_file_sha1,
6 + const char *pack_name,
7 + uint32_t object_count)
8 +{
9 + static const int buf_sz = 128 * 1024;
10 + SHA_CTX c;
11 + struct pack_header hdr;
12 + char *buf;
13 +
14 + if (lseek(pack_fd, 0, SEEK_SET) != 0)
15 + die("Failed seeking to start: %s", strerror(errno));
16 + if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
17 + die("Unable to reread header of %s: %s", pack_name, strerror(errno));
18 + if (lseek(pack_fd, 0, SEEK_SET) != 0)
19 + die("Failed seeking to start: %s", strerror(errno));
20 + hdr.hdr_entries = htonl(object_count);
21 + write_or_die(pack_fd, &hdr, sizeof(hdr));
22 +
23 + SHA1_Init(&c);
24 + SHA1_Update(&c, &hdr, sizeof(hdr));
25 +
26 + buf = xmalloc(buf_sz);
27 + for (;;) {
28 + size_t n = xread(pack_fd, buf, buf_sz);
29 + if (!n)
30 + break;
31 + if (n < 0)
32 + die("Failed to checksum %s: %s", pack_name, strerror(errno));
33 + SHA1_Update(&c, buf, n);
34 + }
35 + free(buf);
36 +
37 + SHA1_Final(pack_file_sha1, &c);
38 + write_or_die(pack_fd, pack_file_sha1, 20);
39 +}
pack.h
+1
@@ -44,6 +44,7 @@ struct pack_idx_header {
44
45
46 extern int verify_pack(struct packed_git *, int);
47 +extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t);
48
49 #define PH_ERROR_EOF (-1)
50 #define PH_ERROR_PACK_SIGNATURE (-2)