3 #include "compat/disk.h"
9 #include "object-store.h"
17 struct diagnose_option
{
18 enum diagnose_mode mode
;
19 const char *option_name
;
22 static struct diagnose_option diagnose_options
[] = {
23 { DIAGNOSE_STATS
, "stats" },
24 { DIAGNOSE_ALL
, "all" },
27 int option_parse_diagnose(const struct option
*opt
, const char *arg
, int unset
)
30 enum diagnose_mode
*diagnose
= opt
->value
;
33 *diagnose
= unset
? DIAGNOSE_NONE
: DIAGNOSE_STATS
;
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
;
44 return error(_("invalid --%s value '%s'"), opt
->long_name
, arg
);
47 static void dir_file_stats_objects(const char *full_path
,
48 size_t full_path_len UNUSED
,
49 const char *file_name
, void *data
)
51 struct strbuf
*buf
= data
;
54 if (!stat(full_path
, &st
))
55 strbuf_addf(buf
, "%-70s %16" PRIuMAX
"\n", file_name
,
56 (uintmax_t)st
.st_size
);
59 static int dir_file_stats(struct object_directory
*object_dir
, void *data
)
61 struct strbuf
*buf
= data
;
63 strbuf_addf(buf
, "Contents of %s:\n", object_dir
->path
);
65 for_each_file_in_pack_dir(object_dir
->path
, dir_file_stats_objects
,
72 * Get the d_type of a dirent. If the d_type is unknown, derive it from
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.
79 static unsigned char get_dtype(struct dirent
*e
, struct strbuf
*path
)
82 unsigned char dtype
= DTYPE(e
);
85 if (dtype
!= DT_UNKNOWN
)
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
))
94 /* determine d_type from st_mode */
95 if (S_ISREG(st
.st_mode
))
97 else if (S_ISDIR(st
.st_mode
))
99 else if (S_ISLNK(st
.st_mode
))
103 strbuf_setlen(path
, base_path_len
);
107 static int count_files(struct strbuf
*path
)
109 DIR *dir
= opendir(path
->buf
);
116 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
117 if (get_dtype(e
, path
) == DT_REG
)
124 static void loose_objs_stats(struct strbuf
*buf
, const char *path
)
126 DIR *dir
= opendir(path
);
131 struct strbuf count_path
= STRBUF_INIT
;
132 size_t base_path_len
;
137 strbuf_addstr(buf
, "Object directory stats for ");
138 strbuf_add_absolute_path(buf
, path
);
139 strbuf_addstr(buf
, ":\n");
141 strbuf_add_absolute_path(&count_path
, path
);
142 strbuf_addch(&count_path
, '/');
143 base_path_len
= count_path
.len
;
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 &&
148 !hex_to_bytes(&c
, e
->d_name
, 1)) {
149 strbuf_setlen(&count_path
, base_path_len
);
150 strbuf_addf(&count_path
, "%s/", e
->d_name
);
151 total
+= (count
= count_files(&count_path
));
152 strbuf_addf(buf
, "%s : %7d files\n", e
->d_name
, count
);
155 strbuf_addf(buf
, "Total: %d loose objects", total
);
157 strbuf_release(&count_path
);
161 static int add_directory_to_archiver(struct strvec
*archiver_args
,
162 const char *path
, int recurse
)
164 int at_root
= !*path
;
167 struct strbuf buf
= STRBUF_INIT
;
171 dir
= opendir(at_root
? "." : path
);
173 if (errno
== ENOENT
) {
174 warning(_("could not archive missing directory '%s'"), path
);
177 return error_errno(_("could not open directory '%s'"), path
);
181 strbuf_addf(&buf
, "%s/", path
);
183 strvec_pushf(archiver_args
, "--prefix=%s", buf
.buf
);
185 while (!res
&& (e
= readdir_skip_dot_and_dotdot(dir
))) {
186 struct strbuf abspath
= STRBUF_INIT
;
189 strbuf_add_absolute_path(&abspath
, at_root
? "." : path
);
190 strbuf_addch(&abspath
, '/');
191 dtype
= get_dtype(e
, &abspath
);
193 strbuf_setlen(&buf
, len
);
194 strbuf_addstr(&buf
, e
->d_name
);
197 strvec_pushf(archiver_args
, "--add-file=%s", buf
.buf
);
198 else if (dtype
!= DT_DIR
)
199 warning(_("skipping '%s', which is neither file nor "
200 "directory"), buf
.buf
);
202 add_directory_to_archiver(archiver_args
,
203 buf
.buf
, recurse
) < 0)
206 strbuf_release(&abspath
);
210 strbuf_release(&buf
);
214 int create_diagnostics_archive(struct strbuf
*zip_path
, enum diagnose_mode mode
)
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
;
221 struct archive_dir archive_dirs
[] = {
226 { ".git/objects/info", 0 }
229 if (mode
== DIAGNOSE_NONE
) {
231 goto diagnose_cleanup
;
234 stdout_fd
= dup(STDOUT_FILENO
);
236 res
= error_errno(_("could not duplicate stdout"));
237 goto diagnose_cleanup
;
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
;
247 strvec_pushl(&archiver_args
, "git-diagnose", "--format=zip", NULL
);
250 strbuf_addstr(&buf
, "Collecting diagnostic info\n\n");
251 get_version_info(&buf
, 1);
253 strbuf_addf(&buf
, "Repository root: %s\n", the_repository
->worktree
);
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
);
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
);
267 strbuf_addstr(&buf
, "--add-virtual-file=objects-local.txt:");
268 loose_objs_stats(&buf
, ".git/objects");
269 strvec_push(&archiver_args
, buf
.buf
);
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
;
284 strvec_pushl(&archiver_args
, "--prefix=",
285 oid_to_hex(the_hash_algo
->empty_tree
), "--", NULL
);
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);
293 error(_("failed to write archive"));
294 goto diagnose_cleanup
;
298 "Diagnostics complete.\n"
299 "All of the gathered info is captured in '%s'\n",
303 if (archiver_fd
>= 0) {
304 dup2(stdout_fd
, STDOUT_FILENO
);
309 strvec_clear(&archiver_args
);
310 strbuf_release(&buf
);