2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
13 #define DATABASE_DIR "/tmp/adu"
14 #define UID_LIST DATABASE_DIR "/" "uid_list"
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,...)
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,
179 /** The columns of the id table. */
180 enum user_table_columns {
181 /** The numer of the directory. */
183 /** The number of bytes of all regular files in this dir owned by this id. */
185 /** The number of files in this dir owned by this id. */
187 /** Number of columns in this table. */
191 static struct osl_column_description user_table_cols[] = {
193 .storage_type = OSL_MAPPED_STORAGE,
194 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
196 .compare_function = uint64_compare,
197 .data_size = sizeof(uint64_t)
200 .storage_type = OSL_MAPPED_STORAGE,
201 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
202 .compare_function = size_compare,
204 .data_size = sizeof(uint64_t)
207 .storage_type = OSL_MAPPED_STORAGE,
208 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
209 .compare_function = size_compare,
211 .data_size = sizeof(uint64_t)
215 static struct osl_table *dir_table;
217 static int add_directory(char *dirname, uint64_t dir_num, uint64_t *dir_size,
220 struct osl_object dir_objects[NUM_DT_COLUMNS];
222 INFO_LOG("adding #%llu: %s\n", (long long unsigned)dir_num, dirname);
223 dir_objects[DT_NAME].data = dirname;
224 dir_objects[DT_NAME].size = strlen(dirname) + 1;
225 dir_objects[DT_NUM].data = &dir_num;
226 dir_objects[DT_NUM].size = sizeof(dir_num);
227 dir_objects[DT_BYTES].data = dir_size;
228 dir_objects[DT_BYTES].size = sizeof(*dir_size);
229 dir_objects[DT_FILES].data = dir_files;
230 dir_objects[DT_FILES].size = sizeof(*dir_files);
232 return osl_add_row(dir_table, dir_objects);
235 static uint32_t num_uids;
237 static int open_user_table(struct user_info *ui, int create)
241 ui->desc = para_malloc(sizeof(*ui->desc));
242 ui->desc->num_columns = NUM_UT_COLUMNS;
244 ui->desc->column_descriptions = user_table_cols;
245 ui->desc->dir = para_strdup(DATABASE_DIR);
246 ui->desc->name = make_message("%u", (unsigned)ui->uid);
248 INFO_LOG(".............................uid #%u: %u\n",
249 (unsigned)num_uids, (unsigned)ui->uid);
251 ret = osl_create_table(ui->desc);
255 ret = osl_open_table(ui->desc, &ui->table);
260 free((char *)ui->desc->name);
261 free((char *)ui->desc->dir);
263 ui->desc->name = NULL;
264 ui->desc->dir = NULL;
270 #define uid_hash_bits 8
271 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
272 #define PRIME1 0x811c9dc5
273 #define PRIME2 0x01000193
275 static void create_hash_table(void)
277 uid_hash_table = para_calloc(uid_hash_table_size
278 * sizeof(struct user_info));
281 static void free_hash_table(void)
283 free(uid_hash_table);
284 uid_hash_table = NULL;
287 static int create_tables(void)
291 ret = osl_create_table(&dir_table_desc);
299 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
301 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
302 % uid_hash_table_size;
305 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
306 + uid_hash_table_size; ui++)
308 enum search_uid_flags {
310 CREATE_USER_TABLE = 2,
313 static int search_uid(uint32_t uid, enum search_uid_flags flags,
314 struct user_info **ui_ptr)
318 for (p = 0; p < uid_hash_table_size; p++) {
319 struct user_info *ui = uid_hash_table + double_hash(uid, p);
327 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
340 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
343 static int update_user_row(struct osl_table *t, uint64_t dir_num,
347 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
349 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
351 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
353 if (ret < 0) { /* this is the first file we add */
354 struct osl_object objects[NUM_UT_COLUMNS];
355 uint64_t num_files = 1;
357 objects[UT_DIR_NUM].data = &dir_num;
358 objects[UT_DIR_NUM].size = sizeof(dir_num);
359 objects[UT_BYTES].data = add;
360 objects[UT_BYTES].size = sizeof(*add);
361 objects[UT_FILES].data = &num_files;
362 objects[UT_FILES].size = sizeof(num_files);
363 INFO_LOG("######################### ret: %d\n", ret);
364 ret = osl_add_row(t, objects);
365 INFO_LOG("######################### ret: %d\n", ret);
367 } else { /* add size and increment file count */
369 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
371 ret = osl_get_object(t, row, UT_BYTES, &obj1);
374 num = *(uint64_t *)obj1.data + *add;
375 ret = osl_update_object(t, row, UT_BYTES, &obj2);
378 ret = osl_get_object(t, row, UT_FILES, &obj1);
381 num = *(uint64_t *)obj1.data + 1;
382 return osl_update_object(t, row, UT_FILES, &obj2);
386 static uint64_t num_dirs;
387 static uint64_t num_files;
388 static uint64_t num_bytes;
390 int scan_dir(char *dirname)
393 struct dirent *entry;
394 int ret, cwd_fd, ret2;
395 uint64_t dir_size = 0, dir_files = 0;
396 uint64_t this_dir_num = num_dirs++;
398 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
399 ret = para_opendir(dirname, &dir, &cwd_fd);
401 if (ret != -ERRNO_TO_ERROR(EACCES))
403 WARNING_LOG("permission denied for %s\n", dirname);
406 while ((entry = readdir(dir))) {
412 struct user_info *ui;
414 if (!strcmp(entry->d_name, "."))
416 if (!strcmp(entry->d_name, ".."))
418 if (lstat(entry->d_name, &s) == -1) {
419 WARNING_LOG("lstat error for %s/%s\n", dirname,
424 if (!S_ISREG(m) && !S_ISDIR(m))
427 tmp = make_message("%s/%s", dirname, entry->d_name);
441 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
446 ret = update_user_row(ui->table, this_dir_num, &size);
450 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
453 ret2 = para_fchdir(cwd_fd);
454 if (ret2 < 0 && ret >= 0)
460 static int get_dir_name(struct osl_row *row, char **name)
462 struct osl_object obj;
463 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
471 enum global_stats_flags {
472 GSF_PRINT_DIRNAME = 1,
475 GSF_COMPUTE_SUMMARY = 8,
478 struct global_stats_info {
480 enum global_stats_flags flags;
483 static int global_stats_loop_function(struct osl_row *row, void *data)
485 struct global_stats_info *gsi = data;
486 struct osl_object obj;
488 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
490 if (!gsi->count && !summary)
491 return -E_LOOP_COMPLETE;
492 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
493 ret = get_dir_name(row, &dirname);
496 printf("%s%s", dirname,
497 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
501 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
503 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
506 files = *(uint64_t *)obj.data;
507 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
508 printf("%llu%s", (long long unsigned)files,
509 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
513 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
515 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
518 bytes = *(uint64_t *)obj.data;
519 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
520 printf("%llu\n", (long long unsigned)bytes);
531 static void print_id_stats(void)
533 struct user_info *ui;
535 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
539 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
540 (long long unsigned)ui->dirs,
541 (long long unsigned)ui->files,
542 (long long unsigned)ui->bytes);
546 enum user_stats_flags {
547 USF_PRINT_DIRNAME = 1,
550 USF_COMPUTE_SUMMARY = 8,
553 struct user_stats_info {
555 enum user_stats_flags flags;
556 struct user_info *ui;
559 static int user_stats_loop_function(struct osl_row *row, void *data)
561 struct user_stats_info *usi = data;
562 struct osl_row *dir_row;
563 struct osl_object obj;
564 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
566 if (!usi->count && !summary)
567 return -E_LOOP_COMPLETE;
568 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
570 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
573 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
576 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
582 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
586 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
588 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
591 files = *(uint64_t *)obj.data;
592 if (usi->count && (usi->flags & USF_PRINT_FILES))
594 (long long unsigned)files,
595 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
598 usi->ui->files += files;
600 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
602 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
605 bytes = *(uint64_t *)obj.data;
606 if (usi->count && (usi->flags & USF_PRINT_BYTES))
607 printf("%llu\n", (long long unsigned)bytes);
609 usi->ui->bytes += bytes;
619 static void print_user_stats(void)
621 struct user_info *ui;
624 struct user_stats_info usi = {
630 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
631 printf("************************************************ uid %u\n",
635 printf("----------------- Largest dirs -------------------\n");
636 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
637 user_stats_loop_function);
638 printf("---------- dirs containing most files ------------\n");
640 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
641 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
642 user_stats_loop_function);
646 static int print_statistics(void)
649 struct global_stats_info gsi = {
651 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
654 printf("----------------- Largest dirs -------------------\n");
655 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
656 global_stats_loop_function);
657 if (ret < 0 && ret != -E_LOOP_COMPLETE)
661 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
662 printf("---------- dirs containing most files ------------\n");
663 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
664 global_stats_loop_function);
665 if (ret < 0 && ret != -E_LOOP_COMPLETE)
668 printf("------------------ Global summary (dirs/files/bytes)\n"
669 "%llu\t%llu\t%llu\n",
670 (long long unsigned)num_dirs, (long long unsigned)num_files,
671 (long long unsigned)num_bytes);
677 static int write_uid_list(void)
681 struct user_info *ui;
682 size_t size = num_uids * sizeof(uint32_t);
687 buf = para_malloc(size);
691 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
693 ret = para_write_file(UID_LIST, buf, size);
698 static int open_dir_table(void)
700 return osl_open_table(&dir_table_desc, &dir_table);
703 static void close_dir_table(void)
709 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
711 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
715 static void close_user_table(struct user_info *ui)
719 if (!ui || !ui->table)
721 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
723 ERROR_LOG("failed to close user table %u: %s\n",
724 (unsigned) ui->uid, error_txt(-ret));
725 free((char *)ui->desc->name);
726 ui->desc->name = NULL;
727 free((char *)ui->desc->dir);
728 ui->desc->dir = NULL;
734 static void close_user_tables(void)
736 struct user_info *ui;
739 close_user_table(ui);
742 static void close_all_tables(void)
749 static int com_create(char *dirname)
751 int ret = create_tables();
754 ret = open_dir_table();
757 ret = scan_dir(dirname);
760 ret = write_uid_list();
766 static int read_uid_file(void)
770 int ret = mmap_full_file(UID_LIST, O_RDONLY, (void **)&map, &size, NULL);
776 /* hash table size should be a power of two and larger than the number of uids */
777 uid_hash_table_size = 4;
778 while (uid_hash_table_size < num_uids)
779 uid_hash_table_size *= 2;
781 for (n = 0; n < num_uids; n++) {
782 uint32_t uid = read_u32(map + n * sizeof(uid));
783 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
788 para_munmap(map, size);
792 static int com_select(void)
796 ret = open_dir_table();
799 ret = read_uid_file();
807 int main(int argc, char **argv)
815 ret = com_create(argv[1]);
820 ERROR_LOG("%s\n", error_txt(-ret));
821 return -EXIT_FAILURE;