3 #include "compat/disk.h"
8 #include "object-store.h"
16 struct diagnose_option
{
17 enum diagnose_mode mode
;
18 const char *option_name
;
21 static struct diagnose_option diagnose_options
[] = {
22 { DIAGNOSE_STATS
, "stats" },
23 { DIAGNOSE_ALL
, "all" },
26 int option_parse_diagnose(const struct option
*opt
, const char *arg
, int unset
)
29 enum diagnose_mode
*diagnose
= opt
->value
;
32 *diagnose
= unset
? DIAGNOSE_NONE
: DIAGNOSE_STATS
;
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
;
43 return error(_("invalid --%s value '%s'"), opt
->long_name
, arg
);
46 static void dir_file_stats_objects(const char *full_path
, size_t full_path_len
,
47 const char *file_name
, void *data
)
49 struct strbuf
*buf
= data
;
52 if (!stat(full_path
, &st
))
53 strbuf_addf(buf
, "%-70s %16" PRIuMAX
"\n", file_name
,
54 (uintmax_t)st
.st_size
);
57 static int dir_file_stats(struct object_directory
*object_dir
, void *data
)
59 struct strbuf
*buf
= data
;
61 strbuf_addf(buf
, "Contents of %s:\n", object_dir
->path
);
63 for_each_file_in_pack_dir(object_dir
->path
, dir_file_stats_objects
,
70 * Get the d_type of a dirent. If the d_type is unknown, derive it from
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.
77 static unsigned char get_dtype(struct dirent
*e
, struct strbuf
*path
)
80 unsigned char dtype
= DTYPE(e
);
83 if (dtype
!= DT_UNKNOWN
)
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
))
92 /* determine d_type from st_mode */
93 if (S_ISREG(st
.st_mode
))
95 else if (S_ISDIR(st
.st_mode
))
97 else if (S_ISLNK(st
.st_mode
))
101 strbuf_setlen(path
, base_path_len
);
105 static int count_files(struct strbuf
*path
)
107 DIR *dir
= opendir(path
->buf
);
114 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
115 if (get_dtype(e
, path
) == DT_REG
)
122 static void loose_objs_stats(struct strbuf
*buf
, const char *path
)
124 DIR *dir
= opendir(path
);
129 struct strbuf count_path
= STRBUF_INIT
;
130 size_t base_path_len
;
135 strbuf_addstr(buf
, "Object directory stats for ");
136 strbuf_add_absolute_path(buf
, path
);
137 strbuf_addstr(buf
, ":\n");
139 strbuf_add_absolute_path(&count_path
, path
);
140 strbuf_addch(&count_path
, '/');
141 base_path_len
= count_path
.len
;
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 &&
146 !hex_to_bytes(&c
, e
->d_name
, 1)) {
147 strbuf_setlen(&count_path
, base_path_len
);
148 strbuf_addf(&count_path
, "%s/", e
->d_name
);
149 total
+= (count
= count_files(&count_path
));
150 strbuf_addf(buf
, "%s : %7d files\n", e
->d_name
, count
);
153 strbuf_addf(buf
, "Total: %d loose objects", total
);
155 strbuf_release(&count_path
);
159 static int add_directory_to_archiver(struct strvec
*archiver_args
,
160 const char *path
, int recurse
)
162 int at_root
= !*path
;
165 struct strbuf buf
= STRBUF_INIT
;
169 dir
= opendir(at_root
? "." : path
);
171 if (errno
== ENOENT
) {
172 warning(_("could not archive missing directory '%s'"), path
);
175 return error_errno(_("could not open directory '%s'"), path
);
179 strbuf_addf(&buf
, "%s/", path
);
181 strvec_pushf(archiver_args
, "--prefix=%s", buf
.buf
);
183 while (!res
&& (e
= readdir_skip_dot_and_dotdot(dir
))) {
184 struct strbuf abspath
= STRBUF_INIT
;
187 strbuf_add_absolute_path(&abspath
, at_root
? "." : path
);
188 strbuf_addch(&abspath
, '/');
189 dtype
= get_dtype(e
, &abspath
);
191 strbuf_setlen(&buf
, len
);
192 strbuf_addstr(&buf
, e
->d_name
);
195 strvec_pushf(archiver_args
, "--add-file=%s", buf
.buf
);
196 else if (dtype
!= DT_DIR
)
197 warning(_("skipping '%s', which is neither file nor "
198 "directory"), buf
.buf
);
200 add_directory_to_archiver(archiver_args
,
201 buf
.buf
, recurse
) < 0)
204 strbuf_release(&abspath
);
208 strbuf_release(&buf
);
212 int create_diagnostics_archive(struct strbuf
*zip_path
, enum diagnose_mode mode
)
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
;
219 struct archive_dir archive_dirs
[] = {
224 { ".git/objects/info", 0 }
227 if (mode
== DIAGNOSE_NONE
) {
229 goto diagnose_cleanup
;
232 stdout_fd
= dup(STDOUT_FILENO
);
234 res
= error_errno(_("could not duplicate stdout"));
235 goto diagnose_cleanup
;
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
;
245 strvec_pushl(&archiver_args
, "git-diagnose", "--format=zip", NULL
);
248 strbuf_addstr(&buf
, "Collecting diagnostic info\n\n");
249 get_version_info(&buf
, 1);
251 strbuf_addf(&buf
, "Repository root: %s\n", the_repository
->worktree
);
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
);
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
);
265 strbuf_addstr(&buf
, "--add-virtual-file=objects-local.txt:");
266 loose_objs_stats(&buf
, ".git/objects");
267 strvec_push(&archiver_args
, buf
.buf
);
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
;
282 strvec_pushl(&archiver_args
, "--prefix=",
283 oid_to_hex(the_hash_algo
->empty_tree
), "--", NULL
);
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);
291 error(_("failed to write archive"));
292 goto diagnose_cleanup
;
296 "Diagnostics complete.\n"
297 "All of the gathered info is captured in '%s'\n",
301 if (archiver_fd
>= 0) {
302 dup2(stdout_fd
, STDOUT_FILENO
);
307 strvec_clear(&archiver_args
);
308 strbuf_release(&buf
);