8077bd923cb2836816cb434b2280a19dc330d52d
[adu.git] / adu.c
1 #include "adu.h"
2 #include <dirent.h> /* readdir() */
3
4 #include "gcc-compat.h"
5 #include "cmdline.h"
6 #include "fd.h"
7 #include "string.h"
8 #include "error.h"
9 #include "portable_io.h"
10
11 DEFINE_ERRLIST;
12
13 /** Command line and config file options. */
14 static struct gengetopt_args_info conf;
15
16 struct user_info {
17 uint32_t uid;
18 struct osl_table *table;
19 uint64_t files;
20 uint64_t bytes;
21 uint64_t dirs;
22 struct osl_table_description *desc;
23 };
24
25 static struct user_info *uid_hash_table;
26
27 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
28 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
29
30
31 /**
32 * The log function.
33 *
34 * \param ll Loglevel.
35 * \param fml Usual format string.
36 *
37 * All XXX_LOG() macros use this function.
38 */
39 __printf_2_3 void __log(int ll, const char* fmt,...)
40 {
41 va_list argp;
42 FILE *outfd;
43 struct tm *tm;
44 time_t t1;
45 char str[255] = "";
46
47 if (ll < conf.loglevel_arg)
48 return;
49 outfd = stderr;
50 time(&t1);
51 tm = localtime(&t1);
52 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
53 fprintf(outfd, "%s ", str);
54 va_start(argp, fmt);
55 vfprintf(outfd, fmt, argp);
56 va_end(argp);
57 }
58
59 /**
60 * Compare the size of two directories
61 *
62 * \param obj1 Pointer to the first object.
63 * \param obj2 Pointer to the second object.
64 *
65 * This function first compares the size values as usual integers. If they compare as
66 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
67 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
68 */
69 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
70 {
71 uint64_t d1 = *(uint64_t *)obj1->data;
72 uint64_t d2 = *(uint64_t *)obj2->data;
73 int ret = NUM_COMPARE(d2, d1);
74
75 if (ret)
76 return ret;
77 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
78 return NUM_COMPARE(obj2->data, obj1->data);
79 }
80
81 /**
82 * Compare two osl objects of string type.
83 *
84 * \param obj1 Pointer to the first object.
85 * \param obj2 Pointer to the second object.
86 *
87 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
88 * are taken into account.
89 *
90 * \return It returns an integer less than, equal to, or greater than zero if
91 * \a obj1 is found, respectively, to be less than, to match, or be greater
92 * than obj2.
93 *
94 * \sa strcmp(3), strncmp(3), osl_compare_func.
95 */
96 static int string_compare(const struct osl_object *obj1,
97 const struct osl_object *obj2)
98 {
99 const char *str1 = (const char *)obj1->data;
100 const char *str2 = (const char *)obj2->data;
101 return strncmp(str1, str2, MIN(obj1->size, obj2->size));
102 }
103
104 /**
105 * Compare two osl objects pointing to unsigned integers of 64 bit size.
106 *
107 * \param obj1 Pointer to the first integer.
108 * \param obj2 Pointer to the second integer.
109 *
110 * \return The values required for an osl compare function.
111 *
112 * \sa osl_compare_func, osl_hash_compare().
113 */
114 static int uint64_compare(const struct osl_object *obj1,
115 const struct osl_object *obj2)
116 {
117 uint64_t d1 = read_u64((const char *)obj1->data);
118 uint64_t d2 = read_u64((const char *)obj2->data);
119
120 if (d1 < d2)
121 return 1;
122 if (d1 > d2)
123 return -1;
124 return 0;
125 }
126
127 /** The columns of the directory table. */
128 enum dir_table_columns {
129 /** The name of the directory. */
130 DT_NAME,
131 /** The dir count number. */
132 DT_NUM,
133 /** The number of bytes of all regular files. */
134 DT_BYTES,
135 /** The number of all regular files. */
136 DT_FILES,
137 /** Number of columns in this table. */
138 NUM_DT_COLUMNS
139 };
140
141 static struct osl_column_description dir_table_cols[] = {
142 [DT_NAME] = {
143 .storage_type = OSL_MAPPED_STORAGE,
144 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
145 .name = "dir",
146 .compare_function = string_compare,
147 },
148 [DT_NUM] = {
149 .storage_type = OSL_MAPPED_STORAGE,
150 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
151 .name = "num",
152 .compare_function = uint64_compare,
153 .data_size = sizeof(uint64_t)
154 },
155 [DT_BYTES] = {
156 .storage_type = OSL_MAPPED_STORAGE,
157 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
158 .compare_function = size_compare,
159 .name = "num_bytes",
160 .data_size = sizeof(uint64_t)
161 },
162 [DT_FILES] = {
163 .storage_type = OSL_MAPPED_STORAGE,
164 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
165 .compare_function = size_compare,
166 .name = "num_files",
167 .data_size = sizeof(uint64_t)
168 }
169 };
170
171 static struct osl_table_description dir_table_desc = {
172 .name = "dir_table",
173 .num_columns = NUM_DT_COLUMNS,
174 .flags = 0,
175 .column_descriptions = dir_table_cols,
176 };
177
178 /** The columns of the id table. */
179 enum user_table_columns {
180 /** The numer of the directory. */
181 UT_DIR_NUM,
182 /** The number of bytes of all regular files in this dir owned by this id. */
183 UT_BYTES,
184 /** The number of files in this dir owned by this id. */
185 UT_FILES,
186 /** Number of columns in this table. */
187 NUM_UT_COLUMNS
188 };
189
190 static struct osl_column_description user_table_cols[] = {
191 [UT_DIR_NUM] = {
192 .storage_type = OSL_MAPPED_STORAGE,
193 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
194 .name = "dir_num",
195 .compare_function = uint64_compare,
196 .data_size = sizeof(uint64_t)
197 },
198 [UT_BYTES] = {
199 .storage_type = OSL_MAPPED_STORAGE,
200 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
201 .compare_function = size_compare,
202 .name = "num_bytes",
203 .data_size = sizeof(uint64_t)
204 },
205 [UT_FILES] = {
206 .storage_type = OSL_MAPPED_STORAGE,
207 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
208 .compare_function = size_compare,
209 .name = "num_files",
210 .data_size = sizeof(uint64_t)
211 },
212 };
213
214 static struct osl_table *dir_table;
215
216 static int add_directory(char *dirname, uint64_t dir_num, uint64_t *dir_size,
217 uint64_t *dir_files)
218 {
219 struct osl_object dir_objects[NUM_DT_COLUMNS];
220
221 INFO_LOG("adding #%llu: %s\n", (long long unsigned)dir_num, dirname);
222 dir_objects[DT_NAME].data = dirname;
223 dir_objects[DT_NAME].size = strlen(dirname) + 1;
224 dir_objects[DT_NUM].data = &dir_num;
225 dir_objects[DT_NUM].size = sizeof(dir_num);
226 dir_objects[DT_BYTES].data = dir_size;
227 dir_objects[DT_BYTES].size = sizeof(*dir_size);
228 dir_objects[DT_FILES].data = dir_files;
229 dir_objects[DT_FILES].size = sizeof(*dir_files);
230
231 return osl_add_row(dir_table, dir_objects);
232 }
233
234 static uint32_t num_uids;
235
236 static int open_user_table(struct user_info *ui, int create)
237 {
238 int ret;
239
240 ui->desc = para_malloc(sizeof(*ui->desc));
241 ui->desc->num_columns = NUM_UT_COLUMNS;
242 ui->desc->flags = 0;
243 ui->desc->column_descriptions = user_table_cols;
244 ui->desc->dir = para_strdup(conf.database_dir_arg);
245 ui->desc->name = make_message("%u", (unsigned)ui->uid);
246 num_uids++;
247 INFO_LOG(".............................uid #%u: %u\n",
248 (unsigned)num_uids, (unsigned)ui->uid);
249 if (create) {
250 ret = osl_create_table(ui->desc);
251 if (ret < 0)
252 goto err;
253 }
254 ret = osl_open_table(ui->desc, &ui->table);
255 if (ret < 0)
256 goto err;
257 return 1;
258 err:
259 free((char *)ui->desc->name);
260 free((char *)ui->desc->dir);
261 free(ui->desc);
262 ui->desc->name = NULL;
263 ui->desc->dir = NULL;
264 ui->desc = NULL;
265 ui->table = NULL;
266 return ret;
267 }
268
269 #define uid_hash_bits 8
270 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
271 #define PRIME1 0x811c9dc5
272 #define PRIME2 0x01000193
273
274 static void create_hash_table(void)
275 {
276 uid_hash_table = para_calloc(uid_hash_table_size
277 * sizeof(struct user_info));
278 }
279
280 static void free_hash_table(void)
281 {
282 free(uid_hash_table);
283 uid_hash_table = NULL;
284 }
285
286 static int create_tables(void)
287 {
288 int ret;
289
290 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
291 ret = osl_create_table(&dir_table_desc);
292 if (ret < 0)
293 return ret;
294 create_hash_table();
295 return 1;
296 }
297
298 /*
299 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
300 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
301 * unused slots in the table are used to store different uids that hash to the
302 * same slot.
303 *
304 * If a hash collision occurs, different slots are successively probed in order
305 * to find an unused slot for the new uid. Probing is implemented via a second
306 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
307 * odd number.
308 *
309 * An odd number is sufficient to make sure each entry of the hash table gets
310 * probed for probe_num between 0 and s-1 because s is a power of two, hence
311 * the second hash value never hash a common divisor with the hash table size.
312 * IOW: h is invertible in the ring [0..s].
313 */
314 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
315 {
316 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
317 % uid_hash_table_size;
318 }
319
320 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
321 + uid_hash_table_size; ui++)
322
323 enum search_uid_flags {
324 OPEN_USER_TABLE = 1,
325 CREATE_USER_TABLE = 2,
326 };
327
328 static int search_uid(uint32_t uid, enum search_uid_flags flags,
329 struct user_info **ui_ptr)
330 {
331 uint32_t p;
332
333 for (p = 0; p < uid_hash_table_size; p++) {
334 struct user_info *ui = uid_hash_table + double_hash(uid, p);
335
336 if (!ui->table) {
337 int ret;
338
339 if (!flags)
340 return -E_BAD_UID;
341 ui->uid = uid;
342 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
343 if (ret < 0)
344 return ret;
345 if (ui_ptr)
346 *ui_ptr = ui;
347 return 1;
348 }
349 if (ui->uid != uid)
350 continue;
351 if (ui_ptr)
352 *ui_ptr = ui;
353 return 0;
354 }
355 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
356 }
357
358 static int update_user_row(struct osl_table *t, uint64_t dir_num,
359 uint64_t *add)
360 {
361 struct osl_row *row;
362 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
363
364 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
365
366 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
367 return ret;
368 if (ret < 0) { /* this is the first file we add */
369 struct osl_object objects[NUM_UT_COLUMNS];
370 uint64_t num_files = 1;
371
372 objects[UT_DIR_NUM].data = &dir_num;
373 objects[UT_DIR_NUM].size = sizeof(dir_num);
374 objects[UT_BYTES].data = add;
375 objects[UT_BYTES].size = sizeof(*add);
376 objects[UT_FILES].data = &num_files;
377 objects[UT_FILES].size = sizeof(num_files);
378 INFO_LOG("######################### ret: %d\n", ret);
379 ret = osl_add_row(t, objects);
380 INFO_LOG("######################### ret: %d\n", ret);
381 return ret;
382 } else { /* add size and increment file count */
383 uint64_t num;
384 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
385
386 ret = osl_get_object(t, row, UT_BYTES, &obj1);
387 if (ret < 0)
388 return ret;
389 num = *(uint64_t *)obj1.data + *add;
390 ret = osl_update_object(t, row, UT_BYTES, &obj2);
391 if (ret < 0)
392 return ret;
393 ret = osl_get_object(t, row, UT_FILES, &obj1);
394 if (ret < 0)
395 return ret;
396 num = *(uint64_t *)obj1.data + 1;
397 return osl_update_object(t, row, UT_FILES, &obj2);
398 }
399 }
400
401 static uint64_t num_dirs;
402 static uint64_t num_files;
403 static uint64_t num_bytes;
404
405 int scan_dir(char *dirname)
406 {
407 DIR *dir;
408 struct dirent *entry;
409 int ret, cwd_fd, ret2;
410 uint64_t dir_size = 0, dir_files = 0;
411 uint64_t this_dir_num = num_dirs++;
412
413 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
414 ret = para_opendir(dirname, &dir, &cwd_fd);
415 if (ret < 0) {
416 if (ret != -ERRNO_TO_ERROR(EACCES))
417 return ret;
418 WARNING_LOG("permission denied for %s\n", dirname);
419 return 1;
420 }
421 while ((entry = readdir(dir))) {
422 mode_t m;
423 char *tmp;
424 struct stat s;
425 uint32_t uid;
426 uint64_t size;
427 struct user_info *ui;
428
429 if (!strcmp(entry->d_name, "."))
430 continue;
431 if (!strcmp(entry->d_name, ".."))
432 continue;
433 if (lstat(entry->d_name, &s) == -1) {
434 WARNING_LOG("lstat error for %s/%s\n", dirname,
435 entry->d_name);
436 continue;
437 }
438 m = s.st_mode;
439 if (!S_ISREG(m) && !S_ISDIR(m))
440 continue;
441 if (S_ISDIR(m)) {
442 tmp = make_message("%s/%s", dirname, entry->d_name);
443 ret = scan_dir(tmp);
444 free(tmp);
445 if (ret < 0)
446 goto out;
447 continue;
448 }
449 /* regular file */
450 size = s.st_size;
451 dir_size += size;
452 num_bytes += size;
453 dir_files++;
454 num_files++;
455 uid = s.st_uid;
456 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
457 if (ret < 0)
458 goto out;
459 ui->bytes += size;
460 ui->files++;
461 ret = update_user_row(ui->table, this_dir_num, &size);
462 if (ret < 0)
463 goto out;
464 }
465 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
466 out:
467 closedir(dir);
468 ret2 = para_fchdir(cwd_fd);
469 if (ret2 < 0 && ret >= 0)
470 ret = ret2;
471 close(cwd_fd);
472 return ret;
473 }
474
475 static int get_dir_name(struct osl_row *row, char **name)
476 {
477 struct osl_object obj;
478 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
479
480 if (ret < 0)
481 return ret;
482 *name = obj.data;
483 return 1;
484 }
485
486 const uint64_t size_unit_divisors[] = {
487 [size_unit_arg_b] = 1ULL,
488 [size_unit_arg_k] = 1024ULL,
489 [size_unit_arg_m] = 1024ULL * 1024ULL,
490 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
491 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
492 };
493
494 const uint64_t count_unit_divisors[] = {
495
496 [count_unit_arg_n] = 1ULL,
497 [count_unit_arg_k] = 1000ULL,
498 [count_unit_arg_m] = 1000ULL * 1000ULL,
499 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
500 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
501 };
502
503 const char size_unit_abbrevs[] = " BKMGT";
504 const char count_unit_abbrevs[] = " KMGT";
505
506 static void format_size_value(enum enum_size_unit unit, uint64_t value, char *result)
507 {
508 if (unit == size_unit_arg_h) /* human readable */
509 for (unit = size_unit_arg_b; unit < size_unit_arg_t && value > size_unit_divisors[unit + 1]; unit++)
510 ; /* nothing */
511 sprintf(result, "%llu%c", (long long unsigned)value / size_unit_divisors[unit], size_unit_abbrevs[unit]);
512 }
513
514 static void format_count_value(enum enum_count_unit unit, uint64_t value, char *result)
515 {
516 if (unit == count_unit_arg_h) /* human readable */
517 for (unit = count_unit_arg_n; unit < count_unit_arg_t && value > count_unit_divisors[unit + 1]; unit++)
518 ; /* nothing */
519 sprintf(result, "%llu%c", (long long unsigned)value / count_unit_divisors[unit], count_unit_abbrevs[unit]);
520 }
521
522 enum global_stats_flags {
523 GSF_PRINT_DIRNAME = 1,
524 GSF_PRINT_BYTES = 2,
525 GSF_PRINT_FILES = 4,
526 GSF_COMPUTE_SUMMARY = 8,
527 };
528
529 struct global_stats_info {
530 uint32_t count;
531 enum global_stats_flags flags;
532 };
533
534 static int global_stats_loop_function(struct osl_row *row, void *data)
535 {
536 struct global_stats_info *gsi = data;
537 struct osl_object obj;
538 char *dirname, formated_value[25];
539 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
540
541 if (!gsi->count && !summary)
542 return -E_LOOP_COMPLETE;
543 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
544 ret = get_dir_name(row, &dirname);
545 if (ret < 0)
546 return ret;
547 printf("%s%s", dirname,
548 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
549 "\t" : "\n"
550 );
551 }
552 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
553 uint64_t files;
554 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
555 if (ret < 0)
556 return ret;
557 files = *(uint64_t *)obj.data;
558 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
559 format_size_value(conf.size_unit_arg, files,
560 formated_value);
561 printf("%s%s", formated_value,
562 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
563 }
564 if (summary)
565 num_files += files;
566 }
567 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
568 uint64_t bytes;
569 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
570 if (ret < 0)
571 return ret;
572 bytes = *(uint64_t *)obj.data;
573 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
574 format_size_value(conf.size_unit_arg, bytes,
575 formated_value);
576 printf("%s\n", formated_value);
577 }
578 if (summary) {
579 num_bytes += bytes;
580 num_dirs++;
581 }
582 }
583 if (gsi->count > 0)
584 gsi->count--;
585 return 1;
586 }
587
588 static void print_id_stats(void)
589 {
590 struct user_info *ui;
591
592 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
593 FOR_EACH_USER(ui) {
594 char formated_dir_count[25], formated_file_count[25],
595 formated_bytes[25];
596 if (!ui->table)
597 continue;
598 format_count_value(conf.count_unit_arg, ui->dirs,
599 formated_dir_count);
600 format_count_value(conf.count_unit_arg, ui->files,
601 formated_file_count);
602 format_size_value(conf.size_unit_arg, ui->bytes,
603 formated_bytes);
604 printf("%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
605 formated_dir_count,
606 formated_file_count,
607 formated_bytes
608 );
609 }
610 }
611
612 enum user_stats_flags {
613 USF_PRINT_DIRNAME = 1,
614 USF_PRINT_BYTES = 2,
615 USF_PRINT_FILES = 4,
616 USF_COMPUTE_SUMMARY = 8,
617 };
618
619 struct user_stats_info {
620 uint32_t count;
621 enum user_stats_flags flags;
622 struct user_info *ui;
623 };
624
625 static int user_stats_loop_function(struct osl_row *row, void *data)
626 {
627 struct user_stats_info *usi = data;
628 struct osl_row *dir_row;
629 struct osl_object obj;
630 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
631 char formated_value[25];
632
633 if (!usi->count && !summary)
634 return -E_LOOP_COMPLETE;
635 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
636 char *dirname;
637 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
638 if (ret < 0)
639 return ret;
640 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
641 if (ret < 0)
642 return ret;
643 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
644 if (ret < 0)
645 return ret;
646 dirname = obj.data;
647 printf("%s%s",
648 dirname,
649 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
650 "\t" : "\n"
651 );
652 }
653 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
654 uint64_t files;
655 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
656 if (ret < 0)
657 return ret;
658 files = *(uint64_t *)obj.data;
659 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
660 format_size_value(conf.size_unit_arg, files,
661 formated_value);
662 printf("%s%s", formated_value,
663 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
664 );
665 }
666 if (summary)
667 usi->ui->files += files;
668 }
669 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
670 uint64_t bytes;
671 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
672 if (ret < 0)
673 return ret;
674 bytes = *(uint64_t *)obj.data;
675 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
676 format_size_value(conf.size_unit_arg, bytes,
677 formated_value);
678 printf("%s\n", formated_value);
679 }
680 if (summary) {
681 usi->ui->bytes += bytes;
682 usi->ui->dirs++;
683 }
684
685 }
686 if (usi->count > 0)
687 usi->count--;
688 return 1;
689 }
690
691 static void print_user_stats(void)
692 {
693 struct user_info *ui;
694
695 FOR_EACH_USER(ui) {
696 struct user_stats_info usi = {
697 .count = conf.limit_arg,
698 .ui = ui
699 };
700 if (!ui->table)
701 continue;
702 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
703 printf("************************************************ uid %u\n",
704 (unsigned) ui->uid);
705 if (!ui->table)
706 continue;
707 printf("----------------- Largest dirs -------------------\n");
708 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
709 user_stats_loop_function);
710 printf("---------- dirs containing most files ------------\n");
711 usi.count = conf.limit_arg,
712 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
713 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
714 user_stats_loop_function);
715 }
716 }
717
718 static int print_statistics(void)
719 {
720 int ret;
721 struct global_stats_info gsi = {
722 .count = conf.limit_arg,
723 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
724 };
725
726 printf("----------------- Largest dirs -------------------\n");
727 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
728 global_stats_loop_function);
729 if (ret < 0 && ret != -E_LOOP_COMPLETE)
730 return ret;
731 gsi.count = conf.limit_arg;
732
733 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
734 printf("---------- dirs containing most files ------------\n");
735 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
736 global_stats_loop_function);
737 if (ret < 0 && ret != -E_LOOP_COMPLETE)
738 return ret;
739
740 printf("------------------ Global summary (dirs/files/bytes)\n"
741 "%llu\t%llu\t%llu\n",
742 (long long unsigned)num_dirs, (long long unsigned)num_files,
743 (long long unsigned)num_bytes);
744 print_user_stats();
745 print_id_stats();
746 return 1;
747 }
748
749 static char *get_uid_list_name(void)
750 {
751 return make_message("%s/uid_list", conf.database_dir_arg);
752 }
753
754 static int write_uid_list(void)
755 {
756 char *buf, *filename;
757 uint32_t count = 0;
758 struct user_info *ui;
759 size_t size = num_uids * sizeof(uint32_t);
760 int ret;
761
762 if (!num_uids)
763 return 0;
764 buf = para_malloc(size);
765 FOR_EACH_USER(ui) {
766 if (!ui->table)
767 continue;
768 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
769 }
770 filename = get_uid_list_name();
771 ret = para_write_file(filename, buf, size);
772 free(filename);
773 free(buf);
774 return ret;
775 }
776
777 static int open_dir_table(void)
778 {
779 if (!dir_table_desc.dir) /* we did not create the table */
780 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
781 return osl_open_table(&dir_table_desc, &dir_table);
782 }
783
784 static void close_dir_table(void)
785 {
786 int ret;
787
788 if (!dir_table)
789 return;
790 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
791 if (ret < 0)
792 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
793 free((char *)dir_table_desc.dir);
794 dir_table = NULL;
795 }
796
797 static void close_user_table(struct user_info *ui)
798 {
799 int ret;
800
801 if (!ui || !ui->table)
802 return;
803 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
804 if (ret < 0)
805 ERROR_LOG("failed to close user table %u: %s\n",
806 (unsigned) ui->uid, error_txt(-ret));
807 free((char *)ui->desc->name);
808 ui->desc->name = NULL;
809 free((char *)ui->desc->dir);
810 ui->desc->dir = NULL;
811 free(ui->desc);
812 ui->desc = NULL;
813 ui->table = NULL;
814 }
815
816 static void close_user_tables(void)
817 {
818 struct user_info *ui;
819
820 FOR_EACH_USER(ui)
821 close_user_table(ui);
822 }
823
824 static void close_all_tables(void)
825 {
826 close_dir_table();
827 close_user_tables();
828 free_hash_table();
829 }
830
831 static int com_create()
832 {
833 int ret = create_tables();
834 if (ret < 0)
835 return ret;
836 ret = open_dir_table();
837 if (ret < 0)
838 return ret;
839 ret = scan_dir(conf.base_dir_arg);
840 if (ret < 0)
841 goto out;
842 ret = write_uid_list();
843 out:
844 close_all_tables();
845 return ret;
846 }
847
848 static int read_uid_file(void)
849 {
850 size_t size;
851 uint32_t n;
852 char *filename = get_uid_list_name(), *map;
853 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
854
855 free(filename);
856 if (ret < 0)
857 return ret;
858 num_uids = size / 4;
859 /* hash table size should be a power of two and larger than the number of uids */
860 uid_hash_table_size = 4;
861 while (uid_hash_table_size < num_uids)
862 uid_hash_table_size *= 2;
863 create_hash_table();
864 for (n = 0; n < num_uids; n++) {
865 uint32_t uid = read_u32(map + n * sizeof(uid));
866 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
867 if (ret < 0)
868 goto out;
869 }
870 out:
871 para_munmap(map, size);
872 return ret;
873 }
874
875 static int com_select(void)
876 {
877 int ret;
878
879 ret = open_dir_table();
880 if (ret < 0)
881 return ret;
882 ret = read_uid_file();
883 if (ret < 0)
884 return ret;
885 print_statistics();
886 close_all_tables();
887 return 1;
888 }
889
890 int main(int argc, char **argv)
891 {
892 int ret;
893 struct cmdline_parser_params params = {
894 .override = 0,
895 .initialize = 1,
896 .check_required = 0,
897 .check_ambiguity = 0,
898 .print_errors = 1
899 };
900
901 cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
902 ret = -E_SYNTAX;
903 if (conf.select_given)
904 ret = com_select();
905 else
906 ret = com_create();
907 if (ret < 0)
908 goto out;
909 out:
910 if (ret < 0) {
911 ERROR_LOG("%s\n", error_txt(-ret));
912 return -EXIT_FAILURE;
913 }
914 return EXIT_SUCCESS;
915 }