2 #include <dirent.h> /* readdir() */
5 #include "gcc-compat.h"
10 #include "portable_io.h"
15 /** Command line and config file options. */
16 static struct gengetopt_args_info conf;
19 /** whether this slot of the hash table is used. */
21 /** whether this uid should be taken into account. */
29 struct osl_table *table;
33 struct osl_table_description *desc;
36 /** The decimal representation of an uint64_t never exceeds that size. */
37 #define FORMATED_VALUE_SIZE 25
39 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
40 + uid_hash_table_size; ui++)
44 * Contains info for each user that owns at least one regular file.
46 * Even users that are not taken into account because of the --uid
47 * option occupy a slot in this hash table. This allows to find out
48 * quicky whether a uid is admissible. And yes, this has to be fast.
50 static struct user_info *uid_hash_table;
52 /* these get filled in by the select command. */
53 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
55 static inline int ui_used(struct user_info *ui)
57 return ui->flags & UI_FL_SLOT_USED;
60 static inline int ui_admissible(struct user_info *ui)
62 return ui->flags & UI_FL_ADMISSIBLE;
70 static struct uid_range *admissible_uids;
72 static inline int check_uid_arg(const char *arg, uint32_t *uid)
74 const uint32_t max = ~0U;
76 * we need an 64-bit int for string -> uid conversion because strtoll()
77 * returns a signed value.
80 int ret = atoi64(arg, &val);
84 if (val < 0 || val > max)
85 return -ERRNO_TO_ERROR(EINVAL);
90 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
93 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
95 if (!p || p == arg) { /* -42 or 42 */
96 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
99 ur->low = p? 0 : ur->high;
106 ret = check_uid_arg(arg, &ur->low);
110 if (*p) { /* 42-4711 */
111 ret = check_uid_arg(p, &ur->high);
115 if (ur->low > ur->high)
116 ret = -ERRNO_TO_ERROR(EINVAL);
119 ERROR_LOG("bad uid option: %s\n", orig_arg);
121 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
127 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
128 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
133 * \param ll Loglevel.
134 * \param fml Usual format string.
136 * All XXX_LOG() macros use this function.
138 __printf_2_3 void __log(int ll, const char* fmt,...)
146 if (ll < conf.loglevel_arg)
151 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
152 fprintf(outfd, "%s ", str);
154 vfprintf(outfd, fmt, argp);
159 * Compare the size of two directories
161 * \param obj1 Pointer to the first object.
162 * \param obj2 Pointer to the second object.
164 * This function first compares the size values as usual integers. If they compare as
165 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
166 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
168 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
170 uint64_t d1 = *(uint64_t *)obj1->data;
171 uint64_t d2 = *(uint64_t *)obj2->data;
172 int ret = NUM_COMPARE(d2, d1);
176 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
177 return NUM_COMPARE(obj2->data, obj1->data);
181 * Compare two osl objects pointing to unsigned integers of 64 bit size.
183 * \param obj1 Pointer to the first integer.
184 * \param obj2 Pointer to the second integer.
186 * \return The values required for an osl compare function.
188 * \sa osl_compare_func, osl_hash_compare().
190 static int uint64_compare(const struct osl_object *obj1,
191 const struct osl_object *obj2)
193 uint64_t d1 = read_u64((const char *)obj1->data);
194 uint64_t d2 = read_u64((const char *)obj2->data);
203 /** The columns of the directory table. */
204 enum dir_table_columns {
205 /** The name of the directory. */
207 /** The dir count number. */
209 /** The number of the parent directory. */
211 /** The number of bytes of all regular files. */
213 /** The number of all regular files. */
215 /** Number of columns in this table. */
219 static struct osl_column_description dir_table_cols[] = {
221 .storage_type = OSL_MAPPED_STORAGE,
226 .storage_type = OSL_MAPPED_STORAGE,
227 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
229 .compare_function = uint64_compare,
230 .data_size = sizeof(uint64_t)
233 .storage_type = OSL_MAPPED_STORAGE,
234 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
235 .name = "parent_num",
236 .compare_function = size_compare,
237 .data_size = sizeof(uint64_t)
240 .storage_type = OSL_MAPPED_STORAGE,
241 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
242 .compare_function = size_compare,
244 .data_size = sizeof(uint64_t)
247 .storage_type = OSL_MAPPED_STORAGE,
248 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
249 .compare_function = size_compare,
251 .data_size = sizeof(uint64_t)
255 static struct osl_table_description dir_table_desc = {
257 .num_columns = NUM_DT_COLUMNS,
259 .column_descriptions = dir_table_cols,
262 /** The columns of the id table. */
263 enum user_table_columns {
264 /** The numer of the directory. */
266 /** The number of bytes of all regular files in this dir owned by this id. */
268 /** The number of files in this dir owned by this id. */
270 /** Number of columns in this table. */
274 static struct osl_column_description user_table_cols[] = {
276 .storage_type = OSL_MAPPED_STORAGE,
277 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
279 .compare_function = uint64_compare,
280 .data_size = sizeof(uint64_t)
283 .storage_type = OSL_MAPPED_STORAGE,
284 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
285 .compare_function = size_compare,
287 .data_size = sizeof(uint64_t)
290 .storage_type = OSL_MAPPED_STORAGE,
291 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
292 .compare_function = size_compare,
294 .data_size = sizeof(uint64_t)
298 static struct osl_table *dir_table;
300 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
301 uint64_t *dir_size, uint64_t *dir_files)
303 struct osl_object dir_objects[NUM_DT_COLUMNS];
305 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
306 dir_objects[DT_NAME].data = dirname;
307 dir_objects[DT_NAME].size = strlen(dirname) + 1;
308 dir_objects[DT_NUM].data = dir_num;
309 dir_objects[DT_NUM].size = sizeof(*dir_num);
310 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
311 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
312 dir_objects[DT_BYTES].data = dir_size;
313 dir_objects[DT_BYTES].size = sizeof(*dir_size);
314 dir_objects[DT_FILES].data = dir_files;
315 dir_objects[DT_FILES].size = sizeof(*dir_files);
316 return osl(osl_add_row(dir_table, dir_objects));
319 static uint32_t num_uids;
321 static int open_user_table(struct user_info *ui, int create)
326 ui->desc = adu_malloc(sizeof(*ui->desc));
327 ui->desc->num_columns = NUM_UT_COLUMNS;
329 ui->desc->column_descriptions = user_table_cols;
330 ui->desc->dir = adu_strdup(conf.database_dir_arg);
331 ui->desc->name = make_message("%u", (unsigned)ui->uid);
332 pw = getpwuid(ui->uid);
333 if (pw && pw->pw_name)
334 ui->pw_name = adu_strdup(pw->pw_name);
336 INFO_LOG(".............................uid #%u: %u\n",
337 (unsigned)num_uids, (unsigned)ui->uid);
339 ret = osl(osl_create_table(ui->desc));
344 ret = osl(osl_open_table(ui->desc, &ui->table));
349 free((char *)ui->desc->name);
350 free((char *)ui->desc->dir);
353 ui->desc->name = NULL;
354 ui->desc->dir = NULL;
361 #define uid_hash_bits 8
362 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
363 #define PRIME1 0x811c9dc5
364 #define PRIME2 0x01000193
366 static void create_hash_table(void)
368 uid_hash_table = adu_calloc(uid_hash_table_size
369 * sizeof(struct user_info));
372 static void free_hash_table(void)
374 free(uid_hash_table);
375 uid_hash_table = NULL;
378 static int create_tables(void)
382 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
383 ret = osl(osl_create_table(&dir_table_desc));
390 static void close_dir_table(void)
396 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
398 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
399 free((char *)dir_table_desc.dir);
403 static void close_user_table(struct user_info *ui)
407 if (!ui || !ui_used(ui) || !ui_admissible(ui))
409 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
411 ERROR_LOG("failed to close user table %u: %s\n",
412 (unsigned) ui->uid, adu_strerror(-ret));
413 free((char *)ui->desc->name);
414 ui->desc->name = NULL;
415 free((char *)ui->desc->dir);
416 ui->desc->dir = NULL;
425 static void close_user_tables(void)
427 struct user_info *ui;
430 close_user_table(ui);
433 static void close_all_tables(void)
442 static void signal_handler(int s)
447 static void check_signals(void)
451 EMERG_LOG("caught signal %d\n", signum);
456 static int init_signals(void)
458 if (signal(SIGINT, &signal_handler) == SIG_ERR)
459 return -E_SIGNAL_SIG_ERR;
460 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
461 return -E_SIGNAL_SIG_ERR;
466 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
467 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
468 * unused slots in the table are used to store different uids that hash to the
471 * If a hash collision occurs, different slots are successively probed in order
472 * to find an unused slot for the new uid. Probing is implemented via a second
473 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
476 * An odd number is sufficient to make sure each entry of the hash table gets
477 * probed for probe_num between 0 and s-1 because s is a power of two, hence
478 * the second hash value has never a common divisor with the hash table size.
479 * IOW: h is invertible in the ring [0..s].
481 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
483 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
484 % uid_hash_table_size;
487 enum search_uid_flags {
489 CREATE_USER_TABLE = 2,
492 static int uid_is_admissible(uint32_t uid)
496 for (i = 0; i < conf.uid_given; i++) {
497 struct uid_range *ur = admissible_uids + i;
499 if (ur->low <= uid && ur->high >= uid)
502 i = !conf.uid_given || i < conf.uid_given;
503 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
508 static int search_uid(uint32_t uid, enum search_uid_flags flags,
509 struct user_info **ui_ptr)
513 for (p = 0; p < uid_hash_table_size; p++) {
514 struct user_info *ui = uid_hash_table + double_hash(uid, p);
521 ui->flags |= UI_FL_SLOT_USED;
522 if (!uid_is_admissible(uid))
524 ui->flags |= UI_FL_ADMISSIBLE;
525 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
539 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
542 static int update_user_row(struct osl_table *t, uint64_t dir_num,
546 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
548 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
550 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
552 if (ret < 0) { /* this is the first file we add */
553 struct osl_object objects[NUM_UT_COLUMNS];
554 uint64_t num_files = 1;
556 objects[UT_DIR_NUM].data = &dir_num;
557 objects[UT_DIR_NUM].size = sizeof(dir_num);
558 objects[UT_BYTES].data = add;
559 objects[UT_BYTES].size = sizeof(*add);
560 objects[UT_FILES].data = &num_files;
561 objects[UT_FILES].size = sizeof(num_files);
562 INFO_LOG("######################### ret: %d\n", ret);
563 ret = osl(osl_add_row(t, objects));
564 INFO_LOG("######################### ret: %d\n", ret);
566 } else { /* add size and increment file count */
568 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
570 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
573 num = *(uint64_t *)obj1.data + *add;
574 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
577 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
580 num = *(uint64_t *)obj1.data + 1;
581 return osl(osl_update_object(t, row, UT_FILES, &obj2));
585 static uint64_t num_dirs;
586 static uint64_t num_files;
587 static uint64_t num_bytes;
589 /* id of the device containing the base dir. */
590 static dev_t device_id;
592 static int scan_dir(char *dirname, uint64_t *parent_dir_num)
595 struct dirent *entry;
596 int ret, cwd_fd, ret2;
597 uint64_t dir_size = 0, dir_files = 0;
598 uint64_t this_dir_num = ++num_dirs;
601 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
602 ret = adu_opendir(dirname, &dir, &cwd_fd);
604 if (ret != -ERRNO_TO_ERROR(EACCES))
606 WARNING_LOG("permission denied for %s\n", dirname);
609 while ((entry = readdir(dir))) {
614 struct user_info *ui;
616 if (!strcmp(entry->d_name, "."))
618 if (!strcmp(entry->d_name, ".."))
620 if (lstat(entry->d_name, &s) == -1) {
621 WARNING_LOG("lstat error for %s/%s\n", dirname,
626 if (!S_ISREG(m) && !S_ISDIR(m))
629 if (conf.one_file_system_given && s.st_dev != device_id)
631 ret = scan_dir(entry->d_name, &this_dir_num);
643 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
648 ret = update_user_row(ui->table, this_dir_num, &size);
652 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
653 &dir_size, &dir_files);
656 ret2 = adu_fchdir(cwd_fd);
657 if (ret2 < 0 && ret >= 0)
663 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
665 char *result = NULL, *tmp;
667 uint64_t val = *dirnum;
668 struct osl_object obj = {.data = &val, .size = sizeof(val)};
672 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
675 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
679 tmp = make_message("%s/%s", (char *)obj.data, result);
683 result = adu_strdup((char *)obj.data);
684 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
687 val = *(uint64_t *)obj.data;
699 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
701 struct osl_object obj;
703 char *this_dir, *prefix = NULL;
706 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
709 this_dir = adu_strdup((char *)obj.data);
710 ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
713 if (!*(uint64_t *)obj.data) {
717 ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
720 *name = make_message("%s/%s", prefix, this_dir);
728 const uint64_t size_unit_divisors[] = {
729 [size_unit_arg_b] = 1ULL,
730 [size_unit_arg_k] = 1024ULL,
731 [size_unit_arg_m] = 1024ULL * 1024ULL,
732 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
733 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
736 const uint64_t count_unit_divisors[] = {
738 [count_unit_arg_n] = 1ULL,
739 [count_unit_arg_k] = 1000ULL,
740 [count_unit_arg_m] = 1000ULL * 1000ULL,
741 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
742 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
745 const char size_unit_abbrevs[] = " BKMGT";
746 const char count_unit_abbrevs[] = " kmgt";
748 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
749 uint64_t value, int print_unit, char *result)
751 enum enum_size_unit u = unit;
752 char unit_buf[2] = "\0\0";
754 if (unit == size_unit_arg_h) /* human readable */
755 for (u = size_unit_arg_b; u < size_unit_arg_t &&
756 value > size_unit_divisors[u + 1]; u++)
759 unit_buf[0] = size_unit_abbrevs[u];
760 sprintf(result, "%llu%s",
761 (long long unsigned)value / size_unit_divisors[u], unit_buf);
765 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
766 uint64_t value, int print_unit, char *result)
768 enum enum_count_unit u = unit;
769 char unit_buf[2] = "\0\0";
771 if (unit == count_unit_arg_h) /* human readable */
772 for (u = count_unit_arg_n; u < count_unit_arg_t &&
773 value > count_unit_divisors[u + 1]; u++)
776 unit_buf[0] = count_unit_abbrevs[u];
777 sprintf(result, "%llu%s",
778 (long long unsigned)value / count_unit_divisors[u], unit_buf);
782 enum global_stats_flags {
783 GSF_PRINT_DIRNAME = 1,
786 GSF_COMPUTE_SUMMARY = 8,
789 struct global_stats_info {
793 enum global_stats_flags flags;
796 static int global_stats_loop_function(struct osl_row *row, void *data)
798 struct global_stats_info *gsi = data;
799 struct osl_object obj;
800 char *dirname, formated_value[FORMATED_VALUE_SIZE];
801 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
804 if (!gsi->count && !summary) {
805 ret = -E_LOOP_COMPLETE;
808 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
810 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
813 files = *(uint64_t *)obj.data;
814 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
815 format_count_value(conf.count_unit_arg, files,
816 conf.count_unit_arg == count_unit_arg_h,
818 printf("\t%s%s", formated_value,
819 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
825 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
827 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
830 bytes = *(uint64_t *)obj.data;
831 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
832 format_size_value(conf.size_unit_arg, bytes,
833 conf.size_unit_arg == size_unit_arg_h,
836 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
838 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
846 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
847 ret = get_dir_name_of_row(row, &dirname);
851 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
860 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
864 static void print_id_stats(void)
866 struct user_info *ui;
868 printf("User summary "
869 "(pw_name/uid/dirs%s/files%s/size%s):\n",
870 count_unit_buf, count_unit_buf, size_unit_buf);
872 char formated_dir_count[FORMATED_VALUE_SIZE],
873 formated_file_count[FORMATED_VALUE_SIZE],
874 formated_bytes[FORMATED_VALUE_SIZE ];
875 if (!ui_used(ui) || !ui_admissible(ui))
877 format_count_value(conf.count_unit_arg, ui->dirs,
878 conf.count_unit_arg == count_unit_arg_h,
880 format_count_value(conf.count_unit_arg, ui->files,
881 conf.count_unit_arg == count_unit_arg_h,
882 formated_file_count);
883 format_size_value(conf.size_unit_arg, ui->bytes,
884 conf.size_unit_arg == size_unit_arg_h,
886 printf("\t%s\t%u\t%s\t%s\t%s\n",
887 ui->pw_name? ui->pw_name : "?",
896 enum user_stats_flags {
897 USF_PRINT_DIRNAME = 1,
900 USF_COMPUTE_SUMMARY = 8,
903 struct user_stats_info {
905 enum user_stats_flags flags;
908 struct user_info *ui;
911 static int user_stats_loop_function(struct osl_row *row, void *data)
913 struct user_stats_info *usi = data;
914 struct osl_object obj;
915 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
916 char formated_value[FORMATED_VALUE_SIZE];
919 if (!usi->count && !summary) {
920 ret = -E_LOOP_COMPLETE;
923 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
925 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
928 files = *(uint64_t *)obj.data;
929 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
930 format_count_value(conf.count_unit_arg, files,
931 conf.count_unit_arg == count_unit_arg_h,
933 printf("\t%s%s", formated_value,
934 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
939 usi->ui->files += files;
941 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
943 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
946 bytes = *(uint64_t *)obj.data;
947 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
948 format_size_value(conf.size_unit_arg, bytes,
949 conf.size_unit_arg == size_unit_arg_h,
952 (usi->flags & USF_PRINT_FILES)? "" : "\t",
954 usi->flags & USF_PRINT_DIRNAME? "\t" : "\n"
958 usi->ui->bytes += bytes;
963 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
965 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
968 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
972 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
981 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
985 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
989 assert(ret == -E_OSL);
990 if (osl_errno != E_OSL_LOOP)
991 /* error not caused by loop function returning negative. */
993 assert(loop_ret < 0);
994 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
996 if (loop_ret == -E_OSL) { /* osl error in loop function */
997 assert(loop_osl_errno);
998 osl_errno = loop_osl_errno;
1003 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
1004 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
1006 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
1007 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
1010 static int print_user_stats(void)
1012 struct user_info *ui;
1016 struct user_stats_info usi = {
1017 .count = conf.limit_arg,
1020 if (!ui_used(ui) || !ui_admissible(ui))
1022 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
1023 printf("%s (uid %u), by size%s:\n",
1024 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
1026 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
1027 &usi.ret, &usi.osl_errno);
1030 printf("\n%s (uid %u), by file count%s:\n",
1031 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
1033 usi.count = conf.limit_arg,
1034 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
1035 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
1036 &usi.ret, &usi.osl_errno);
1044 static void print_global_summary(void)
1046 char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
1047 s[FORMATED_VALUE_SIZE];
1048 enum enum_count_unit ud, uf;
1049 enum enum_size_unit us;
1051 ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d);
1052 uf = format_count_value(conf.count_unit_arg, num_files, 0, f);
1053 us = format_size_value(conf.size_unit_arg, num_bytes, 0, s);
1055 printf("Global summary "
1056 "(dirs(%c)/files(%c)/size(%c))\n"
1058 count_unit_abbrevs[ud],
1059 count_unit_abbrevs[uf],
1060 size_unit_abbrevs[us],
1066 static int print_statistics(void)
1069 struct global_stats_info gsi = {
1070 .count = conf.limit_arg,
1071 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
1074 printf("By size%s:\n",
1076 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
1077 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
1082 gsi.count = conf.limit_arg;
1083 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
1084 printf("By file count%s:\n",
1086 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
1087 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
1091 print_global_summary();
1097 static char *get_uid_list_name(void)
1099 return make_message("%s/uid_list", conf.database_dir_arg);
1102 static int write_uid_list(void)
1104 char *buf, *filename;
1106 struct user_info *ui;
1107 size_t size = num_uids * sizeof(uint32_t);
1112 buf = adu_malloc(size);
1114 if (!ui_used(ui) || !ui_admissible(ui))
1116 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
1117 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
1119 filename = get_uid_list_name();
1120 ret = adu_write_file(filename, buf, size);
1126 static int open_dir_table(void)
1128 if (!dir_table_desc.dir) /* we did not create the table */
1129 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
1130 return osl(osl_open_table(&dir_table_desc, &dir_table));
1132 static int com_create()
1134 uint64_t zero = 0ULL;
1136 struct stat statbuf;
1138 if (lstat(conf.base_dir_arg, &statbuf) == -1)
1139 return -ERRNO_TO_ERROR(errno);
1140 if (!S_ISDIR(statbuf.st_mode))
1141 return -ERRNO_TO_ERROR(ENOTDIR);
1142 device_id = statbuf.st_dev;
1143 ret = create_tables();
1147 ret = open_dir_table();
1151 ret = scan_dir(conf.base_dir_arg, &zero);
1154 ret = write_uid_list();
1160 static int read_uid_file(void)
1164 char *filename = get_uid_list_name(), *map;
1165 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
1168 INFO_LOG("failed to map %s\n", filename);
1172 num_uids = size / 4;
1173 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
1175 /* hash table size should be a power of two and larger than the number of uids */
1176 uid_hash_table_size = 4;
1177 while (uid_hash_table_size < num_uids)
1178 uid_hash_table_size *= 2;
1179 create_hash_table();
1180 for (n = 0; n < num_uids; n++) {
1181 uint32_t uid = read_u32(map + n * sizeof(uid));
1182 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
1187 adu_munmap(map, size);
1191 static int com_select(void)
1195 if (conf.count_unit_arg != count_unit_arg_h)
1196 count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
1198 count_unit_buf[0] = '\0';
1199 if (conf.size_unit_arg != size_unit_arg_h)
1200 size_unit_buf[1] = size_unit_abbrevs[conf.size_unit_arg];
1202 size_unit_buf[0] = '\0';
1204 ret = open_dir_table();
1208 ret = read_uid_file();
1212 ret = print_statistics();
1217 static int check_args(void)
1221 /* remove trailing slashes from base-dir arg */
1222 if (conf.base_dir_given) {
1223 size_t len = strlen(conf.base_dir_arg);
1225 if (!len) /* empty string */
1226 return -ERRNO_TO_ERROR(EINVAL);
1227 if (!--len) /* length 1 is always OK */
1229 if (conf.base_dir_arg[len] != '/')
1230 break; /* no trailing slash, also OK */
1231 conf.base_dir_arg[len] = '\0';
1234 if (!conf.uid_given)
1236 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
1237 for (i = 0; i < conf.uid_given; i++) {
1238 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
1244 free(admissible_uids);
1245 admissible_uids = NULL;
1249 int main(int argc, char **argv)
1252 struct cmdline_parser_params params = {
1255 .check_required = 0,
1256 .check_ambiguity = 0,
1260 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1261 ret = init_signals();
1268 if (conf.select_given)
1275 free(admissible_uids);
1277 ERROR_LOG("%s\n", adu_strerror(-ret));
1278 return -EXIT_FAILURE;
1280 return EXIT_SUCCESS;