git-verify-pack: more careful path handling
[alt-git.git] / verify-pack.c
blob77b3d282dad6cb02f2d986e2e2497a38b22f8b5f
1 #include "cache.h"
2 #include "pack.h"
4 static int verify_one_pack(const char *path, int verbose)
6 char arg[PATH_MAX];
7 int len;
8 struct packed_git *g;
10 len = strlcpy(arg, path, PATH_MAX);
11 if (len >= PATH_MAX)
12 return error("name too long: %s", path);
14 while (1) {
15 /* Should name foo.idx, but foo.pack may be named;
16 * convert it to foo.idx
18 if (has_extension(arg, len, ".pack")) {
19 strcpy(arg + len - 5, ".idx");
20 len--;
22 /* Should name foo.idx now */
23 if ((g = add_packed_git(arg, len, 1)))
24 break;
25 /* No? did you name just foo? */
26 strcpy(arg + len, ".idx");
27 len += 4;
28 if ((g = add_packed_git(arg, len, 1)))
29 break;
30 return error("packfile %s not found.", arg);
32 return verify_pack(g, verbose);
35 static const char verify_pack_usage[] = "git-verify-pack [-v] <pack>...";
37 int main(int ac, char **av)
39 int errs = 0;
40 int verbose = 0;
41 int no_more_options = 0;
42 int nothing_done = 1;
44 while (1 < ac) {
45 if (!no_more_options && av[1][0] == '-') {
46 if (!strcmp("-v", av[1]))
47 verbose = 1;
48 else if (!strcmp("--", av[1]))
49 no_more_options = 1;
50 else
51 usage(verify_pack_usage);
53 else {
54 if (verify_one_pack(av[1], verbose))
55 errs++;
56 nothing_done = 0;
58 ac--; av++;
61 if (nothing_done)
62 usage(verify_pack_usage);
64 return !!errs;