put tab between number of files and number of bytes in dir_stats
[adu.git] / adu.c
1 #include "adu.h"
2 #include <dirent.h> /* readdir() */
3
4 #include "gcc-compat.h"
5 #include "osl.h"
6 #include "fd.h"
7 #include "hash.h"
8 #include "string.h"
9 #include "error.h"
10
11 DEFINE_ERRLIST;
12
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)))
15
16
17 /**
18 * The log function.
19 *
20 * \param ll Loglevel.
21 * \param fml Usual format string.
22 *
23 * All XXX_LOG() macros use this function.
24 */
25 __printf_2_3 void __log(int ll, const char* fmt,...)
26 {
27 va_list argp;
28 FILE *outfd;
29 struct tm *tm;
30 time_t t1;
31 char str[255] = "";
32
33 if (ll < 4)
34 return;
35 outfd = stderr;
36 time(&t1);
37 tm = localtime(&t1);
38 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
39 fprintf(outfd, "%s ", str);
40 va_start(argp, fmt);
41 vfprintf(outfd, fmt, argp);
42 va_end(argp);
43 }
44
45 /**
46 * Compare the size of two directories
47 *
48 * \param obj1 Pointer to the first object.
49 * \param obj2 Pointer to the second object.
50 *
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.
54 */
55 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
56 {
57 uint64_t d1 = *(uint64_t *)obj1->data;
58 uint64_t d2 = *(uint64_t *)obj2->data;
59 int ret = NUM_COMPARE(d2, d1);
60
61 if (ret)
62 return ret;
63 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
64 return NUM_COMPARE(obj2->data, obj1->data);
65 }
66
67 /**
68 * Compare two osl objects of string type.
69 *
70 * \param obj1 Pointer to the first object.
71 * \param obj2 Pointer to the second object.
72 *
73 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
74 * are taken into account.
75 *
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
78 * obj2.
79 *
80 * \sa strcmp(3), strncmp(3), osl_compare_func.
81 */
82 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
83 {
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));
87 }
88
89 /** The columns of the directory table. */
90 enum dir_table_columns {
91 /** The name of the directory. */
92 DT_NAME,
93 /** The hash value of the name. */
94 DT_HASH,
95 /** The number of bytes of all regular files. */
96 DT_BYTES,
97 /** The number of all regular files. */
98 DT_FILES,
99 /** Number of columns in this table. */
100 NUM_DT_COLUMNS
101 };
102
103 static struct osl_column_description dir_table_cols[] = {
104 [DT_NAME] = {
105 .storage_type = OSL_MAPPED_STORAGE,
106 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
107 .name = "dir",
108 .compare_function = string_compare,
109 },
110 [DT_HASH] = {
111 .storage_type = OSL_MAPPED_STORAGE,
112 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
113 .name = "hash",
114 .compare_function = osl_hash_compare,
115 .data_size = HASH_SIZE
116 },
117 [DT_BYTES] = {
118 .storage_type = OSL_MAPPED_STORAGE,
119 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
120 .compare_function = size_compare,
121 .name = "num_bytes",
122 .data_size = sizeof(uint64_t)
123 },
124 [DT_FILES] = {
125 .storage_type = OSL_MAPPED_STORAGE,
126 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
127 .compare_function = size_compare,
128 .name = "num_files",
129 .data_size = sizeof(uint64_t)
130 }
131 };
132
133 static struct osl_table_description dir_table_desc = {
134 .name = "dir_table",
135 .num_columns = NUM_DT_COLUMNS,
136 .flags = 0,
137 .column_descriptions = dir_table_cols,
138 .dir = "/tmp/adu"
139 };
140
141 /** The columns of the id table. */
142 enum id_table_columns {
143 /** The user id. */
144 IDT_UID,
145 /** The number of bytes of all regular files owned by this id. */
146 IDT_BYTES,
147 /** The number of regular files owned by this id. */
148 IDT_FILES,
149 /** The user table for this uid. */
150 IDT_TABLE,
151 /** Number of columns in this table. */
152 NUM_IDT_COLUMNS
153 };
154
155 static struct osl_column_description id_table_cols[] = {
156 [IDT_UID] = {
157 .storage_type = OSL_MAPPED_STORAGE,
158 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
159 .name = "uid",
160 .compare_function = uint32_compare,
161 .data_size = sizeof(uint32_t)
162 },
163 [IDT_BYTES] = {
164 .storage_type = OSL_MAPPED_STORAGE,
165 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
166 .compare_function = size_compare,
167 .name = "num_bytes",
168 .data_size = sizeof(uint64_t)
169 },
170 [IDT_FILES] = {
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)
176 },
177 [IDT_TABLE] = {
178 .storage_type = OSL_NO_STORAGE,
179 .storage_flags = OSL_FIXED_SIZE | OSL_UNIQUE,
180 .name = "user_table",
181 .data_size = sizeof(void *)
182 }
183 };
184
185 static struct osl_table_description id_table_desc = {
186 .name = "id_table",
187 .num_columns = NUM_IDT_COLUMNS,
188 .flags = 0,
189 .column_descriptions = id_table_cols,
190 .dir = "/tmp/adu"
191 };
192
193 /** The columns of the id table. */
194 enum user_table_columns {
195 /** The hash of the directory. */
196 UT_DIR_HASH,
197 /** The number of bytes of all regular files in this dir owned by this id. */
198 UT_BYTES,
199 /** The number of files in this dir owned by this id. */
200 UT_FILES,
201 /** Number of columns in this table. */
202 NUM_UT_COLUMNS
203 };
204
205 static struct osl_column_description user_table_cols[] = {
206 [UT_DIR_HASH] = {
207 .storage_type = OSL_MAPPED_STORAGE,
208 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
209 .name = "dir_hash",
210 .compare_function = osl_hash_compare,
211 .data_size = HASH_SIZE
212 },
213 [IDT_BYTES] = {
214 .storage_type = OSL_MAPPED_STORAGE,
215 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
216 .compare_function = size_compare,
217 .name = "num_bytes",
218 .data_size = sizeof(uint64_t)
219 },
220 [IDT_FILES] = {
221 .storage_type = OSL_MAPPED_STORAGE,
222 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
223 .compare_function = size_compare,
224 .name = "num_files",
225 .data_size = sizeof(uint64_t)
226 },
227 };
228
229 static struct osl_table_description user_table_desc = {
230 .num_columns = NUM_UT_COLUMNS,
231 .flags = 0,
232 .column_descriptions = user_table_cols,
233 .dir = "/tmp/adu"
234 };
235 static struct osl_table *dir_table;
236 static struct osl_table *id_table;
237
238 static int create_tables(void)
239 {
240 int ret = osl_create_table(&dir_table_desc);
241 if (ret < 0)
242 return ret;
243 ret = osl_create_table(&id_table_desc);
244 if (ret < 0)
245 return ret;
246 return 1;
247 }
248
249 int add_directory(char *dirname, uint64_t *dir_size, uint64_t *dir_files)
250 {
251 struct osl_object dir_objects[NUM_DT_COLUMNS];
252 HASH_TYPE hash[HASH_SIZE];
253
254 INFO_LOG("scanning %s\n", dirname);
255 dir_objects[DT_NAME].data = dirname;
256 dir_objects[DT_NAME].size = strlen(dirname) + 1;
257 hash_function(dirname, strlen(dirname), hash);
258 dir_objects[DT_HASH].data = hash;
259 dir_objects[DT_HASH].size = HASH_SIZE;
260 dir_objects[DT_BYTES].data = dir_size;
261 dir_objects[DT_BYTES].size = sizeof(*dir_size);
262 dir_objects[DT_FILES].data = dir_files;
263 dir_objects[DT_FILES].size = sizeof(*dir_files);
264
265 return osl_add_row(dir_table, dir_objects);
266 }
267
268 int create_and_open_user_table(uint32_t uid, struct osl_table **t)
269 {
270 int ret;
271 struct osl_table_description *desc = para_malloc(sizeof(*desc));
272
273 desc->num_columns = NUM_UT_COLUMNS;
274 desc->flags = 0;
275 desc->column_descriptions = user_table_cols;
276 desc->dir = para_strdup("/tmp/adu");
277 desc->name = make_message("%u", uid);
278 INFO_LOG("................................. %u\n", uid);
279 // user_table_desc.name = make_message("%u", uid);
280 ret = osl_create_table(desc);
281 if (ret < 0)
282 return ret;
283 return osl_open_table(desc, t);
284 }
285
286 static int insert_id_row(uint32_t uid, struct osl_table *t, struct osl_row **row)
287 {
288 struct osl_object objects[NUM_IDT_COLUMNS];
289 uint64_t num = 0;
290
291 struct osl_table **table_ptr = para_malloc(sizeof(*table_ptr));
292 *table_ptr = t;
293
294 INFO_LOG("§§§§§§§§§§§§§§§§§§§§§ uid: %d, t: %p\n", uid, t);
295 objects[IDT_UID].data = &uid;
296 objects[IDT_UID].size = sizeof(uid);
297 objects[IDT_BYTES].data = &num;
298 objects[IDT_BYTES].size = sizeof(num);
299 objects[IDT_FILES].data = &num;
300 objects[IDT_FILES].size = sizeof(num);
301 objects[IDT_TABLE].data = table_ptr;
302 objects[IDT_TABLE].size = sizeof(*table_ptr);
303 return osl_add_and_get_row(id_table, objects, row);
304 }
305
306 static int get_user_table(struct osl_row *row, struct osl_table **t)
307 {
308 struct osl_object obj;
309
310 int ret = osl_get_object(id_table, row, IDT_TABLE, &obj);
311 if (ret < 0)
312 return ret;
313 *t = *(struct osl_table **)obj.data;
314 INFO_LOG("^^^^^^^^^^^^^^^^^^ t: %p\n", *t);
315 return 1;
316 }
317
318 static int add_id_bytes(struct osl_row *row, uint64_t *add)
319 {
320 uint64_t num;
321 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
322
323 /* update number of bytes */
324 int ret = osl_get_object(id_table, row, IDT_BYTES, &obj1);
325 if (ret < 0)
326 return ret;
327 num = *(uint64_t *)obj1.data + *add;
328 ret = osl_update_object(id_table, row, IDT_BYTES, &obj2);
329 if (ret < 0)
330 return ret;
331 /* increment number of files */
332 ret = osl_get_object(id_table, row, IDT_FILES, &obj1);
333 if (ret < 0)
334 return ret;
335 num = *(uint64_t *)obj1.data + 1;
336 return osl_update_object(id_table, row, IDT_FILES, &obj2);
337 }
338
339 static int update_user_row(struct osl_table *t, HASH_TYPE *hash,
340 uint64_t *add)
341 {
342 struct osl_row *row;
343 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
344
345 int ret = osl_get_row(t, UT_DIR_HASH, &obj, &row);
346
347 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
348 return ret;
349 if (ret < 0) { /* this is the first file we add */
350 struct osl_object objects[NUM_UT_COLUMNS];
351 uint64_t num_files = 1;
352
353 objects[UT_DIR_HASH].data = hash;
354 objects[UT_DIR_HASH].size = HASH_SIZE;
355 objects[UT_BYTES].data = add;
356 objects[UT_BYTES].size = sizeof(*add);
357 objects[UT_FILES].data = &num_files;
358 objects[UT_FILES].size = sizeof(num_files);
359 INFO_LOG("######################### ret: %d\n", ret);
360 ret = osl_add_row(t, objects);
361 INFO_LOG("######################### ret: %d\n", ret);
362 return ret;
363 } else { /* add size and increment file count */
364 uint64_t num;
365 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
366
367 ret = osl_get_object(t, row, UT_BYTES, &obj1);
368 if (ret < 0)
369 return ret;
370 num = *(uint64_t *)obj1.data + *add;
371 ret = osl_update_object(t, row, UT_BYTES, &obj2);
372 if (ret < 0)
373 return ret;
374 ret = osl_get_object(t, row, UT_FILES, &obj1);
375 if (ret < 0)
376 return ret;
377 num = *(uint64_t *)obj1.data + 1;
378 return osl_update_object(t, row, UT_FILES, &obj2);
379 }
380 }
381
382 int scan_dir(char *dirname)
383 {
384 DIR *dir;
385 struct dirent *entry;
386 int ret, cwd_fd, ret2;
387 uint64_t dir_size = 0, dir_files = 0;
388 struct osl_object obj;
389 HASH_TYPE hash[HASH_SIZE];
390
391 INFO_LOG("----------------- %s\n", dirname);
392 ret = para_opendir(dirname, &dir, &cwd_fd);
393 if (ret < 0) {
394 if (ret != -ERRNO_TO_ERROR(EACCES))
395 return ret;
396 WARNING_LOG("permission denied for %s\n", dirname);
397 return 1;
398 }
399 hash_function(dirname, strlen(dirname), hash);
400 while ((entry = readdir(dir))) {
401 mode_t m;
402 char *tmp;
403 struct stat s;
404 uint32_t uid;
405 uint64_t size;
406 struct osl_row *id_row;
407 struct osl_table *user_table;
408
409 if (!strcmp(entry->d_name, "."))
410 continue;
411 if (!strcmp(entry->d_name, ".."))
412 continue;
413 if (lstat(entry->d_name, &s) == -1)
414 continue;
415 m = s.st_mode;
416 if (!S_ISREG(m) && !S_ISDIR(m))
417 continue;
418 tmp = make_message("%s/%s", dirname, entry->d_name);
419 if (S_ISDIR(m)) {
420 ret = scan_dir(tmp);
421 free(tmp);
422 if (ret < 0)
423 goto out;
424 continue;
425 }
426 /* regular file */
427 size = s.st_size;
428 dir_size += size;
429 dir_files++;
430 uid = s.st_uid;
431 INFO_LOG("++++++++++++++++++++++++++ %s, uid: %u\n", entry->d_name, uid);
432 obj.data = &uid;
433 obj.size = sizeof(uid);
434 ret = osl_get_row(id_table, IDT_UID, &obj, &id_row);
435 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
436 goto out;
437 if (ret < 0) {
438 ret = create_and_open_user_table(uid, &user_table);
439 if (ret < 0)
440 goto out;
441 ret = insert_id_row(uid, user_table, &id_row);
442 if (ret < 0)
443 goto out;
444 } else {
445 ret = get_user_table(id_row, &user_table);
446 if (ret < 0)
447 goto out;
448 }
449 ret = add_id_bytes(id_row, &size);
450 if (ret < 0)
451 goto out;
452 INFO_LOG("user_table: %p\n", user_table);
453 ret = update_user_row(user_table, hash, &size);
454 INFO_LOG("update_user ret: %d\n", ret);
455 if (ret < 0)
456 goto out;
457 }
458 ret = add_directory(dirname, &dir_size, &dir_files);
459 out:
460 closedir(dir);
461 ret2 = para_fchdir(cwd_fd);
462 if (ret2 < 0 && ret >= 0)
463 ret = ret2;
464 close(cwd_fd);
465 return ret;
466 }
467
468 static int get_dir_name(struct osl_row *row, char **name)
469 {
470 struct osl_object obj;
471 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
472
473 if (ret < 0)
474 return ret;
475 *name = obj.data;
476 return 1;
477 }
478
479 static int print_dirname_and_size(struct osl_row *row, void *data)
480 {
481 unsigned *count = data;
482 struct osl_object obj;
483 char *name;
484 int ret;
485
486 if ((*count)++ > 100)
487 return -E_LOOP_COMPLETE;
488 ret = get_dir_name(row, &name);
489 if (ret < 0)
490 return ret;
491 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
492 if (ret < 0)
493 return ret;
494 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
495 return 1;
496 }
497
498 static int print_dirname_and_file_count(struct osl_row *row, void *data)
499 {
500 unsigned *count = data;
501 struct osl_object obj;
502 char *name;
503 int ret;
504
505 if ((*count)++ > 100)
506 return -E_LOOP_COMPLETE;
507 ret = get_dir_name(row, &name);
508 if (ret < 0)
509 return ret;
510 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
511 if (ret < 0)
512 return ret;
513 printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
514 return 1;
515 }
516
517 static int print_id_stats(struct osl_row *row, __a_unused void *data)
518 {
519 struct osl_object obj;
520 uint32_t uid;
521 uint64_t bytes, files;
522 int ret = osl_get_object(id_table, row, IDT_UID, &obj);
523
524 if (ret < 0)
525 return ret;
526 uid = *(uint32_t *)obj.data;
527 ret = osl_get_object(id_table, row, IDT_BYTES, &obj);
528 if (ret < 0)
529 return ret;
530 bytes = *(uint64_t *)obj.data;
531 ret = osl_get_object(id_table, row, IDT_FILES, &obj);
532 if (ret < 0)
533 return ret;
534 files = *(uint64_t *)obj.data;
535
536 printf("%u\t%llu\t%llu\n", (unsigned)uid, (long long unsigned)files,
537 (long long unsigned)bytes);
538 return 1;
539 }
540
541 struct id_dir_stat_info {
542 unsigned count;
543 struct osl_table *user_table;
544 };
545
546 static int print_big_dir(struct osl_row *row, void *data)
547 {
548 struct id_dir_stat_info *info = data;
549 info->count++;
550 int ret;
551 struct osl_row *dir_row;
552 char *dirname;
553 uint64_t bytes;
554 struct osl_object obj;
555
556 if (info->count > 10)
557 return -E_LOOP_COMPLETE;
558 ret = osl_get_object(info->user_table, row, UT_BYTES, &obj);
559 if (ret < 0)
560 return ret;
561 bytes = *(uint64_t *)obj.data;
562 ret = osl_get_object(info->user_table, row, UT_DIR_HASH, &obj);
563 if (ret < 0)
564 return ret;
565 ret = osl_get_row(dir_table, DT_HASH, &obj, &dir_row);
566 if (ret < 0)
567 return ret;
568 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
569 if (ret < 0)
570 return ret;
571 dirname = obj.data;
572 printf("%s: %llu\n", dirname, (long long unsigned)bytes);
573 return 1;
574 }
575
576 static int print_id_dir_stats(struct osl_row *row, __a_unused void *data)
577 {
578 struct osl_object obj;
579 uint32_t uid;
580 int ret = osl_get_object(id_table, row, IDT_UID, &obj);
581 struct id_dir_stat_info info = {.count = 0};
582
583 if (ret < 0)
584 return ret;
585 uid = *(uint32_t *)obj.data;
586
587 ret = osl_get_object(id_table, row, IDT_TABLE, &obj);
588 if (ret < 0)
589 return ret;
590 info.user_table = *(struct osl_table **)obj.data;
591
592 printf("************************* Big dirs owned by uid %u\n", (unsigned) uid);
593 osl_rbtree_loop_reverse(info.user_table, IDT_BYTES, &info, print_big_dir);
594 return 1;
595 }
596
597 static int print_statistics(void)
598 {
599 unsigned count = 0;
600 int ret;
601
602 printf("************************* Biggest dirs\n");
603 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &count, print_dirname_and_size);
604 if (ret < 0 && ret != -E_LOOP_COMPLETE)
605 return ret;
606 count = 0;
607 printf("************************* dirs containing many files\n");
608 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &count, print_dirname_and_file_count);
609 if (ret < 0 && ret != -E_LOOP_COMPLETE)
610 return ret;
611
612 printf("************************* dirs stats by owner\n");
613 ret = osl_rbtree_loop(id_table, IDT_BYTES, NULL, print_id_stats);
614 if (ret < 0)
615 return ret;
616
617 return osl_rbtree_loop(id_table, IDT_BYTES, NULL, print_id_dir_stats);
618 }
619
620
621 int main(int argc, char **argv)
622 {
623 int ret = create_tables();
624 if (ret < 0)
625 goto out;
626 ret = osl_open_table(&dir_table_desc, &dir_table);
627 if (ret < 0)
628 goto out;
629 ret = osl_open_table(&id_table_desc, &id_table);
630 if (ret < 0)
631 goto out;
632 ret = -E_SYNTAX;
633 if (argc != 2)
634 goto out;
635 ret = scan_dir(argv[1]);
636 if (ret < 0)
637 goto out;
638 print_statistics();
639 out:
640 if (ret < 0) {
641 ERROR_LOG("%s\n", error_txt(-ret));
642 return -EXIT_FAILURE;
643 }
644 return EXIT_SUCCESS;
645 }
646