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 user_table_columns {
143 /** The numer of the directory. */
145 /** The number of bytes of all regular files in this dir owned by this id. */
147 /** The number of files in this dir owned by this id. */
149 /** Number of columns in this table. */
153 static struct osl_column_description user_table_cols[] = {
155 .storage_type = OSL_MAPPED_STORAGE,
156 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
158 .compare_function = uint32_compare,
159 .data_size = sizeof(uint32_t)
162 .storage_type = OSL_MAPPED_STORAGE,
163 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
164 .compare_function = size_compare,
166 .data_size = sizeof(uint64_t)
169 .storage_type = OSL_MAPPED_STORAGE,
170 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
171 .compare_function = size_compare,
173 .data_size = sizeof(uint64_t)
177 static struct osl_table *dir_table;
179 int add_directory(char *dirname, uint32_t dir_num, uint64_t *dir_size,
182 struct osl_object dir_objects[NUM_DT_COLUMNS];
184 INFO_LOG("adding #%u: %s\n", dir_num, dirname);
185 dir_objects[DT_NAME].data = dirname;
186 dir_objects[DT_NAME].size = strlen(dirname) + 1;
187 dir_objects[DT_NUM].data = &dir_num;
188 dir_objects[DT_NUM].size = sizeof(dir_num);
189 dir_objects[DT_BYTES].data = dir_size;
190 dir_objects[DT_BYTES].size = sizeof(*dir_size);
191 dir_objects[DT_FILES].data = dir_files;
192 dir_objects[DT_FILES].size = sizeof(*dir_files);
194 return osl_add_row(dir_table, dir_objects);
197 int create_and_open_user_table(uint32_t uid, struct osl_table **t)
200 struct osl_table_description *desc = para_malloc(sizeof(*desc));
202 desc->num_columns = NUM_UT_COLUMNS;
204 desc->column_descriptions = user_table_cols;
205 desc->dir = para_strdup("/tmp/adu");
206 desc->name = make_message("%u", uid);
207 INFO_LOG("................................. %u\n", uid);
208 // user_table_desc.name = make_message("%u", uid);
209 ret = osl_create_table(desc);
212 return osl_open_table(desc, t);
216 #define uid_hash_bits 8
217 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
218 #define PRIME1 0x811c9dc5
219 #define PRIME2 0x01000193
223 struct osl_table *table;
228 static struct user_info *uid_hash_table;
230 static void create_hash_table(void)
232 uid_hash_table = para_calloc(uid_hash_table_size
233 * sizeof(struct user_info));
236 static int create_tables(void)
239 return osl_create_table(&dir_table_desc);
243 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
245 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num) % uid_hash_table_size;
248 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < uid_hash_table \
249 + uid_hash_table_size; ui++)
251 static int search_uid(uint32_t uid, int insert, struct user_info **ui)
255 for (p = 0; p < uid_hash_table_size; p++) {
256 struct user_info *i = uid_hash_table + double_hash(uid, p);
260 int ret = create_and_open_user_table(uid, &i->table);
272 return insert? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
275 static int update_user_row(struct osl_table *t, uint32_t dir_num,
279 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
281 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
283 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
285 if (ret < 0) { /* this is the first file we add */
286 struct osl_object objects[NUM_UT_COLUMNS];
287 uint64_t num_files = 1;
289 objects[UT_DIR_NUM].data = &dir_num;
290 objects[UT_DIR_NUM].size = sizeof(dir_num);
291 objects[UT_BYTES].data = add;
292 objects[UT_BYTES].size = sizeof(*add);
293 objects[UT_FILES].data = &num_files;
294 objects[UT_FILES].size = sizeof(num_files);
295 INFO_LOG("######################### ret: %d\n", ret);
296 ret = osl_add_row(t, objects);
297 INFO_LOG("######################### ret: %d\n", ret);
299 } else { /* add size and increment file count */
301 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
303 ret = osl_get_object(t, row, UT_BYTES, &obj1);
306 num = *(uint64_t *)obj1.data + *add;
307 ret = osl_update_object(t, row, UT_BYTES, &obj2);
310 ret = osl_get_object(t, row, UT_FILES, &obj1);
313 num = *(uint64_t *)obj1.data + 1;
314 return osl_update_object(t, row, UT_FILES, &obj2);
318 static uint32_t num_dirs;
319 static uint32_t num_files;
320 static uint64_t num_bytes;
322 int scan_dir(char *dirname)
325 struct dirent *entry;
326 int ret, cwd_fd, ret2;
327 uint64_t dir_size = 0, dir_files = 0;
328 uint32_t this_dir_num = num_dirs++;
330 DEBUG_LOG("----------------- %u: %s\n", num_dirs, dirname);
331 ret = para_opendir(dirname, &dir, &cwd_fd);
333 if (ret != -ERRNO_TO_ERROR(EACCES))
335 WARNING_LOG("permission denied for %s\n", dirname);
338 while ((entry = readdir(dir))) {
344 struct user_info *ui;
346 if (!strcmp(entry->d_name, "."))
348 if (!strcmp(entry->d_name, ".."))
350 if (lstat(entry->d_name, &s) == -1) {
351 WARNING_LOG("lstat error for %s/%s\n", dirname,
356 if (!S_ISREG(m) && !S_ISDIR(m))
359 tmp = make_message("%s/%s", dirname, entry->d_name);
373 ret = search_uid(uid, 1, &ui);
378 ret = update_user_row(ui->table, this_dir_num, &size);
382 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
385 ret2 = para_fchdir(cwd_fd);
386 if (ret2 < 0 && ret >= 0)
392 static int get_dir_name(struct osl_row *row, char **name)
394 struct osl_object obj;
395 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
403 static int print_dirname_and_size(struct osl_row *row, void *data)
405 unsigned *count = data;
406 struct osl_object obj;
410 if ((*count)++ > 100)
411 return -E_LOOP_COMPLETE;
412 ret = get_dir_name(row, &name);
415 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
418 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
422 static int print_dirname_and_file_count(struct osl_row *row, void *data)
424 unsigned *count = data;
425 struct osl_object obj;
429 if ((*count)++ > 100)
430 return -E_LOOP_COMPLETE;
431 ret = get_dir_name(row, &name);
434 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
437 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
441 static void print_id_stats(void)
443 struct user_info *ui;
448 printf("%u\t%llu\t%llu\n", (unsigned)ui->uid, (long long unsigned)ui->files,
449 (long long unsigned)ui->bytes);
453 struct big_dir_info {
455 struct osl_table *user_table;
458 static int print_big_dir(struct osl_row *row, void *data)
460 struct big_dir_info *bdi = data;
462 struct osl_row *dir_row;
465 struct osl_object obj;
467 if (bdi->count++ > 10)
468 return -E_LOOP_COMPLETE;
469 ret = osl_get_object(bdi->user_table, row, UT_BYTES, &obj);
472 bytes = *(uint64_t *)obj.data;
473 ret = osl_get_object(bdi->user_table, row, UT_DIR_NUM, &obj);
476 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
479 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
483 printf("%s: %llu\n", dirname, (long long unsigned)bytes);
487 static void print_id_dir_stats(void)
489 struct user_info *ui;
492 struct big_dir_info bdi = {.count = 0};
495 bdi.user_table = ui->table;
496 printf("************************* Big dirs owned by uid %u\n", (unsigned) ui->uid);
497 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &bdi, print_big_dir);
501 static int print_statistics(void)
506 printf("Summary: %u dirs, %u files, %llu bytes\n", (unsigned)num_dirs,
507 (unsigned)num_files, (long long unsigned)num_bytes);
508 printf("************************* Biggest dirs\n");
509 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &count, print_dirname_and_size);
510 if (ret < 0 && ret != -E_LOOP_COMPLETE)
513 printf("************************* dirs containing many files\n");
514 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &count, print_dirname_and_file_count);
515 if (ret < 0 && ret != -E_LOOP_COMPLETE)
518 printf("************************* dirs stats by owner\n");
520 print_id_dir_stats();
525 int main(int argc, char **argv)
527 int ret = create_tables();
530 ret = osl_open_table(&dir_table_desc, &dir_table);
536 ret = scan_dir(argv[1]);
542 ERROR_LOG("%s\n", error_txt(-ret));
543 return -EXIT_FAILURE;