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