93305d5ee865ed458b97c5149465d9a603811ea1
[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 enum global_stats_flags {
487         GSF_PRINT_DIRNAME = 1,
488         GSF_PRINT_BYTES = 2,
489         GSF_PRINT_FILES = 4,
490         GSF_COMPUTE_SUMMARY = 8,
491 };
492
493 struct global_stats_info {
494         uint32_t count;
495         enum global_stats_flags flags;
496 };
497
498 static int global_stats_loop_function(struct osl_row *row, void *data)
499 {
500         struct global_stats_info *gsi = data;
501         struct osl_object obj;
502         char *dirname;
503         int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
504
505         if (!gsi->count && !summary)
506                 return -E_LOOP_COMPLETE;
507         if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
508                 ret = get_dir_name(row, &dirname);
509                 if (ret < 0)
510                         return ret;
511                 printf("%s%s", dirname,
512                         (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
513                                 "\t" : "\n"
514                 );
515         }
516         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
517                 uint64_t files;
518                 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
519                 if (ret < 0)
520                         return ret;
521                 files = *(uint64_t *)obj.data;
522                 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
523                         printf("%llu%s", (long long unsigned)files,
524                                 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
525                 if (summary)
526                         num_files += files;
527         }
528         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
529                 uint64_t bytes;
530                 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
531                 if (ret < 0)
532                         return ret;
533                 bytes = *(uint64_t *)obj.data;
534                 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
535                         printf("%llu\n", (long long unsigned)bytes);
536                 if (summary) {
537                         num_bytes += bytes;
538                         num_dirs++;
539                 }
540         }
541         if (gsi->count > 0)
542                 gsi->count--;
543         return 1;
544 }
545
546 static void print_id_stats(void)
547 {
548         struct user_info *ui;
549
550         printf("--------------------- user summary (uid/dirs/files/bytes):\n");
551         FOR_EACH_USER(ui) {
552                 if (!ui->table)
553                         continue;
554                 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
555                         (long long unsigned)ui->dirs,
556                         (long long unsigned)ui->files,
557                         (long long unsigned)ui->bytes);
558         }
559 }
560
561 enum user_stats_flags {
562         USF_PRINT_DIRNAME = 1,
563         USF_PRINT_BYTES = 2,
564         USF_PRINT_FILES = 4,
565         USF_COMPUTE_SUMMARY = 8,
566 };
567
568 struct user_stats_info {
569         uint32_t count;
570         enum user_stats_flags flags;
571         struct user_info *ui;
572 };
573
574 static int user_stats_loop_function(struct osl_row *row, void *data)
575 {
576         struct user_stats_info *usi = data;
577         struct osl_row *dir_row;
578         struct osl_object obj;
579         int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
580
581         if (!usi->count && !summary)
582                 return -E_LOOP_COMPLETE;
583         if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
584                 char *dirname;
585                 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
586                 if (ret < 0)
587                         return ret;
588                 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
589                 if (ret < 0)
590                         return ret;
591                 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
592                 if (ret < 0)
593                         return ret;
594                 dirname = obj.data;
595                 printf("%s%s",
596                         dirname,
597                         (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
598                                 "\t" : "\n"
599                 );
600         }
601         if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
602                 uint64_t files;
603                 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
604                 if (ret < 0)
605                         return ret;
606                 files = *(uint64_t *)obj.data;
607                 if (usi->count && (usi->flags & USF_PRINT_FILES))
608                         printf("%llu%s",
609                                 (long long unsigned)files,
610                                 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
611                         );
612                 if (summary)
613                         usi->ui->files += files;
614         }
615         if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
616                 uint64_t bytes;
617                 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
618                 if (ret < 0)
619                         return ret;
620                 bytes = *(uint64_t *)obj.data;
621                 if (usi->count && (usi->flags & USF_PRINT_BYTES))
622                         printf("%llu\n", (long long unsigned)bytes);
623                 if (summary) {
624                         usi->ui->bytes += bytes;
625                         usi->ui->dirs++;
626                 }
627
628         }
629         if (usi->count > 0)
630                 usi->count--;
631         return 1;
632 }
633
634 static void print_user_stats(void)
635 {
636         struct user_info *ui;
637
638         FOR_EACH_USER(ui) {
639                 struct user_stats_info usi = {
640                         .count = conf.limit_arg,
641                         .ui = ui
642                 };
643                 if (!ui->table)
644                         continue;
645                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
646                 printf("************************************************ uid %u\n",
647                         (unsigned) ui->uid);
648                 if (!ui->table)
649                         continue;
650                 printf("----------------- Largest dirs -------------------\n");
651                 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
652                         user_stats_loop_function);
653                 printf("---------- dirs containing most files ------------\n");
654                 usi.count = conf.limit_arg,
655                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
656                 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
657                         user_stats_loop_function);
658         }
659 }
660
661 static int print_statistics(void)
662 {
663         int ret;
664         struct global_stats_info gsi = {
665                 .count = conf.limit_arg,
666                 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
667         };
668
669         printf("----------------- Largest dirs -------------------\n");
670         ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
671                 global_stats_loop_function);
672         if (ret < 0 && ret != -E_LOOP_COMPLETE)
673                 return ret;
674         gsi.count = conf.limit_arg;
675
676         gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
677         printf("---------- dirs containing most files ------------\n");
678         ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
679                 global_stats_loop_function);
680         if (ret < 0 && ret != -E_LOOP_COMPLETE)
681                 return ret;
682
683         printf("------------------ Global summary (dirs/files/bytes)\n"
684                 "%llu\t%llu\t%llu\n",
685                 (long long unsigned)num_dirs, (long long unsigned)num_files,
686                 (long long unsigned)num_bytes);
687         print_user_stats();
688         print_id_stats();
689         return 1;
690 }
691
692 static char *get_uid_list_name(void)
693 {
694         return make_message("%s/uid_list", conf.database_dir_arg);
695 }
696
697 static int write_uid_list(void)
698 {
699         char *buf, *filename;
700         uint32_t count = 0;
701         struct user_info *ui;
702         size_t size = num_uids * sizeof(uint32_t);
703         int ret;
704
705         if (!num_uids)
706                 return 0;
707         buf = para_malloc(size);
708         FOR_EACH_USER(ui) {
709                 if (!ui->table)
710                         continue;
711                 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
712         }
713         filename = get_uid_list_name();
714         ret = para_write_file(filename, buf, size);
715         free(filename);
716         free(buf);
717         return ret;
718 }
719
720 static int open_dir_table(void)
721 {
722         if (!dir_table_desc.dir) /* we did not create the table */
723                 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
724         return osl_open_table(&dir_table_desc, &dir_table);
725 }
726
727 static void close_dir_table(void)
728 {
729         int ret;
730
731         if (!dir_table)
732                 return;
733         ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
734         if (ret < 0)
735                 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
736         free((char *)dir_table_desc.dir);
737         dir_table = NULL;
738 }
739
740 static void close_user_table(struct user_info *ui)
741 {
742         int ret;
743
744         if (!ui || !ui->table)
745                 return;
746         ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
747         if (ret < 0)
748                 ERROR_LOG("failed to close user table %u: %s\n",
749                         (unsigned) ui->uid, error_txt(-ret));
750         free((char *)ui->desc->name);
751         ui->desc->name = NULL;
752         free((char *)ui->desc->dir);
753         ui->desc->dir = NULL;
754         free(ui->desc);
755         ui->desc = NULL;
756         ui->table = NULL;
757 }
758
759 static void close_user_tables(void)
760 {
761         struct user_info *ui;
762
763         FOR_EACH_USER(ui)
764                 close_user_table(ui);
765 }
766
767 static void close_all_tables(void)
768 {
769         close_dir_table();
770         close_user_tables();
771         free_hash_table();
772 }
773
774 static int com_create()
775 {
776         int ret = create_tables();
777         if (ret < 0)
778                 return ret;
779         ret = open_dir_table();
780         if (ret < 0)
781                 return ret;
782         ret = scan_dir(conf.base_dir_arg);
783         if (ret < 0)
784                 goto out;
785         ret = write_uid_list();
786 out:
787         close_all_tables();
788         return ret;
789 }
790
791 static int read_uid_file(void)
792 {
793         size_t size;
794         uint32_t n;
795         char *filename = get_uid_list_name(), *map;
796         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
797
798         free(filename);
799         if (ret < 0)
800                 return ret;
801         num_uids = size / 4;
802         /* hash table size should be a power of two and larger than the number of uids */
803         uid_hash_table_size = 4;
804         while (uid_hash_table_size < num_uids)
805                 uid_hash_table_size *= 2;
806         create_hash_table();
807         for (n = 0; n < num_uids; n++) {
808                 uint32_t uid = read_u32(map + n * sizeof(uid));
809                 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
810                 if (ret < 0)
811                         goto out;
812         }
813 out:
814         para_munmap(map, size);
815         return ret;
816 }
817
818 static int com_select(void)
819 {
820         int ret;
821
822         ret = open_dir_table();
823         if (ret < 0)
824                 return ret;
825         ret = read_uid_file();
826         if (ret < 0)
827                 return ret;
828         print_statistics();
829         close_all_tables();
830         return 1;
831 }
832
833 int main(int argc, char **argv)
834 {
835         int ret;
836         struct cmdline_parser_params params = {
837                 .override = 0,
838                 .initialize = 1,
839                 .check_required = 0,
840                 .check_ambiguity = 0,
841                 .print_errors = 1
842         };
843
844         cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
845         ret = -E_SYNTAX;
846         if (conf.select_given)
847                 ret = com_select();
848         else
849                 ret = com_create();
850         if (ret < 0)
851                 goto out;
852 out:
853         if (ret < 0) {
854                 ERROR_LOG("%s\n", error_txt(-ret));
855                 return -EXIT_FAILURE;
856         }
857         return EXIT_SUCCESS;
858 }