2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file select.c The select mode of adu. */
9 #include <dirent.h> /* readdir() */
11 #include "gcc-compat.h"
16 #include "portable_io.h"
18 /** Global dir count. */
19 static uint64_t num_dirs;
20 /** Global files count. */
21 static uint64_t num_files;
22 /** Global bytes count. */
23 static uint64_t num_bytes;
25 /** The decimal representation of an uint64_t never exceeds that size. */
26 #define FORMATED_VALUE_SIZE 25
28 /* these get filled in by the select command. */
29 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
31 enum global_stats_flags {
32 GSF_PRINT_DIRNAME = 1,
35 GSF_COMPUTE_SUMMARY = 8,
38 struct global_stats_info {
42 enum global_stats_flags flags;
45 enum user_stats_flags {
46 USF_PRINT_DIRNAME = 1,
49 USF_COMPUTE_SUMMARY = 8,
52 struct user_stats_info {
54 enum user_stats_flags flags;
60 static const uint64_t size_unit_divisors[] = {
61 [size_unit_arg_b] = 1ULL,
62 [size_unit_arg_k] = 1024ULL,
63 [size_unit_arg_m] = 1024ULL * 1024ULL,
64 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
65 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
68 static const uint64_t count_unit_divisors[] = {
70 [count_unit_arg_n] = 1ULL,
71 [count_unit_arg_k] = 1000ULL,
72 [count_unit_arg_m] = 1000ULL * 1000ULL,
73 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
74 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
77 static const char size_unit_abbrevs[] = " BKMGT";
78 static const char count_unit_abbrevs[] = " kmgt";
80 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
81 uint64_t value, int print_unit, char *result)
83 enum enum_size_unit u = unit;
84 char unit_buf[2] = "\0\0";
86 if (unit == size_unit_arg_h) /* human readable */
87 for (u = size_unit_arg_b; u < size_unit_arg_t &&
88 value > size_unit_divisors[u + 1]; u++)
91 unit_buf[0] = size_unit_abbrevs[u];
92 sprintf(result, "%llu%s",
93 (long long unsigned)value / size_unit_divisors[u], unit_buf);
97 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
98 uint64_t value, int print_unit, char *result)
100 enum enum_count_unit u = unit;
101 char unit_buf[2] = "\0\0";
103 if (unit == count_unit_arg_h) /* human readable */
104 for (u = count_unit_arg_n; u < count_unit_arg_t &&
105 value > count_unit_divisors[u + 1]; u++)
108 unit_buf[0] = count_unit_abbrevs[u];
109 sprintf(result, "%llu%s",
110 (long long unsigned)value / count_unit_divisors[u], unit_buf);
114 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
116 char *result = NULL, *tmp;
118 uint64_t val = *dirnum;
119 struct osl_object obj = {.data = &val, .size = sizeof(val)};
123 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
126 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
130 tmp = make_message("%s/%s", (char *)obj.data, result);
134 result = adu_strdup((char *)obj.data);
135 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
138 val = *(uint64_t *)obj.data;
150 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
152 struct osl_object obj;
154 char *this_dir, *prefix = NULL;
157 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
160 this_dir = adu_strdup((char *)obj.data);
161 ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
164 if (!*(uint64_t *)obj.data) {
168 ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
171 *name = make_message("%s/%s", prefix, this_dir);
178 static int user_stats_loop_function(struct osl_row *row, void *data)
180 struct user_stats_info *usi = data;
181 struct osl_object obj;
182 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
183 char formated_value[FORMATED_VALUE_SIZE];
186 if (!usi->count && !summary) {
187 ret = -E_LOOP_COMPLETE;
190 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
192 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
195 files = *(uint64_t *)obj.data;
196 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
197 format_count_value(conf.count_unit_arg, files,
198 conf.count_unit_arg == count_unit_arg_h,
200 printf("\t%s%s", formated_value,
201 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
206 usi->ui->files += files;
208 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
210 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
213 bytes = *(uint64_t *)obj.data;
214 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
215 format_size_value(conf.size_unit_arg, bytes,
216 conf.size_unit_arg == size_unit_arg_h,
219 (usi->flags & USF_PRINT_FILES)? "" : "\t",
221 usi->flags & USF_PRINT_DIRNAME? "\t" : "\n"
225 usi->ui->bytes += bytes;
230 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
232 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
235 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
239 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
248 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
252 static int global_stats_loop_function(struct osl_row *row, void *data)
254 struct global_stats_info *gsi = data;
255 struct osl_object obj;
256 char *dirname, formated_value[FORMATED_VALUE_SIZE];
257 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
260 if (!gsi->count && !summary) {
261 ret = -E_LOOP_COMPLETE;
264 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
266 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
269 files = *(uint64_t *)obj.data;
270 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
271 format_count_value(conf.count_unit_arg, files,
272 conf.count_unit_arg == count_unit_arg_h,
274 printf("\t%s%s", formated_value,
275 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
281 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
283 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
286 bytes = *(uint64_t *)obj.data;
287 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
288 format_size_value(conf.size_unit_arg, bytes,
289 conf.size_unit_arg == size_unit_arg_h,
292 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
294 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
302 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
303 ret = get_dir_name_of_row(row, &dirname);
307 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
316 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
320 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
324 assert(ret == -E_OSL);
325 if (osl_errno != E_OSL_LOOP)
326 /* error not caused by loop function returning negative. */
328 assert(loop_ret < 0);
329 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
331 if (loop_ret == -E_OSL) { /* osl error in loop function */
332 assert(loop_osl_errno);
333 osl_errno = loop_osl_errno;
338 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
339 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
341 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
342 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
345 static void print_global_summary(void)
347 char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
348 s[FORMATED_VALUE_SIZE];
349 enum enum_count_unit ud, uf;
350 enum enum_size_unit us;
352 ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d);
353 uf = format_count_value(conf.count_unit_arg, num_files, 0, f);
354 us = format_size_value(conf.size_unit_arg, num_bytes, 0, s);
356 printf("Global summary "
357 "(dirs(%c)/files(%c)/size(%c))\n"
359 count_unit_abbrevs[ud],
360 count_unit_abbrevs[uf],
361 size_unit_abbrevs[us],
367 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
369 char formated_dir_count[FORMATED_VALUE_SIZE],
370 formated_file_count[FORMATED_VALUE_SIZE],
371 formated_bytes[FORMATED_VALUE_SIZE ];
373 format_count_value(conf.count_unit_arg, ui->dirs,
374 conf.count_unit_arg == count_unit_arg_h,
376 format_count_value(conf.count_unit_arg, ui->files,
377 conf.count_unit_arg == count_unit_arg_h,
378 formated_file_count);
379 format_size_value(conf.size_unit_arg, ui->bytes,
380 conf.size_unit_arg == size_unit_arg_h,
382 printf("\t%s\t%u\t%s\t%s\t%s\n",
383 ui->pw_name? ui->pw_name : "?",
392 static void print_user_summary(void)
394 printf("User summary "
395 "(pw_name/uid/dirs%s/files%s/size%s):\n",
396 count_unit_buf, count_unit_buf, size_unit_buf);
397 for_each_admissible_user(print_user_summary_line, NULL);
400 static int print_user_stat(struct user_info *ui, __a_unused void *data)
403 struct user_stats_info usi = {
404 .count = conf.limit_arg,
408 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
409 printf("%s (uid %u), by size%s:\n",
410 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
412 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
413 &usi.ret, &usi.osl_errno);
416 printf("\n%s (uid %u), by file count%s:\n",
417 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
419 usi.count = conf.limit_arg,
420 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
421 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
422 &usi.ret, &usi.osl_errno);
429 static int print_user_stats(void)
431 return for_each_admissible_user(print_user_stat, NULL);
434 static int print_statistics(void)
437 struct global_stats_info gsi = {
438 .count = conf.limit_arg,
439 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
442 printf("By size%s:\n",
444 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
445 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
450 gsi.count = conf.limit_arg;
451 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
452 printf("By file count%s:\n",
454 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
455 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
459 print_global_summary();
461 print_user_summary();
465 static int read_uid_file(void)
469 char *filename = get_uid_list_name(), *map;
470 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
474 INFO_LOG("failed to map %s\n", filename);
479 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
482 * Compute number of hash table bits. The hash table size must be a
483 * power of two and larger than the number of uids.
486 while (1 << bits < num_uids)
488 create_hash_table(bits);
489 for (n = 0; n < num_uids; n++) {
490 uint32_t uid = read_u32(map + n * sizeof(uid));
491 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
496 adu_munmap(map, size);
504 if (conf.count_unit_arg != count_unit_arg_h)
505 count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
507 count_unit_buf[0] = '\0';
508 if (conf.size_unit_arg != size_unit_arg_h)
509 size_unit_buf[1] = size_unit_abbrevs[conf.size_unit_arg];
511 size_unit_buf[0] = '\0';
513 ret = open_dir_table(0);
517 ret = read_uid_file();
521 ret = print_statistics();