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