]> git.tuebingen.mpg.de Git - adu.git/blob - adu.c
Implement the select command.
[adu.git] / adu.c
1 #include "adu.h"
2 #include <dirent.h> /* readdir() */
3
4 #include "gcc-compat.h"
5 #include "osl.h"
6 #include "fd.h"
7 #include "hash.h"
8 #include "string.h"
9 #include "error.h"
10
11 DEFINE_ERRLIST;
12
13 #define DATABASE_DIR "/tmp/adu"
14 #define UID_LIST DATABASE_DIR "/" "uid_list"
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 < 4)
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         .dir = DATABASE_DIR
177 };
178
179 /** The columns of the id table. */
180 enum user_table_columns {
181         /** The numer of the directory. */
182         UT_DIR_NUM,
183         /** The number of bytes of all regular files in this dir owned by this id. */
184         UT_BYTES,
185         /** The number of files in this dir owned by this id. */
186         UT_FILES,
187         /** Number of columns in this table. */
188         NUM_UT_COLUMNS
189 };
190
191 static struct osl_column_description user_table_cols[] = {
192         [UT_DIR_NUM] = {
193                 .storage_type = OSL_MAPPED_STORAGE,
194                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
195                 .name = "dir_num",
196                 .compare_function = uint64_compare,
197                 .data_size = sizeof(uint64_t)
198         },
199         [UT_BYTES] = {
200                 .storage_type = OSL_MAPPED_STORAGE,
201                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
202                 .compare_function = size_compare,
203                 .name = "num_bytes",
204                 .data_size = sizeof(uint64_t)
205         },
206         [UT_FILES] = {
207                 .storage_type = OSL_MAPPED_STORAGE,
208                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
209                 .compare_function = size_compare,
210                 .name = "num_files",
211                 .data_size = sizeof(uint64_t)
212         },
213 };
214
215 static struct osl_table *dir_table;
216
217 static int add_directory(char *dirname, uint64_t dir_num, uint64_t *dir_size,
218                 uint64_t *dir_files)
219 {
220         struct osl_object dir_objects[NUM_DT_COLUMNS];
221
222         INFO_LOG("adding #%llu: %s\n", (long long unsigned)dir_num, dirname);
223         dir_objects[DT_NAME].data = dirname;
224         dir_objects[DT_NAME].size = strlen(dirname) + 1;
225         dir_objects[DT_NUM].data = &dir_num;
226         dir_objects[DT_NUM].size = sizeof(dir_num);
227         dir_objects[DT_BYTES].data = dir_size;
228         dir_objects[DT_BYTES].size = sizeof(*dir_size);
229         dir_objects[DT_FILES].data = dir_files;
230         dir_objects[DT_FILES].size = sizeof(*dir_files);
231
232         return osl_add_row(dir_table, dir_objects);
233 }
234
235 static uint32_t num_uids;
236
237 static int open_user_table(struct user_info *ui, int create)
238 {
239         int ret;
240
241         ui->desc = para_malloc(sizeof(*ui->desc));
242         ui->desc->num_columns = NUM_UT_COLUMNS;
243         ui->desc->flags = 0;
244         ui->desc->column_descriptions = user_table_cols;
245         ui->desc->dir = para_strdup(DATABASE_DIR);
246         ui->desc->name = make_message("%u", (unsigned)ui->uid);
247         num_uids++;
248         INFO_LOG(".............................uid #%u: %u\n",
249                 (unsigned)num_uids, (unsigned)ui->uid);
250         if (create) {
251                 ret = osl_create_table(ui->desc);
252                 if (ret < 0)
253                         goto err;
254         }
255         ret = osl_open_table(ui->desc, &ui->table);
256         if (ret < 0)
257                 goto err;
258         return 1;
259 err:
260         free((char *)ui->desc->name);
261         free((char *)ui->desc->dir);
262         free(ui->desc);
263         ui->desc->name = NULL;
264         ui->desc->dir = NULL;
265         ui->desc = NULL;
266         ui->table = NULL;
267         return ret;
268 }
269
270 #define uid_hash_bits 8
271 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
272 #define PRIME1 0x811c9dc5
273 #define PRIME2 0x01000193
274
275 static void create_hash_table(void)
276 {
277         uid_hash_table = para_calloc(uid_hash_table_size
278                 * sizeof(struct user_info));
279 }
280
281 static void free_hash_table(void)
282 {
283         free(uid_hash_table);
284         uid_hash_table = NULL;
285 }
286
287 static int create_tables(void)
288 {
289         int ret;
290
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 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
300 {
301         return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
302                 % uid_hash_table_size;
303 }
304
305 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
306                 + uid_hash_table_size; ui++)
307
308 enum search_uid_flags {
309         OPEN_USER_TABLE = 1,
310         CREATE_USER_TABLE = 2,
311 };
312
313 static int search_uid(uint32_t uid, enum search_uid_flags flags,
314                 struct user_info **ui_ptr)
315 {
316         uint32_t p;
317
318         for (p = 0; p < uid_hash_table_size; p++) {
319                 struct user_info *ui = uid_hash_table + double_hash(uid, p);
320
321                 if (!ui->table) {
322                         int ret;
323
324                         if (!flags)
325                                 return -E_BAD_UID;
326                         ui->uid = uid;
327                         ret = open_user_table(ui, flags & CREATE_USER_TABLE);
328                         if (ret < 0)
329                                 return ret;
330                         if (ui_ptr)
331                                 *ui_ptr = ui;
332                         return 1;
333                 }
334                 if (ui->uid != uid)
335                         continue;
336                 if (ui_ptr)
337                         *ui_ptr = ui;
338                 return 0;
339         }
340         return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
341 }
342
343 static int update_user_row(struct osl_table *t, uint64_t dir_num,
344                 uint64_t *add)
345 {
346         struct osl_row *row;
347         struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
348
349         int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
350
351         if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
352                 return ret;
353         if (ret < 0) { /* this is the first file we add */
354                 struct osl_object objects[NUM_UT_COLUMNS];
355                 uint64_t num_files = 1;
356
357                 objects[UT_DIR_NUM].data = &dir_num;
358                 objects[UT_DIR_NUM].size = sizeof(dir_num);
359                 objects[UT_BYTES].data = add;
360                 objects[UT_BYTES].size = sizeof(*add);
361                 objects[UT_FILES].data = &num_files;
362                 objects[UT_FILES].size = sizeof(num_files);
363                 INFO_LOG("######################### ret: %d\n", ret);
364                 ret = osl_add_row(t, objects);
365                 INFO_LOG("######################### ret: %d\n", ret);
366                 return ret;
367         } else { /* add size and increment file count */
368                 uint64_t num;
369                 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
370
371                 ret = osl_get_object(t, row, UT_BYTES, &obj1);
372                 if (ret < 0)
373                         return ret;
374                 num = *(uint64_t *)obj1.data + *add;
375                 ret = osl_update_object(t, row, UT_BYTES, &obj2);
376                 if (ret < 0)
377                         return ret;
378                 ret = osl_get_object(t, row, UT_FILES, &obj1);
379                 if (ret < 0)
380                         return ret;
381                 num = *(uint64_t *)obj1.data + 1;
382                 return osl_update_object(t, row, UT_FILES, &obj2);
383         }
384 }
385
386 static uint64_t num_dirs;
387 static uint64_t num_files;
388 static uint64_t num_bytes;
389
390 int scan_dir(char *dirname)
391 {
392         DIR *dir;
393         struct dirent *entry;
394         int ret, cwd_fd, ret2;
395         uint64_t dir_size = 0, dir_files = 0;
396         uint64_t this_dir_num = num_dirs++;
397
398         DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
399         ret = para_opendir(dirname, &dir, &cwd_fd);
400         if (ret < 0) {
401                 if (ret != -ERRNO_TO_ERROR(EACCES))
402                         return ret;
403                 WARNING_LOG("permission denied for %s\n", dirname);
404                 return 1;
405         }
406         while ((entry = readdir(dir))) {
407                 mode_t m;
408                 char *tmp;
409                 struct stat s;
410                 uint32_t uid;
411                 uint64_t size;
412                 struct user_info *ui;
413
414                 if (!strcmp(entry->d_name, "."))
415                         continue;
416                 if (!strcmp(entry->d_name, ".."))
417                         continue;
418                 if (lstat(entry->d_name, &s) == -1) {
419                         WARNING_LOG("lstat error for %s/%s\n", dirname,
420                                 entry->d_name);
421                         continue;
422                 }
423                 m = s.st_mode;
424                 if (!S_ISREG(m) && !S_ISDIR(m))
425                         continue;
426                 if (S_ISDIR(m)) {
427                         tmp = make_message("%s/%s", dirname, entry->d_name);
428                         ret = scan_dir(tmp);
429                         free(tmp);
430                         if (ret < 0)
431                                 goto out;
432                         continue;
433                 }
434                 /* regular file */
435                 size = s.st_size;
436                 dir_size += size;
437                 num_bytes += size;
438                 dir_files++;
439                 num_files++;
440                 uid = s.st_uid;
441                 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
442                 if (ret < 0)
443                         goto out;
444                 ui->bytes += size;
445                 ui->files++;
446                 ret = update_user_row(ui->table, this_dir_num, &size);
447                 if (ret < 0)
448                         goto out;
449         }
450         ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
451 out:
452         closedir(dir);
453         ret2 = para_fchdir(cwd_fd);
454         if (ret2 < 0 && ret >= 0)
455                 ret = ret2;
456         close(cwd_fd);
457         return ret;
458 }
459
460 static int get_dir_name(struct osl_row *row, char **name)
461 {
462         struct osl_object obj;
463         int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
464
465         if (ret < 0)
466                 return ret;
467         *name = obj.data;
468         return 1;
469 }
470
471 enum global_stats_flags {
472         GSF_PRINT_DIRNAME = 1,
473         GSF_PRINT_BYTES = 2,
474         GSF_PRINT_FILES = 4,
475         GSF_COMPUTE_SUMMARY = 8,
476 };
477
478 struct global_stats_info {
479         uint32_t count;
480         enum global_stats_flags flags;
481 };
482
483 static int global_stats_loop_function(struct osl_row *row, void *data)
484 {
485         struct global_stats_info *gsi = data;
486         struct osl_object obj;
487         char *dirname;
488         int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
489
490         if (!gsi->count && !summary)
491                 return -E_LOOP_COMPLETE;
492         if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
493                 ret = get_dir_name(row, &dirname);
494                 if (ret < 0)
495                         return ret;
496                 printf("%s%s", dirname,
497                         (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
498                                 "\t" : "\n"
499                 );
500         }
501         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
502                 uint64_t files;
503                 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
504                 if (ret < 0)
505                         return ret;
506                 files = *(uint64_t *)obj.data;
507                 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
508                         printf("%llu%s", (long long unsigned)files,
509                                 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
510                 if (summary)
511                         num_files += files;
512         }
513         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
514                 uint64_t bytes;
515                 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
516                 if (ret < 0)
517                         return ret;
518                 bytes = *(uint64_t *)obj.data;
519                 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
520                         printf("%llu\n", (long long unsigned)bytes);
521                 if (summary) {
522                         num_bytes += bytes;
523                         num_dirs++;
524                 }
525         }
526         if (gsi->count)
527                 gsi->count--;
528         return 1;
529 }
530
531 static void print_id_stats(void)
532 {
533         struct user_info *ui;
534
535         printf("--------------------- user summary (uid/dirs/files/bytes):\n");
536         FOR_EACH_USER(ui) {
537                 if (!ui->table)
538                         continue;
539                 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
540                         (long long unsigned)ui->dirs,
541                         (long long unsigned)ui->files,
542                         (long long unsigned)ui->bytes);
543         }
544 }
545
546 enum user_stats_flags {
547         USF_PRINT_DIRNAME = 1,
548         USF_PRINT_BYTES = 2,
549         USF_PRINT_FILES = 4,
550         USF_COMPUTE_SUMMARY = 8,
551 };
552
553 struct user_stats_info {
554         uint32_t count;
555         enum user_stats_flags flags;
556         struct user_info *ui;
557 };
558
559 static int user_stats_loop_function(struct osl_row *row, void *data)
560 {
561         struct user_stats_info *usi = data;
562         struct osl_row *dir_row;
563         struct osl_object obj;
564         int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
565
566         if (!usi->count && !summary)
567                 return -E_LOOP_COMPLETE;
568         if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
569                 char *dirname;
570                 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
571                 if (ret < 0)
572                         return ret;
573                 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
574                 if (ret < 0)
575                         return ret;
576                 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
577                 if (ret < 0)
578                         return ret;
579                 dirname = obj.data;
580                 printf("%s%s",
581                         dirname,
582                         (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
583                                 "\t" : "\n"
584                 );
585         }
586         if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
587                 uint64_t files;
588                 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
589                 if (ret < 0)
590                         return ret;
591                 files = *(uint64_t *)obj.data;
592                 if (usi->count && (usi->flags & USF_PRINT_FILES))
593                         printf("%llu%s",
594                                 (long long unsigned)files,
595                                 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
596                         );
597                 if (summary)
598                         usi->ui->files += files;
599         }
600         if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
601                 uint64_t bytes;
602                 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
603                 if (ret < 0)
604                         return ret;
605                 bytes = *(uint64_t *)obj.data;
606                 if (usi->count && (usi->flags & USF_PRINT_BYTES))
607                         printf("%llu\n", (long long unsigned)bytes);
608                 if (summary) {
609                         usi->ui->bytes += bytes;
610                         usi->ui->dirs++;
611                 }
612
613         }
614         if (usi->count)
615                 usi->count--;
616         return 1;
617 }
618
619 static void print_user_stats(void)
620 {
621         struct user_info *ui;
622
623         FOR_EACH_USER(ui) {
624                 struct user_stats_info usi = {
625                         .count = 10,
626                         .ui = ui
627                 };
628                 if (!ui->table)
629                         continue;
630                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
631                 printf("************************************************ uid %u\n",
632                         (unsigned) ui->uid);
633                 if (!ui->table)
634                         continue;
635                 printf("----------------- Largest dirs -------------------\n");
636                 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
637                         user_stats_loop_function);
638                 printf("---------- dirs containing most files ------------\n");
639                 usi.count = 10;
640                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
641                 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
642                         user_stats_loop_function);
643         }
644 }
645
646 static int print_statistics(void)
647 {
648         int ret;
649         struct global_stats_info gsi = {
650                 .count = 10,
651                 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
652         };
653
654         printf("----------------- Largest dirs -------------------\n");
655         ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
656                 global_stats_loop_function);
657         if (ret < 0 && ret != -E_LOOP_COMPLETE)
658                 return ret;
659         gsi.count = 10;
660
661         gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
662         printf("---------- dirs containing most files ------------\n");
663         ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
664                 global_stats_loop_function);
665         if (ret < 0 && ret != -E_LOOP_COMPLETE)
666                 return ret;
667
668         printf("------------------ Global summary (dirs/files/bytes)\n"
669                 "%llu\t%llu\t%llu\n",
670                 (long long unsigned)num_dirs, (long long unsigned)num_files,
671                 (long long unsigned)num_bytes);
672         print_user_stats();
673         print_id_stats();
674         return 1;
675 }
676
677 static int write_uid_list(void)
678 {
679         char *buf;
680         uint32_t count = 0;
681         struct user_info *ui;
682         size_t size = num_uids * sizeof(uint32_t);
683         int ret;
684
685         if (!num_uids)
686                 return 0;
687         buf = para_malloc(size);
688         FOR_EACH_USER(ui) {
689                 if (!ui->table)
690                         continue;
691                 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
692         }
693         ret = para_write_file(UID_LIST, buf, size);
694         free(buf);
695         return ret;
696 }
697
698 static int open_dir_table(void)
699 {
700         return osl_open_table(&dir_table_desc, &dir_table);
701 }
702
703 static void close_dir_table(void)
704 {
705         int ret;
706
707         if (!dir_table)
708                 return;
709         ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
710         if (ret < 0)
711                 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
712         dir_table = NULL;
713 }
714
715 static void close_user_table(struct user_info *ui)
716 {
717         int ret;
718
719         if (!ui || !ui->table)
720                 return;
721         ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
722         if (ret < 0)
723                 ERROR_LOG("failed to close user table %u: %s\n",
724                         (unsigned) ui->uid, error_txt(-ret));
725         free((char *)ui->desc->name);
726         ui->desc->name = NULL;
727         free((char *)ui->desc->dir);
728         ui->desc->dir = NULL;
729         free(ui->desc);
730         ui->desc = NULL;
731         ui->table = NULL;
732 }
733
734 static void close_user_tables(void)
735 {
736         struct user_info *ui;
737
738         FOR_EACH_USER(ui)
739                 close_user_table(ui);
740 }
741
742 static void close_all_tables(void)
743 {
744         close_dir_table();
745         close_user_tables();
746         free_hash_table();
747 }
748
749 static int com_create(char *dirname)
750 {
751         int ret = create_tables();
752         if (ret < 0)
753                 return ret;
754         ret = open_dir_table();
755         if (ret < 0)
756                 return ret;
757         ret = scan_dir(dirname);
758         if (ret < 0)
759                 goto out;
760         ret = write_uid_list();
761 out:
762         close_all_tables();
763         return ret;
764 }
765
766 static int read_uid_file(void)
767 {
768         char *map;
769         size_t size;
770         int ret = mmap_full_file(UID_LIST, O_RDONLY, (void **)&map, &size, NULL);
771         uint32_t n;
772
773         if (ret < 0)
774                 return ret;
775         num_uids = size / 4;
776         /* hash table size should be a power of two and larger than the number of uids */
777         uid_hash_table_size = 4;
778         while (uid_hash_table_size < num_uids)
779                 uid_hash_table_size *= 2;
780         create_hash_table();
781         for (n = 0; n < num_uids; n++) {
782                 uint32_t uid = read_u32(map + n * sizeof(uid));
783                 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
784                 if (ret < 0)
785                         goto out;
786         }
787 out:
788         para_munmap(map, size);
789         return ret;
790 }
791
792 static int com_select(void)
793 {
794         int ret;
795
796         ret = open_dir_table();
797         if (ret < 0)
798                 return ret;
799         ret = read_uid_file();
800         if (ret < 0)
801                 return ret;
802         print_statistics();
803         close_all_tables();
804         return 1;
805 }
806
807 int main(int argc, char **argv)
808 {
809         int ret = -E_SYNTAX;
810         if (argc > 2)
811                 goto out;
812         if (argc == 1)
813                 ret = com_select();
814         else
815                 ret = com_create(argv[1]);
816         if (ret < 0)
817                 goto out;
818 out:
819         if (ret < 0) {
820                 ERROR_LOG("%s\n", error_txt(-ret));
821                 return -EXIT_FAILURE;
822         }
823         return EXIT_SUCCESS;
824 }