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);
247 INFO_LOG(".............................uid #%u: %u\n",
248 (unsigned)num_uids, (unsigned)ui->uid);
250 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 enum global_stats_flags {
487 GSF_PRINT_DIRNAME = 1,
490 GSF_COMPUTE_SUMMARY = 8,
493 struct global_stats_info {
495 enum global_stats_flags flags;
498 static int global_stats_loop_function(struct osl_row *row, void *data)
500 struct global_stats_info *gsi = data;
501 struct osl_object obj;
503 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
505 if (!gsi->count && !summary)
506 return -E_LOOP_COMPLETE;
507 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
508 ret = get_dir_name(row, &dirname);
511 printf("%s%s", dirname,
512 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
516 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
518 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
521 files = *(uint64_t *)obj.data;
522 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
523 printf("%llu%s", (long long unsigned)files,
524 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
528 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
530 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
533 bytes = *(uint64_t *)obj.data;
534 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
535 printf("%llu\n", (long long unsigned)bytes);
546 static void print_id_stats(void)
548 struct user_info *ui;
550 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
554 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
555 (long long unsigned)ui->dirs,
556 (long long unsigned)ui->files,
557 (long long unsigned)ui->bytes);
561 enum user_stats_flags {
562 USF_PRINT_DIRNAME = 1,
565 USF_COMPUTE_SUMMARY = 8,
568 struct user_stats_info {
570 enum user_stats_flags flags;
571 struct user_info *ui;
574 static int user_stats_loop_function(struct osl_row *row, void *data)
576 struct user_stats_info *usi = data;
577 struct osl_row *dir_row;
578 struct osl_object obj;
579 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
581 if (!usi->count && !summary)
582 return -E_LOOP_COMPLETE;
583 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
585 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
588 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
591 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
597 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
601 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
603 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
606 files = *(uint64_t *)obj.data;
607 if (usi->count && (usi->flags & USF_PRINT_FILES))
609 (long long unsigned)files,
610 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
613 usi->ui->files += files;
615 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
617 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
620 bytes = *(uint64_t *)obj.data;
621 if (usi->count && (usi->flags & USF_PRINT_BYTES))
622 printf("%llu\n", (long long unsigned)bytes);
624 usi->ui->bytes += bytes;
634 static void print_user_stats(void)
636 struct user_info *ui;
639 struct user_stats_info usi = {
640 .count = conf.limit_arg,
645 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
646 printf("************************************************ uid %u\n",
650 printf("----------------- Largest dirs -------------------\n");
651 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
652 user_stats_loop_function);
653 printf("---------- dirs containing most files ------------\n");
654 usi.count = conf.limit_arg,
655 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
656 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
657 user_stats_loop_function);
661 static int print_statistics(void)
664 struct global_stats_info gsi = {
665 .count = conf.limit_arg,
666 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
669 printf("----------------- Largest dirs -------------------\n");
670 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
671 global_stats_loop_function);
672 if (ret < 0 && ret != -E_LOOP_COMPLETE)
674 gsi.count = conf.limit_arg;
676 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
677 printf("---------- dirs containing most files ------------\n");
678 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
679 global_stats_loop_function);
680 if (ret < 0 && ret != -E_LOOP_COMPLETE)
683 printf("------------------ Global summary (dirs/files/bytes)\n"
684 "%llu\t%llu\t%llu\n",
685 (long long unsigned)num_dirs, (long long unsigned)num_files,
686 (long long unsigned)num_bytes);
692 static char *get_uid_list_name(void)
694 return make_message("%s/uid_list", conf.database_dir_arg);
697 static int write_uid_list(void)
699 char *buf, *filename;
701 struct user_info *ui;
702 size_t size = num_uids * sizeof(uint32_t);
707 buf = para_malloc(size);
711 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
713 filename = get_uid_list_name();
714 ret = para_write_file(filename, buf, size);
720 static int open_dir_table(void)
722 if (!dir_table_desc.dir) /* we did not create the table */
723 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
724 return osl_open_table(&dir_table_desc, &dir_table);
727 static void close_dir_table(void)
733 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
735 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
736 free((char *)dir_table_desc.dir);
740 static void close_user_table(struct user_info *ui)
744 if (!ui || !ui->table)
746 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
748 ERROR_LOG("failed to close user table %u: %s\n",
749 (unsigned) ui->uid, error_txt(-ret));
750 free((char *)ui->desc->name);
751 ui->desc->name = NULL;
752 free((char *)ui->desc->dir);
753 ui->desc->dir = NULL;
759 static void close_user_tables(void)
761 struct user_info *ui;
764 close_user_table(ui);
767 static void close_all_tables(void)
774 static int com_create()
776 int ret = create_tables();
779 ret = open_dir_table();
782 ret = scan_dir(conf.base_dir_arg);
785 ret = write_uid_list();
791 static int read_uid_file(void)
795 char *filename = get_uid_list_name(), *map;
796 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
802 /* hash table size should be a power of two and larger than the number of uids */
803 uid_hash_table_size = 4;
804 while (uid_hash_table_size < num_uids)
805 uid_hash_table_size *= 2;
807 for (n = 0; n < num_uids; n++) {
808 uint32_t uid = read_u32(map + n * sizeof(uid));
809 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
814 para_munmap(map, size);
818 static int com_select(void)
822 ret = open_dir_table();
825 ret = read_uid_file();
833 int main(int argc, char **argv)
836 struct cmdline_parser_params params = {
840 .check_ambiguity = 0,
844 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
846 if (conf.select_given)
854 ERROR_LOG("%s\n", error_txt(-ret));
855 return -EXIT_FAILURE;