Elijah Newren | 5e3f94d | 2023-04-22 20:17:23 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 2 | #include "diagnose.h" |
| 3 | #include "compat/disk.h" |
| 4 | #include "archive.h" |
| 5 | #include "dir.h" |
| 6 | #include "help.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 7 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 8 | #include "hex.h" |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 9 | #include "strvec.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 10 | #include "object-store-ll.h" |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 11 | #include "packfile.h" |
SZEDER Gábor | 49fd551 | 2023-03-19 17:27:11 +0100 | [diff] [blame] | 12 | #include "parse-options.h" |
Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 13 | #include "write-or-die.h" |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 14 | |
Victoria Dye | 33cba72 | 2022-08-12 20:10:14 +0000 | [diff] [blame] | 15 | struct archive_dir { |
| 16 | const char *path; |
| 17 | int recursive; |
| 18 | }; |
| 19 | |
Victoria Dye | 7ecf193 | 2022-08-12 20:10:16 +0000 | [diff] [blame] | 20 | struct diagnose_option { |
| 21 | enum diagnose_mode mode; |
| 22 | const char *option_name; |
| 23 | }; |
| 24 | |
| 25 | static struct diagnose_option diagnose_options[] = { |
| 26 | { DIAGNOSE_STATS, "stats" }, |
| 27 | { DIAGNOSE_ALL, "all" }, |
| 28 | }; |
| 29 | |
| 30 | int option_parse_diagnose(const struct option *opt, const char *arg, int unset) |
| 31 | { |
| 32 | int i; |
| 33 | enum diagnose_mode *diagnose = opt->value; |
| 34 | |
| 35 | if (!arg) { |
| 36 | *diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS; |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | for (i = 0; i < ARRAY_SIZE(diagnose_options); i++) { |
| 41 | if (!strcmp(arg, diagnose_options[i].option_name)) { |
| 42 | *diagnose = diagnose_options[i].mode; |
| 43 | return 0; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return error(_("invalid --%s value '%s'"), opt->long_name, arg); |
| 48 | } |
| 49 | |
Jeff King | be252d3 | 2023-02-24 01:39:24 -0500 | [diff] [blame] | 50 | static void dir_file_stats_objects(const char *full_path, |
| 51 | size_t full_path_len UNUSED, |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 52 | const char *file_name, void *data) |
| 53 | { |
| 54 | struct strbuf *buf = data; |
| 55 | struct stat st; |
| 56 | |
| 57 | if (!stat(full_path, &st)) |
| 58 | strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name, |
| 59 | (uintmax_t)st.st_size); |
| 60 | } |
| 61 | |
| 62 | static int dir_file_stats(struct object_directory *object_dir, void *data) |
| 63 | { |
| 64 | struct strbuf *buf = data; |
| 65 | |
| 66 | strbuf_addf(buf, "Contents of %s:\n", object_dir->path); |
| 67 | |
| 68 | for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects, |
| 69 | data); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 74 | static int count_files(struct strbuf *path) |
| 75 | { |
| 76 | DIR *dir = opendir(path->buf); |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 77 | struct dirent *e; |
| 78 | int count = 0; |
| 79 | |
| 80 | if (!dir) |
| 81 | return 0; |
| 82 | |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 83 | while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) |
Victoria Dye | aa79636 | 2023-10-09 21:58:55 +0000 | [diff] [blame] | 84 | if (get_dtype(e, path, 0) == DT_REG) |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 85 | count++; |
| 86 | |
| 87 | closedir(dir); |
| 88 | return count; |
| 89 | } |
| 90 | |
| 91 | static void loose_objs_stats(struct strbuf *buf, const char *path) |
| 92 | { |
| 93 | DIR *dir = opendir(path); |
| 94 | struct dirent *e; |
| 95 | int count; |
| 96 | int total = 0; |
| 97 | unsigned char c; |
| 98 | struct strbuf count_path = STRBUF_INIT; |
| 99 | size_t base_path_len; |
| 100 | |
| 101 | if (!dir) |
| 102 | return; |
| 103 | |
| 104 | strbuf_addstr(buf, "Object directory stats for "); |
| 105 | strbuf_add_absolute_path(buf, path); |
| 106 | strbuf_addstr(buf, ":\n"); |
| 107 | |
| 108 | strbuf_add_absolute_path(&count_path, path); |
| 109 | strbuf_addch(&count_path, '/'); |
| 110 | base_path_len = count_path.len; |
| 111 | |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 112 | while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) |
Victoria Dye | aa79636 | 2023-10-09 21:58:55 +0000 | [diff] [blame] | 113 | if (get_dtype(e, &count_path, 0) == DT_DIR && |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 114 | strlen(e->d_name) == 2 && |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 115 | !hex_to_bytes(&c, e->d_name, 1)) { |
| 116 | strbuf_setlen(&count_path, base_path_len); |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 117 | strbuf_addf(&count_path, "%s/", e->d_name); |
| 118 | total += (count = count_files(&count_path)); |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 119 | strbuf_addf(buf, "%s : %7d files\n", e->d_name, count); |
| 120 | } |
| 121 | |
| 122 | strbuf_addf(buf, "Total: %d loose objects", total); |
| 123 | |
| 124 | strbuf_release(&count_path); |
| 125 | closedir(dir); |
| 126 | } |
| 127 | |
| 128 | static int add_directory_to_archiver(struct strvec *archiver_args, |
| 129 | const char *path, int recurse) |
| 130 | { |
| 131 | int at_root = !*path; |
| 132 | DIR *dir; |
| 133 | struct dirent *e; |
| 134 | struct strbuf buf = STRBUF_INIT; |
| 135 | size_t len; |
| 136 | int res = 0; |
| 137 | |
| 138 | dir = opendir(at_root ? "." : path); |
| 139 | if (!dir) { |
| 140 | if (errno == ENOENT) { |
| 141 | warning(_("could not archive missing directory '%s'"), path); |
| 142 | return 0; |
| 143 | } |
| 144 | return error_errno(_("could not open directory '%s'"), path); |
| 145 | } |
| 146 | |
| 147 | if (!at_root) |
| 148 | strbuf_addf(&buf, "%s/", path); |
| 149 | len = buf.len; |
| 150 | strvec_pushf(archiver_args, "--prefix=%s", buf.buf); |
| 151 | |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 152 | while (!res && (e = readdir_skip_dot_and_dotdot(dir))) { |
| 153 | struct strbuf abspath = STRBUF_INIT; |
| 154 | unsigned char dtype; |
| 155 | |
| 156 | strbuf_add_absolute_path(&abspath, at_root ? "." : path); |
| 157 | strbuf_addch(&abspath, '/'); |
Victoria Dye | aa79636 | 2023-10-09 21:58:55 +0000 | [diff] [blame] | 158 | dtype = get_dtype(e, &abspath, 0); |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 159 | |
| 160 | strbuf_setlen(&buf, len); |
| 161 | strbuf_addstr(&buf, e->d_name); |
| 162 | |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 163 | if (dtype == DT_REG) |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 164 | strvec_pushf(archiver_args, "--add-file=%s", buf.buf); |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 165 | else if (dtype != DT_DIR) |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 166 | warning(_("skipping '%s', which is neither file nor " |
| 167 | "directory"), buf.buf); |
| 168 | else if (recurse && |
| 169 | add_directory_to_archiver(archiver_args, |
| 170 | buf.buf, recurse) < 0) |
| 171 | res = -1; |
Victoria Dye | cb98e1d | 2022-09-17 18:16:55 +0000 | [diff] [blame] | 172 | |
| 173 | strbuf_release(&abspath); |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | closedir(dir); |
| 177 | strbuf_release(&buf); |
| 178 | return res; |
| 179 | } |
| 180 | |
Victoria Dye | 33cba72 | 2022-08-12 20:10:14 +0000 | [diff] [blame] | 181 | int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode) |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 182 | { |
| 183 | struct strvec archiver_args = STRVEC_INIT; |
| 184 | char **argv_copy = NULL; |
| 185 | int stdout_fd = -1, archiver_fd = -1; |
| 186 | struct strbuf buf = STRBUF_INIT; |
Victoria Dye | 33cba72 | 2022-08-12 20:10:14 +0000 | [diff] [blame] | 187 | int res, i; |
| 188 | struct archive_dir archive_dirs[] = { |
| 189 | { ".git", 0 }, |
| 190 | { ".git/hooks", 0 }, |
| 191 | { ".git/info", 0 }, |
| 192 | { ".git/logs", 1 }, |
| 193 | { ".git/objects/info", 0 } |
| 194 | }; |
| 195 | |
| 196 | if (mode == DIAGNOSE_NONE) { |
| 197 | res = 0; |
| 198 | goto diagnose_cleanup; |
| 199 | } |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 200 | |
| 201 | stdout_fd = dup(STDOUT_FILENO); |
| 202 | if (stdout_fd < 0) { |
| 203 | res = error_errno(_("could not duplicate stdout")); |
| 204 | goto diagnose_cleanup; |
| 205 | } |
| 206 | |
| 207 | archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666); |
| 208 | if (dup2(archiver_fd, STDOUT_FILENO) < 0) { |
| 209 | res = error_errno(_("could not redirect output")); |
| 210 | goto diagnose_cleanup; |
| 211 | } |
| 212 | |
| 213 | init_zip_archiver(); |
| 214 | strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL); |
| 215 | |
| 216 | strbuf_reset(&buf); |
| 217 | strbuf_addstr(&buf, "Collecting diagnostic info\n\n"); |
| 218 | get_version_info(&buf, 1); |
| 219 | |
| 220 | strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree); |
| 221 | get_disk_info(&buf); |
| 222 | write_or_die(stdout_fd, buf.buf, buf.len); |
| 223 | strvec_pushf(&archiver_args, |
| 224 | "--add-virtual-file=diagnostics.log:%.*s", |
| 225 | (int)buf.len, buf.buf); |
| 226 | |
| 227 | strbuf_reset(&buf); |
| 228 | strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:"); |
| 229 | dir_file_stats(the_repository->objects->odb, &buf); |
| 230 | foreach_alt_odb(dir_file_stats, &buf); |
| 231 | strvec_push(&archiver_args, buf.buf); |
| 232 | |
| 233 | strbuf_reset(&buf); |
| 234 | strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:"); |
| 235 | loose_objs_stats(&buf, ".git/objects"); |
| 236 | strvec_push(&archiver_args, buf.buf); |
| 237 | |
Victoria Dye | 33cba72 | 2022-08-12 20:10:14 +0000 | [diff] [blame] | 238 | /* Only include this if explicitly requested */ |
| 239 | if (mode == DIAGNOSE_ALL) { |
| 240 | for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) { |
| 241 | if (add_directory_to_archiver(&archiver_args, |
| 242 | archive_dirs[i].path, |
| 243 | archive_dirs[i].recursive)) { |
| 244 | res = error_errno(_("could not add directory '%s' to archiver"), |
| 245 | archive_dirs[i].path); |
| 246 | goto diagnose_cleanup; |
| 247 | } |
| 248 | } |
| 249 | } |
Victoria Dye | bb2c349 | 2022-08-12 20:10:13 +0000 | [diff] [blame] | 250 | |
| 251 | strvec_pushl(&archiver_args, "--prefix=", |
| 252 | oid_to_hex(the_hash_algo->empty_tree), "--", NULL); |
| 253 | |
| 254 | /* `write_archive()` modifies the `argv` passed to it. Let it. */ |
| 255 | argv_copy = xmemdupz(archiver_args.v, |
| 256 | sizeof(char *) * archiver_args.nr); |
| 257 | res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL, |
| 258 | the_repository, NULL, 0); |
| 259 | if (res) { |
| 260 | error(_("failed to write archive")); |
| 261 | goto diagnose_cleanup; |
| 262 | } |
| 263 | |
| 264 | fprintf(stderr, "\n" |
| 265 | "Diagnostics complete.\n" |
| 266 | "All of the gathered info is captured in '%s'\n", |
| 267 | zip_path->buf); |
| 268 | |
| 269 | diagnose_cleanup: |
| 270 | if (archiver_fd >= 0) { |
| 271 | dup2(stdout_fd, STDOUT_FILENO); |
| 272 | close(stdout_fd); |
| 273 | close(archiver_fd); |
| 274 | } |
| 275 | free(argv_copy); |
| 276 | strvec_clear(&archiver_args); |
| 277 | strbuf_release(&buf); |
| 278 | |
| 279 | return res; |
| 280 | } |