2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
13 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
14 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
21 * \param fml Usual format string.
23 * All XXX_LOG() macros use this function.
25 __printf_2_3 void __log(int ll, const char* fmt,...)
38 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
39 fprintf(outfd, "%s ", str);
41 vfprintf(outfd, fmt, argp);
46 * Compare the size of two directories
48 * \param obj1 Pointer to the first object.
49 * \param obj2 Pointer to the second object.
51 * This function first compares the size values as usual integers. If they compare as
52 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
53 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
55 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
57 uint64_t d1 = *(uint64_t *)obj1->data;
58 uint64_t d2 = *(uint64_t *)obj2->data;
59 int ret = NUM_COMPARE(d2, d1);
63 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
64 return NUM_COMPARE(obj2->data, obj1->data);
68 * Compare two osl objects of string type.
70 * \param obj1 Pointer to the first object.
71 * \param obj2 Pointer to the second object.
73 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
74 * are taken into account.
76 * \return It returns an integer less than, equal to, or greater than zero if
77 * \a obj1 is found, respectively, to be less than, to match, or be greater than
80 * \sa strcmp(3), strncmp(3), osl_compare_func.
82 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
84 const char *str1 = (const char *)obj1->data;
85 const char *str2 = (const char *)obj2->data;
86 return strncmp(str1, str2, MIN(obj1->size, obj2->size));
89 /** The columns of the directory table. */
90 enum dir_table_columns {
91 /** The name of the directory. */
93 /** The dir count number. */
95 /** The number of bytes of all regular files. */
97 /** The number of all regular files. */
99 /** Number of columns in this table. */
103 static struct osl_column_description dir_table_cols[] = {
105 .storage_type = OSL_MAPPED_STORAGE,
106 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
108 .compare_function = string_compare,
111 .storage_type = OSL_MAPPED_STORAGE,
112 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
114 .compare_function = uint32_compare,
115 .data_size = sizeof(uint32_t)
118 .storage_type = OSL_MAPPED_STORAGE,
119 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
120 .compare_function = size_compare,
122 .data_size = sizeof(uint64_t)
125 .storage_type = OSL_MAPPED_STORAGE,
126 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
127 .compare_function = size_compare,
129 .data_size = sizeof(uint64_t)
133 static struct osl_table_description dir_table_desc = {
135 .num_columns = NUM_DT_COLUMNS,
137 .column_descriptions = dir_table_cols,
141 /** The columns of the id table. */
142 enum id_table_columns {
145 /** The number of bytes of all regular files owned by this id. */
147 /** The number of regular files owned by this id. */
149 /** The user table for this uid. */
151 /** Number of columns in this table. */
155 static struct osl_column_description id_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,
174 .name = "num_filess",
175 .data_size = sizeof(uint64_t)
178 .storage_type = OSL_NO_STORAGE,
179 .storage_flags = OSL_FIXED_SIZE | OSL_UNIQUE,
180 .name = "user_table",
181 .data_size = sizeof(void *)
185 static struct osl_table_description id_table_desc = {
187 .num_columns = NUM_IDT_COLUMNS,
189 .column_descriptions = id_table_cols,
193 /** The columns of the id table. */
194 enum user_table_columns {
195 /** The numer of the directory. */
197 /** The number of bytes of all regular files in this dir owned by this id. */
199 /** The number of files in this dir owned by this id. */
201 /** Number of columns in this table. */
205 static struct osl_column_description user_table_cols[] = {
207 .storage_type = OSL_MAPPED_STORAGE,
208 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
210 .compare_function = uint32_compare,
211 .data_size = sizeof(uint32_t)
214 .storage_type = OSL_MAPPED_STORAGE,
215 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
216 .compare_function = size_compare,
218 .data_size = sizeof(uint64_t)
221 .storage_type = OSL_MAPPED_STORAGE,
222 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
223 .compare_function = size_compare,
225 .data_size = sizeof(uint64_t)
229 static struct osl_table_description user_table_desc = {
230 .num_columns = NUM_UT_COLUMNS,
232 .column_descriptions = user_table_cols,
235 static struct osl_table *dir_table;
236 static struct osl_table *id_table;
238 static int create_tables(void)
240 int ret = osl_create_table(&dir_table_desc);
243 ret = osl_create_table(&id_table_desc);
249 int add_directory(char *dirname, uint32_t dir_num, uint64_t *dir_size,
252 struct osl_object dir_objects[NUM_DT_COLUMNS];
254 INFO_LOG("adding #%u: %s\n", dir_num, dirname);
255 dir_objects[DT_NAME].data = dirname;
256 dir_objects[DT_NAME].size = strlen(dirname) + 1;
257 dir_objects[DT_NUM].data = &dir_num;
258 dir_objects[DT_NUM].size = sizeof(dir_num);
259 dir_objects[DT_BYTES].data = dir_size;
260 dir_objects[DT_BYTES].size = sizeof(*dir_size);
261 dir_objects[DT_FILES].data = dir_files;
262 dir_objects[DT_FILES].size = sizeof(*dir_files);
264 return osl_add_row(dir_table, dir_objects);
267 int create_and_open_user_table(uint32_t uid, struct osl_table **t)
270 struct osl_table_description *desc = para_malloc(sizeof(*desc));
272 desc->num_columns = NUM_UT_COLUMNS;
274 desc->column_descriptions = user_table_cols;
275 desc->dir = para_strdup("/tmp/adu");
276 desc->name = make_message("%u", uid);
277 INFO_LOG("................................. %u\n", uid);
278 // user_table_desc.name = make_message("%u", uid);
279 ret = osl_create_table(desc);
282 return osl_open_table(desc, t);
285 static int insert_id_row(uint32_t uid, struct osl_table *t, struct osl_row **row)
287 struct osl_object objects[NUM_IDT_COLUMNS];
290 struct osl_table **table_ptr = para_malloc(sizeof(*table_ptr));
293 INFO_LOG("§§§§§§§§§§§§§§§§§§§§§ uid: %d, t: %p\n", uid, t);
294 objects[IDT_UID].data = &uid;
295 objects[IDT_UID].size = sizeof(uid);
296 objects[IDT_BYTES].data = #
297 objects[IDT_BYTES].size = sizeof(num);
298 objects[IDT_FILES].data = #
299 objects[IDT_FILES].size = sizeof(num);
300 objects[IDT_TABLE].data = table_ptr;
301 objects[IDT_TABLE].size = sizeof(*table_ptr);
302 return osl_add_and_get_row(id_table, objects, row);
305 static int get_user_table(struct osl_row *row, struct osl_table **t)
307 struct osl_object obj;
309 int ret = osl_get_object(id_table, row, IDT_TABLE, &obj);
312 *t = *(struct osl_table **)obj.data;
313 INFO_LOG("^^^^^^^^^^^^^^^^^^ t: %p\n", *t);
317 static int add_id_bytes(struct osl_row *row, uint64_t *add)
320 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
322 /* update number of bytes */
323 int ret = osl_get_object(id_table, row, IDT_BYTES, &obj1);
326 num = *(uint64_t *)obj1.data + *add;
327 ret = osl_update_object(id_table, row, IDT_BYTES, &obj2);
330 /* increment number of files */
331 ret = osl_get_object(id_table, row, IDT_FILES, &obj1);
334 num = *(uint64_t *)obj1.data + 1;
335 return osl_update_object(id_table, row, IDT_FILES, &obj2);
338 static int update_user_row(struct osl_table *t, uint32_t dir_num,
342 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
344 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
346 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
348 if (ret < 0) { /* this is the first file we add */
349 struct osl_object objects[NUM_UT_COLUMNS];
350 uint64_t num_files = 1;
352 objects[UT_DIR_NUM].data = &dir_num;
353 objects[UT_DIR_NUM].size = sizeof(dir_num);
354 objects[UT_BYTES].data = add;
355 objects[UT_BYTES].size = sizeof(*add);
356 objects[UT_FILES].data = &num_files;
357 objects[UT_FILES].size = sizeof(num_files);
358 INFO_LOG("######################### ret: %d\n", ret);
359 ret = osl_add_row(t, objects);
360 INFO_LOG("######################### ret: %d\n", ret);
362 } else { /* add size and increment file count */
364 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
366 ret = osl_get_object(t, row, UT_BYTES, &obj1);
369 num = *(uint64_t *)obj1.data + *add;
370 ret = osl_update_object(t, row, UT_BYTES, &obj2);
373 ret = osl_get_object(t, row, UT_FILES, &obj1);
376 num = *(uint64_t *)obj1.data + 1;
377 return osl_update_object(t, row, UT_FILES, &obj2);
381 static uint32_t dir_num;
383 int scan_dir(char *dirname)
386 struct dirent *entry;
387 int ret, cwd_fd, ret2;
388 uint64_t dir_size = 0, dir_files = 0;
389 struct osl_object obj;
391 INFO_LOG("----------------- %s\n", dirname);
392 ret = para_opendir(dirname, &dir, &cwd_fd);
394 if (ret != -ERRNO_TO_ERROR(EACCES))
396 WARNING_LOG("permission denied for %s\n", dirname);
399 while ((entry = readdir(dir))) {
405 struct osl_row *id_row;
406 struct osl_table *user_table;
408 if (!strcmp(entry->d_name, "."))
410 if (!strcmp(entry->d_name, ".."))
412 if (lstat(entry->d_name, &s) == -1)
415 if (!S_ISREG(m) && !S_ISDIR(m))
417 tmp = make_message("%s/%s", dirname, entry->d_name);
430 INFO_LOG("++++++++++++++++++++++++++ %s, uid: %u\n", entry->d_name, uid);
432 obj.size = sizeof(uid);
433 ret = osl_get_row(id_table, IDT_UID, &obj, &id_row);
434 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
437 ret = create_and_open_user_table(uid, &user_table);
440 ret = insert_id_row(uid, user_table, &id_row);
444 ret = get_user_table(id_row, &user_table);
448 ret = add_id_bytes(id_row, &size);
451 INFO_LOG("user_table: %p\n", user_table);
452 ret = update_user_row(user_table, dir_num, &size);
453 INFO_LOG("update_user ret: %d\n", ret);
457 ret = add_directory(dirname, dir_num++, &dir_size, &dir_files);
460 ret2 = para_fchdir(cwd_fd);
461 if (ret2 < 0 && ret >= 0)
467 static int get_dir_name(struct osl_row *row, char **name)
469 struct osl_object obj;
470 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
478 static int print_dirname_and_size(struct osl_row *row, void *data)
480 unsigned *count = data;
481 struct osl_object obj;
485 if ((*count)++ > 100)
486 return -E_LOOP_COMPLETE;
487 ret = get_dir_name(row, &name);
490 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
493 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
497 static int print_dirname_and_file_count(struct osl_row *row, void *data)
499 unsigned *count = data;
500 struct osl_object obj;
504 if ((*count)++ > 100)
505 return -E_LOOP_COMPLETE;
506 ret = get_dir_name(row, &name);
509 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
512 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
516 static int print_id_stats(struct osl_row *row, __a_unused void *data)
518 struct osl_object obj;
520 uint64_t bytes, files;
521 int ret = osl_get_object(id_table, row, IDT_UID, &obj);
525 uid = *(uint32_t *)obj.data;
526 ret = osl_get_object(id_table, row, IDT_BYTES, &obj);
529 bytes = *(uint64_t *)obj.data;
530 ret = osl_get_object(id_table, row, IDT_FILES, &obj);
533 files = *(uint64_t *)obj.data;
535 printf("%u\t%llu\t%llu\n", (unsigned)uid, (long long unsigned)files,
536 (long long unsigned)bytes);
540 struct id_dir_stat_info {
542 struct osl_table *user_table;
545 static int print_big_dir(struct osl_row *row, void *data)
547 struct id_dir_stat_info *info = data;
550 struct osl_row *dir_row;
553 struct osl_object obj;
555 if (info->count > 10)
556 return -E_LOOP_COMPLETE;
557 ret = osl_get_object(info->user_table, row, UT_BYTES, &obj);
560 bytes = *(uint64_t *)obj.data;
561 ret = osl_get_object(info->user_table, row, UT_DIR_NUM, &obj);
564 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
567 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
571 printf("%s: %llu\n", dirname, (long long unsigned)bytes);
575 static int print_id_dir_stats(struct osl_row *row, __a_unused void *data)
577 struct osl_object obj;
579 int ret = osl_get_object(id_table, row, IDT_UID, &obj);
580 struct id_dir_stat_info info = {.count = 0};
584 uid = *(uint32_t *)obj.data;
586 ret = osl_get_object(id_table, row, IDT_TABLE, &obj);
589 info.user_table = *(struct osl_table **)obj.data;
591 printf("************************* Big dirs owned by uid %u\n", (unsigned) uid);
592 osl_rbtree_loop_reverse(info.user_table, IDT_BYTES, &info, print_big_dir);
596 static int print_statistics(void)
601 printf("************************* Biggest dirs\n");
602 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &count, print_dirname_and_size);
603 if (ret < 0 && ret != -E_LOOP_COMPLETE)
606 printf("************************* dirs containing many files\n");
607 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &count, print_dirname_and_file_count);
608 if (ret < 0 && ret != -E_LOOP_COMPLETE)
611 printf("************************* dirs stats by owner\n");
612 ret = osl_rbtree_loop(id_table, IDT_BYTES, NULL, print_id_stats);
616 return osl_rbtree_loop(id_table, IDT_BYTES, NULL, print_id_dir_stats);
620 int main(int argc, char **argv)
622 int ret = create_tables();
625 ret = osl_open_table(&dir_table_desc, &dir_table);
628 ret = osl_open_table(&id_table_desc, &id_table);
634 ret = scan_dir(argv[1]);
640 ERROR_LOG("%s\n", error_txt(-ret));
641 return -EXIT_FAILURE;