2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
9 #include "portable_io.h"
13 /** Command line and config file options. */
14 static struct gengetopt_args_info conf;
18 struct osl_table *table;
22 struct osl_table_description *desc;
25 static struct user_info *uid_hash_table;
27 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
28 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
35 * \param fml Usual format string.
37 * All XXX_LOG() macros use this function.
39 __printf_2_3 void __log(int ll, const char* fmt,...)
47 if (ll < conf.loglevel_arg)
52 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
53 fprintf(outfd, "%s ", str);
55 vfprintf(outfd, fmt, argp);
60 * Compare the size of two directories
62 * \param obj1 Pointer to the first object.
63 * \param obj2 Pointer to the second object.
65 * This function first compares the size values as usual integers. If they compare as
66 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
67 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
69 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
71 uint64_t d1 = *(uint64_t *)obj1->data;
72 uint64_t d2 = *(uint64_t *)obj2->data;
73 int ret = NUM_COMPARE(d2, d1);
77 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
78 return NUM_COMPARE(obj2->data, obj1->data);
82 * Compare two osl objects of string type.
84 * \param obj1 Pointer to the first object.
85 * \param obj2 Pointer to the second object.
87 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
88 * are taken into account.
90 * \return It returns an integer less than, equal to, or greater than zero if
91 * \a obj1 is found, respectively, to be less than, to match, or be greater
94 * \sa strcmp(3), strncmp(3), osl_compare_func.
96 static int string_compare(const struct osl_object *obj1,
97 const struct osl_object *obj2)
99 const char *str1 = (const char *)obj1->data;
100 const char *str2 = (const char *)obj2->data;
101 return strncmp(str1, str2, MIN(obj1->size, obj2->size));
105 * Compare two osl objects pointing to unsigned integers of 64 bit size.
107 * \param obj1 Pointer to the first integer.
108 * \param obj2 Pointer to the second integer.
110 * \return The values required for an osl compare function.
112 * \sa osl_compare_func, osl_hash_compare().
114 static int uint64_compare(const struct osl_object *obj1,
115 const struct osl_object *obj2)
117 uint64_t d1 = read_u64((const char *)obj1->data);
118 uint64_t d2 = read_u64((const char *)obj2->data);
127 /** The columns of the directory table. */
128 enum dir_table_columns {
129 /** The name of the directory. */
131 /** The dir count number. */
133 /** The number of bytes of all regular files. */
135 /** The number of all regular files. */
137 /** Number of columns in this table. */
141 static struct osl_column_description dir_table_cols[] = {
143 .storage_type = OSL_MAPPED_STORAGE,
144 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
146 .compare_function = string_compare,
149 .storage_type = OSL_MAPPED_STORAGE,
150 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
152 .compare_function = uint64_compare,
153 .data_size = sizeof(uint64_t)
156 .storage_type = OSL_MAPPED_STORAGE,
157 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
158 .compare_function = size_compare,
160 .data_size = sizeof(uint64_t)
163 .storage_type = OSL_MAPPED_STORAGE,
164 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
165 .compare_function = size_compare,
167 .data_size = sizeof(uint64_t)
171 static struct osl_table_description dir_table_desc = {
173 .num_columns = NUM_DT_COLUMNS,
175 .column_descriptions = dir_table_cols,
178 /** The columns of the id table. */
179 enum user_table_columns {
180 /** The numer of the directory. */
182 /** The number of bytes of all regular files in this dir owned by this id. */
184 /** The number of files in this dir owned by this id. */
186 /** Number of columns in this table. */
190 static struct osl_column_description user_table_cols[] = {
192 .storage_type = OSL_MAPPED_STORAGE,
193 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
195 .compare_function = uint64_compare,
196 .data_size = sizeof(uint64_t)
199 .storage_type = OSL_MAPPED_STORAGE,
200 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
201 .compare_function = size_compare,
203 .data_size = sizeof(uint64_t)
206 .storage_type = OSL_MAPPED_STORAGE,
207 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
208 .compare_function = size_compare,
210 .data_size = sizeof(uint64_t)
214 static struct osl_table *dir_table;
216 static int add_directory(char *dirname, uint64_t dir_num, uint64_t *dir_size,
219 struct osl_object dir_objects[NUM_DT_COLUMNS];
221 INFO_LOG("adding #%llu: %s\n", (long long unsigned)dir_num, dirname);
222 dir_objects[DT_NAME].data = dirname;
223 dir_objects[DT_NAME].size = strlen(dirname) + 1;
224 dir_objects[DT_NUM].data = &dir_num;
225 dir_objects[DT_NUM].size = sizeof(dir_num);
226 dir_objects[DT_BYTES].data = dir_size;
227 dir_objects[DT_BYTES].size = sizeof(*dir_size);
228 dir_objects[DT_FILES].data = dir_files;
229 dir_objects[DT_FILES].size = sizeof(*dir_files);
231 return osl_add_row(dir_table, dir_objects);
234 static uint32_t num_uids;
236 static int open_user_table(struct user_info *ui, int create)
240 ui->desc = para_malloc(sizeof(*ui->desc));
241 ui->desc->num_columns = NUM_UT_COLUMNS;
243 ui->desc->column_descriptions = user_table_cols;
244 ui->desc->dir = para_strdup(conf.database_dir_arg);
245 ui->desc->name = make_message("%u", (unsigned)ui->uid);
246 INFO_LOG(".............................uid #%u: %u\n",
247 (unsigned)num_uids, (unsigned)ui->uid);
249 ret = osl_create_table(ui->desc);
254 ret = osl_open_table(ui->desc, &ui->table);
259 free((char *)ui->desc->name);
260 free((char *)ui->desc->dir);
262 ui->desc->name = NULL;
263 ui->desc->dir = NULL;
269 #define uid_hash_bits 8
270 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
271 #define PRIME1 0x811c9dc5
272 #define PRIME2 0x01000193
274 static void create_hash_table(void)
276 uid_hash_table = para_calloc(uid_hash_table_size
277 * sizeof(struct user_info));
280 static void free_hash_table(void)
282 free(uid_hash_table);
283 uid_hash_table = NULL;
286 static int create_tables(void)
290 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
291 ret = osl_create_table(&dir_table_desc);
299 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
300 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
301 * unused slots in the table are used to store different uids that hash to the
304 * If a hash collision occurs, different slots are successively probed in order
305 * to find an unused slot for the new uid. Probing is implemented via a second
306 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
309 * An odd number is sufficient to make sure each entry of the hash table gets
310 * probed for probe_num between 0 and s-1 because s is a power of two, hence
311 * the second hash value never hash a common divisor with the hash table size.
312 * IOW: h is invertible in the ring [0..s].
314 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
316 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
317 % uid_hash_table_size;
320 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
321 + uid_hash_table_size; ui++)
323 enum search_uid_flags {
325 CREATE_USER_TABLE = 2,
328 static int search_uid(uint32_t uid, enum search_uid_flags flags,
329 struct user_info **ui_ptr)
333 for (p = 0; p < uid_hash_table_size; p++) {
334 struct user_info *ui = uid_hash_table + double_hash(uid, p);
342 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
355 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
358 static int update_user_row(struct osl_table *t, uint64_t dir_num,
362 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
364 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
366 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
368 if (ret < 0) { /* this is the first file we add */
369 struct osl_object objects[NUM_UT_COLUMNS];
370 uint64_t num_files = 1;
372 objects[UT_DIR_NUM].data = &dir_num;
373 objects[UT_DIR_NUM].size = sizeof(dir_num);
374 objects[UT_BYTES].data = add;
375 objects[UT_BYTES].size = sizeof(*add);
376 objects[UT_FILES].data = &num_files;
377 objects[UT_FILES].size = sizeof(num_files);
378 INFO_LOG("######################### ret: %d\n", ret);
379 ret = osl_add_row(t, objects);
380 INFO_LOG("######################### ret: %d\n", ret);
382 } else { /* add size and increment file count */
384 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
386 ret = osl_get_object(t, row, UT_BYTES, &obj1);
389 num = *(uint64_t *)obj1.data + *add;
390 ret = osl_update_object(t, row, UT_BYTES, &obj2);
393 ret = osl_get_object(t, row, UT_FILES, &obj1);
396 num = *(uint64_t *)obj1.data + 1;
397 return osl_update_object(t, row, UT_FILES, &obj2);
401 static uint64_t num_dirs;
402 static uint64_t num_files;
403 static uint64_t num_bytes;
405 int scan_dir(char *dirname)
408 struct dirent *entry;
409 int ret, cwd_fd, ret2;
410 uint64_t dir_size = 0, dir_files = 0;
411 uint64_t this_dir_num = num_dirs++;
413 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
414 ret = para_opendir(dirname, &dir, &cwd_fd);
416 if (ret != -ERRNO_TO_ERROR(EACCES))
418 WARNING_LOG("permission denied for %s\n", dirname);
421 while ((entry = readdir(dir))) {
427 struct user_info *ui;
429 if (!strcmp(entry->d_name, "."))
431 if (!strcmp(entry->d_name, ".."))
433 if (lstat(entry->d_name, &s) == -1) {
434 WARNING_LOG("lstat error for %s/%s\n", dirname,
439 if (!S_ISREG(m) && !S_ISDIR(m))
442 tmp = make_message("%s/%s", dirname, entry->d_name);
456 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
461 ret = update_user_row(ui->table, this_dir_num, &size);
465 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
468 ret2 = para_fchdir(cwd_fd);
469 if (ret2 < 0 && ret >= 0)
475 static int get_dir_name(struct osl_row *row, char **name)
477 struct osl_object obj;
478 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
486 const uint64_t size_unit_divisors[] = {
487 [size_unit_arg_b] = 1ULL,
488 [size_unit_arg_k] = 1024ULL,
489 [size_unit_arg_m] = 1024ULL * 1024ULL,
490 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
491 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
494 const uint64_t count_unit_divisors[] = {
496 [count_unit_arg_n] = 1ULL,
497 [count_unit_arg_k] = 1000ULL,
498 [count_unit_arg_m] = 1000ULL * 1000ULL,
499 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
500 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
503 const char size_unit_abbrevs[] = " BKMGT";
504 const char count_unit_abbrevs[] = " KMGT";
506 static void format_size_value(enum enum_size_unit unit, uint64_t value, char *result)
508 if (unit == size_unit_arg_h) /* human readable */
509 for (unit = size_unit_arg_b; unit < size_unit_arg_t && value > size_unit_divisors[unit + 1]; unit++)
511 sprintf(result, "%llu%c", (long long unsigned)value / size_unit_divisors[unit], size_unit_abbrevs[unit]);
514 static void format_count_value(enum enum_count_unit unit, uint64_t value, char *result)
516 if (unit == count_unit_arg_h) /* human readable */
517 for (unit = count_unit_arg_n; unit < count_unit_arg_t && value > count_unit_divisors[unit + 1]; unit++)
519 sprintf(result, "%llu%c", (long long unsigned)value / count_unit_divisors[unit], count_unit_abbrevs[unit]);
522 enum global_stats_flags {
523 GSF_PRINT_DIRNAME = 1,
526 GSF_COMPUTE_SUMMARY = 8,
529 struct global_stats_info {
531 enum global_stats_flags flags;
534 static int global_stats_loop_function(struct osl_row *row, void *data)
536 struct global_stats_info *gsi = data;
537 struct osl_object obj;
538 char *dirname, formated_value[25];
539 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
541 if (!gsi->count && !summary)
542 return -E_LOOP_COMPLETE;
543 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
544 ret = get_dir_name(row, &dirname);
547 printf("%s%s", dirname,
548 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
552 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
554 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
557 files = *(uint64_t *)obj.data;
558 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
559 format_size_value(conf.size_unit_arg, files,
561 printf("%s%s", formated_value,
562 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
567 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
569 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
572 bytes = *(uint64_t *)obj.data;
573 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
574 format_size_value(conf.size_unit_arg, bytes,
576 printf("%s\n", formated_value);
588 static void print_id_stats(void)
590 struct user_info *ui;
592 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
594 char formated_dir_count[25], formated_file_count[25],
598 format_count_value(conf.count_unit_arg, ui->dirs,
600 format_count_value(conf.count_unit_arg, ui->files,
601 formated_file_count);
602 format_size_value(conf.size_unit_arg, ui->bytes,
604 printf("%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
612 enum user_stats_flags {
613 USF_PRINT_DIRNAME = 1,
616 USF_COMPUTE_SUMMARY = 8,
619 struct user_stats_info {
621 enum user_stats_flags flags;
622 struct user_info *ui;
625 static int user_stats_loop_function(struct osl_row *row, void *data)
627 struct user_stats_info *usi = data;
628 struct osl_row *dir_row;
629 struct osl_object obj;
630 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
631 char formated_value[25];
633 if (!usi->count && !summary)
634 return -E_LOOP_COMPLETE;
635 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
637 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
640 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
643 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
649 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
653 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
655 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
658 files = *(uint64_t *)obj.data;
659 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
660 format_size_value(conf.size_unit_arg, files,
662 printf("%s%s", formated_value,
663 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
667 usi->ui->files += files;
669 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
671 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
674 bytes = *(uint64_t *)obj.data;
675 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
676 format_size_value(conf.size_unit_arg, bytes,
678 printf("%s\n", formated_value);
681 usi->ui->bytes += bytes;
691 static void print_user_stats(void)
693 struct user_info *ui;
696 struct user_stats_info usi = {
697 .count = conf.limit_arg,
702 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
703 printf("************************************************ uid %u\n",
707 printf("----------------- Largest dirs -------------------\n");
708 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
709 user_stats_loop_function);
710 printf("---------- dirs containing most files ------------\n");
711 usi.count = conf.limit_arg,
712 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
713 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
714 user_stats_loop_function);
718 static int print_statistics(void)
721 struct global_stats_info gsi = {
722 .count = conf.limit_arg,
723 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
726 printf("----------------- Largest dirs -------------------\n");
727 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
728 global_stats_loop_function);
729 if (ret < 0 && ret != -E_LOOP_COMPLETE)
731 gsi.count = conf.limit_arg;
733 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
734 printf("---------- dirs containing most files ------------\n");
735 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
736 global_stats_loop_function);
737 if (ret < 0 && ret != -E_LOOP_COMPLETE)
740 printf("------------------ Global summary (dirs/files/bytes)\n"
741 "%llu\t%llu\t%llu\n",
742 (long long unsigned)num_dirs, (long long unsigned)num_files,
743 (long long unsigned)num_bytes);
749 static char *get_uid_list_name(void)
751 return make_message("%s/uid_list", conf.database_dir_arg);
754 static int write_uid_list(void)
756 char *buf, *filename;
758 struct user_info *ui;
759 size_t size = num_uids * sizeof(uint32_t);
764 buf = para_malloc(size);
768 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
770 filename = get_uid_list_name();
771 ret = para_write_file(filename, buf, size);
777 static int open_dir_table(void)
779 if (!dir_table_desc.dir) /* we did not create the table */
780 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
781 return osl_open_table(&dir_table_desc, &dir_table);
784 static void close_dir_table(void)
790 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
792 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
793 free((char *)dir_table_desc.dir);
797 static void close_user_table(struct user_info *ui)
801 if (!ui || !ui->table)
803 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
805 ERROR_LOG("failed to close user table %u: %s\n",
806 (unsigned) ui->uid, error_txt(-ret));
807 free((char *)ui->desc->name);
808 ui->desc->name = NULL;
809 free((char *)ui->desc->dir);
810 ui->desc->dir = NULL;
816 static void close_user_tables(void)
818 struct user_info *ui;
821 close_user_table(ui);
824 static void close_all_tables(void)
831 static int com_create()
833 int ret = create_tables();
836 ret = open_dir_table();
839 ret = scan_dir(conf.base_dir_arg);
842 ret = write_uid_list();
848 static int read_uid_file(void)
852 char *filename = get_uid_list_name(), *map;
853 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
859 /* hash table size should be a power of two and larger than the number of uids */
860 uid_hash_table_size = 4;
861 while (uid_hash_table_size < num_uids)
862 uid_hash_table_size *= 2;
864 for (n = 0; n < num_uids; n++) {
865 uint32_t uid = read_u32(map + n * sizeof(uid));
866 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
871 para_munmap(map, size);
875 static int com_select(void)
879 ret = open_dir_table();
882 ret = read_uid_file();
890 int main(int argc, char **argv)
893 struct cmdline_parser_params params = {
897 .check_ambiguity = 0,
901 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
903 if (conf.select_given)
911 ERROR_LOG("%s\n", error_txt(-ret));
912 return -EXIT_FAILURE;