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