2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
9 #include "portable_io.h"
13 #define DATABASE_DIR "/tmp/adu"
14 #define UID_LIST DATABASE_DIR "/" "uid_list"
16 /** Command line and config file options. */
17 static struct gengetopt_args_info conf;
21 struct osl_table *table;
25 struct osl_table_description *desc;
28 static struct user_info *uid_hash_table;
30 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
31 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
38 * \param fml Usual format string.
40 * All XXX_LOG() macros use this function.
42 __printf_2_3 void __log(int ll, const char* fmt,...)
50 if (ll < conf.loglevel_arg)
55 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
56 fprintf(outfd, "%s ", str);
58 vfprintf(outfd, fmt, argp);
63 * Compare the size of two directories
65 * \param obj1 Pointer to the first object.
66 * \param obj2 Pointer to the second object.
68 * This function first compares the size values as usual integers. If they compare as
69 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
70 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
72 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
74 uint64_t d1 = *(uint64_t *)obj1->data;
75 uint64_t d2 = *(uint64_t *)obj2->data;
76 int ret = NUM_COMPARE(d2, d1);
80 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
81 return NUM_COMPARE(obj2->data, obj1->data);
85 * Compare two osl objects of string type.
87 * \param obj1 Pointer to the first object.
88 * \param obj2 Pointer to the second object.
90 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
91 * are taken into account.
93 * \return It returns an integer less than, equal to, or greater than zero if
94 * \a obj1 is found, respectively, to be less than, to match, or be greater
97 * \sa strcmp(3), strncmp(3), osl_compare_func.
99 static int string_compare(const struct osl_object *obj1,
100 const struct osl_object *obj2)
102 const char *str1 = (const char *)obj1->data;
103 const char *str2 = (const char *)obj2->data;
104 return strncmp(str1, str2, MIN(obj1->size, obj2->size));
108 * Compare two osl objects pointing to unsigned integers of 64 bit size.
110 * \param obj1 Pointer to the first integer.
111 * \param obj2 Pointer to the second integer.
113 * \return The values required for an osl compare function.
115 * \sa osl_compare_func, osl_hash_compare().
117 static int uint64_compare(const struct osl_object *obj1,
118 const struct osl_object *obj2)
120 uint64_t d1 = read_u64((const char *)obj1->data);
121 uint64_t d2 = read_u64((const char *)obj2->data);
130 /** The columns of the directory table. */
131 enum dir_table_columns {
132 /** The name of the directory. */
134 /** The dir count number. */
136 /** The number of bytes of all regular files. */
138 /** The number of all regular files. */
140 /** Number of columns in this table. */
144 static struct osl_column_description dir_table_cols[] = {
146 .storage_type = OSL_MAPPED_STORAGE,
147 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
149 .compare_function = string_compare,
152 .storage_type = OSL_MAPPED_STORAGE,
153 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
155 .compare_function = uint64_compare,
156 .data_size = sizeof(uint64_t)
159 .storage_type = OSL_MAPPED_STORAGE,
160 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
161 .compare_function = size_compare,
163 .data_size = sizeof(uint64_t)
166 .storage_type = OSL_MAPPED_STORAGE,
167 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
168 .compare_function = size_compare,
170 .data_size = sizeof(uint64_t)
174 static struct osl_table_description dir_table_desc = {
176 .num_columns = NUM_DT_COLUMNS,
178 .column_descriptions = dir_table_cols,
182 /** The columns of the id table. */
183 enum user_table_columns {
184 /** The numer of the directory. */
186 /** The number of bytes of all regular files in this dir owned by this id. */
188 /** The number of files in this dir owned by this id. */
190 /** Number of columns in this table. */
194 static struct osl_column_description user_table_cols[] = {
196 .storage_type = OSL_MAPPED_STORAGE,
197 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
199 .compare_function = uint64_compare,
200 .data_size = sizeof(uint64_t)
203 .storage_type = OSL_MAPPED_STORAGE,
204 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
205 .compare_function = size_compare,
207 .data_size = sizeof(uint64_t)
210 .storage_type = OSL_MAPPED_STORAGE,
211 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
212 .compare_function = size_compare,
214 .data_size = sizeof(uint64_t)
218 static struct osl_table *dir_table;
220 static int add_directory(char *dirname, uint64_t dir_num, uint64_t *dir_size,
223 struct osl_object dir_objects[NUM_DT_COLUMNS];
225 INFO_LOG("adding #%llu: %s\n", (long long unsigned)dir_num, dirname);
226 dir_objects[DT_NAME].data = dirname;
227 dir_objects[DT_NAME].size = strlen(dirname) + 1;
228 dir_objects[DT_NUM].data = &dir_num;
229 dir_objects[DT_NUM].size = sizeof(dir_num);
230 dir_objects[DT_BYTES].data = dir_size;
231 dir_objects[DT_BYTES].size = sizeof(*dir_size);
232 dir_objects[DT_FILES].data = dir_files;
233 dir_objects[DT_FILES].size = sizeof(*dir_files);
235 return osl_add_row(dir_table, dir_objects);
238 static uint32_t num_uids;
240 static int open_user_table(struct user_info *ui, int create)
244 ui->desc = para_malloc(sizeof(*ui->desc));
245 ui->desc->num_columns = NUM_UT_COLUMNS;
247 ui->desc->column_descriptions = user_table_cols;
248 ui->desc->dir = para_strdup(DATABASE_DIR);
249 ui->desc->name = make_message("%u", (unsigned)ui->uid);
251 INFO_LOG(".............................uid #%u: %u\n",
252 (unsigned)num_uids, (unsigned)ui->uid);
254 ret = osl_create_table(ui->desc);
258 ret = osl_open_table(ui->desc, &ui->table);
263 free((char *)ui->desc->name);
264 free((char *)ui->desc->dir);
266 ui->desc->name = NULL;
267 ui->desc->dir = NULL;
273 #define uid_hash_bits 8
274 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
275 #define PRIME1 0x811c9dc5
276 #define PRIME2 0x01000193
278 static void create_hash_table(void)
280 uid_hash_table = para_calloc(uid_hash_table_size
281 * sizeof(struct user_info));
284 static void free_hash_table(void)
286 free(uid_hash_table);
287 uid_hash_table = NULL;
290 static int create_tables(void)
294 ret = osl_create_table(&dir_table_desc);
302 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
303 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
304 * unused slots in the table are used to store different uids that hash to the
307 * If a hash collision occurs, different slots are successively probed in order
308 * to find an unused slot for the new uid. Probing is implemented via a second
309 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
312 * An odd number is sufficient to make sure each entry of the hash table gets
313 * probed for probe_num between 0 and s-1 because s is a power of two, hence
314 * the second hash value never hash a common divisor with the hash table size.
315 * IOW: h is invertible in the ring [0..s].
317 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
319 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
320 % uid_hash_table_size;
323 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
324 + uid_hash_table_size; ui++)
326 enum search_uid_flags {
328 CREATE_USER_TABLE = 2,
331 static int search_uid(uint32_t uid, enum search_uid_flags flags,
332 struct user_info **ui_ptr)
336 for (p = 0; p < uid_hash_table_size; p++) {
337 struct user_info *ui = uid_hash_table + double_hash(uid, p);
345 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
358 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
361 static int update_user_row(struct osl_table *t, uint64_t dir_num,
365 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
367 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
369 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
371 if (ret < 0) { /* this is the first file we add */
372 struct osl_object objects[NUM_UT_COLUMNS];
373 uint64_t num_files = 1;
375 objects[UT_DIR_NUM].data = &dir_num;
376 objects[UT_DIR_NUM].size = sizeof(dir_num);
377 objects[UT_BYTES].data = add;
378 objects[UT_BYTES].size = sizeof(*add);
379 objects[UT_FILES].data = &num_files;
380 objects[UT_FILES].size = sizeof(num_files);
381 INFO_LOG("######################### ret: %d\n", ret);
382 ret = osl_add_row(t, objects);
383 INFO_LOG("######################### ret: %d\n", ret);
385 } else { /* add size and increment file count */
387 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
389 ret = osl_get_object(t, row, UT_BYTES, &obj1);
392 num = *(uint64_t *)obj1.data + *add;
393 ret = osl_update_object(t, row, UT_BYTES, &obj2);
396 ret = osl_get_object(t, row, UT_FILES, &obj1);
399 num = *(uint64_t *)obj1.data + 1;
400 return osl_update_object(t, row, UT_FILES, &obj2);
404 static uint64_t num_dirs;
405 static uint64_t num_files;
406 static uint64_t num_bytes;
408 int scan_dir(char *dirname)
411 struct dirent *entry;
412 int ret, cwd_fd, ret2;
413 uint64_t dir_size = 0, dir_files = 0;
414 uint64_t this_dir_num = num_dirs++;
416 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
417 ret = para_opendir(dirname, &dir, &cwd_fd);
419 if (ret != -ERRNO_TO_ERROR(EACCES))
421 WARNING_LOG("permission denied for %s\n", dirname);
424 while ((entry = readdir(dir))) {
430 struct user_info *ui;
432 if (!strcmp(entry->d_name, "."))
434 if (!strcmp(entry->d_name, ".."))
436 if (lstat(entry->d_name, &s) == -1) {
437 WARNING_LOG("lstat error for %s/%s\n", dirname,
442 if (!S_ISREG(m) && !S_ISDIR(m))
445 tmp = make_message("%s/%s", dirname, entry->d_name);
459 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
464 ret = update_user_row(ui->table, this_dir_num, &size);
468 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
471 ret2 = para_fchdir(cwd_fd);
472 if (ret2 < 0 && ret >= 0)
478 static int get_dir_name(struct osl_row *row, char **name)
480 struct osl_object obj;
481 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
489 enum global_stats_flags {
490 GSF_PRINT_DIRNAME = 1,
493 GSF_COMPUTE_SUMMARY = 8,
496 struct global_stats_info {
498 enum global_stats_flags flags;
501 static int global_stats_loop_function(struct osl_row *row, void *data)
503 struct global_stats_info *gsi = data;
504 struct osl_object obj;
506 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
508 if (!gsi->count && !summary)
509 return -E_LOOP_COMPLETE;
510 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
511 ret = get_dir_name(row, &dirname);
514 printf("%s%s", dirname,
515 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
519 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
521 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
524 files = *(uint64_t *)obj.data;
525 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
526 printf("%llu%s", (long long unsigned)files,
527 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
531 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
533 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
536 bytes = *(uint64_t *)obj.data;
537 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
538 printf("%llu\n", (long long unsigned)bytes);
549 static void print_id_stats(void)
551 struct user_info *ui;
553 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
557 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
558 (long long unsigned)ui->dirs,
559 (long long unsigned)ui->files,
560 (long long unsigned)ui->bytes);
564 enum user_stats_flags {
565 USF_PRINT_DIRNAME = 1,
568 USF_COMPUTE_SUMMARY = 8,
571 struct user_stats_info {
573 enum user_stats_flags flags;
574 struct user_info *ui;
577 static int user_stats_loop_function(struct osl_row *row, void *data)
579 struct user_stats_info *usi = data;
580 struct osl_row *dir_row;
581 struct osl_object obj;
582 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
584 if (!usi->count && !summary)
585 return -E_LOOP_COMPLETE;
586 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
588 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
591 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
594 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
600 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
604 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
606 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
609 files = *(uint64_t *)obj.data;
610 if (usi->count && (usi->flags & USF_PRINT_FILES))
612 (long long unsigned)files,
613 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
616 usi->ui->files += files;
618 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
620 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
623 bytes = *(uint64_t *)obj.data;
624 if (usi->count && (usi->flags & USF_PRINT_BYTES))
625 printf("%llu\n", (long long unsigned)bytes);
627 usi->ui->bytes += bytes;
637 static void print_user_stats(void)
639 struct user_info *ui;
642 struct user_stats_info usi = {
648 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
649 printf("************************************************ uid %u\n",
653 printf("----------------- Largest dirs -------------------\n");
654 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
655 user_stats_loop_function);
656 printf("---------- dirs containing most files ------------\n");
658 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
659 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
660 user_stats_loop_function);
664 static int print_statistics(void)
667 struct global_stats_info gsi = {
669 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
672 printf("----------------- Largest dirs -------------------\n");
673 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
674 global_stats_loop_function);
675 if (ret < 0 && ret != -E_LOOP_COMPLETE)
679 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
680 printf("---------- dirs containing most files ------------\n");
681 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
682 global_stats_loop_function);
683 if (ret < 0 && ret != -E_LOOP_COMPLETE)
686 printf("------------------ Global summary (dirs/files/bytes)\n"
687 "%llu\t%llu\t%llu\n",
688 (long long unsigned)num_dirs, (long long unsigned)num_files,
689 (long long unsigned)num_bytes);
695 static int write_uid_list(void)
699 struct user_info *ui;
700 size_t size = num_uids * sizeof(uint32_t);
705 buf = para_malloc(size);
709 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
711 ret = para_write_file(UID_LIST, buf, size);
716 static int open_dir_table(void)
718 return osl_open_table(&dir_table_desc, &dir_table);
721 static void close_dir_table(void)
727 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
729 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
733 static void close_user_table(struct user_info *ui)
737 if (!ui || !ui->table)
739 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
741 ERROR_LOG("failed to close user table %u: %s\n",
742 (unsigned) ui->uid, error_txt(-ret));
743 free((char *)ui->desc->name);
744 ui->desc->name = NULL;
745 free((char *)ui->desc->dir);
746 ui->desc->dir = NULL;
752 static void close_user_tables(void)
754 struct user_info *ui;
757 close_user_table(ui);
760 static void close_all_tables(void)
767 static int com_create()
769 int ret = create_tables();
772 ret = open_dir_table();
775 ret = scan_dir(conf.base_dir_arg);
778 ret = write_uid_list();
784 static int read_uid_file(void)
788 int ret = mmap_full_file(UID_LIST, O_RDONLY, (void **)&map, &size, NULL);
794 /* hash table size should be a power of two and larger than the number of uids */
795 uid_hash_table_size = 4;
796 while (uid_hash_table_size < num_uids)
797 uid_hash_table_size *= 2;
799 for (n = 0; n < num_uids; n++) {
800 uint32_t uid = read_u32(map + n * sizeof(uid));
801 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
806 para_munmap(map, size);
810 static int com_select(void)
814 ret = open_dir_table();
817 ret = read_uid_file();
825 int main(int argc, char **argv)
828 struct cmdline_parser_params params = {
832 .check_ambiguity = 0,
836 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
838 if (conf.select_given)
846 ERROR_LOG("%s\n", error_txt(-ret));
847 return -EXIT_FAILURE;