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
37 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
38 + uid_hash_table_size; ui++)
42 * Contains info for each user that owns at least one regular file.
44 * Even users that are not taken into account because of the --uid
45 * option occupy a slot in this hash table. This allows to find out
46 * quicky whether a uid is admissible. And yes, this has to be fast.
48 static struct user_info *uid_hash_table;
50 /* these get filled in by the select command. */
51 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
53 static inline int ui_used(struct user_info *ui)
55 return ui->flags & UI_FL_SLOT_USED;
58 static inline int ui_admissible(struct user_info *ui)
60 return ui->flags & UI_FL_ADMISSIBLE;
68 static struct uid_range *admissible_uids;
70 static inline int check_uid_arg(const char *arg, uint32_t *uid)
72 const uint32_t max = ~0U;
74 * we need an 64-bit int for string -> uid conversion because strtoll()
75 * returns a signed value.
78 int ret = atoi64(arg, &val);
82 if (val < 0 || val > max)
83 return -ERRNO_TO_ERROR(EINVAL);
88 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
91 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
93 if (!p || p == arg) { /* -42 or 42 */
94 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
97 ur->low = p? 0 : ur->high;
104 ret = check_uid_arg(arg, &ur->low);
108 if (*p) { /* 42-4711 */
109 ret = check_uid_arg(p, &ur->high);
113 if (ur->low > ur->high)
114 ret = -ERRNO_TO_ERROR(EINVAL);
117 ERROR_LOG("bad uid option: %s\n", orig_arg);
119 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
125 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
126 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
131 * \param ll Loglevel.
132 * \param fml Usual format string.
134 * All XXX_LOG() macros use this function.
136 __printf_2_3 void __log(int ll, const char* fmt,...)
144 if (ll < conf.loglevel_arg)
149 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
150 fprintf(outfd, "%s ", str);
152 vfprintf(outfd, fmt, argp);
157 * Compare the size of two directories
159 * \param obj1 Pointer to the first object.
160 * \param obj2 Pointer to the second object.
162 * This function first compares the size values as usual integers. If they compare as
163 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
164 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
166 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
168 uint64_t d1 = *(uint64_t *)obj1->data;
169 uint64_t d2 = *(uint64_t *)obj2->data;
170 int ret = NUM_COMPARE(d2, d1);
174 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
175 return NUM_COMPARE(obj2->data, obj1->data);
179 * Compare two osl objects pointing to unsigned integers of 64 bit size.
181 * \param obj1 Pointer to the first integer.
182 * \param obj2 Pointer to the second integer.
184 * \return The values required for an osl compare function.
186 * \sa osl_compare_func, osl_hash_compare().
188 static int uint64_compare(const struct osl_object *obj1,
189 const struct osl_object *obj2)
191 uint64_t d1 = read_u64((const char *)obj1->data);
192 uint64_t d2 = read_u64((const char *)obj2->data);
201 /** The columns of the directory table. */
202 enum dir_table_columns {
203 /** The name of the directory. */
205 /** The dir count number. */
207 /** The number of the parent directory. */
209 /** The number of bytes of all regular files. */
211 /** The number of all regular files. */
213 /** Number of columns in this table. */
217 static struct osl_column_description dir_table_cols[] = {
219 .storage_type = OSL_MAPPED_STORAGE,
224 .storage_type = OSL_MAPPED_STORAGE,
225 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
227 .compare_function = uint64_compare,
228 .data_size = sizeof(uint64_t)
231 .storage_type = OSL_MAPPED_STORAGE,
232 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
233 .name = "parent_num",
234 .compare_function = size_compare,
235 .data_size = sizeof(uint64_t)
238 .storage_type = OSL_MAPPED_STORAGE,
239 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
240 .compare_function = size_compare,
242 .data_size = sizeof(uint64_t)
245 .storage_type = OSL_MAPPED_STORAGE,
246 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
247 .compare_function = size_compare,
249 .data_size = sizeof(uint64_t)
253 static struct osl_table_description dir_table_desc = {
255 .num_columns = NUM_DT_COLUMNS,
257 .column_descriptions = dir_table_cols,
260 /** The columns of the id table. */
261 enum user_table_columns {
262 /** The numer of the directory. */
264 /** The number of bytes of all regular files in this dir owned by this id. */
266 /** The number of files in this dir owned by this id. */
268 /** Number of columns in this table. */
272 static struct osl_column_description user_table_cols[] = {
274 .storage_type = OSL_MAPPED_STORAGE,
275 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
277 .compare_function = uint64_compare,
278 .data_size = sizeof(uint64_t)
281 .storage_type = OSL_MAPPED_STORAGE,
282 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
283 .compare_function = size_compare,
285 .data_size = sizeof(uint64_t)
288 .storage_type = OSL_MAPPED_STORAGE,
289 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
290 .compare_function = size_compare,
292 .data_size = sizeof(uint64_t)
296 static struct osl_table *dir_table;
298 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
299 uint64_t *dir_size, uint64_t *dir_files)
301 struct osl_object dir_objects[NUM_DT_COLUMNS];
303 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
304 dir_objects[DT_NAME].data = dirname;
305 dir_objects[DT_NAME].size = strlen(dirname) + 1;
306 dir_objects[DT_NUM].data = dir_num;
307 dir_objects[DT_NUM].size = sizeof(*dir_num);
308 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
309 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
310 dir_objects[DT_BYTES].data = dir_size;
311 dir_objects[DT_BYTES].size = sizeof(*dir_size);
312 dir_objects[DT_FILES].data = dir_files;
313 dir_objects[DT_FILES].size = sizeof(*dir_files);
314 return osl(osl_add_row(dir_table, dir_objects));
317 static uint32_t num_uids;
319 static int open_user_table(struct user_info *ui, int create)
323 ui->desc = adu_malloc(sizeof(*ui->desc));
324 ui->desc->num_columns = NUM_UT_COLUMNS;
326 ui->desc->column_descriptions = user_table_cols;
327 ui->desc->dir = adu_strdup(conf.database_dir_arg);
328 ui->desc->name = make_message("%u", (unsigned)ui->uid);
329 INFO_LOG(".............................uid #%u: %u\n",
330 (unsigned)num_uids, (unsigned)ui->uid);
332 ret = osl(osl_create_table(ui->desc));
337 ret = osl(osl_open_table(ui->desc, &ui->table));
342 free((char *)ui->desc->name);
343 free((char *)ui->desc->dir);
345 ui->desc->name = NULL;
346 ui->desc->dir = NULL;
353 #define uid_hash_bits 8
354 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
355 #define PRIME1 0x811c9dc5
356 #define PRIME2 0x01000193
358 static void create_hash_table(void)
360 uid_hash_table = adu_calloc(uid_hash_table_size
361 * sizeof(struct user_info));
364 static void free_hash_table(void)
366 free(uid_hash_table);
367 uid_hash_table = NULL;
370 static int create_tables(void)
374 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
375 ret = osl(osl_create_table(&dir_table_desc));
382 static void close_dir_table(void)
388 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
390 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
391 free((char *)dir_table_desc.dir);
395 static void close_user_table(struct user_info *ui)
399 if (!ui || !ui_used(ui) || !ui_admissible(ui))
401 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
403 ERROR_LOG("failed to close user table %u: %s\n",
404 (unsigned) ui->uid, adu_strerror(-ret));
405 free((char *)ui->desc->name);
406 ui->desc->name = NULL;
407 free((char *)ui->desc->dir);
408 ui->desc->dir = NULL;
415 static void close_user_tables(void)
417 struct user_info *ui;
420 close_user_table(ui);
423 static void close_all_tables(void)
432 static void signal_handler(int s)
437 static void check_signals(void)
441 EMERG_LOG("caught signal %d\n", signum);
446 static int init_signals(void)
448 if (signal(SIGINT, &signal_handler) == SIG_ERR)
449 return -E_SIGNAL_SIG_ERR;
450 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
451 return -E_SIGNAL_SIG_ERR;
456 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
457 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
458 * unused slots in the table are used to store different uids that hash to the
461 * If a hash collision occurs, different slots are successively probed in order
462 * to find an unused slot for the new uid. Probing is implemented via a second
463 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
466 * An odd number is sufficient to make sure each entry of the hash table gets
467 * probed for probe_num between 0 and s-1 because s is a power of two, hence
468 * the second hash value has never a common divisor with the hash table size.
469 * IOW: h is invertible in the ring [0..s].
471 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
473 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
474 % uid_hash_table_size;
477 enum search_uid_flags {
479 CREATE_USER_TABLE = 2,
482 static int uid_is_admissible(uint32_t uid)
486 for (i = 0; i < conf.uid_given; i++) {
487 struct uid_range *ur = admissible_uids + i;
489 if (ur->low <= uid && ur->high >= uid)
492 i = !conf.uid_given || i < conf.uid_given;
493 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
498 static int search_uid(uint32_t uid, enum search_uid_flags flags,
499 struct user_info **ui_ptr)
503 for (p = 0; p < uid_hash_table_size; p++) {
504 struct user_info *ui = uid_hash_table + double_hash(uid, p);
511 ui->flags |= UI_FL_SLOT_USED;
512 if (!uid_is_admissible(uid))
514 ui->flags |= UI_FL_ADMISSIBLE;
515 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
529 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
532 static int update_user_row(struct osl_table *t, uint64_t dir_num,
536 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
538 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
540 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
542 if (ret < 0) { /* this is the first file we add */
543 struct osl_object objects[NUM_UT_COLUMNS];
544 uint64_t num_files = 1;
546 objects[UT_DIR_NUM].data = &dir_num;
547 objects[UT_DIR_NUM].size = sizeof(dir_num);
548 objects[UT_BYTES].data = add;
549 objects[UT_BYTES].size = sizeof(*add);
550 objects[UT_FILES].data = &num_files;
551 objects[UT_FILES].size = sizeof(num_files);
552 INFO_LOG("######################### ret: %d\n", ret);
553 ret = osl(osl_add_row(t, objects));
554 INFO_LOG("######################### ret: %d\n", ret);
556 } else { /* add size and increment file count */
558 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
560 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
563 num = *(uint64_t *)obj1.data + *add;
564 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
567 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
570 num = *(uint64_t *)obj1.data + 1;
571 return osl(osl_update_object(t, row, UT_FILES, &obj2));
575 static uint64_t num_dirs;
576 static uint64_t num_files;
577 static uint64_t num_bytes;
579 /* id of the device containing the base dir. */
580 static dev_t device_id;
582 static int scan_dir(char *dirname, uint64_t *parent_dir_num)
585 struct dirent *entry;
586 int ret, cwd_fd, ret2;
587 uint64_t dir_size = 0, dir_files = 0;
588 uint64_t this_dir_num = ++num_dirs;
591 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
592 ret = adu_opendir(dirname, &dir, &cwd_fd);
594 if (ret != -ERRNO_TO_ERROR(EACCES))
596 WARNING_LOG("permission denied for %s\n", dirname);
599 while ((entry = readdir(dir))) {
604 struct user_info *ui;
606 if (!strcmp(entry->d_name, "."))
608 if (!strcmp(entry->d_name, ".."))
610 if (lstat(entry->d_name, &s) == -1) {
611 WARNING_LOG("lstat error for %s/%s\n", dirname,
616 if (!S_ISREG(m) && !S_ISDIR(m))
619 if (conf.one_file_system_given && s.st_dev != device_id)
621 ret = scan_dir(entry->d_name, &this_dir_num);
633 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
638 ret = update_user_row(ui->table, this_dir_num, &size);
642 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
643 &dir_size, &dir_files);
646 ret2 = adu_fchdir(cwd_fd);
647 if (ret2 < 0 && ret >= 0)
653 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
655 char *result = NULL, *tmp;
657 uint64_t val = *dirnum;
658 struct osl_object obj = {.data = &val, .size = sizeof(val)};
662 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
665 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
669 tmp = make_message("%s/%s", (char *)obj.data, result);
673 result = adu_strdup((char *)obj.data);
674 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
677 val = *(uint64_t *)obj.data;
689 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
691 struct osl_object obj;
693 char *this_dir, *prefix = NULL;
696 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
699 this_dir = adu_strdup((char *)obj.data);
700 ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
703 if (!*(uint64_t *)obj.data) {
707 ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
710 *name = make_message("%s/%s", prefix, this_dir);
718 const uint64_t size_unit_divisors[] = {
719 [size_unit_arg_b] = 1ULL,
720 [size_unit_arg_k] = 1024ULL,
721 [size_unit_arg_m] = 1024ULL * 1024ULL,
722 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
723 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
726 const uint64_t count_unit_divisors[] = {
728 [count_unit_arg_n] = 1ULL,
729 [count_unit_arg_k] = 1000ULL,
730 [count_unit_arg_m] = 1000ULL * 1000ULL,
731 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
732 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
735 const char size_unit_abbrevs[] = " BKMGT";
736 const char count_unit_abbrevs[] = " kmgt";
738 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
739 uint64_t value, int print_unit, char *result)
741 enum enum_size_unit u = unit;
742 char unit_buf[2] = "\0\0";
744 if (unit == size_unit_arg_h) /* human readable */
745 for (u = size_unit_arg_b; u < size_unit_arg_t &&
746 value > size_unit_divisors[u + 1]; u++)
749 unit_buf[0] = size_unit_abbrevs[u];
750 sprintf(result, "%llu%s",
751 (long long unsigned)value / size_unit_divisors[u], unit_buf);
755 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
756 uint64_t value, int print_unit, char *result)
758 enum enum_count_unit u = unit;
759 char unit_buf[2] = "\0\0";
761 if (unit == count_unit_arg_h) /* human readable */
762 for (u = count_unit_arg_n; u < count_unit_arg_t &&
763 value > count_unit_divisors[u + 1]; u++)
766 unit_buf[0] = count_unit_abbrevs[u];
767 sprintf(result, "%llu%s",
768 (long long unsigned)value / count_unit_divisors[u], unit_buf);
772 enum global_stats_flags {
773 GSF_PRINT_DIRNAME = 1,
776 GSF_COMPUTE_SUMMARY = 8,
779 struct global_stats_info {
783 enum global_stats_flags flags;
786 static int global_stats_loop_function(struct osl_row *row, void *data)
788 struct global_stats_info *gsi = data;
789 struct osl_object obj;
790 char *dirname, formated_value[FORMATED_VALUE_SIZE];
791 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
794 if (!gsi->count && !summary) {
795 ret = -E_LOOP_COMPLETE;
798 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
800 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
803 files = *(uint64_t *)obj.data;
804 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
805 format_count_value(conf.count_unit_arg, files,
806 conf.count_unit_arg == count_unit_arg_h,
808 printf("\t%s%s", formated_value,
809 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
815 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
817 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
820 bytes = *(uint64_t *)obj.data;
821 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
822 format_size_value(conf.size_unit_arg, bytes,
823 conf.size_unit_arg == size_unit_arg_h,
826 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
828 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
836 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
837 ret = get_dir_name_of_row(row, &dirname);
841 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
850 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
854 static void print_id_stats(void)
856 struct user_info *ui;
858 printf("User summary "
859 "(uid/dirs%s/files%s/size%s):\n",
860 count_unit_buf, count_unit_buf, size_unit_buf);
862 char formated_dir_count[FORMATED_VALUE_SIZE],
863 formated_file_count[FORMATED_VALUE_SIZE],
864 formated_bytes[FORMATED_VALUE_SIZE ];
865 if (!ui_used(ui) || !ui_admissible(ui))
867 format_count_value(conf.count_unit_arg, ui->dirs,
868 conf.count_unit_arg == count_unit_arg_h,
870 format_count_value(conf.count_unit_arg, ui->files,
871 conf.count_unit_arg == count_unit_arg_h,
872 formated_file_count);
873 format_size_value(conf.size_unit_arg, ui->bytes,
874 conf.size_unit_arg == size_unit_arg_h,
876 printf("\t%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
884 enum user_stats_flags {
885 USF_PRINT_DIRNAME = 1,
888 USF_COMPUTE_SUMMARY = 8,
891 struct user_stats_info {
893 enum user_stats_flags flags;
896 struct user_info *ui;
899 static int user_stats_loop_function(struct osl_row *row, void *data)
901 struct user_stats_info *usi = data;
902 struct osl_object obj;
903 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
904 char formated_value[FORMATED_VALUE_SIZE];
907 if (!usi->count && !summary) {
908 ret = -E_LOOP_COMPLETE;
911 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
913 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
916 files = *(uint64_t *)obj.data;
917 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
918 format_count_value(conf.count_unit_arg, files,
919 conf.count_unit_arg == count_unit_arg_h,
921 printf("\t%s%s", formated_value,
922 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
927 usi->ui->files += files;
929 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
931 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
934 bytes = *(uint64_t *)obj.data;
935 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
936 format_size_value(conf.size_unit_arg, bytes,
937 conf.size_unit_arg == size_unit_arg_h,
940 (usi->flags & USF_PRINT_FILES)? "" : "\t",
942 usi->flags & USF_PRINT_DIRNAME? "\t" : "\n"
946 usi->ui->bytes += bytes;
951 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
953 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
956 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
960 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
969 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
973 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
977 assert(ret == -E_OSL);
978 if (osl_errno != E_OSL_LOOP)
979 /* error not caused by loop function returning negative. */
981 assert(loop_ret < 0);
982 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
984 if (loop_ret == -E_OSL) { /* osl error in loop function */
985 assert(loop_osl_errno);
986 osl_errno = loop_osl_errno;
991 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
992 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
994 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
995 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
998 static int print_user_stats(void)
1000 struct user_info *ui;
1004 struct user_stats_info usi = {
1005 .count = conf.limit_arg,
1008 if (!ui_used(ui) || !ui_admissible(ui))
1010 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
1011 printf("uid %u, by size%s:\n",
1012 (unsigned) ui->uid, size_unit_buf);
1013 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
1014 &usi.ret, &usi.osl_errno);
1017 printf("\nuid %u, by file count%s:\n",
1018 (unsigned) ui->uid, count_unit_buf);
1019 usi.count = conf.limit_arg,
1020 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
1021 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
1022 &usi.ret, &usi.osl_errno);
1030 static void print_global_summary(void)
1032 char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
1033 s[FORMATED_VALUE_SIZE];
1034 enum enum_count_unit ud, uf;
1035 enum enum_size_unit us;
1037 ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d);
1038 uf = format_count_value(conf.count_unit_arg, num_files, 0, f);
1039 us = format_size_value(conf.size_unit_arg, num_bytes, 0, s);
1041 printf("Global summary "
1042 "(dirs(%c)/files(%c)/size(%c))\n"
1044 count_unit_abbrevs[ud],
1045 count_unit_abbrevs[uf],
1046 size_unit_abbrevs[us],
1052 static int print_statistics(void)
1055 struct global_stats_info gsi = {
1056 .count = conf.limit_arg,
1057 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
1060 printf("By size%s:\n",
1062 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
1063 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
1068 gsi.count = conf.limit_arg;
1069 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
1070 printf("By file count%s:\n",
1072 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
1073 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
1077 print_global_summary();
1083 static char *get_uid_list_name(void)
1085 return make_message("%s/uid_list", conf.database_dir_arg);
1088 static int write_uid_list(void)
1090 char *buf, *filename;
1092 struct user_info *ui;
1093 size_t size = num_uids * sizeof(uint32_t);
1098 buf = adu_malloc(size);
1100 if (!ui_used(ui) || !ui_admissible(ui))
1102 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
1103 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
1105 filename = get_uid_list_name();
1106 ret = adu_write_file(filename, buf, size);
1112 static int open_dir_table(void)
1114 if (!dir_table_desc.dir) /* we did not create the table */
1115 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
1116 return osl(osl_open_table(&dir_table_desc, &dir_table));
1118 static int com_create()
1120 uint64_t zero = 0ULL;
1122 struct stat statbuf;
1124 if (lstat(conf.base_dir_arg, &statbuf) == -1)
1125 return -ERRNO_TO_ERROR(errno);
1126 if (!S_ISDIR(statbuf.st_mode))
1127 return -ERRNO_TO_ERROR(ENOTDIR);
1128 device_id = statbuf.st_dev;
1129 ret = create_tables();
1133 ret = open_dir_table();
1137 ret = scan_dir(conf.base_dir_arg, &zero);
1140 ret = write_uid_list();
1146 static int read_uid_file(void)
1150 char *filename = get_uid_list_name(), *map;
1151 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
1154 INFO_LOG("failed to map %s\n", filename);
1158 num_uids = size / 4;
1159 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
1161 /* hash table size should be a power of two and larger than the number of uids */
1162 uid_hash_table_size = 4;
1163 while (uid_hash_table_size < num_uids)
1164 uid_hash_table_size *= 2;
1165 create_hash_table();
1166 for (n = 0; n < num_uids; n++) {
1167 uint32_t uid = read_u32(map + n * sizeof(uid));
1168 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
1173 adu_munmap(map, size);
1177 static int com_select(void)
1181 if (conf.count_unit_arg != count_unit_arg_h)
1182 count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
1184 count_unit_buf[0] = '\0';
1185 if (conf.size_unit_arg != size_unit_arg_h)
1186 size_unit_buf[1] = size_unit_abbrevs[conf.size_unit_arg];
1188 size_unit_buf[0] = '\0';
1190 ret = open_dir_table();
1194 ret = read_uid_file();
1198 ret = print_statistics();
1203 static int check_args(void)
1207 /* remove trailing slashes from base-dir arg */
1208 if (conf.base_dir_given) {
1209 size_t len = strlen(conf.base_dir_arg);
1211 if (!len) /* empty string */
1212 return -ERRNO_TO_ERROR(EINVAL);
1213 if (!--len) /* length 1 is always OK */
1215 if (conf.base_dir_arg[len] != '/')
1216 break; /* no trailing slash, also OK */
1217 conf.base_dir_arg[len] = '\0';
1220 if (!conf.uid_given)
1222 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
1223 for (i = 0; i < conf.uid_given; i++) {
1224 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
1230 free(admissible_uids);
1231 admissible_uids = NULL;
1235 int main(int argc, char **argv)
1238 struct cmdline_parser_params params = {
1241 .check_required = 0,
1242 .check_ambiguity = 0,
1246 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1247 ret = init_signals();
1254 if (conf.select_given)
1261 free(admissible_uids);
1263 ERROR_LOG("%s\n", adu_strerror(-ret));
1264 return -EXIT_FAILURE;
1266 return EXIT_SUCCESS;