]> git.ipfire.org Git - thirdparty/git.git/blame - diagnose.c
mingw: use lowercase includes for some Windows headers
[thirdparty/git.git] / diagnose.c
CommitLineData
5e3f94df 1#include "git-compat-util.h"
bb2c3495
VD
2#include "diagnose.h"
3#include "compat/disk.h"
4#include "archive.h"
5#include "dir.h"
6#include "help.h"
f394e093 7#include "gettext.h"
41771fa4 8#include "hex.h"
bb2c3495
VD
9#include "strvec.h"
10#include "object-store.h"
11#include "packfile.h"
49fd5511 12#include "parse-options.h"
d48be35c 13#include "write-or-die.h"
bb2c3495 14
33cba726
VD
15struct archive_dir {
16 const char *path;
17 int recursive;
18};
19
7ecf193f
VD
20struct diagnose_option {
21 enum diagnose_mode mode;
22 const char *option_name;
23};
24
25static struct diagnose_option diagnose_options[] = {
26 { DIAGNOSE_STATS, "stats" },
27 { DIAGNOSE_ALL, "all" },
28};
29
30int 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
be252d33
JK
50static void dir_file_stats_objects(const char *full_path,
51 size_t full_path_len UNUSED,
bb2c3495
VD
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
62static 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
cb98e1d5
VD
74/*
75 * Get the d_type of a dirent. If the d_type is unknown, derive it from
76 * stat.st_mode.
77 *
78 * Note that 'path' is assumed to have a trailing slash. It is also modified
79 * in-place during the execution of the function, but is then reverted to its
80 * original value before returning.
81 */
82static unsigned char get_dtype(struct dirent *e, struct strbuf *path)
bb2c3495 83{
cb98e1d5
VD
84 struct stat st;
85 unsigned char dtype = DTYPE(e);
86 size_t base_path_len;
87
88 if (dtype != DT_UNKNOWN)
89 return dtype;
90
91 /* d_type unknown in dirent, try to fall back on lstat results */
92 base_path_len = path->len;
93 strbuf_addstr(path, e->d_name);
94 if (lstat(path->buf, &st))
95 goto cleanup;
96
97 /* determine d_type from st_mode */
98 if (S_ISREG(st.st_mode))
99 dtype = DT_REG;
100 else if (S_ISDIR(st.st_mode))
101 dtype = DT_DIR;
102 else if (S_ISLNK(st.st_mode))
103 dtype = DT_LNK;
104
105cleanup:
106 strbuf_setlen(path, base_path_len);
107 return dtype;
108}
109
110static int count_files(struct strbuf *path)
111{
112 DIR *dir = opendir(path->buf);
bb2c3495
VD
113 struct dirent *e;
114 int count = 0;
115
116 if (!dir)
117 return 0;
118
cb98e1d5
VD
119 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
120 if (get_dtype(e, path) == DT_REG)
bb2c3495
VD
121 count++;
122
123 closedir(dir);
124 return count;
125}
126
127static void loose_objs_stats(struct strbuf *buf, const char *path)
128{
129 DIR *dir = opendir(path);
130 struct dirent *e;
131 int count;
132 int total = 0;
133 unsigned char c;
134 struct strbuf count_path = STRBUF_INIT;
135 size_t base_path_len;
136
137 if (!dir)
138 return;
139
140 strbuf_addstr(buf, "Object directory stats for ");
141 strbuf_add_absolute_path(buf, path);
142 strbuf_addstr(buf, ":\n");
143
144 strbuf_add_absolute_path(&count_path, path);
145 strbuf_addch(&count_path, '/');
146 base_path_len = count_path.len;
147
cb98e1d5
VD
148 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
149 if (get_dtype(e, &count_path) == DT_DIR &&
150 strlen(e->d_name) == 2 &&
bb2c3495
VD
151 !hex_to_bytes(&c, e->d_name, 1)) {
152 strbuf_setlen(&count_path, base_path_len);
cb98e1d5
VD
153 strbuf_addf(&count_path, "%s/", e->d_name);
154 total += (count = count_files(&count_path));
bb2c3495
VD
155 strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
156 }
157
158 strbuf_addf(buf, "Total: %d loose objects", total);
159
160 strbuf_release(&count_path);
161 closedir(dir);
162}
163
164static int add_directory_to_archiver(struct strvec *archiver_args,
165 const char *path, int recurse)
166{
167 int at_root = !*path;
168 DIR *dir;
169 struct dirent *e;
170 struct strbuf buf = STRBUF_INIT;
171 size_t len;
172 int res = 0;
173
174 dir = opendir(at_root ? "." : path);
175 if (!dir) {
176 if (errno == ENOENT) {
177 warning(_("could not archive missing directory '%s'"), path);
178 return 0;
179 }
180 return error_errno(_("could not open directory '%s'"), path);
181 }
182
183 if (!at_root)
184 strbuf_addf(&buf, "%s/", path);
185 len = buf.len;
186 strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
187
cb98e1d5
VD
188 while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
189 struct strbuf abspath = STRBUF_INIT;
190 unsigned char dtype;
191
192 strbuf_add_absolute_path(&abspath, at_root ? "." : path);
193 strbuf_addch(&abspath, '/');
194 dtype = get_dtype(e, &abspath);
bb2c3495
VD
195
196 strbuf_setlen(&buf, len);
197 strbuf_addstr(&buf, e->d_name);
198
cb98e1d5 199 if (dtype == DT_REG)
bb2c3495 200 strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
cb98e1d5 201 else if (dtype != DT_DIR)
bb2c3495
VD
202 warning(_("skipping '%s', which is neither file nor "
203 "directory"), buf.buf);
204 else if (recurse &&
205 add_directory_to_archiver(archiver_args,
206 buf.buf, recurse) < 0)
207 res = -1;
cb98e1d5
VD
208
209 strbuf_release(&abspath);
bb2c3495
VD
210 }
211
212 closedir(dir);
213 strbuf_release(&buf);
214 return res;
215}
216
33cba726 217int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
bb2c3495
VD
218{
219 struct strvec archiver_args = STRVEC_INIT;
220 char **argv_copy = NULL;
221 int stdout_fd = -1, archiver_fd = -1;
222 struct strbuf buf = STRBUF_INIT;
33cba726
VD
223 int res, i;
224 struct archive_dir archive_dirs[] = {
225 { ".git", 0 },
226 { ".git/hooks", 0 },
227 { ".git/info", 0 },
228 { ".git/logs", 1 },
229 { ".git/objects/info", 0 }
230 };
231
232 if (mode == DIAGNOSE_NONE) {
233 res = 0;
234 goto diagnose_cleanup;
235 }
bb2c3495
VD
236
237 stdout_fd = dup(STDOUT_FILENO);
238 if (stdout_fd < 0) {
239 res = error_errno(_("could not duplicate stdout"));
240 goto diagnose_cleanup;
241 }
242
243 archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
244 if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
245 res = error_errno(_("could not redirect output"));
246 goto diagnose_cleanup;
247 }
248
249 init_zip_archiver();
250 strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
251
252 strbuf_reset(&buf);
253 strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
254 get_version_info(&buf, 1);
255
256 strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
257 get_disk_info(&buf);
258 write_or_die(stdout_fd, buf.buf, buf.len);
259 strvec_pushf(&archiver_args,
260 "--add-virtual-file=diagnostics.log:%.*s",
261 (int)buf.len, buf.buf);
262
263 strbuf_reset(&buf);
264 strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
265 dir_file_stats(the_repository->objects->odb, &buf);
266 foreach_alt_odb(dir_file_stats, &buf);
267 strvec_push(&archiver_args, buf.buf);
268
269 strbuf_reset(&buf);
270 strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
271 loose_objs_stats(&buf, ".git/objects");
272 strvec_push(&archiver_args, buf.buf);
273
33cba726
VD
274 /* Only include this if explicitly requested */
275 if (mode == DIAGNOSE_ALL) {
276 for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
277 if (add_directory_to_archiver(&archiver_args,
278 archive_dirs[i].path,
279 archive_dirs[i].recursive)) {
280 res = error_errno(_("could not add directory '%s' to archiver"),
281 archive_dirs[i].path);
282 goto diagnose_cleanup;
283 }
284 }
285 }
bb2c3495
VD
286
287 strvec_pushl(&archiver_args, "--prefix=",
288 oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
289
290 /* `write_archive()` modifies the `argv` passed to it. Let it. */
291 argv_copy = xmemdupz(archiver_args.v,
292 sizeof(char *) * archiver_args.nr);
293 res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
294 the_repository, NULL, 0);
295 if (res) {
296 error(_("failed to write archive"));
297 goto diagnose_cleanup;
298 }
299
300 fprintf(stderr, "\n"
301 "Diagnostics complete.\n"
302 "All of the gathered info is captured in '%s'\n",
303 zip_path->buf);
304
305diagnose_cleanup:
306 if (archiver_fd >= 0) {
307 dup2(stdout_fd, STDOUT_FILENO);
308 close(stdout_fd);
309 close(archiver_fd);
310 }
311 free(argv_copy);
312 strvec_clear(&archiver_args);
313 strbuf_release(&buf);
314
315 return res;
316}