6 static const char *base_name
;
7 static const char unpack_usage
[] = "git-unpack-objects basename";
11 unsigned char sha1
[20];
14 static void *pack_base
;
15 static unsigned long pack_size
;
17 static struct pack_entry
**pack_list
;
19 static void *map_file(const char *suffix
, unsigned long *sizep
)
21 static char pathname
[PATH_MAX
];
27 len
= snprintf(pathname
, PATH_MAX
, "%s.%s", base_name
, suffix
);
29 die("bad pack base-name");
30 fd
= open(pathname
, O_RDONLY
);
31 if (fd
< 0 || fstat(fd
, &st
))
32 die("unable to open '%s'", pathname
);
35 die("bad pack file '%s'", pathname
);
36 map
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
37 if (-1 == (int)(long)map
)
38 die("unable to mmap '%s'", pathname
);
44 static int sort_by_offset(const void *_a
, const void *_b
)
46 struct pack_entry
*a
= *(struct pack_entry
**)_a
;
47 struct pack_entry
*b
= *(struct pack_entry
**)_b
;
50 o1
= ntohl(a
->offset
);
51 o2
= ntohl(b
->offset
);
52 return o1
< o2
? -1 : 1;
55 static int check_index(void *index
, unsigned long idx_size
)
57 unsigned int *array
= index
;
62 return error("index file too small");
64 for (i
= 0; i
< 256; i
++) {
65 unsigned int n
= ntohl(array
[i
]);
67 return error("non-monotonic index");
70 if (idx_size
!= 4*256 + nr
* 24) {
71 printf("idx_size=%lu, expected %u (%u)\n", idx_size
, 4*256 + nr
* 24, nr
);
72 return error("wrong index file size");
76 pack_list
= xmalloc(nr
* sizeof(struct pack_entry
*));
77 for (i
= 0; i
< nr
; i
++)
78 pack_list
[i
] = index
+ 4*256 + i
*24;
80 qsort(pack_list
, nr
, sizeof(*pack_list
), sort_by_offset
);
82 printf("%d entries\n", nr
);
86 static void unpack_entry(struct pack_entry
*entry
)
92 /* Have we done this one already due to deltas based on it? */
93 if (lookup_object(entry
->sha1
))
96 offset
= ntohl(entry
->offset
);
97 if (offset
> pack_size
- 5)
98 die("object offset outside of pack file");
99 pack
= pack_base
+ offset
;
100 offset
= pack_size
- offset
;
102 case 'C': case 'T': case 'B':
103 size
= (pack
[1] << 24) + (pack
[2] << 16) + (pack
[3] << 8) + pack
[4];
104 printf("%s %c %lu\n", sha1_to_hex(entry
->sha1
), *pack
, size
);
107 printf("%s D", sha1_to_hex(entry
->sha1
));
108 printf(" %s\n", sha1_to_hex(pack
+1));
111 die("corrupted pack file");
116 * We unpack from the end, older files first. Now, usually
117 * there are deltas etc, so we'll not actually write the
118 * objects in that order, but we might as well try..
120 static void unpack_all(void)
125 struct pack_entry
*entry
= pack_list
[i
];
130 int main(int argc
, char **argv
)
133 unsigned long idx_size
;
136 for (i
= 1 ; i
< argc
; i
++) {
137 const char *arg
= argv
[i
];
140 if (!strcmp(arg
, "-n")) {
152 index
= map_file("idx", &idx_size
);
153 pack_base
= map_file("pack", &pack_size
);
154 if (check_index(index
, idx_size
) < 0)
155 die("bad index file");