2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
14 #define DATABASE_DIR "/tmp/adu"
15 #define UID_LIST DATABASE_DIR "/" "uid_list"
17 /** Command line and config file options. */
18 static struct gengetopt_args_info conf;
22 struct osl_table *table;
26 struct osl_table_description *desc;
29 static struct user_info *uid_hash_table;
31 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
32 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
39 * \param fml Usual format string.
41 * All XXX_LOG() macros use this function.
43 __printf_2_3 void __log(int ll, const char* fmt,...)
51 if (ll < conf.loglevel_arg)
56 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
57 fprintf(outfd, "%s ", str);
59 vfprintf(outfd, fmt, argp);
64 * Compare the size of two directories
66 * \param obj1 Pointer to the first object.
67 * \param obj2 Pointer to the second object.
69 * This function first compares the size values as usual integers. If they compare as
70 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
71 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
73 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
75 uint64_t d1 = *(uint64_t *)obj1->data;
76 uint64_t d2 = *(uint64_t *)obj2->data;
77 int ret = NUM_COMPARE(d2, d1);
81 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
82 return NUM_COMPARE(obj2->data, obj1->data);
86 * Compare two osl objects of string type.
88 * \param obj1 Pointer to the first object.
89 * \param obj2 Pointer to the second object.
91 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
92 * are taken into account.
94 * \return It returns an integer less than, equal to, or greater than zero if
95 * \a obj1 is found, respectively, to be less than, to match, or be greater
98 * \sa strcmp(3), strncmp(3), osl_compare_func.
100 static int string_compare(const struct osl_object *obj1,
101 const struct osl_object *obj2)
103 const char *str1 = (const char *)obj1->data;
104 const char *str2 = (const char *)obj2->data;
105 return strncmp(str1, str2, MIN(obj1->size, obj2->size));
109 * Compare two osl objects pointing to unsigned integers of 64 bit size.
111 * \param obj1 Pointer to the first integer.
112 * \param obj2 Pointer to the second integer.
114 * \return The values required for an osl compare function.
116 * \sa osl_compare_func, osl_hash_compare().
118 static int uint64_compare(const struct osl_object *obj1,
119 const struct osl_object *obj2)
121 uint64_t d1 = read_u64((const char *)obj1->data);
122 uint64_t d2 = read_u64((const char *)obj2->data);
131 /** The columns of the directory table. */
132 enum dir_table_columns {
133 /** The name of the directory. */
135 /** The dir count number. */
137 /** The number of bytes of all regular files. */
139 /** The number of all regular files. */
141 /** Number of columns in this table. */
145 static struct osl_column_description dir_table_cols[] = {
147 .storage_type = OSL_MAPPED_STORAGE,
148 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
150 .compare_function = string_compare,
153 .storage_type = OSL_MAPPED_STORAGE,
154 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
156 .compare_function = uint64_compare,
157 .data_size = sizeof(uint64_t)
160 .storage_type = OSL_MAPPED_STORAGE,
161 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
162 .compare_function = size_compare,
164 .data_size = sizeof(uint64_t)
167 .storage_type = OSL_MAPPED_STORAGE,
168 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
169 .compare_function = size_compare,
171 .data_size = sizeof(uint64_t)
175 static struct osl_table_description dir_table_desc = {
177 .num_columns = NUM_DT_COLUMNS,
179 .column_descriptions = dir_table_cols,
183 /** The columns of the id table. */
184 enum user_table_columns {
185 /** The numer of the directory. */
187 /** The number of bytes of all regular files in this dir owned by this id. */
189 /** The number of files in this dir owned by this id. */
191 /** Number of columns in this table. */
195 static struct osl_column_description user_table_cols[] = {
197 .storage_type = OSL_MAPPED_STORAGE,
198 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
200 .compare_function = uint64_compare,
201 .data_size = sizeof(uint64_t)
204 .storage_type = OSL_MAPPED_STORAGE,
205 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
206 .compare_function = size_compare,
208 .data_size = sizeof(uint64_t)
211 .storage_type = OSL_MAPPED_STORAGE,
212 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
213 .compare_function = size_compare,
215 .data_size = sizeof(uint64_t)
219 static struct osl_table *dir_table;
221 static int add_directory(char *dirname, uint64_t dir_num, uint64_t *dir_size,
224 struct osl_object dir_objects[NUM_DT_COLUMNS];
226 INFO_LOG("adding #%llu: %s\n", (long long unsigned)dir_num, dirname);
227 dir_objects[DT_NAME].data = dirname;
228 dir_objects[DT_NAME].size = strlen(dirname) + 1;
229 dir_objects[DT_NUM].data = &dir_num;
230 dir_objects[DT_NUM].size = sizeof(dir_num);
231 dir_objects[DT_BYTES].data = dir_size;
232 dir_objects[DT_BYTES].size = sizeof(*dir_size);
233 dir_objects[DT_FILES].data = dir_files;
234 dir_objects[DT_FILES].size = sizeof(*dir_files);
236 return osl_add_row(dir_table, dir_objects);
239 static uint32_t num_uids;
241 static int open_user_table(struct user_info *ui, int create)
245 ui->desc = para_malloc(sizeof(*ui->desc));
246 ui->desc->num_columns = NUM_UT_COLUMNS;
248 ui->desc->column_descriptions = user_table_cols;
249 ui->desc->dir = para_strdup(DATABASE_DIR);
250 ui->desc->name = make_message("%u", (unsigned)ui->uid);
252 INFO_LOG(".............................uid #%u: %u\n",
253 (unsigned)num_uids, (unsigned)ui->uid);
255 ret = osl_create_table(ui->desc);
259 ret = osl_open_table(ui->desc, &ui->table);
264 free((char *)ui->desc->name);
265 free((char *)ui->desc->dir);
267 ui->desc->name = NULL;
268 ui->desc->dir = NULL;
274 #define uid_hash_bits 8
275 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
276 #define PRIME1 0x811c9dc5
277 #define PRIME2 0x01000193
279 static void create_hash_table(void)
281 uid_hash_table = para_calloc(uid_hash_table_size
282 * sizeof(struct user_info));
285 static void free_hash_table(void)
287 free(uid_hash_table);
288 uid_hash_table = NULL;
291 static int create_tables(void)
295 ret = osl_create_table(&dir_table_desc);
303 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
304 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
305 * unused slots in the table are used to store different uids that hash to the
308 * If a hash collision occurs, different slots are successively probed in order
309 * to find an unused slot for the new uid. Probing is implemented via a second
310 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
313 * An odd number is sufficient to make sure each entry of the hash table gets
314 * probed for probe_num between 0 and s-1 because s is a power of two, hence
315 * the second hash value never hash a common divisor with the hash table size.
316 * IOW: h is invertible in the ring [0..s].
318 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
320 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
321 % uid_hash_table_size;
324 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
325 + uid_hash_table_size; ui++)
327 enum search_uid_flags {
329 CREATE_USER_TABLE = 2,
332 static int search_uid(uint32_t uid, enum search_uid_flags flags,
333 struct user_info **ui_ptr)
337 for (p = 0; p < uid_hash_table_size; p++) {
338 struct user_info *ui = uid_hash_table + double_hash(uid, p);
346 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
359 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
362 static int update_user_row(struct osl_table *t, uint64_t dir_num,
366 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
368 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
370 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
372 if (ret < 0) { /* this is the first file we add */
373 struct osl_object objects[NUM_UT_COLUMNS];
374 uint64_t num_files = 1;
376 objects[UT_DIR_NUM].data = &dir_num;
377 objects[UT_DIR_NUM].size = sizeof(dir_num);
378 objects[UT_BYTES].data = add;
379 objects[UT_BYTES].size = sizeof(*add);
380 objects[UT_FILES].data = &num_files;
381 objects[UT_FILES].size = sizeof(num_files);
382 INFO_LOG("######################### ret: %d\n", ret);
383 ret = osl_add_row(t, objects);
384 INFO_LOG("######################### ret: %d\n", ret);
386 } else { /* add size and increment file count */
388 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
390 ret = osl_get_object(t, row, UT_BYTES, &obj1);
393 num = *(uint64_t *)obj1.data + *add;
394 ret = osl_update_object(t, row, UT_BYTES, &obj2);
397 ret = osl_get_object(t, row, UT_FILES, &obj1);
400 num = *(uint64_t *)obj1.data + 1;
401 return osl_update_object(t, row, UT_FILES, &obj2);
405 static uint64_t num_dirs;
406 static uint64_t num_files;
407 static uint64_t num_bytes;
409 int scan_dir(char *dirname)
412 struct dirent *entry;
413 int ret, cwd_fd, ret2;
414 uint64_t dir_size = 0, dir_files = 0;
415 uint64_t this_dir_num = num_dirs++;
417 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
418 ret = para_opendir(dirname, &dir, &cwd_fd);
420 if (ret != -ERRNO_TO_ERROR(EACCES))
422 WARNING_LOG("permission denied for %s\n", dirname);
425 while ((entry = readdir(dir))) {
431 struct user_info *ui;
433 if (!strcmp(entry->d_name, "."))
435 if (!strcmp(entry->d_name, ".."))
437 if (lstat(entry->d_name, &s) == -1) {
438 WARNING_LOG("lstat error for %s/%s\n", dirname,
443 if (!S_ISREG(m) && !S_ISDIR(m))
446 tmp = make_message("%s/%s", dirname, entry->d_name);
460 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
465 ret = update_user_row(ui->table, this_dir_num, &size);
469 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
472 ret2 = para_fchdir(cwd_fd);
473 if (ret2 < 0 && ret >= 0)
479 static int get_dir_name(struct osl_row *row, char **name)
481 struct osl_object obj;
482 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
490 enum global_stats_flags {
491 GSF_PRINT_DIRNAME = 1,
494 GSF_COMPUTE_SUMMARY = 8,
497 struct global_stats_info {
499 enum global_stats_flags flags;
502 static int global_stats_loop_function(struct osl_row *row, void *data)
504 struct global_stats_info *gsi = data;
505 struct osl_object obj;
507 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
509 if (!gsi->count && !summary)
510 return -E_LOOP_COMPLETE;
511 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
512 ret = get_dir_name(row, &dirname);
515 printf("%s%s", dirname,
516 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
520 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
522 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
525 files = *(uint64_t *)obj.data;
526 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
527 printf("%llu%s", (long long unsigned)files,
528 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
532 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
534 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
537 bytes = *(uint64_t *)obj.data;
538 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
539 printf("%llu\n", (long long unsigned)bytes);
550 static void print_id_stats(void)
552 struct user_info *ui;
554 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
558 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
559 (long long unsigned)ui->dirs,
560 (long long unsigned)ui->files,
561 (long long unsigned)ui->bytes);
565 enum user_stats_flags {
566 USF_PRINT_DIRNAME = 1,
569 USF_COMPUTE_SUMMARY = 8,
572 struct user_stats_info {
574 enum user_stats_flags flags;
575 struct user_info *ui;
578 static int user_stats_loop_function(struct osl_row *row, void *data)
580 struct user_stats_info *usi = data;
581 struct osl_row *dir_row;
582 struct osl_object obj;
583 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
585 if (!usi->count && !summary)
586 return -E_LOOP_COMPLETE;
587 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
589 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
592 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
595 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
601 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
605 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
607 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
610 files = *(uint64_t *)obj.data;
611 if (usi->count && (usi->flags & USF_PRINT_FILES))
613 (long long unsigned)files,
614 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
617 usi->ui->files += files;
619 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
621 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
624 bytes = *(uint64_t *)obj.data;
625 if (usi->count && (usi->flags & USF_PRINT_BYTES))
626 printf("%llu\n", (long long unsigned)bytes);
628 usi->ui->bytes += bytes;
638 static void print_user_stats(void)
640 struct user_info *ui;
643 struct user_stats_info usi = {
649 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
650 printf("************************************************ uid %u\n",
654 printf("----------------- Largest dirs -------------------\n");
655 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
656 user_stats_loop_function);
657 printf("---------- dirs containing most files ------------\n");
659 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
660 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
661 user_stats_loop_function);
665 static int print_statistics(void)
668 struct global_stats_info gsi = {
670 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
673 printf("----------------- Largest dirs -------------------\n");
674 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
675 global_stats_loop_function);
676 if (ret < 0 && ret != -E_LOOP_COMPLETE)
680 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
681 printf("---------- dirs containing most files ------------\n");
682 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
683 global_stats_loop_function);
684 if (ret < 0 && ret != -E_LOOP_COMPLETE)
687 printf("------------------ Global summary (dirs/files/bytes)\n"
688 "%llu\t%llu\t%llu\n",
689 (long long unsigned)num_dirs, (long long unsigned)num_files,
690 (long long unsigned)num_bytes);
696 static int write_uid_list(void)
700 struct user_info *ui;
701 size_t size = num_uids * sizeof(uint32_t);
706 buf = para_malloc(size);
710 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
712 ret = para_write_file(UID_LIST, buf, size);
717 static int open_dir_table(void)
719 return osl_open_table(&dir_table_desc, &dir_table);
722 static void close_dir_table(void)
728 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
730 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
734 static void close_user_table(struct user_info *ui)
738 if (!ui || !ui->table)
740 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
742 ERROR_LOG("failed to close user table %u: %s\n",
743 (unsigned) ui->uid, error_txt(-ret));
744 free((char *)ui->desc->name);
745 ui->desc->name = NULL;
746 free((char *)ui->desc->dir);
747 ui->desc->dir = NULL;
753 static void close_user_tables(void)
755 struct user_info *ui;
758 close_user_table(ui);
761 static void close_all_tables(void)
768 static int com_create()
770 int ret = create_tables();
773 ret = open_dir_table();
776 ret = scan_dir(conf.base_dir_arg);
779 ret = write_uid_list();
785 static int read_uid_file(void)
789 int ret = mmap_full_file(UID_LIST, O_RDONLY, (void **)&map, &size, NULL);
795 /* hash table size should be a power of two and larger than the number of uids */
796 uid_hash_table_size = 4;
797 while (uid_hash_table_size < num_uids)
798 uid_hash_table_size *= 2;
800 for (n = 0; n < num_uids; n++) {
801 uint32_t uid = read_u32(map + n * sizeof(uid));
802 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
807 para_munmap(map, size);
811 static int com_select(void)
815 ret = open_dir_table();
818 ret = read_uid_file();
826 int main(int argc, char **argv)
829 struct cmdline_parser_params params = {
833 .check_ambiguity = 0,
837 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
839 if (conf.select_given)
847 ERROR_LOG("%s\n", error_txt(-ret));
848 return -EXIT_FAILURE;