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