]>
git.ipfire.org Git - thirdparty/git.git/blob - unpack-objects.c
73d0c2d63260c620703f28ff6834fab4705e8f4e
4 static const char *base_name
;
5 static const char unpack_usage
[] = "git-unpack-objects basename";
9 unsigned char sha1
[20];
12 static struct pack_entry
**pack_list
;
14 static void *map_file(const char *suffix
, unsigned long *sizep
)
16 static char pathname
[PATH_MAX
];
22 len
= snprintf(pathname
, PATH_MAX
, "%s.%s", base_name
, suffix
);
24 die("bad pack base-name");
25 fd
= open(pathname
, O_RDONLY
);
26 if (fd
< 0 || fstat(fd
, &st
))
27 die("unable to open '%s'", pathname
);
30 die("bad pack file '%s'", pathname
);
31 map
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
32 if (-1 == (int)(long)map
)
33 die("unable to mmap '%s'", pathname
);
39 static int sort_by_offset(const void *_a
, const void *_b
)
41 struct pack_entry
*a
= *(struct pack_entry
**)_a
;
42 struct pack_entry
*b
= *(struct pack_entry
**)_b
;
45 o1
= ntohl(a
->offset
);
46 o2
= ntohl(b
->offset
);
47 return o1
< o2
? -1 : 1;
50 static int check_index(void *index
, unsigned long idx_size
)
52 unsigned int *array
= index
;
57 return error("index file too small");
59 for (i
= 0; i
< 256; i
++) {
60 unsigned int n
= ntohl(array
[i
]);
62 return error("non-monotonic index");
65 if (idx_size
!= 4*256 + nr
* 24) {
66 printf("idx_size=%d, expected %d (%d)\n", idx_size
, 4*256 + nr
* 24, nr
);
67 return error("wrong index file size");
71 pack_list
= xmalloc(nr
* sizeof(struct pack_entry
*));
72 for (i
= 0; i
< nr
; i
++)
73 pack_list
[i
] = index
+ 4*256 + i
*24;
75 qsort(pack_list
, nr
, sizeof(*pack_list
), sort_by_offset
);
77 printf("%d entries\n", nr
);
81 int main(int argc
, char **argv
)
84 unsigned long idx_size
, pack_size
;
87 for (i
= 1 ; i
< argc
; i
++) {
88 const char *arg
= argv
[i
];
91 /* Maybe we'll have some flags here some day.. */
100 index
= map_file("idx", &idx_size
);
101 pack
= map_file("pack", &pack_size
);
102 if (check_index(index
, idx_size
) < 0)
103 die("bad index file");