2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
13 #define DATABASE_DIR "/tmp/adu"
15 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
16 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
23 * \param fml Usual format string.
25 * All XXX_LOG() macros use this function.
27 __printf_2_3 void __log(int ll, const char* fmt,...)
40 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
41 fprintf(outfd, "%s ", str);
43 vfprintf(outfd, fmt, argp);
48 * Compare the size of two directories
50 * \param obj1 Pointer to the first object.
51 * \param obj2 Pointer to the second object.
53 * This function first compares the size values as usual integers. If they compare as
54 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
55 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
57 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
59 uint64_t d1 = *(uint64_t *)obj1->data;
60 uint64_t d2 = *(uint64_t *)obj2->data;
61 int ret = NUM_COMPARE(d2, d1);
65 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
66 return NUM_COMPARE(obj2->data, obj1->data);
70 * Compare two osl objects of string type.
72 * \param obj1 Pointer to the first object.
73 * \param obj2 Pointer to the second object.
75 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
76 * are taken into account.
78 * \return It returns an integer less than, equal to, or greater than zero if
79 * \a obj1 is found, respectively, to be less than, to match, or be greater than
82 * \sa strcmp(3), strncmp(3), osl_compare_func.
84 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
86 const char *str1 = (const char *)obj1->data;
87 const char *str2 = (const char *)obj2->data;
88 return strncmp(str1, str2, MIN(obj1->size, obj2->size));
91 /** The columns of the directory table. */
92 enum dir_table_columns {
93 /** The name of the directory. */
95 /** The dir count number. */
97 /** The number of bytes of all regular files. */
99 /** The number of all regular files. */
101 /** Number of columns in this table. */
105 static struct osl_column_description dir_table_cols[] = {
107 .storage_type = OSL_MAPPED_STORAGE,
108 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
110 .compare_function = string_compare,
113 .storage_type = OSL_MAPPED_STORAGE,
114 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
116 .compare_function = uint32_compare,
117 .data_size = sizeof(uint32_t)
120 .storage_type = OSL_MAPPED_STORAGE,
121 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
122 .compare_function = size_compare,
124 .data_size = sizeof(uint64_t)
127 .storage_type = OSL_MAPPED_STORAGE,
128 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
129 .compare_function = size_compare,
131 .data_size = sizeof(uint64_t)
135 static struct osl_table_description dir_table_desc = {
137 .num_columns = NUM_DT_COLUMNS,
139 .column_descriptions = dir_table_cols,
143 /** The columns of the id table. */
144 enum user_table_columns {
145 /** The numer of the directory. */
147 /** The number of bytes of all regular files in this dir owned by this id. */
149 /** The number of files in this dir owned by this id. */
151 /** Number of columns in this table. */
155 static struct osl_column_description user_table_cols[] = {
157 .storage_type = OSL_MAPPED_STORAGE,
158 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
160 .compare_function = uint32_compare,
161 .data_size = sizeof(uint32_t)
164 .storage_type = OSL_MAPPED_STORAGE,
165 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
166 .compare_function = size_compare,
168 .data_size = sizeof(uint64_t)
171 .storage_type = OSL_MAPPED_STORAGE,
172 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
173 .compare_function = size_compare,
175 .data_size = sizeof(uint64_t)
179 static struct osl_table *dir_table;
181 int add_directory(char *dirname, uint32_t dir_num, uint64_t *dir_size,
184 struct osl_object dir_objects[NUM_DT_COLUMNS];
186 INFO_LOG("adding #%u: %s\n", dir_num, dirname);
187 dir_objects[DT_NAME].data = dirname;
188 dir_objects[DT_NAME].size = strlen(dirname) + 1;
189 dir_objects[DT_NUM].data = &dir_num;
190 dir_objects[DT_NUM].size = sizeof(dir_num);
191 dir_objects[DT_BYTES].data = dir_size;
192 dir_objects[DT_BYTES].size = sizeof(*dir_size);
193 dir_objects[DT_FILES].data = dir_files;
194 dir_objects[DT_FILES].size = sizeof(*dir_files);
196 return osl_add_row(dir_table, dir_objects);
199 static uint32_t num_uids;
201 int create_and_open_user_table(uint32_t uid, struct osl_table **t)
204 struct osl_table_description *desc = para_malloc(sizeof(*desc));
206 desc->num_columns = NUM_UT_COLUMNS;
208 desc->column_descriptions = user_table_cols;
209 desc->dir = para_strdup(DATABASE_DIR);
210 desc->name = make_message("%u", uid);
212 INFO_LOG(".............................uid #%u: %u\n",
213 (unsigned)num_uids, (unsigned)uid);
214 // user_table_desc.name = make_message("%u", uid);
215 ret = osl_create_table(desc);
218 return osl_open_table(desc, t);
222 #define uid_hash_bits 8
223 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
224 #define PRIME1 0x811c9dc5
225 #define PRIME2 0x01000193
229 struct osl_table *table;
234 static struct user_info *uid_hash_table;
236 static void create_hash_table(void)
238 uid_hash_table = para_calloc(uid_hash_table_size
239 * sizeof(struct user_info));
242 static int create_tables(void)
245 return osl_create_table(&dir_table_desc);
249 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
251 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num) % uid_hash_table_size;
254 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < uid_hash_table \
255 + uid_hash_table_size; ui++)
257 static int search_uid(uint32_t uid, int insert, struct user_info **ui)
261 for (p = 0; p < uid_hash_table_size; p++) {
262 struct user_info *i = uid_hash_table + double_hash(uid, p);
266 int ret = create_and_open_user_table(uid, &i->table);
278 return insert? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
281 static int update_user_row(struct osl_table *t, uint32_t dir_num,
285 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
287 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
289 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
291 if (ret < 0) { /* this is the first file we add */
292 struct osl_object objects[NUM_UT_COLUMNS];
293 uint64_t num_files = 1;
295 objects[UT_DIR_NUM].data = &dir_num;
296 objects[UT_DIR_NUM].size = sizeof(dir_num);
297 objects[UT_BYTES].data = add;
298 objects[UT_BYTES].size = sizeof(*add);
299 objects[UT_FILES].data = &num_files;
300 objects[UT_FILES].size = sizeof(num_files);
301 INFO_LOG("######################### ret: %d\n", ret);
302 ret = osl_add_row(t, objects);
303 INFO_LOG("######################### ret: %d\n", ret);
305 } else { /* add size and increment file count */
307 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
309 ret = osl_get_object(t, row, UT_BYTES, &obj1);
312 num = *(uint64_t *)obj1.data + *add;
313 ret = osl_update_object(t, row, UT_BYTES, &obj2);
316 ret = osl_get_object(t, row, UT_FILES, &obj1);
319 num = *(uint64_t *)obj1.data + 1;
320 return osl_update_object(t, row, UT_FILES, &obj2);
324 static uint32_t num_dirs;
325 static uint32_t num_files;
326 static uint64_t num_bytes;
328 int scan_dir(char *dirname)
331 struct dirent *entry;
332 int ret, cwd_fd, ret2;
333 uint64_t dir_size = 0, dir_files = 0;
334 uint32_t this_dir_num = num_dirs++;
336 DEBUG_LOG("----------------- %u: %s\n", num_dirs, dirname);
337 ret = para_opendir(dirname, &dir, &cwd_fd);
339 if (ret != -ERRNO_TO_ERROR(EACCES))
341 WARNING_LOG("permission denied for %s\n", dirname);
344 while ((entry = readdir(dir))) {
350 struct user_info *ui;
352 if (!strcmp(entry->d_name, "."))
354 if (!strcmp(entry->d_name, ".."))
356 if (lstat(entry->d_name, &s) == -1) {
357 WARNING_LOG("lstat error for %s/%s\n", dirname,
362 if (!S_ISREG(m) && !S_ISDIR(m))
365 tmp = make_message("%s/%s", dirname, entry->d_name);
379 ret = search_uid(uid, 1, &ui);
384 ret = update_user_row(ui->table, this_dir_num, &size);
388 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
391 ret2 = para_fchdir(cwd_fd);
392 if (ret2 < 0 && ret >= 0)
398 static int get_dir_name(struct osl_row *row, char **name)
400 struct osl_object obj;
401 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
409 static int print_dirname_and_size(struct osl_row *row, void *data)
411 unsigned *count = data;
412 struct osl_object obj;
416 if ((*count)++ > 100)
417 return -E_LOOP_COMPLETE;
418 ret = get_dir_name(row, &name);
421 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
424 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
428 static int print_dirname_and_file_count(struct osl_row *row, void *data)
430 unsigned *count = data;
431 struct osl_object obj;
435 if ((*count)++ > 100)
436 return -E_LOOP_COMPLETE;
437 ret = get_dir_name(row, &name);
440 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
443 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
447 static void print_id_stats(void)
449 struct user_info *ui;
454 printf("%u\t%llu\t%llu\n", (unsigned)ui->uid, (long long unsigned)ui->files,
455 (long long unsigned)ui->bytes);
459 struct big_dir_info {
461 struct osl_table *user_table;
464 static int print_big_dir(struct osl_row *row, void *data)
466 struct big_dir_info *bdi = data;
468 struct osl_row *dir_row;
471 struct osl_object obj;
473 if (bdi->count++ > 10)
474 return -E_LOOP_COMPLETE;
475 ret = osl_get_object(bdi->user_table, row, UT_BYTES, &obj);
478 bytes = *(uint64_t *)obj.data;
479 ret = osl_get_object(bdi->user_table, row, UT_DIR_NUM, &obj);
482 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
485 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
489 printf("%s: %llu\n", dirname, (long long unsigned)bytes);
493 static void print_id_dir_stats(void)
495 struct user_info *ui;
498 struct big_dir_info bdi = {.count = 0};
501 bdi.user_table = ui->table;
502 printf("************************* Big dirs owned by uid %u\n", (unsigned) ui->uid);
503 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &bdi, print_big_dir);
507 static int print_statistics(void)
512 printf("Summary: %u dirs, %u files, %llu bytes\n", (unsigned)num_dirs,
513 (unsigned)num_files, (long long unsigned)num_bytes);
514 printf("************************* Biggest dirs\n");
515 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &count, print_dirname_and_size);
516 if (ret < 0 && ret != -E_LOOP_COMPLETE)
519 printf("************************* dirs containing many files\n");
520 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &count, print_dirname_and_file_count);
521 if (ret < 0 && ret != -E_LOOP_COMPLETE)
524 printf("************************* dirs stats by owner\n");
526 print_id_dir_stats();
530 static int write_uid_list(void)
532 char *buf, *filename = DATABASE_DIR "/" "uid_list";
534 struct user_info *ui;
535 size_t size = num_uids * sizeof(uint32_t);
540 buf = para_malloc(size);
544 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
546 ret = para_write_file(filename, buf, size);
551 static int com_create(char *dirname)
553 int ret = scan_dir(dirname);
557 return write_uid_list();
560 int main(int argc, char **argv)
562 int ret = create_tables();
565 ret = osl_open_table(&dir_table_desc, &dir_table);
571 ret = com_create(argv[1]);
577 ERROR_LOG("%s\n", error_txt(-ret));
578 return -EXIT_FAILURE;