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,...)
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 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
305 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
306 % uid_hash_table_size;
309 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
310 + uid_hash_table_size; ui++)
312 enum search_uid_flags {
314 CREATE_USER_TABLE = 2,
317 static int search_uid(uint32_t uid, enum search_uid_flags flags,
318 struct user_info **ui_ptr)
322 for (p = 0; p < uid_hash_table_size; p++) {
323 struct user_info *ui = uid_hash_table + double_hash(uid, p);
331 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
344 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
347 static int update_user_row(struct osl_table *t, uint64_t dir_num,
351 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
353 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
355 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
357 if (ret < 0) { /* this is the first file we add */
358 struct osl_object objects[NUM_UT_COLUMNS];
359 uint64_t num_files = 1;
361 objects[UT_DIR_NUM].data = &dir_num;
362 objects[UT_DIR_NUM].size = sizeof(dir_num);
363 objects[UT_BYTES].data = add;
364 objects[UT_BYTES].size = sizeof(*add);
365 objects[UT_FILES].data = &num_files;
366 objects[UT_FILES].size = sizeof(num_files);
367 INFO_LOG("######################### ret: %d\n", ret);
368 ret = osl_add_row(t, objects);
369 INFO_LOG("######################### ret: %d\n", ret);
371 } else { /* add size and increment file count */
373 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
375 ret = osl_get_object(t, row, UT_BYTES, &obj1);
378 num = *(uint64_t *)obj1.data + *add;
379 ret = osl_update_object(t, row, UT_BYTES, &obj2);
382 ret = osl_get_object(t, row, UT_FILES, &obj1);
385 num = *(uint64_t *)obj1.data + 1;
386 return osl_update_object(t, row, UT_FILES, &obj2);
390 static uint64_t num_dirs;
391 static uint64_t num_files;
392 static uint64_t num_bytes;
394 int scan_dir(char *dirname)
397 struct dirent *entry;
398 int ret, cwd_fd, ret2;
399 uint64_t dir_size = 0, dir_files = 0;
400 uint64_t this_dir_num = num_dirs++;
402 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
403 ret = para_opendir(dirname, &dir, &cwd_fd);
405 if (ret != -ERRNO_TO_ERROR(EACCES))
407 WARNING_LOG("permission denied for %s\n", dirname);
410 while ((entry = readdir(dir))) {
416 struct user_info *ui;
418 if (!strcmp(entry->d_name, "."))
420 if (!strcmp(entry->d_name, ".."))
422 if (lstat(entry->d_name, &s) == -1) {
423 WARNING_LOG("lstat error for %s/%s\n", dirname,
428 if (!S_ISREG(m) && !S_ISDIR(m))
431 tmp = make_message("%s/%s", dirname, entry->d_name);
445 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
450 ret = update_user_row(ui->table, this_dir_num, &size);
454 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
457 ret2 = para_fchdir(cwd_fd);
458 if (ret2 < 0 && ret >= 0)
464 static int get_dir_name(struct osl_row *row, char **name)
466 struct osl_object obj;
467 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
475 enum global_stats_flags {
476 GSF_PRINT_DIRNAME = 1,
479 GSF_COMPUTE_SUMMARY = 8,
482 struct global_stats_info {
484 enum global_stats_flags flags;
487 static int global_stats_loop_function(struct osl_row *row, void *data)
489 struct global_stats_info *gsi = data;
490 struct osl_object obj;
492 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
494 if (!gsi->count && !summary)
495 return -E_LOOP_COMPLETE;
496 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
497 ret = get_dir_name(row, &dirname);
500 printf("%s%s", dirname,
501 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
505 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
507 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
510 files = *(uint64_t *)obj.data;
511 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
512 printf("%llu%s", (long long unsigned)files,
513 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
517 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
519 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
522 bytes = *(uint64_t *)obj.data;
523 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
524 printf("%llu\n", (long long unsigned)bytes);
535 static void print_id_stats(void)
537 struct user_info *ui;
539 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
543 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
544 (long long unsigned)ui->dirs,
545 (long long unsigned)ui->files,
546 (long long unsigned)ui->bytes);
550 enum user_stats_flags {
551 USF_PRINT_DIRNAME = 1,
554 USF_COMPUTE_SUMMARY = 8,
557 struct user_stats_info {
559 enum user_stats_flags flags;
560 struct user_info *ui;
563 static int user_stats_loop_function(struct osl_row *row, void *data)
565 struct user_stats_info *usi = data;
566 struct osl_row *dir_row;
567 struct osl_object obj;
568 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
570 if (!usi->count && !summary)
571 return -E_LOOP_COMPLETE;
572 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
574 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
577 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
580 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
586 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
590 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
592 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
595 files = *(uint64_t *)obj.data;
596 if (usi->count && (usi->flags & USF_PRINT_FILES))
598 (long long unsigned)files,
599 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
602 usi->ui->files += files;
604 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
606 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
609 bytes = *(uint64_t *)obj.data;
610 if (usi->count && (usi->flags & USF_PRINT_BYTES))
611 printf("%llu\n", (long long unsigned)bytes);
613 usi->ui->bytes += bytes;
623 static void print_user_stats(void)
625 struct user_info *ui;
628 struct user_stats_info usi = {
634 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
635 printf("************************************************ uid %u\n",
639 printf("----------------- Largest dirs -------------------\n");
640 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
641 user_stats_loop_function);
642 printf("---------- dirs containing most files ------------\n");
644 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
645 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
646 user_stats_loop_function);
650 static int print_statistics(void)
653 struct global_stats_info gsi = {
655 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
658 printf("----------------- Largest dirs -------------------\n");
659 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
660 global_stats_loop_function);
661 if (ret < 0 && ret != -E_LOOP_COMPLETE)
665 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
666 printf("---------- dirs containing most files ------------\n");
667 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
668 global_stats_loop_function);
669 if (ret < 0 && ret != -E_LOOP_COMPLETE)
672 printf("------------------ Global summary (dirs/files/bytes)\n"
673 "%llu\t%llu\t%llu\n",
674 (long long unsigned)num_dirs, (long long unsigned)num_files,
675 (long long unsigned)num_bytes);
681 static int write_uid_list(void)
685 struct user_info *ui;
686 size_t size = num_uids * sizeof(uint32_t);
691 buf = para_malloc(size);
695 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
697 ret = para_write_file(UID_LIST, buf, size);
702 static int open_dir_table(void)
704 return osl_open_table(&dir_table_desc, &dir_table);
707 static void close_dir_table(void)
713 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
715 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
719 static void close_user_table(struct user_info *ui)
723 if (!ui || !ui->table)
725 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
727 ERROR_LOG("failed to close user table %u: %s\n",
728 (unsigned) ui->uid, error_txt(-ret));
729 free((char *)ui->desc->name);
730 ui->desc->name = NULL;
731 free((char *)ui->desc->dir);
732 ui->desc->dir = NULL;
738 static void close_user_tables(void)
740 struct user_info *ui;
743 close_user_table(ui);
746 static void close_all_tables(void)
753 static int com_create(char *dirname)
755 int ret = create_tables();
758 ret = open_dir_table();
761 ret = scan_dir(dirname);
764 ret = write_uid_list();
770 static int read_uid_file(void)
774 int ret = mmap_full_file(UID_LIST, O_RDONLY, (void **)&map, &size, NULL);
780 /* hash table size should be a power of two and larger than the number of uids */
781 uid_hash_table_size = 4;
782 while (uid_hash_table_size < num_uids)
783 uid_hash_table_size *= 2;
785 for (n = 0; n < num_uids; n++) {
786 uint32_t uid = read_u32(map + n * sizeof(uid));
787 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
792 para_munmap(map, size);
796 static int com_select(void)
800 ret = open_dir_table();
803 ret = read_uid_file();
811 int main(int argc, char **argv)
814 struct cmdline_parser_params params = {
818 .check_ambiguity = 0,
822 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
828 ret = com_create(argv[1]);
833 ERROR_LOG("%s\n", error_txt(-ret));
834 return -EXIT_FAILURE;