]> git.tuebingen.mpg.de Git - adu.git/blob - adu.c
97d55e115774a1373791b5a4fa0f7c1e89df8510
[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 int osl_errno;
13
14 /** Command line and config file options. */
15 static struct gengetopt_args_info conf;
16
17 enum uid_info_flags {
18         /** whether this slot of the hash table is used. */
19         UI_FL_SLOT_USED = 1,
20         /** whether this uid should be taken into account. */
21         UI_FL_ADMISSIBLE = 2,
22 };
23
24 struct user_info {
25         uint32_t uid;
26         uint32_t flags;
27         struct osl_table *table;
28         uint64_t files;
29         uint64_t bytes;
30         uint64_t dirs;
31         struct osl_table_description *desc;
32 };
33
34 /** The decimal representation of an uint64_t never exceeds that size. */
35 #define FORMATED_VALUE_SIZE 25
36
37 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
38                 + uid_hash_table_size; ui++)
39
40
41 /**
42  * Contains info for each user that owns at least one regular file.
43  *
44  * Even users that are not taken into account because of the --uid
45  * option occupy a slot in this hash table. This allows to find out
46  * quicky whether a uid is admissible. And yes, this has to be fast.
47  */
48 static struct user_info *uid_hash_table;
49
50 /* these get filled in by the select command. */
51 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
52
53 static inline int ui_used(struct user_info *ui)
54 {
55         return ui->flags & UI_FL_SLOT_USED;
56 }
57
58 static inline int ui_admissible(struct user_info *ui)
59 {
60         return ui->flags & UI_FL_ADMISSIBLE;
61 }
62
63 struct uid_range {
64         uint32_t low;
65         uint32_t high;
66 };
67
68 static struct uid_range *admissible_uids;
69
70 static inline int check_uid_arg(const char *arg, uint32_t *uid)
71 {
72         const uint32_t max = ~0U;
73         /*
74          * we need an 64-bit int for string -> uid conversion because strtoll()
75          * returns a signed value.
76          */
77         int64_t val;
78         int ret = atoi64(arg, &val);
79
80         if (ret < 0)
81                 return ret;
82         if (val < 0 || val > max)
83                 return -ERRNO_TO_ERROR(EINVAL);
84         *uid = val;
85         return 1;
86 }
87
88 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
89 {
90         int ret;
91         char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
92
93         if (!p || p == arg) { /* -42 or 42 */
94                 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
95                 if (ret < 0)
96                         goto out;
97                 ur->low = p? 0 : ur->high;
98                 ret = 1;
99                 goto out;
100         }
101         /* 42- or 42-4711 */
102         *p = '\0';
103         p++;
104         ret = check_uid_arg(arg, &ur->low);
105         if (ret < 0)
106                 goto out;
107         ur->high = ~0U;
108         if (*p) { /* 42-4711 */
109                 ret = check_uid_arg(p, &ur->high);
110                 if (ret < 0)
111                         goto out;
112         }
113         if (ur->low > ur->high)
114                 ret = -ERRNO_TO_ERROR(EINVAL);
115 out:
116         if (ret < 0)
117                 ERROR_LOG("bad uid option: %s\n", orig_arg);
118         else
119                 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
120                         ur->high);
121         free(arg);
122         return ret;
123 }
124
125 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
126 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
127
128 /**
129  * The log function.
130  *
131  * \param ll Loglevel.
132  * \param fml Usual format string.
133  *
134  * All XXX_LOG() macros use this function.
135  */
136 __printf_2_3 void __log(int ll, const char* fmt,...)
137 {
138         va_list argp;
139         FILE *outfd;
140         struct tm *tm;
141         time_t t1;
142         char str[255] = "";
143
144         if (ll < conf.loglevel_arg)
145                 return;
146         outfd = stderr;
147         time(&t1);
148         tm = localtime(&t1);
149         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
150         fprintf(outfd, "%s ", str);
151         va_start(argp, fmt);
152         vfprintf(outfd, fmt, argp);
153         va_end(argp);
154 }
155
156 /**
157  * Compare the size of two directories
158  *
159  * \param obj1 Pointer to the first object.
160  * \param obj2 Pointer to the second object.
161  *
162  * This function first compares the size values as usual integers. If they compare as
163  * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
164  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
165  */
166 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
167 {
168         uint64_t d1 = *(uint64_t *)obj1->data;
169         uint64_t d2 = *(uint64_t *)obj2->data;
170         int ret = NUM_COMPARE(d2, d1);
171
172         if (ret)
173                 return ret;
174         //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
175         return NUM_COMPARE(obj2->data, obj1->data);
176 }
177
178 /**
179  * Compare two osl objects pointing to unsigned integers of 64 bit size.
180  *
181  * \param obj1 Pointer to the first integer.
182  * \param obj2 Pointer to the second integer.
183  *
184  * \return The values required for an osl compare function.
185  *
186  * \sa osl_compare_func, osl_hash_compare().
187  */
188 static int uint64_compare(const struct osl_object *obj1,
189                 const struct osl_object *obj2)
190 {
191         uint64_t d1 = read_u64((const char *)obj1->data);
192         uint64_t d2 = read_u64((const char *)obj2->data);
193
194         if (d1 < d2)
195                 return 1;
196         if (d1 > d2)
197                 return -1;
198         return 0;
199 }
200
201 /** The columns of the directory table. */
202 enum dir_table_columns {
203         /** The name of the directory. */
204         DT_NAME,
205         /** The dir count number. */
206         DT_NUM,
207         /** The number of the parent directory. */
208         DT_PARENT_NUM,
209         /** The number of bytes of all regular files. */
210         DT_BYTES,
211         /** The number of all regular files. */
212         DT_FILES,
213         /** Number of columns in this table. */
214         NUM_DT_COLUMNS
215 };
216
217 static struct osl_column_description dir_table_cols[] = {
218         [DT_NAME] = {
219                 .storage_type = OSL_MAPPED_STORAGE,
220                 .storage_flags = 0,
221                 .name = "dir",
222         },
223         [DT_NUM] = {
224                 .storage_type = OSL_MAPPED_STORAGE,
225                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
226                 .name = "num",
227                 .compare_function = uint64_compare,
228                 .data_size = sizeof(uint64_t)
229         },
230         [DT_PARENT_NUM] = {
231                 .storage_type = OSL_MAPPED_STORAGE,
232                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
233                 .name = "parent_num",
234                 .compare_function = size_compare,
235                 .data_size = sizeof(uint64_t)
236         },
237         [DT_BYTES] = {
238                 .storage_type = OSL_MAPPED_STORAGE,
239                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
240                 .compare_function = size_compare,
241                 .name = "num_bytes",
242                 .data_size = sizeof(uint64_t)
243         },
244         [DT_FILES] = {
245                 .storage_type = OSL_MAPPED_STORAGE,
246                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
247                 .compare_function = size_compare,
248                 .name = "num_files",
249                 .data_size = sizeof(uint64_t)
250         }
251 };
252
253 static struct osl_table_description dir_table_desc = {
254         .name = "dir_table",
255         .num_columns = NUM_DT_COLUMNS,
256         .flags = 0,
257         .column_descriptions = dir_table_cols,
258 };
259
260 /** The columns of the id table. */
261 enum user_table_columns {
262         /** The numer of the directory. */
263         UT_DIR_NUM,
264         /** The number of bytes of all regular files in this dir owned by this id. */
265         UT_BYTES,
266         /** The number of files in this dir owned by this id. */
267         UT_FILES,
268         /** Number of columns in this table. */
269         NUM_UT_COLUMNS
270 };
271
272 static struct osl_column_description user_table_cols[] = {
273         [UT_DIR_NUM] = {
274                 .storage_type = OSL_MAPPED_STORAGE,
275                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
276                 .name = "dir_num",
277                 .compare_function = uint64_compare,
278                 .data_size = sizeof(uint64_t)
279         },
280         [UT_BYTES] = {
281                 .storage_type = OSL_MAPPED_STORAGE,
282                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
283                 .compare_function = size_compare,
284                 .name = "num_bytes",
285                 .data_size = sizeof(uint64_t)
286         },
287         [UT_FILES] = {
288                 .storage_type = OSL_MAPPED_STORAGE,
289                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
290                 .compare_function = size_compare,
291                 .name = "num_files",
292                 .data_size = sizeof(uint64_t)
293         },
294 };
295
296 static struct osl_table *dir_table;
297
298 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
299                 uint64_t *dir_size, uint64_t *dir_files)
300 {
301         struct osl_object dir_objects[NUM_DT_COLUMNS];
302
303         INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
304         dir_objects[DT_NAME].data = dirname;
305         dir_objects[DT_NAME].size = strlen(dirname) + 1;
306         dir_objects[DT_NUM].data = dir_num;
307         dir_objects[DT_NUM].size = sizeof(*dir_num);
308         dir_objects[DT_PARENT_NUM].data = parent_dir_num;
309         dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
310         dir_objects[DT_BYTES].data = dir_size;
311         dir_objects[DT_BYTES].size = sizeof(*dir_size);
312         dir_objects[DT_FILES].data = dir_files;
313         dir_objects[DT_FILES].size = sizeof(*dir_files);
314         return osl(osl_add_row(dir_table, dir_objects));
315 }
316
317 static uint32_t num_uids;
318
319 static int open_user_table(struct user_info *ui, int create)
320 {
321         int ret;
322
323         ui->desc = adu_malloc(sizeof(*ui->desc));
324         ui->desc->num_columns = NUM_UT_COLUMNS;
325         ui->desc->flags = 0;
326         ui->desc->column_descriptions = user_table_cols;
327         ui->desc->dir = adu_strdup(conf.database_dir_arg);
328         ui->desc->name = make_message("%u", (unsigned)ui->uid);
329         INFO_LOG(".............................uid #%u: %u\n",
330                 (unsigned)num_uids, (unsigned)ui->uid);
331         if (create) {
332                 ret = osl(osl_create_table(ui->desc));
333                 if (ret < 0)
334                         goto err;
335                 num_uids++;
336         }
337         ret = osl(osl_open_table(ui->desc, &ui->table));
338         if (ret < 0)
339                 goto err;
340         return 1;
341 err:
342         free((char *)ui->desc->name);
343         free((char *)ui->desc->dir);
344         free(ui->desc);
345         ui->desc->name = NULL;
346         ui->desc->dir = NULL;
347         ui->desc = NULL;
348         ui->table = NULL;
349         ui->flags = 0;
350         return ret;
351 }
352
353 #define uid_hash_bits 8
354 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
355 #define PRIME1 0x811c9dc5
356 #define PRIME2 0x01000193
357
358 static void create_hash_table(void)
359 {
360         uid_hash_table = adu_calloc(uid_hash_table_size
361                 * sizeof(struct user_info));
362 }
363
364 static void free_hash_table(void)
365 {
366         free(uid_hash_table);
367         uid_hash_table = NULL;
368 }
369
370 static int create_tables(void)
371 {
372         int ret;
373
374         dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
375         ret = osl(osl_create_table(&dir_table_desc));
376         if (ret < 0)
377                 return ret;
378         create_hash_table();
379         return 1;
380 }
381
382 static void close_dir_table(void)
383 {
384         int ret;
385
386         if (!dir_table)
387                 return;
388         ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
389         if (ret < 0)
390                 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
391         free((char *)dir_table_desc.dir);
392         dir_table = NULL;
393 }
394
395 static void close_user_table(struct user_info *ui)
396 {
397         int ret;
398
399         if (!ui || !ui_used(ui) || !ui_admissible(ui))
400                 return;
401         ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
402         if (ret < 0)
403                 ERROR_LOG("failed to close user table %u: %s\n",
404                         (unsigned) ui->uid, adu_strerror(-ret));
405         free((char *)ui->desc->name);
406         ui->desc->name = NULL;
407         free((char *)ui->desc->dir);
408         ui->desc->dir = NULL;
409         free(ui->desc);
410         ui->desc = NULL;
411         ui->table = NULL;
412         ui->flags = 0;
413 }
414
415 static void close_user_tables(void)
416 {
417         struct user_info *ui;
418
419         FOR_EACH_USER(ui)
420                 close_user_table(ui);
421 }
422
423 static void close_all_tables(void)
424 {
425         close_dir_table();
426         close_user_tables();
427         free_hash_table();
428 }
429
430 static int signum;
431
432 static void signal_handler(int s)
433 {
434         signum = s;
435 }
436
437 static void check_signals(void)
438 {
439         if (likely(!signum))
440                 return;
441         EMERG_LOG("caught signal %d\n", signum);
442         close_all_tables();
443         exit(EXIT_FAILURE);
444 }
445
446 static int init_signals(void)
447 {
448         if (signal(SIGINT, &signal_handler) == SIG_ERR)
449                 return -E_SIGNAL_SIG_ERR;
450         if (signal(SIGTERM, &signal_handler) == SIG_ERR)
451                 return -E_SIGNAL_SIG_ERR;
452         return 1;
453 }
454
455 /*
456  * We use a hash table of size s=2^uid_hash_bits to map the uids into the
457  * interval [0..s]. Hash collisions are treated by open addressing, i.e.
458  * unused slots in the table are used to store different uids that hash to the
459  * same slot.
460  *
461  * If a hash collision occurs, different slots are successively probed in order
462  * to find an unused slot for the new uid. Probing is implemented via a second
463  * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
464  * odd number.
465  *
466  * An odd number is sufficient to make sure each entry of the hash table gets
467  * probed for probe_num between 0 and s-1 because s is a power of two, hence
468  * the second hash value has never a common divisor with the hash table size.
469  * IOW: h is invertible in the ring [0..s].
470  */
471 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
472 {
473         return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
474                 % uid_hash_table_size;
475 }
476
477 enum search_uid_flags {
478         OPEN_USER_TABLE = 1,
479         CREATE_USER_TABLE = 2,
480 };
481
482 static int uid_is_admissible(uint32_t uid)
483 {
484         int i;
485
486         for (i = 0; i < conf.uid_given; i++) {
487                 struct uid_range *ur = admissible_uids + i;
488
489                 if (ur->low <= uid && ur->high >= uid)
490                         break;
491         }
492         i = !conf.uid_given || i < conf.uid_given;
493         DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
494                 i? "" : "not ");
495         return i;
496 }
497
498 static int search_uid(uint32_t uid, enum search_uid_flags flags,
499                 struct user_info **ui_ptr)
500 {
501         uint32_t p;
502
503         for (p = 0; p < uid_hash_table_size; p++) {
504                 struct user_info *ui = uid_hash_table + double_hash(uid, p);
505
506                 if (!ui_used(ui)) {
507                         int ret;
508                         if (!flags)
509                                 return -E_BAD_UID;
510                         ui->uid = uid;
511                         ui->flags |= UI_FL_SLOT_USED;
512                         if (!uid_is_admissible(uid))
513                                 return 0;
514                         ui->flags |= UI_FL_ADMISSIBLE;
515                         ret = open_user_table(ui, flags & CREATE_USER_TABLE);
516                         if (ret < 0)
517                                 return ret;
518
519                         if (ui_ptr)
520                                 *ui_ptr = ui;
521                         return 1;
522                 }
523                 if (ui->uid != uid)
524                         continue;
525                 if (ui_ptr)
526                         *ui_ptr = ui;
527                 return 0;
528         }
529         return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
530 }
531
532 static int update_user_row(struct osl_table *t, uint64_t dir_num,
533                 uint64_t *add)
534 {
535         struct osl_row *row;
536         struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
537
538         int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
539
540         if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
541                 return ret;
542         if (ret < 0) { /* this is the first file we add */
543                 struct osl_object objects[NUM_UT_COLUMNS];
544                 uint64_t num_files = 1;
545
546                 objects[UT_DIR_NUM].data = &dir_num;
547                 objects[UT_DIR_NUM].size = sizeof(dir_num);
548                 objects[UT_BYTES].data = add;
549                 objects[UT_BYTES].size = sizeof(*add);
550                 objects[UT_FILES].data = &num_files;
551                 objects[UT_FILES].size = sizeof(num_files);
552                 INFO_LOG("######################### ret: %d\n", ret);
553                 ret = osl(osl_add_row(t, objects));
554                 INFO_LOG("######################### ret: %d\n", ret);
555                 return ret;
556         } else { /* add size and increment file count */
557                 uint64_t num;
558                 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
559
560                 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
561                 if (ret < 0)
562                         return ret;
563                 num = *(uint64_t *)obj1.data + *add;
564                 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
565                 if (ret < 0)
566                         return ret;
567                 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
568                 if (ret < 0)
569                         return ret;
570                 num = *(uint64_t *)obj1.data + 1;
571                 return osl(osl_update_object(t, row, UT_FILES, &obj2));
572         }
573 }
574
575 static uint64_t num_dirs;
576 static uint64_t num_files;
577 static uint64_t num_bytes;
578
579 static int scan_dir(char *dirname, uint64_t *parent_dir_num)
580 {
581         DIR *dir;
582         struct dirent *entry;
583         int ret, cwd_fd, ret2;
584         uint64_t dir_size = 0, dir_files = 0;
585         uint64_t this_dir_num = ++num_dirs;
586
587         check_signals();
588         DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
589         ret = adu_opendir(dirname, &dir, &cwd_fd);
590         if (ret < 0) {
591                 if (ret != -ERRNO_TO_ERROR(EACCES))
592                         return ret;
593                 WARNING_LOG("permission denied for %s\n", dirname);
594                 return 1;
595         }
596         while ((entry = readdir(dir))) {
597                 mode_t m;
598                 struct stat s;
599                 uint32_t uid;
600                 uint64_t size;
601                 struct user_info *ui;
602
603                 if (!strcmp(entry->d_name, "."))
604                         continue;
605                 if (!strcmp(entry->d_name, ".."))
606                         continue;
607                 if (lstat(entry->d_name, &s) == -1) {
608                         WARNING_LOG("lstat error for %s/%s\n", dirname,
609                                 entry->d_name);
610                         continue;
611                 }
612                 m = s.st_mode;
613                 if (!S_ISREG(m) && !S_ISDIR(m))
614                         continue;
615                 if (S_ISDIR(m)) {
616                         ret = scan_dir(entry->d_name, &this_dir_num);
617                         if (ret < 0)
618                                 goto out;
619                         continue;
620                 }
621                 /* regular file */
622                 size = s.st_size;
623                 dir_size += size;
624                 num_bytes += size;
625                 dir_files++;
626                 num_files++;
627                 uid = s.st_uid;
628                 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
629                 if (ret < 0)
630                         goto out;
631                 ui->bytes += size;
632                 ui->files++;
633                 ret = update_user_row(ui->table, this_dir_num, &size);
634                 if (ret < 0)
635                         goto out;
636         }
637         ret = add_directory(dirname, &this_dir_num, parent_dir_num,
638                         &dir_size, &dir_files);
639 out:
640         closedir(dir);
641         ret2 = adu_fchdir(cwd_fd);
642         if (ret2 < 0 && ret >= 0)
643                 ret = ret2;
644         close(cwd_fd);
645         return ret;
646 }
647
648 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
649 {
650         char *result = NULL, *tmp;
651         struct osl_row *row;
652         uint64_t val = *dirnum;
653         struct osl_object obj = {.data = &val, .size = sizeof(val)};
654         int ret;
655
656 again:
657         ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
658         if (ret < 0)
659                 goto out;
660         ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
661         if (ret < 0)
662                 goto out;
663         if (result) {
664                 tmp = make_message("%s/%s", (char *)obj.data, result);
665                 free(result);
666                 result = tmp;
667         } else
668                 result = adu_strdup((char *)obj.data);
669         ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
670         if (ret < 0)
671                 goto out;
672         val = *(uint64_t *)obj.data;
673         if (val)
674                 goto again;
675 out:
676         if (ret < 0) {
677                 free(result);
678                 *name = NULL;
679         } else
680                 *name = result;
681         return ret;
682 }
683
684 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
685 {
686         struct osl_object obj;
687         int ret;
688         char *this_dir, *prefix = NULL;
689
690         *name = NULL;
691         ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
692         if (ret < 0)
693                 return ret;
694         this_dir = adu_strdup((char *)obj.data);
695         ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
696         if (ret < 0)
697                 goto out;
698         if (!*(uint64_t *)obj.data) {
699                 *name = this_dir;
700                 return 1;
701         }
702         ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
703         if (ret < 0)
704                 goto out;
705         *name = make_message("%s/%s", prefix, this_dir);
706         free(prefix);
707         ret = 1;
708 out:
709         free(this_dir);
710         return ret;
711 }
712
713 const uint64_t size_unit_divisors[] = {
714         [size_unit_arg_b] = 1ULL,
715         [size_unit_arg_k] = 1024ULL,
716         [size_unit_arg_m] = 1024ULL * 1024ULL,
717         [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
718         [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
719 };
720
721 const uint64_t count_unit_divisors[] = {
722
723         [count_unit_arg_n] = 1ULL,
724         [count_unit_arg_k] = 1000ULL,
725         [count_unit_arg_m] = 1000ULL * 1000ULL,
726         [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
727         [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
728 };
729
730 const char size_unit_abbrevs[] = " BKMGT";
731 const char count_unit_abbrevs[] = "  kmgt";
732
733 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
734                 uint64_t value, int print_unit, char *result)
735 {
736         enum enum_size_unit u = unit;
737         char unit_buf[2] = "\0\0";
738
739         if (unit == size_unit_arg_h) /* human readable */
740                 for (u = size_unit_arg_b; u < size_unit_arg_t &&
741                                 value > size_unit_divisors[u + 1]; u++)
742                         ; /* nothing */
743         if (print_unit)
744                 unit_buf[0] = size_unit_abbrevs[u];
745         sprintf(result, "%llu%s",
746                 (long long unsigned)value / size_unit_divisors[u], unit_buf);
747         return u;
748 }
749
750 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
751                 uint64_t value, int print_unit, char *result)
752 {
753         enum enum_count_unit u = unit;
754         char unit_buf[2] = "\0\0";
755
756         if (unit == count_unit_arg_h) /* human readable */
757                 for (u = count_unit_arg_n; u < count_unit_arg_t &&
758                                 value > count_unit_divisors[u + 1]; u++)
759                         ; /* nothing */
760         if (print_unit)
761                 unit_buf[0] = count_unit_abbrevs[u];
762         sprintf(result, "%llu%s",
763                 (long long unsigned)value / count_unit_divisors[u], unit_buf);
764         return u;
765 }
766
767 enum global_stats_flags {
768         GSF_PRINT_DIRNAME = 1,
769         GSF_PRINT_BYTES = 2,
770         GSF_PRINT_FILES = 4,
771         GSF_COMPUTE_SUMMARY = 8,
772 };
773
774 struct global_stats_info {
775         uint32_t count;
776         int ret;
777         int osl_errno;
778         enum global_stats_flags flags;
779 };
780
781 static int global_stats_loop_function(struct osl_row *row, void *data)
782 {
783         struct global_stats_info *gsi = data;
784         struct osl_object obj;
785         char *dirname, formated_value[FORMATED_VALUE_SIZE];
786         int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
787
788         check_signals();
789         if (!gsi->count && !summary) {
790                 ret = -E_LOOP_COMPLETE;
791                 goto err;
792         }
793         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
794                 uint64_t files;
795                 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
796                 if (ret < 0)
797                         goto err;
798                 files = *(uint64_t *)obj.data;
799                 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
800                         format_count_value(conf.count_unit_arg, files,
801                                 conf.count_unit_arg == count_unit_arg_h,
802                                 formated_value);
803                         printf("\t%s%s", formated_value,
804                                 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
805                                 "\t" : "\n");
806                 }
807                 if (summary)
808                         num_files += files;
809         }
810         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
811                 uint64_t bytes;
812                 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
813                 if (ret < 0)
814                         goto err;
815                 bytes = *(uint64_t *)obj.data;
816                 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
817                         format_size_value(conf.size_unit_arg, bytes,
818                                 conf.size_unit_arg == size_unit_arg_h,
819                                 formated_value);
820                         printf("%s%s%s",
821                                 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
822                                 formated_value,
823                                 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
824                         );
825                 }
826                 if (summary) {
827                         num_bytes += bytes;
828                         num_dirs++;
829                 }
830         }
831         if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
832                 ret = get_dir_name_of_row(row, &dirname);
833                 if (ret < 0)
834                         goto err;
835                 printf("%s%s\n",
836                         (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
837                         dirname);
838                 free(dirname);
839         }
840         if (gsi->count > 0)
841                 gsi->count--;
842         return 1;
843 err:
844         gsi->ret = ret;
845         gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
846         return -1;
847 }
848
849 static void print_id_stats(void)
850 {
851         struct user_info *ui;
852
853         printf("User summary "
854                 "(uid/dirs%s/files%s/size%s):\n",
855                 count_unit_buf, count_unit_buf, size_unit_buf);
856         FOR_EACH_USER(ui) {
857                 char formated_dir_count[FORMATED_VALUE_SIZE],
858                         formated_file_count[FORMATED_VALUE_SIZE],
859                         formated_bytes[FORMATED_VALUE_SIZE ];
860                 if (!ui_used(ui) || !ui_admissible(ui))
861                         continue;
862                 format_count_value(conf.count_unit_arg, ui->dirs,
863                         conf.count_unit_arg == count_unit_arg_h,
864                         formated_dir_count);
865                 format_count_value(conf.count_unit_arg, ui->files,
866                         conf.count_unit_arg == count_unit_arg_h,
867                         formated_file_count);
868                 format_size_value(conf.size_unit_arg, ui->bytes,
869                         conf.size_unit_arg == size_unit_arg_h,
870                         formated_bytes);
871                 printf("\t%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
872                         formated_dir_count,
873                         formated_file_count,
874                         formated_bytes
875                 );
876         }
877 }
878
879 enum user_stats_flags {
880         USF_PRINT_DIRNAME = 1,
881         USF_PRINT_BYTES = 2,
882         USF_PRINT_FILES = 4,
883         USF_COMPUTE_SUMMARY = 8,
884 };
885
886 struct user_stats_info {
887         uint32_t count;
888         enum user_stats_flags flags;
889         int ret;
890         int osl_errno;
891         struct user_info *ui;
892 };
893
894 static int user_stats_loop_function(struct osl_row *row, void *data)
895 {
896         struct user_stats_info *usi = data;
897         struct osl_object obj;
898         int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
899         char formated_value[FORMATED_VALUE_SIZE];
900
901         check_signals();
902         if (!usi->count && !summary) {
903                 ret = -E_LOOP_COMPLETE;
904                 goto err;
905         }
906         if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
907                 uint64_t files;
908                 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
909                 if (ret < 0)
910                         goto err;
911                 files = *(uint64_t *)obj.data;
912                 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
913                         format_count_value(conf.count_unit_arg, files,
914                                 conf.count_unit_arg == count_unit_arg_h,
915                                 formated_value);
916                         printf("\t%s%s", formated_value,
917                                 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
918                                         "\t" : "\n"
919                         );
920                 }
921                 if (summary)
922                         usi->ui->files += files;
923         }
924         if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
925                 uint64_t bytes;
926                 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
927                 if (ret < 0)
928                         goto err;
929                 bytes = *(uint64_t *)obj.data;
930                 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
931                         format_size_value(conf.size_unit_arg, bytes,
932                                 conf.size_unit_arg == size_unit_arg_h,
933                                 formated_value);
934                         printf("%s%s%s",
935                                 (usi->flags & USF_PRINT_FILES)? "" : "\t",
936                                 formated_value,
937                                 usi->flags & USF_PRINT_DIRNAME?  "\t" : "\n"
938                         );
939                 }
940                 if (summary) {
941                         usi->ui->bytes += bytes;
942                         usi->ui->dirs++;
943                 }
944
945         }
946         if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
947                 char *dirname;
948                 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
949                 if (ret < 0)
950                         goto err;
951                 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
952                 if (ret < 0)
953                         goto err;
954                 printf("%s%s\n",
955                         (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
956                         dirname);
957                 free(dirname);
958         }
959         if (usi->count > 0)
960                 usi->count--;
961         return 1;
962 err:
963         usi->ret = ret;
964         usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
965         return -1;
966 }
967
968 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
969 {
970         if (ret >= 0)
971                 return ret;
972         assert(ret == -E_OSL);
973         if (osl_errno != E_OSL_LOOP)
974                 /* error not caused by loop function returning negative. */
975                 return ret;
976         assert(loop_ret < 0);
977         if (loop_ret == -E_LOOP_COMPLETE) /* no error */
978                 return 1;
979         if (loop_ret == -E_OSL) { /* osl error in loop function */
980                 assert(loop_osl_errno);
981                 osl_errno = loop_osl_errno;
982         }
983         return loop_ret;
984 }
985
986 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
987                 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
988 {
989         int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
990         return check_loop_return(ret, *loop_ret, *loop_osl_errno);
991 }
992
993 static int print_user_stats(void)
994 {
995         struct user_info *ui;
996         int ret;
997
998         FOR_EACH_USER(ui) {
999                 struct user_stats_info usi = {
1000                         .count = conf.limit_arg,
1001                         .ui = ui
1002                 };
1003                 if (!ui_used(ui) || !ui_admissible(ui))
1004                         continue;
1005                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
1006                 printf("uid %u, by size%s:\n",
1007                         (unsigned) ui->uid, size_unit_buf);
1008                 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
1009                         &usi.ret, &usi.osl_errno);
1010                 if (ret < 0)
1011                         return ret;
1012                 printf("\nuid %u, by file count%s:\n",
1013                         (unsigned) ui->uid, count_unit_buf);
1014                 usi.count = conf.limit_arg,
1015                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
1016                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
1017                         &usi.ret, &usi.osl_errno);
1018                 if (ret < 0)
1019                         return ret;
1020                 printf("\n");
1021         }
1022         return 1;
1023 }
1024
1025 static void print_global_summary(void)
1026 {
1027         char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
1028                 s[FORMATED_VALUE_SIZE];
1029         enum enum_count_unit ud, uf;
1030         enum enum_size_unit us;
1031
1032         ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d);
1033         uf = format_count_value(conf.count_unit_arg, num_files, 0, f);
1034         us = format_size_value(conf.size_unit_arg, num_bytes, 0, s);
1035
1036         printf("Global summary "
1037                 "(dirs(%c)/files(%c)/size(%c))\n"
1038                 "\t%s\t%s\t%s\n\n",
1039                 count_unit_abbrevs[ud],
1040                 count_unit_abbrevs[uf],
1041                 size_unit_abbrevs[us],
1042                 d, f, s
1043         );
1044
1045 }
1046
1047 static int print_statistics(void)
1048 {
1049         int ret;
1050         struct global_stats_info gsi = {
1051                 .count = conf.limit_arg,
1052                 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
1053         };
1054
1055         printf("By size%s:\n",
1056                 size_unit_buf);
1057         ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
1058                 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
1059         if (ret < 0)
1060                 return ret;
1061         printf("\n");
1062
1063         gsi.count = conf.limit_arg;
1064         gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
1065         printf("By file count%s:\n",
1066                 count_unit_buf);
1067         ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
1068                 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
1069         if (ret < 0)
1070                 return ret;
1071         printf("\n");
1072         print_global_summary();
1073         print_user_stats();
1074         print_id_stats();
1075         return 1;
1076 }
1077
1078 static char *get_uid_list_name(void)
1079 {
1080         return make_message("%s/uid_list", conf.database_dir_arg);
1081 }
1082
1083 static int write_uid_list(void)
1084 {
1085         char *buf, *filename;
1086         uint32_t count = 0;
1087         struct user_info *ui;
1088         size_t size = num_uids * sizeof(uint32_t);
1089         int ret;
1090
1091         if (!num_uids)
1092                 return 0;
1093         buf = adu_malloc(size);
1094         FOR_EACH_USER(ui) {
1095                 if (!ui_used(ui) || !ui_admissible(ui))
1096                         continue;
1097                 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
1098                 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
1099         }
1100         filename = get_uid_list_name();
1101         ret = adu_write_file(filename, buf, size);
1102         free(filename);
1103         free(buf);
1104         return ret;
1105 }
1106
1107 static int open_dir_table(void)
1108 {
1109         if (!dir_table_desc.dir) /* we did not create the table */
1110                 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
1111         return osl(osl_open_table(&dir_table_desc, &dir_table));
1112 }
1113 static int com_create()
1114 {
1115         uint64_t zero = 0ULL;
1116         int ret = create_tables();
1117
1118         if (ret < 0)
1119                 return ret;
1120         check_signals();
1121         ret = open_dir_table();
1122         if (ret < 0)
1123                 return ret;
1124         check_signals();
1125         ret = scan_dir(conf.base_dir_arg, &zero);
1126         if (ret < 0)
1127                 goto out;
1128         ret = write_uid_list();
1129 out:
1130         close_all_tables();
1131         return ret;
1132 }
1133
1134 static int read_uid_file(void)
1135 {
1136         size_t size;
1137         uint32_t n;
1138         char *filename = get_uid_list_name(), *map;
1139         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
1140
1141         if (ret < 0) {
1142                 INFO_LOG("failed to map %s\n", filename);
1143                 free(filename);
1144                 return ret;
1145         }
1146         num_uids = size / 4;
1147         INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
1148         free(filename);
1149         /* hash table size should be a power of two and larger than the number of uids */
1150         uid_hash_table_size = 4;
1151         while (uid_hash_table_size < num_uids)
1152                 uid_hash_table_size *= 2;
1153         create_hash_table();
1154         for (n = 0; n < num_uids; n++) {
1155                 uint32_t uid = read_u32(map + n * sizeof(uid));
1156                 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
1157                 if (ret < 0)
1158                         goto out;
1159         }
1160 out:
1161         adu_munmap(map, size);
1162         return ret;
1163 }
1164
1165 static int com_select(void)
1166 {
1167         int ret;
1168
1169         if (conf.count_unit_arg != count_unit_arg_h)
1170                 count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
1171         else
1172                 count_unit_buf[0] = '\0';
1173         if (conf.size_unit_arg != size_unit_arg_h)
1174                 size_unit_buf[1] = size_unit_abbrevs[conf.size_unit_arg];
1175         else
1176                 size_unit_buf[0] = '\0';
1177
1178         ret = open_dir_table();
1179         if (ret < 0)
1180                 return ret;
1181         check_signals();
1182         ret = read_uid_file();
1183         if (ret < 0)
1184                 return ret;
1185         check_signals();
1186         ret = print_statistics();
1187         close_all_tables();
1188         return ret;
1189 }
1190
1191 static int check_args(void)
1192 {
1193         int i, ret;
1194
1195         /* remove trailing slashes from base-dir arg */
1196         if (conf.base_dir_given) {
1197                 size_t len = strlen(conf.base_dir_arg);
1198                 for (;;) {
1199                         if (!len) /* empty string */
1200                                 return -ERRNO_TO_ERROR(EINVAL);
1201                         if (!--len) /* length 1 is always OK */
1202                                 break;
1203                         if (conf.base_dir_arg[len] != '/')
1204                                 break; /* no trailing slash, also OK */
1205                         conf.base_dir_arg[len] = '\0';
1206                 }
1207         }
1208         if (!conf.uid_given)
1209                 return 0;
1210         admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
1211         for (i = 0; i < conf.uid_given; i++) {
1212                 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
1213                 if (ret < 0)
1214                         goto err;
1215         }
1216         return 1;
1217 err:
1218         free(admissible_uids);
1219         admissible_uids = NULL;
1220         return ret;
1221 }
1222
1223 int main(int argc, char **argv)
1224 {
1225         int ret;
1226         struct cmdline_parser_params params = {
1227                 .override = 0,
1228                 .initialize = 1,
1229                 .check_required = 0,
1230                 .check_ambiguity = 0,
1231                 .print_errors = 1
1232         };
1233
1234         cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
1235         ret = init_signals();
1236         if (ret < 0)
1237                 goto out;
1238         ret = check_args();
1239         if (ret < 0)
1240                 goto out;
1241         ret = -E_SYNTAX;
1242         if (conf.select_given)
1243                 ret = com_select();
1244         else
1245                 ret = com_create();
1246         if (ret < 0)
1247                 goto out;
1248 out:
1249         free(admissible_uids);
1250         if (ret < 0) {
1251                 ERROR_LOG("%s\n", adu_strerror(-ret));
1252                 return -EXIT_FAILURE;
1253         }
1254         return EXIT_SUCCESS;
1255 }