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 int create_and_open_user_table(uint32_t uid, struct osl_table **t)
202 struct osl_table_description *desc = para_malloc(sizeof(*desc));
204 desc->num_columns = NUM_UT_COLUMNS;
206 desc->column_descriptions = user_table_cols;
207 desc->dir = para_strdup(DATABASE_DIR);
208 desc->name = make_message("%u", uid);
209 INFO_LOG("................................. %u\n", uid);
210 // user_table_desc.name = make_message("%u", uid);
211 ret = osl_create_table(desc);
214 return osl_open_table(desc, t);
218 #define uid_hash_bits 8
219 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
220 #define PRIME1 0x811c9dc5
221 #define PRIME2 0x01000193
225 struct osl_table *table;
230 static struct user_info *uid_hash_table;
232 static void create_hash_table(void)
234 uid_hash_table = para_calloc(uid_hash_table_size
235 * sizeof(struct user_info));
238 static int create_tables(void)
241 return osl_create_table(&dir_table_desc);
245 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
247 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num) % uid_hash_table_size;
250 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < uid_hash_table \
251 + uid_hash_table_size; ui++)
253 static int search_uid(uint32_t uid, int insert, struct user_info **ui)
257 for (p = 0; p < uid_hash_table_size; p++) {
258 struct user_info *i = uid_hash_table + double_hash(uid, p);
262 int ret = create_and_open_user_table(uid, &i->table);
274 return insert? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
277 static int update_user_row(struct osl_table *t, uint32_t dir_num,
281 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
283 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
285 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
287 if (ret < 0) { /* this is the first file we add */
288 struct osl_object objects[NUM_UT_COLUMNS];
289 uint64_t num_files = 1;
291 objects[UT_DIR_NUM].data = &dir_num;
292 objects[UT_DIR_NUM].size = sizeof(dir_num);
293 objects[UT_BYTES].data = add;
294 objects[UT_BYTES].size = sizeof(*add);
295 objects[UT_FILES].data = &num_files;
296 objects[UT_FILES].size = sizeof(num_files);
297 INFO_LOG("######################### ret: %d\n", ret);
298 ret = osl_add_row(t, objects);
299 INFO_LOG("######################### ret: %d\n", ret);
301 } else { /* add size and increment file count */
303 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
305 ret = osl_get_object(t, row, UT_BYTES, &obj1);
308 num = *(uint64_t *)obj1.data + *add;
309 ret = osl_update_object(t, row, UT_BYTES, &obj2);
312 ret = osl_get_object(t, row, UT_FILES, &obj1);
315 num = *(uint64_t *)obj1.data + 1;
316 return osl_update_object(t, row, UT_FILES, &obj2);
320 static uint32_t num_dirs;
321 static uint32_t num_files;
322 static uint64_t num_bytes;
324 int scan_dir(char *dirname)
327 struct dirent *entry;
328 int ret, cwd_fd, ret2;
329 uint64_t dir_size = 0, dir_files = 0;
330 uint32_t this_dir_num = num_dirs++;
332 DEBUG_LOG("----------------- %u: %s\n", num_dirs, dirname);
333 ret = para_opendir(dirname, &dir, &cwd_fd);
335 if (ret != -ERRNO_TO_ERROR(EACCES))
337 WARNING_LOG("permission denied for %s\n", dirname);
340 while ((entry = readdir(dir))) {
346 struct user_info *ui;
348 if (!strcmp(entry->d_name, "."))
350 if (!strcmp(entry->d_name, ".."))
352 if (lstat(entry->d_name, &s) == -1) {
353 WARNING_LOG("lstat error for %s/%s\n", dirname,
358 if (!S_ISREG(m) && !S_ISDIR(m))
361 tmp = make_message("%s/%s", dirname, entry->d_name);
375 ret = search_uid(uid, 1, &ui);
380 ret = update_user_row(ui->table, this_dir_num, &size);
384 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
387 ret2 = para_fchdir(cwd_fd);
388 if (ret2 < 0 && ret >= 0)
394 static int get_dir_name(struct osl_row *row, char **name)
396 struct osl_object obj;
397 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
405 static int print_dirname_and_size(struct osl_row *row, void *data)
407 unsigned *count = data;
408 struct osl_object obj;
412 if ((*count)++ > 100)
413 return -E_LOOP_COMPLETE;
414 ret = get_dir_name(row, &name);
417 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
420 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
424 static int print_dirname_and_file_count(struct osl_row *row, void *data)
426 unsigned *count = data;
427 struct osl_object obj;
431 if ((*count)++ > 100)
432 return -E_LOOP_COMPLETE;
433 ret = get_dir_name(row, &name);
436 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
439 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
443 static void print_id_stats(void)
445 struct user_info *ui;
450 printf("%u\t%llu\t%llu\n", (unsigned)ui->uid, (long long unsigned)ui->files,
451 (long long unsigned)ui->bytes);
455 struct big_dir_info {
457 struct osl_table *user_table;
460 static int print_big_dir(struct osl_row *row, void *data)
462 struct big_dir_info *bdi = data;
464 struct osl_row *dir_row;
467 struct osl_object obj;
469 if (bdi->count++ > 10)
470 return -E_LOOP_COMPLETE;
471 ret = osl_get_object(bdi->user_table, row, UT_BYTES, &obj);
474 bytes = *(uint64_t *)obj.data;
475 ret = osl_get_object(bdi->user_table, row, UT_DIR_NUM, &obj);
478 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
481 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
485 printf("%s: %llu\n", dirname, (long long unsigned)bytes);
489 static void print_id_dir_stats(void)
491 struct user_info *ui;
494 struct big_dir_info bdi = {.count = 0};
497 bdi.user_table = ui->table;
498 printf("************************* Big dirs owned by uid %u\n", (unsigned) ui->uid);
499 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &bdi, print_big_dir);
503 static int print_statistics(void)
508 printf("Summary: %u dirs, %u files, %llu bytes\n", (unsigned)num_dirs,
509 (unsigned)num_files, (long long unsigned)num_bytes);
510 printf("************************* Biggest dirs\n");
511 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &count, print_dirname_and_size);
512 if (ret < 0 && ret != -E_LOOP_COMPLETE)
515 printf("************************* dirs containing many files\n");
516 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &count, print_dirname_and_file_count);
517 if (ret < 0 && ret != -E_LOOP_COMPLETE)
520 printf("************************* dirs stats by owner\n");
522 print_id_dir_stats();
527 int main(int argc, char **argv)
529 int ret = create_tables();
532 ret = osl_open_table(&dir_table_desc, &dir_table);
538 ret = scan_dir(argv[1]);
544 ERROR_LOG("%s\n", error_txt(-ret));
545 return -EXIT_FAILURE;