2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
9 #include "portable_io.h"
14 /** Command line and config file options. */
15 static struct gengetopt_args_info conf;
18 /** whether this slot of the hash table is used. */
20 /** whether this uid should be taken into account. */
27 struct osl_table *table;
31 struct osl_table_description *desc;
34 /** The decimal representation of an uint64_t never exceeds that size. */
35 #define FORMATED_VALUE_SIZE 25
39 * Contains info for each user that owns at least one regular file.
41 * Even users that are not taken into account because of the --uid
42 * option occupy a slot in this hash table. This allows to find out
43 * quicky whether a uid is admissible. And yes, this has to be fast.
45 static struct user_info *uid_hash_table;
47 /* these get filled in by the select command. */
48 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
50 static inline int ui_used(struct user_info *ui)
52 return ui->flags & UI_FL_SLOT_USED;
55 static inline int ui_admissible(struct user_info *ui)
57 return ui->flags & UI_FL_ADMISSIBLE;
65 static struct uid_range *admissible_uids;
67 static inline int check_uid_arg(const char *arg, uint32_t *uid)
69 const uint32_t max = ~0U;
71 * we need an 64-bit int for string -> uid conversion because strtoll()
72 * returns a signed value.
75 int ret = para_atoi64(arg, &val);
79 if (val < 0 || val > max)
80 return -ERRNO_TO_ERROR(EINVAL);
85 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
88 char *arg = para_strdup(orig_arg), *p = strchr(arg, '-');
90 if (!p || p == arg) { /* -42 or 42 */
91 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
94 ur->low = p? 0 : ur->high;
101 ret = check_uid_arg(arg, &ur->low);
105 if (*p) { /* 42-4711 */
106 ret = check_uid_arg(p, &ur->high);
110 if (ur->low > ur->high)
111 ret = -ERRNO_TO_ERROR(EINVAL);
114 ERROR_LOG("bad uid option: %s\n", orig_arg);
116 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
123 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
124 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
129 * \param ll Loglevel.
130 * \param fml Usual format string.
132 * All XXX_LOG() macros use this function.
134 __printf_2_3 void __log(int ll, const char* fmt,...)
142 if (ll < conf.loglevel_arg)
147 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
148 fprintf(outfd, "%s ", str);
150 vfprintf(outfd, fmt, argp);
155 * Compare the size of two directories
157 * \param obj1 Pointer to the first object.
158 * \param obj2 Pointer to the second object.
160 * This function first compares the size values as usual integers. If they compare as
161 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
162 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
164 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
166 uint64_t d1 = *(uint64_t *)obj1->data;
167 uint64_t d2 = *(uint64_t *)obj2->data;
168 int ret = NUM_COMPARE(d2, d1);
172 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
173 return NUM_COMPARE(obj2->data, obj1->data);
177 * Compare two osl objects pointing to unsigned integers of 64 bit size.
179 * \param obj1 Pointer to the first integer.
180 * \param obj2 Pointer to the second integer.
182 * \return The values required for an osl compare function.
184 * \sa osl_compare_func, osl_hash_compare().
186 static int uint64_compare(const struct osl_object *obj1,
187 const struct osl_object *obj2)
189 uint64_t d1 = read_u64((const char *)obj1->data);
190 uint64_t d2 = read_u64((const char *)obj2->data);
199 /** The columns of the directory table. */
200 enum dir_table_columns {
201 /** The name of the directory. */
203 /** The dir count number. */
205 /** The number of the parent directory. */
207 /** The number of bytes of all regular files. */
209 /** The number of all regular files. */
211 /** Number of columns in this table. */
215 static struct osl_column_description dir_table_cols[] = {
217 .storage_type = OSL_MAPPED_STORAGE,
222 .storage_type = OSL_MAPPED_STORAGE,
223 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
225 .compare_function = uint64_compare,
226 .data_size = sizeof(uint64_t)
229 .storage_type = OSL_MAPPED_STORAGE,
230 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
231 .name = "parent_num",
232 .compare_function = size_compare,
233 .data_size = sizeof(uint64_t)
236 .storage_type = OSL_MAPPED_STORAGE,
237 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
238 .compare_function = size_compare,
240 .data_size = sizeof(uint64_t)
243 .storage_type = OSL_MAPPED_STORAGE,
244 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
245 .compare_function = size_compare,
247 .data_size = sizeof(uint64_t)
251 static struct osl_table_description dir_table_desc = {
253 .num_columns = NUM_DT_COLUMNS,
255 .column_descriptions = dir_table_cols,
258 /** The columns of the id table. */
259 enum user_table_columns {
260 /** The numer of the directory. */
262 /** The number of bytes of all regular files in this dir owned by this id. */
264 /** The number of files in this dir owned by this id. */
266 /** Number of columns in this table. */
270 static struct osl_column_description user_table_cols[] = {
272 .storage_type = OSL_MAPPED_STORAGE,
273 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
275 .compare_function = uint64_compare,
276 .data_size = sizeof(uint64_t)
279 .storage_type = OSL_MAPPED_STORAGE,
280 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
281 .compare_function = size_compare,
283 .data_size = sizeof(uint64_t)
286 .storage_type = OSL_MAPPED_STORAGE,
287 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
288 .compare_function = size_compare,
290 .data_size = sizeof(uint64_t)
294 static struct osl_table *dir_table;
296 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
297 uint64_t *dir_size, uint64_t *dir_files)
299 struct osl_object dir_objects[NUM_DT_COLUMNS];
301 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
302 dir_objects[DT_NAME].data = dirname;
303 dir_objects[DT_NAME].size = strlen(dirname) + 1;
304 dir_objects[DT_NUM].data = dir_num;
305 dir_objects[DT_NUM].size = sizeof(*dir_num);
306 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
307 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
308 dir_objects[DT_BYTES].data = dir_size;
309 dir_objects[DT_BYTES].size = sizeof(*dir_size);
310 dir_objects[DT_FILES].data = dir_files;
311 dir_objects[DT_FILES].size = sizeof(*dir_files);
312 return osl(osl_add_row(dir_table, dir_objects));
315 static uint32_t num_uids;
317 static int open_user_table(struct user_info *ui, int create)
321 ui->desc = para_malloc(sizeof(*ui->desc));
322 ui->desc->num_columns = NUM_UT_COLUMNS;
324 ui->desc->column_descriptions = user_table_cols;
325 ui->desc->dir = para_strdup(conf.database_dir_arg);
326 ui->desc->name = make_message("%u", (unsigned)ui->uid);
327 INFO_LOG(".............................uid #%u: %u\n",
328 (unsigned)num_uids, (unsigned)ui->uid);
330 ret = osl(osl_create_table(ui->desc));
335 ret = osl(osl_open_table(ui->desc, &ui->table));
340 free((char *)ui->desc->name);
341 free((char *)ui->desc->dir);
343 ui->desc->name = NULL;
344 ui->desc->dir = NULL;
351 #define uid_hash_bits 8
352 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
353 #define PRIME1 0x811c9dc5
354 #define PRIME2 0x01000193
356 static void create_hash_table(void)
358 uid_hash_table = para_calloc(uid_hash_table_size
359 * sizeof(struct user_info));
362 static void free_hash_table(void)
364 free(uid_hash_table);
365 uid_hash_table = NULL;
368 static int create_tables(void)
372 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
373 ret = osl(osl_create_table(&dir_table_desc));
381 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
382 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
383 * unused slots in the table are used to store different uids that hash to the
386 * If a hash collision occurs, different slots are successively probed in order
387 * to find an unused slot for the new uid. Probing is implemented via a second
388 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
391 * An odd number is sufficient to make sure each entry of the hash table gets
392 * probed for probe_num between 0 and s-1 because s is a power of two, hence
393 * the second hash value has never a common divisor with the hash table size.
394 * IOW: h is invertible in the ring [0..s].
396 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
398 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
399 % uid_hash_table_size;
402 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
403 + uid_hash_table_size; ui++)
405 enum search_uid_flags {
407 CREATE_USER_TABLE = 2,
410 static int uid_is_admissible(uint32_t uid)
414 for (i = 0; i < conf.uid_given; i++) {
415 struct uid_range *ur = admissible_uids + i;
417 if (ur->low <= uid && ur->high >= uid)
420 i = !conf.uid_given || i < conf.uid_given;
421 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
426 static int search_uid(uint32_t uid, enum search_uid_flags flags,
427 struct user_info **ui_ptr)
431 for (p = 0; p < uid_hash_table_size; p++) {
432 struct user_info *ui = uid_hash_table + double_hash(uid, p);
439 ui->flags |= UI_FL_SLOT_USED;
440 if (!uid_is_admissible(uid))
442 ui->flags |= UI_FL_ADMISSIBLE;
443 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
457 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
460 static int update_user_row(struct osl_table *t, uint64_t dir_num,
464 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
466 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
468 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
470 if (ret < 0) { /* this is the first file we add */
471 struct osl_object objects[NUM_UT_COLUMNS];
472 uint64_t num_files = 1;
474 objects[UT_DIR_NUM].data = &dir_num;
475 objects[UT_DIR_NUM].size = sizeof(dir_num);
476 objects[UT_BYTES].data = add;
477 objects[UT_BYTES].size = sizeof(*add);
478 objects[UT_FILES].data = &num_files;
479 objects[UT_FILES].size = sizeof(num_files);
480 INFO_LOG("######################### ret: %d\n", ret);
481 ret = osl(osl_add_row(t, objects));
482 INFO_LOG("######################### ret: %d\n", ret);
484 } else { /* add size and increment file count */
486 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
488 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
491 num = *(uint64_t *)obj1.data + *add;
492 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
495 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
498 num = *(uint64_t *)obj1.data + 1;
499 return osl(osl_update_object(t, row, UT_FILES, &obj2));
503 static uint64_t num_dirs;
504 static uint64_t num_files;
505 static uint64_t num_bytes;
507 int scan_dir(char *dirname, uint64_t *parent_dir_num)
510 struct dirent *entry;
511 int ret, cwd_fd, ret2;
512 uint64_t dir_size = 0, dir_files = 0;
513 uint64_t this_dir_num = ++num_dirs;
515 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
516 ret = para_opendir(dirname, &dir, &cwd_fd);
518 if (ret != -ERRNO_TO_ERROR(EACCES))
520 WARNING_LOG("permission denied for %s\n", dirname);
523 while ((entry = readdir(dir))) {
528 struct user_info *ui;
530 if (!strcmp(entry->d_name, "."))
532 if (!strcmp(entry->d_name, ".."))
534 if (lstat(entry->d_name, &s) == -1) {
535 WARNING_LOG("lstat error for %s/%s\n", dirname,
540 if (!S_ISREG(m) && !S_ISDIR(m))
543 ret = scan_dir(entry->d_name, &this_dir_num);
555 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
560 ret = update_user_row(ui->table, this_dir_num, &size);
564 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
565 &dir_size, &dir_files);
568 ret2 = para_fchdir(cwd_fd);
569 if (ret2 < 0 && ret >= 0)
575 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
577 char *result = NULL, *tmp;
579 uint64_t val = *dirnum;
580 struct osl_object obj = {.data = &val, .size = sizeof(val)};
584 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
587 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
591 tmp = make_message("%s/%s", (char *)obj.data, result);
595 result = para_strdup((char *)obj.data);
596 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
599 val = *(uint64_t *)obj.data;
611 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
613 struct osl_object obj;
615 char *this_dir, *prefix = NULL;
618 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
621 this_dir = para_strdup((char *)obj.data);
622 ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
625 if (!*(uint64_t *)obj.data) {
629 ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
632 *name = make_message("%s/%s", prefix, this_dir);
640 const uint64_t size_unit_divisors[] = {
641 [size_unit_arg_b] = 1ULL,
642 [size_unit_arg_k] = 1024ULL,
643 [size_unit_arg_m] = 1024ULL * 1024ULL,
644 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
645 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
648 const uint64_t count_unit_divisors[] = {
650 [count_unit_arg_n] = 1ULL,
651 [count_unit_arg_k] = 1000ULL,
652 [count_unit_arg_m] = 1000ULL * 1000ULL,
653 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
654 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
657 const char size_unit_abbrevs[] = " BKMGT";
658 const char count_unit_abbrevs[] = " kmgt";
660 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
661 uint64_t value, int print_unit, char *result)
663 enum enum_size_unit u = unit;
664 char unit_buf[2] = "\0\0";
666 if (unit == size_unit_arg_h) /* human readable */
667 for (u = size_unit_arg_b; u < size_unit_arg_t &&
668 value > size_unit_divisors[u + 1]; u++)
671 unit_buf[0] = size_unit_abbrevs[u];
672 sprintf(result, "%llu%s",
673 (long long unsigned)value / size_unit_divisors[u], unit_buf);
677 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
678 uint64_t value, int print_unit, char *result)
680 enum enum_count_unit u = unit;
681 char unit_buf[2] = "\0\0";
683 if (unit == count_unit_arg_h) /* human readable */
684 for (u = count_unit_arg_n; u < count_unit_arg_t &&
685 value > count_unit_divisors[u + 1]; u++)
688 unit_buf[0] = count_unit_abbrevs[u];
689 sprintf(result, "%llu%s",
690 (long long unsigned)value / count_unit_divisors[u], unit_buf);
694 enum global_stats_flags {
695 GSF_PRINT_DIRNAME = 1,
698 GSF_COMPUTE_SUMMARY = 8,
701 struct global_stats_info {
705 enum global_stats_flags flags;
708 static int global_stats_loop_function(struct osl_row *row, void *data)
710 struct global_stats_info *gsi = data;
711 struct osl_object obj;
712 char *dirname, formated_value[FORMATED_VALUE_SIZE];
713 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
715 if (!gsi->count && !summary) {
716 ret = -E_LOOP_COMPLETE;
719 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
721 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
724 files = *(uint64_t *)obj.data;
725 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
726 format_count_value(conf.count_unit_arg, files,
727 conf.count_unit_arg == count_unit_arg_h,
729 printf("\t%s%s", formated_value,
730 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
736 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
738 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
741 bytes = *(uint64_t *)obj.data;
742 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
743 format_size_value(conf.size_unit_arg, bytes,
744 conf.size_unit_arg == size_unit_arg_h,
747 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
749 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
757 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
758 ret = get_dir_name_of_row(row, &dirname);
762 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
771 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
775 static void print_id_stats(void)
777 struct user_info *ui;
779 printf("User summary "
780 "(uid/dirs%s/files%s/size%s):\n",
781 count_unit_buf, count_unit_buf, size_unit_buf);
783 char formated_dir_count[FORMATED_VALUE_SIZE],
784 formated_file_count[FORMATED_VALUE_SIZE],
785 formated_bytes[FORMATED_VALUE_SIZE ];
786 if (!ui_used(ui) || !ui_admissible(ui))
788 format_count_value(conf.count_unit_arg, ui->dirs,
789 conf.count_unit_arg == count_unit_arg_h,
791 format_count_value(conf.count_unit_arg, ui->files,
792 conf.count_unit_arg == count_unit_arg_h,
793 formated_file_count);
794 format_size_value(conf.size_unit_arg, ui->bytes,
795 conf.size_unit_arg == size_unit_arg_h,
797 printf("\t%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
805 enum user_stats_flags {
806 USF_PRINT_DIRNAME = 1,
809 USF_COMPUTE_SUMMARY = 8,
812 struct user_stats_info {
814 enum user_stats_flags flags;
817 struct user_info *ui;
820 static int user_stats_loop_function(struct osl_row *row, void *data)
822 struct user_stats_info *usi = data;
823 struct osl_object obj;
824 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
825 char formated_value[FORMATED_VALUE_SIZE];
827 if (!usi->count && !summary) {
828 ret = -E_LOOP_COMPLETE;
831 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
833 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
836 files = *(uint64_t *)obj.data;
837 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
838 format_count_value(conf.count_unit_arg, files,
839 conf.count_unit_arg == count_unit_arg_h,
841 printf("\t%s%s", formated_value,
842 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
847 usi->ui->files += files;
849 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
851 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
854 bytes = *(uint64_t *)obj.data;
855 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
856 format_size_value(conf.size_unit_arg, bytes,
857 conf.size_unit_arg == size_unit_arg_h,
860 (usi->flags & USF_PRINT_FILES)? "" : "\t",
862 usi->flags & USF_PRINT_DIRNAME? "\t" : "\n"
866 usi->ui->bytes += bytes;
871 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
873 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
876 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
880 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
889 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
893 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
897 assert(ret == -E_OSL);
898 if (osl_errno != E_OSL_LOOP)
899 /* error not caused by loop function returning negative. */
901 assert(loop_ret < 0);
902 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
904 if (loop_ret == -E_OSL) { /* osl error in loop function */
905 assert(loop_osl_errno);
906 osl_errno = loop_osl_errno;
911 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
912 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
914 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
915 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
918 static int print_user_stats(void)
920 struct user_info *ui;
924 struct user_stats_info usi = {
925 .count = conf.limit_arg,
928 if (!ui_used(ui) || !ui_admissible(ui))
930 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
931 printf("uid %u, by size%s:\n",
932 (unsigned) ui->uid, size_unit_buf);
933 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
934 &usi.ret, &usi.osl_errno);
937 printf("\nuid %u, by file count%s:\n",
938 (unsigned) ui->uid, count_unit_buf);
939 usi.count = conf.limit_arg,
940 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
941 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
942 &usi.ret, &usi.osl_errno);
950 static void print_global_summary(void)
952 char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
953 s[FORMATED_VALUE_SIZE];
954 enum enum_count_unit ud, uf;
955 enum enum_size_unit us;
957 ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d);
958 uf = format_count_value(conf.count_unit_arg, num_files, 0, f);
959 us = format_size_value(conf.size_unit_arg, num_bytes, 0, s);
961 printf("Global summary "
962 "(dirs(%c)/files(%c)/size(%c))\n"
964 count_unit_abbrevs[ud],
965 count_unit_abbrevs[uf],
966 size_unit_abbrevs[us],
972 static int print_statistics(void)
975 struct global_stats_info gsi = {
976 .count = conf.limit_arg,
977 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
980 printf("By size%s:\n",
982 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
983 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
988 gsi.count = conf.limit_arg;
989 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
990 printf("By file count%s:\n",
992 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
993 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
997 print_global_summary();
1003 static char *get_uid_list_name(void)
1005 return make_message("%s/uid_list", conf.database_dir_arg);
1008 static int write_uid_list(void)
1010 char *buf, *filename;
1012 struct user_info *ui;
1013 size_t size = num_uids * sizeof(uint32_t);
1018 buf = para_malloc(size);
1020 if (!ui_used(ui) || !ui_admissible(ui))
1022 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
1023 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
1025 filename = get_uid_list_name();
1026 ret = para_write_file(filename, buf, size);
1032 static int open_dir_table(void)
1034 if (!dir_table_desc.dir) /* we did not create the table */
1035 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
1036 return osl(osl_open_table(&dir_table_desc, &dir_table));
1039 static void close_dir_table(void)
1045 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
1047 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
1048 free((char *)dir_table_desc.dir);
1052 static void close_user_table(struct user_info *ui)
1056 if (!ui || !ui_used(ui) || !ui_admissible(ui))
1058 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
1060 ERROR_LOG("failed to close user table %u: %s\n",
1061 (unsigned) ui->uid, adu_strerror(-ret));
1062 free((char *)ui->desc->name);
1063 ui->desc->name = NULL;
1064 free((char *)ui->desc->dir);
1065 ui->desc->dir = NULL;
1072 static void close_user_tables(void)
1074 struct user_info *ui;
1077 close_user_table(ui);
1080 static void close_all_tables(void)
1083 close_user_tables();
1087 static int com_create()
1089 uint64_t zero = 0ULL;
1090 int ret = create_tables();
1094 ret = open_dir_table();
1097 ret = scan_dir(conf.base_dir_arg, &zero);
1100 ret = write_uid_list();
1106 static int read_uid_file(void)
1110 char *filename = get_uid_list_name(), *map;
1111 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
1114 INFO_LOG("failed to map %s\n", filename);
1118 num_uids = size / 4;
1119 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
1121 /* hash table size should be a power of two and larger than the number of uids */
1122 uid_hash_table_size = 4;
1123 while (uid_hash_table_size < num_uids)
1124 uid_hash_table_size *= 2;
1125 create_hash_table();
1126 for (n = 0; n < num_uids; n++) {
1127 uint32_t uid = read_u32(map + n * sizeof(uid));
1128 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
1133 para_munmap(map, size);
1137 static int com_select(void)
1141 if (conf.count_unit_arg != count_unit_arg_h)
1142 count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
1144 count_unit_buf[0] = '\0';
1145 if (conf.size_unit_arg != size_unit_arg_h)
1146 size_unit_buf[1] = size_unit_abbrevs[conf.size_unit_arg];
1148 size_unit_buf[0] = '\0';
1150 ret = open_dir_table();
1153 ret = read_uid_file();
1156 ret = print_statistics();
1161 static int check_args(void)
1165 /* remove trailing slashes from base-dir arg */
1166 if (conf.base_dir_given) {
1167 size_t len = strlen(conf.base_dir_arg);
1169 if (!len) /* empty string */
1170 return -ERRNO_TO_ERROR(EINVAL);
1171 if (!--len) /* length 1 is always OK */
1173 if (conf.base_dir_arg[len] != '/')
1174 break; /* no trailing slash, also OK */
1175 conf.base_dir_arg[len] = '\0';
1178 if (!conf.uid_given)
1180 admissible_uids = para_malloc(conf.uid_given * sizeof(*admissible_uids));
1181 for (i = 0; i < conf.uid_given; i++) {
1182 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
1188 free(admissible_uids);
1189 admissible_uids = NULL;
1193 int main(int argc, char **argv)
1196 struct cmdline_parser_params params = {
1199 .check_required = 0,
1200 .check_ambiguity = 0,
1204 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1209 if (conf.select_given)
1216 free(admissible_uids);
1218 ERROR_LOG("%s\n", adu_strerror(-ret));
1219 return -EXIT_FAILURE;
1221 return EXIT_SUCCESS;