]> git.ipfire.org Git - thirdparty/git.git/blame - diagnose.c
l10n: zh_CN v2.38.0 rounds 1 & 2
[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"
7#include "strvec.h"
8#include "object-store.h"
9#include "packfile.h"
10
33cba726
VD
11struct archive_dir {
12 const char *path;
13 int recursive;
14};
15
7ecf193f
VD
16struct diagnose_option {
17 enum diagnose_mode mode;
18 const char *option_name;
19};
20
21static struct diagnose_option diagnose_options[] = {
22 { DIAGNOSE_STATS, "stats" },
23 { DIAGNOSE_ALL, "all" },
24};
25
26int option_parse_diagnose(const struct option *opt, const char *arg, int unset)
27{
28 int i;
29 enum diagnose_mode *diagnose = opt->value;
30
31 if (!arg) {
32 *diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS;
33 return 0;
34 }
35
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;
40 }
41 }
42
43 return error(_("invalid --%s value '%s'"), opt->long_name, arg);
44}
45
bb2c3495
VD
46static void dir_file_stats_objects(const char *full_path, size_t full_path_len,
47 const char *file_name, void *data)
48{
49 struct strbuf *buf = data;
50 struct stat st;
51
52 if (!stat(full_path, &st))
53 strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name,
54 (uintmax_t)st.st_size);
55}
56
57static int dir_file_stats(struct object_directory *object_dir, void *data)
58{
59 struct strbuf *buf = data;
60
61 strbuf_addf(buf, "Contents of %s:\n", object_dir->path);
62
63 for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects,
64 data);
65
66 return 0;
67}
68
cb98e1d5
VD
69/*
70 * Get the d_type of a dirent. If the d_type is unknown, derive it from
71 * stat.st_mode.
72 *
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.
76 */
77static unsigned char get_dtype(struct dirent *e, struct strbuf *path)
bb2c3495 78{
cb98e1d5
VD
79 struct stat st;
80 unsigned char dtype = DTYPE(e);
81 size_t base_path_len;
82
83 if (dtype != DT_UNKNOWN)
84 return dtype;
85
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;
91
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;
99
100cleanup:
101 strbuf_setlen(path, base_path_len);
102 return dtype;
103}
104
105static int count_files(struct strbuf *path)
106{
107 DIR *dir = opendir(path->buf);
bb2c3495
VD
108 struct dirent *e;
109 int count = 0;
110
111 if (!dir)
112 return 0;
113
cb98e1d5
VD
114 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
115 if (get_dtype(e, path) == DT_REG)
bb2c3495
VD
116 count++;
117
118 closedir(dir);
119 return count;
120}
121
122static void loose_objs_stats(struct strbuf *buf, const char *path)
123{
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;
131
132 if (!dir)
133 return;
134
135 strbuf_addstr(buf, "Object directory stats for ");
136 strbuf_add_absolute_path(buf, path);
137 strbuf_addstr(buf, ":\n");
138
139 strbuf_add_absolute_path(&count_path, path);
140 strbuf_addch(&count_path, '/');
141 base_path_len = count_path.len;
142
cb98e1d5
VD
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 &&
bb2c3495
VD
146 !hex_to_bytes(&c, e->d_name, 1)) {
147 strbuf_setlen(&count_path, base_path_len);
cb98e1d5
VD
148 strbuf_addf(&count_path, "%s/", e->d_name);
149 total += (count = count_files(&count_path));
bb2c3495
VD
150 strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
151 }
152
153 strbuf_addf(buf, "Total: %d loose objects", total);
154
155 strbuf_release(&count_path);
156 closedir(dir);
157}
158
159static int add_directory_to_archiver(struct strvec *archiver_args,
160 const char *path, int recurse)
161{
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;
168
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;
174 }
175 return error_errno(_("could not open directory '%s'"), path);
176 }
177
178 if (!at_root)
179 strbuf_addf(&buf, "%s/", path);
180 len = buf.len;
181 strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
182
cb98e1d5
VD
183 while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
184 struct strbuf abspath = STRBUF_INIT;
185 unsigned char dtype;
186
187 strbuf_add_absolute_path(&abspath, at_root ? "." : path);
188 strbuf_addch(&abspath, '/');
189 dtype = get_dtype(e, &abspath);
bb2c3495
VD
190
191 strbuf_setlen(&buf, len);
192 strbuf_addstr(&buf, e->d_name);
193
cb98e1d5 194 if (dtype == DT_REG)
bb2c3495 195 strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
cb98e1d5 196 else if (dtype != DT_DIR)
bb2c3495
VD
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;
cb98e1d5
VD
203
204 strbuf_release(&abspath);
bb2c3495
VD
205 }
206
207 closedir(dir);
208 strbuf_release(&buf);
209 return res;
210}
211
33cba726 212int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
bb2c3495
VD
213{
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;
33cba726
VD
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 }
225 };
226
227 if (mode == DIAGNOSE_NONE) {
228 res = 0;
229 goto diagnose_cleanup;
230 }
bb2c3495
VD
231
232 stdout_fd = dup(STDOUT_FILENO);
233 if (stdout_fd < 0) {
234 res = error_errno(_("could not duplicate stdout"));
235 goto diagnose_cleanup;
236 }
237
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;
242 }
243
244 init_zip_archiver();
245 strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
246
247 strbuf_reset(&buf);
248 strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
249 get_version_info(&buf, 1);
250
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);
257
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);
263
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);
268
33cba726
VD
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;
278 }
279 }
280 }
bb2c3495
VD
281
282 strvec_pushl(&archiver_args, "--prefix=",
283 oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
284
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;
293 }
294
295 fprintf(stderr, "\n"
296 "Diagnostics complete.\n"
297 "All of the gathered info is captured in '%s'\n",
298 zip_path->buf);
299
300diagnose_cleanup:
301 if (archiver_fd >= 0) {
302 dup2(stdout_fd, STDOUT_FILENO);
303 close(stdout_fd);
304 close(archiver_fd);
305 }
306 free(argv_copy);
307 strvec_clear(&archiver_args);
308 strbuf_release(&buf);
309
310 return res;
311}