l10n: Update Catalan translation
[alt-git.git] / diagnose.c
blob8f2656989666b67a3281d29a83f73ff82a006a1d
1 #include "cache.h"
2 #include "diagnose.h"
3 #include "compat/disk.h"
4 #include "archive.h"
5 #include "dir.h"
6 #include "help.h"
7 #include "strvec.h"
8 #include "object-store.h"
9 #include "packfile.h"
11 struct archive_dir {
12 const char *path;
13 int recursive;
16 struct diagnose_option {
17 enum diagnose_mode mode;
18 const char *option_name;
21 static struct diagnose_option diagnose_options[] = {
22 { DIAGNOSE_STATS, "stats" },
23 { DIAGNOSE_ALL, "all" },
26 int option_parse_diagnose(const struct option *opt, const char *arg, int unset)
28 int i;
29 enum diagnose_mode *diagnose = opt->value;
31 if (!arg) {
32 *diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS;
33 return 0;
36 for (i = 0; i < ARRAY_SIZE(diagnose_options); i++) {
37 if (!strcmp(arg, diagnose_options[i].option_name)) {
38 *diagnose = diagnose_options[i].mode;
39 return 0;
43 return error(_("invalid --%s value '%s'"), opt->long_name, arg);
46 static void dir_file_stats_objects(const char *full_path, size_t full_path_len,
47 const char *file_name, void *data)
49 struct strbuf *buf = data;
50 struct stat st;
52 if (!stat(full_path, &st))
53 strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name,
54 (uintmax_t)st.st_size);
57 static int dir_file_stats(struct object_directory *object_dir, void *data)
59 struct strbuf *buf = data;
61 strbuf_addf(buf, "Contents of %s:\n", object_dir->path);
63 for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects,
64 data);
66 return 0;
70 * Get the d_type of a dirent. If the d_type is unknown, derive it from
71 * stat.st_mode.
73 * Note that 'path' is assumed to have a trailing slash. It is also modified
74 * in-place during the execution of the function, but is then reverted to its
75 * original value before returning.
77 static unsigned char get_dtype(struct dirent *e, struct strbuf *path)
79 struct stat st;
80 unsigned char dtype = DTYPE(e);
81 size_t base_path_len;
83 if (dtype != DT_UNKNOWN)
84 return dtype;
86 /* d_type unknown in dirent, try to fall back on lstat results */
87 base_path_len = path->len;
88 strbuf_addstr(path, e->d_name);
89 if (lstat(path->buf, &st))
90 goto cleanup;
92 /* determine d_type from st_mode */
93 if (S_ISREG(st.st_mode))
94 dtype = DT_REG;
95 else if (S_ISDIR(st.st_mode))
96 dtype = DT_DIR;
97 else if (S_ISLNK(st.st_mode))
98 dtype = DT_LNK;
100 cleanup:
101 strbuf_setlen(path, base_path_len);
102 return dtype;
105 static int count_files(struct strbuf *path)
107 DIR *dir = opendir(path->buf);
108 struct dirent *e;
109 int count = 0;
111 if (!dir)
112 return 0;
114 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
115 if (get_dtype(e, path) == DT_REG)
116 count++;
118 closedir(dir);
119 return count;
122 static void loose_objs_stats(struct strbuf *buf, const char *path)
124 DIR *dir = opendir(path);
125 struct dirent *e;
126 int count;
127 int total = 0;
128 unsigned char c;
129 struct strbuf count_path = STRBUF_INIT;
130 size_t base_path_len;
132 if (!dir)
133 return;
135 strbuf_addstr(buf, "Object directory stats for ");
136 strbuf_add_absolute_path(buf, path);
137 strbuf_addstr(buf, ":\n");
139 strbuf_add_absolute_path(&count_path, path);
140 strbuf_addch(&count_path, '/');
141 base_path_len = count_path.len;
143 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
144 if (get_dtype(e, &count_path) == DT_DIR &&
145 strlen(e->d_name) == 2 &&
146 !hex_to_bytes(&c, e->d_name, 1)) {
147 strbuf_setlen(&count_path, base_path_len);
148 strbuf_addf(&count_path, "%s/", e->d_name);
149 total += (count = count_files(&count_path));
150 strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
153 strbuf_addf(buf, "Total: %d loose objects", total);
155 strbuf_release(&count_path);
156 closedir(dir);
159 static int add_directory_to_archiver(struct strvec *archiver_args,
160 const char *path, int recurse)
162 int at_root = !*path;
163 DIR *dir;
164 struct dirent *e;
165 struct strbuf buf = STRBUF_INIT;
166 size_t len;
167 int res = 0;
169 dir = opendir(at_root ? "." : path);
170 if (!dir) {
171 if (errno == ENOENT) {
172 warning(_("could not archive missing directory '%s'"), path);
173 return 0;
175 return error_errno(_("could not open directory '%s'"), path);
178 if (!at_root)
179 strbuf_addf(&buf, "%s/", path);
180 len = buf.len;
181 strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
183 while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
184 struct strbuf abspath = STRBUF_INIT;
185 unsigned char dtype;
187 strbuf_add_absolute_path(&abspath, at_root ? "." : path);
188 strbuf_addch(&abspath, '/');
189 dtype = get_dtype(e, &abspath);
191 strbuf_setlen(&buf, len);
192 strbuf_addstr(&buf, e->d_name);
194 if (dtype == DT_REG)
195 strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
196 else if (dtype != DT_DIR)
197 warning(_("skipping '%s', which is neither file nor "
198 "directory"), buf.buf);
199 else if (recurse &&
200 add_directory_to_archiver(archiver_args,
201 buf.buf, recurse) < 0)
202 res = -1;
204 strbuf_release(&abspath);
207 closedir(dir);
208 strbuf_release(&buf);
209 return res;
212 int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
214 struct strvec archiver_args = STRVEC_INIT;
215 char **argv_copy = NULL;
216 int stdout_fd = -1, archiver_fd = -1;
217 struct strbuf buf = STRBUF_INIT;
218 int res, i;
219 struct archive_dir archive_dirs[] = {
220 { ".git", 0 },
221 { ".git/hooks", 0 },
222 { ".git/info", 0 },
223 { ".git/logs", 1 },
224 { ".git/objects/info", 0 }
227 if (mode == DIAGNOSE_NONE) {
228 res = 0;
229 goto diagnose_cleanup;
232 stdout_fd = dup(STDOUT_FILENO);
233 if (stdout_fd < 0) {
234 res = error_errno(_("could not duplicate stdout"));
235 goto diagnose_cleanup;
238 archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
239 if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
240 res = error_errno(_("could not redirect output"));
241 goto diagnose_cleanup;
244 init_zip_archiver();
245 strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
247 strbuf_reset(&buf);
248 strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
249 get_version_info(&buf, 1);
251 strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
252 get_disk_info(&buf);
253 write_or_die(stdout_fd, buf.buf, buf.len);
254 strvec_pushf(&archiver_args,
255 "--add-virtual-file=diagnostics.log:%.*s",
256 (int)buf.len, buf.buf);
258 strbuf_reset(&buf);
259 strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
260 dir_file_stats(the_repository->objects->odb, &buf);
261 foreach_alt_odb(dir_file_stats, &buf);
262 strvec_push(&archiver_args, buf.buf);
264 strbuf_reset(&buf);
265 strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
266 loose_objs_stats(&buf, ".git/objects");
267 strvec_push(&archiver_args, buf.buf);
269 /* Only include this if explicitly requested */
270 if (mode == DIAGNOSE_ALL) {
271 for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
272 if (add_directory_to_archiver(&archiver_args,
273 archive_dirs[i].path,
274 archive_dirs[i].recursive)) {
275 res = error_errno(_("could not add directory '%s' to archiver"),
276 archive_dirs[i].path);
277 goto diagnose_cleanup;
282 strvec_pushl(&archiver_args, "--prefix=",
283 oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
285 /* `write_archive()` modifies the `argv` passed to it. Let it. */
286 argv_copy = xmemdupz(archiver_args.v,
287 sizeof(char *) * archiver_args.nr);
288 res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
289 the_repository, NULL, 0);
290 if (res) {
291 error(_("failed to write archive"));
292 goto diagnose_cleanup;
295 fprintf(stderr, "\n"
296 "Diagnostics complete.\n"
297 "All of the gathered info is captured in '%s'\n",
298 zip_path->buf);
300 diagnose_cleanup:
301 if (archiver_fd >= 0) {
302 dup2(stdout_fd, STDOUT_FILENO);
303 close(stdout_fd);
304 close(archiver_fd);
306 free(argv_copy);
307 strvec_clear(&archiver_args);
308 strbuf_release(&buf);
310 return res;