Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 2 | #include "cache.h" |
| 3 | #include "pack.h" |
| 4 | |
Rene Scharfe | ae9c86f | 2006-08-10 17:02:32 +0200 | [diff] [blame] | 5 | static int verify_one_pack(const char *path, int verbose) |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 6 | { |
Rene Scharfe | ae9c86f | 2006-08-10 17:02:32 +0200 | [diff] [blame] | 7 | char arg[PATH_MAX]; |
| 8 | int len; |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 9 | struct packed_git *pack; |
| 10 | int err; |
Rene Scharfe | ae9c86f | 2006-08-10 17:02:32 +0200 | [diff] [blame] | 11 | |
| 12 | len = strlcpy(arg, path, PATH_MAX); |
| 13 | if (len >= PATH_MAX) |
| 14 | return error("name too long: %s", path); |
| 15 | |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 16 | /* |
| 17 | * In addition to "foo.idx" we accept "foo.pack" and "foo"; |
| 18 | * normalize these forms to "foo.idx" for add_packed_git(). |
| 19 | */ |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 20 | if (has_extension(arg, ".pack")) { |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 21 | strcpy(arg + len - 5, ".idx"); |
| 22 | len--; |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 23 | } else if (!has_extension(arg, ".idx")) { |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 24 | if (len + 4 >= PATH_MAX) |
| 25 | return error("name too long: %s.idx", arg); |
| 26 | strcpy(arg + len, ".idx"); |
| 27 | len += 4; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 28 | } |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 29 | |
Rene Scharfe | f711ab5 | 2006-08-10 17:02:36 +0200 | [diff] [blame] | 30 | /* |
| 31 | * add_packed_git() uses our buffer (containing "foo.idx") to |
| 32 | * build the pack filename ("foo.pack"). Make sure it fits. |
| 33 | */ |
| 34 | if (len + 1 >= PATH_MAX) { |
| 35 | arg[len - 4] = '\0'; |
| 36 | return error("name too long: %s.pack", arg); |
| 37 | } |
| 38 | |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 39 | pack = add_packed_git(arg, len, 1); |
| 40 | if (!pack) |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 41 | return error("packfile %s not found.", arg); |
| 42 | |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 43 | err = verify_pack(pack, verbose); |
| 44 | free(pack); |
| 45 | |
| 46 | return err; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Petr Baudis | 4d1f119 | 2005-07-29 11:01:26 +0200 | [diff] [blame] | 49 | static const char verify_pack_usage[] = "git-verify-pack [-v] <pack>..."; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 50 | |
Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 51 | int cmd_verify_pack(int argc, const char **argv, const char *prefix) |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 52 | { |
Rene Scharfe | 0eaf22f | 2006-08-10 17:02:37 +0200 | [diff] [blame] | 53 | int err = 0; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 54 | int verbose = 0; |
| 55 | int no_more_options = 0; |
Rene Scharfe | 6f05b57 | 2006-08-10 17:02:31 +0200 | [diff] [blame] | 56 | int nothing_done = 1; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 57 | |
Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 58 | while (1 < argc) { |
| 59 | if (!no_more_options && argv[1][0] == '-') { |
| 60 | if (!strcmp("-v", argv[1])) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 61 | verbose = 1; |
Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 62 | else if (!strcmp("--", argv[1])) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 63 | no_more_options = 1; |
| 64 | else |
| 65 | usage(verify_pack_usage); |
| 66 | } |
| 67 | else { |
Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 68 | if (verify_one_pack(argv[1], verbose)) |
Rene Scharfe | 0eaf22f | 2006-08-10 17:02:37 +0200 | [diff] [blame] | 69 | err = 1; |
Rene Scharfe | 6f05b57 | 2006-08-10 17:02:31 +0200 | [diff] [blame] | 70 | nothing_done = 0; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 71 | } |
Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 72 | argc--; argv++; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 73 | } |
Rene Scharfe | 6f05b57 | 2006-08-10 17:02:31 +0200 | [diff] [blame] | 74 | |
| 75 | if (nothing_done) |
| 76 | usage(verify_pack_usage); |
| 77 | |
Rene Scharfe | 0eaf22f | 2006-08-10 17:02:37 +0200 | [diff] [blame] | 78 | return err; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 79 | } |