Add documentation for double_hash().
[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 < conf.loglevel_arg)
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 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
304 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
305 * unused slots in the table are used to store different uids that hash to the
306 * same slot.
307 *
308 * If a hash collision occurs, different slots are successively probed in order
309 * to find an unused slot for the new uid. Probing is implemented via a second
310 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
311 * odd number.
312 *
313 * An odd number is sufficient to make sure each entry of the hash table gets
314 * probed for probe_num between 0 and s-1 because s is a power of two, hence
315 * the second hash value never hash a common divisor with the hash table size.
316 * IOW: h is invertible in the ring [0..s].
317 */
318 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
319 {
320 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
321 % uid_hash_table_size;
322 }
323
324 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
325 + uid_hash_table_size; ui++)
326
327 enum search_uid_flags {
328 OPEN_USER_TABLE = 1,
329 CREATE_USER_TABLE = 2,
330 };
331
332 static int search_uid(uint32_t uid, enum search_uid_flags flags,
333 struct user_info **ui_ptr)
334 {
335 uint32_t p;
336
337 for (p = 0; p < uid_hash_table_size; p++) {
338 struct user_info *ui = uid_hash_table + double_hash(uid, p);
339
340 if (!ui->table) {
341 int ret;
342
343 if (!flags)
344 return -E_BAD_UID;
345 ui->uid = uid;
346 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
347 if (ret < 0)
348 return ret;
349 if (ui_ptr)
350 *ui_ptr = ui;
351 return 1;
352 }
353 if (ui->uid != uid)
354 continue;
355 if (ui_ptr)
356 *ui_ptr = ui;
357 return 0;
358 }
359 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
360 }
361
362 static int update_user_row(struct osl_table *t, uint64_t dir_num,
363 uint64_t *add)
364 {
365 struct osl_row *row;
366 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
367
368 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
369
370 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
371 return ret;
372 if (ret < 0) { /* this is the first file we add */
373 struct osl_object objects[NUM_UT_COLUMNS];
374 uint64_t num_files = 1;
375
376 objects[UT_DIR_NUM].data = &dir_num;
377 objects[UT_DIR_NUM].size = sizeof(dir_num);
378 objects[UT_BYTES].data = add;
379 objects[UT_BYTES].size = sizeof(*add);
380 objects[UT_FILES].data = &num_files;
381 objects[UT_FILES].size = sizeof(num_files);
382 INFO_LOG("######################### ret: %d\n", ret);
383 ret = osl_add_row(t, objects);
384 INFO_LOG("######################### ret: %d\n", ret);
385 return ret;
386 } else { /* add size and increment file count */
387 uint64_t num;
388 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
389
390 ret = osl_get_object(t, row, UT_BYTES, &obj1);
391 if (ret < 0)
392 return ret;
393 num = *(uint64_t *)obj1.data + *add;
394 ret = osl_update_object(t, row, UT_BYTES, &obj2);
395 if (ret < 0)
396 return ret;
397 ret = osl_get_object(t, row, UT_FILES, &obj1);
398 if (ret < 0)
399 return ret;
400 num = *(uint64_t *)obj1.data + 1;
401 return osl_update_object(t, row, UT_FILES, &obj2);
402 }
403 }
404
405 static uint64_t num_dirs;
406 static uint64_t num_files;
407 static uint64_t num_bytes;
408
409 int scan_dir(char *dirname)
410 {
411 DIR *dir;
412 struct dirent *entry;
413 int ret, cwd_fd, ret2;
414 uint64_t dir_size = 0, dir_files = 0;
415 uint64_t this_dir_num = num_dirs++;
416
417 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
418 ret = para_opendir(dirname, &dir, &cwd_fd);
419 if (ret < 0) {
420 if (ret != -ERRNO_TO_ERROR(EACCES))
421 return ret;
422 WARNING_LOG("permission denied for %s\n", dirname);
423 return 1;
424 }
425 while ((entry = readdir(dir))) {
426 mode_t m;
427 char *tmp;
428 struct stat s;
429 uint32_t uid;
430 uint64_t size;
431 struct user_info *ui;
432
433 if (!strcmp(entry->d_name, "."))
434 continue;
435 if (!strcmp(entry->d_name, ".."))
436 continue;
437 if (lstat(entry->d_name, &s) == -1) {
438 WARNING_LOG("lstat error for %s/%s\n", dirname,
439 entry->d_name);
440 continue;
441 }
442 m = s.st_mode;
443 if (!S_ISREG(m) && !S_ISDIR(m))
444 continue;
445 if (S_ISDIR(m)) {
446 tmp = make_message("%s/%s", dirname, entry->d_name);
447 ret = scan_dir(tmp);
448 free(tmp);
449 if (ret < 0)
450 goto out;
451 continue;
452 }
453 /* regular file */
454 size = s.st_size;
455 dir_size += size;
456 num_bytes += size;
457 dir_files++;
458 num_files++;
459 uid = s.st_uid;
460 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
461 if (ret < 0)
462 goto out;
463 ui->bytes += size;
464 ui->files++;
465 ret = update_user_row(ui->table, this_dir_num, &size);
466 if (ret < 0)
467 goto out;
468 }
469 ret = add_directory(dirname, this_dir_num, &dir_size, &dir_files);
470 out:
471 closedir(dir);
472 ret2 = para_fchdir(cwd_fd);
473 if (ret2 < 0 && ret >= 0)
474 ret = ret2;
475 close(cwd_fd);
476 return ret;
477 }
478
479 static int get_dir_name(struct osl_row *row, char **name)
480 {
481 struct osl_object obj;
482 int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
483
484 if (ret < 0)
485 return ret;
486 *name = obj.data;
487 return 1;
488 }
489
490 enum global_stats_flags {
491 GSF_PRINT_DIRNAME = 1,
492 GSF_PRINT_BYTES = 2,
493 GSF_PRINT_FILES = 4,
494 GSF_COMPUTE_SUMMARY = 8,
495 };
496
497 struct global_stats_info {
498 uint32_t count;
499 enum global_stats_flags flags;
500 };
501
502 static int global_stats_loop_function(struct osl_row *row, void *data)
503 {
504 struct global_stats_info *gsi = data;
505 struct osl_object obj;
506 char *dirname;
507 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
508
509 if (!gsi->count && !summary)
510 return -E_LOOP_COMPLETE;
511 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
512 ret = get_dir_name(row, &dirname);
513 if (ret < 0)
514 return ret;
515 printf("%s%s", dirname,
516 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
517 "\t" : "\n"
518 );
519 }
520 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
521 uint64_t files;
522 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
523 if (ret < 0)
524 return ret;
525 files = *(uint64_t *)obj.data;
526 if (gsi->count && (gsi->flags & GSF_PRINT_FILES))
527 printf("%llu%s", (long long unsigned)files,
528 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
529 if (summary)
530 num_files += files;
531 }
532 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
533 uint64_t bytes;
534 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
535 if (ret < 0)
536 return ret;
537 bytes = *(uint64_t *)obj.data;
538 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES))
539 printf("%llu\n", (long long unsigned)bytes);
540 if (summary) {
541 num_bytes += bytes;
542 num_dirs++;
543 }
544 }
545 if (gsi->count)
546 gsi->count--;
547 return 1;
548 }
549
550 static void print_id_stats(void)
551 {
552 struct user_info *ui;
553
554 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
555 FOR_EACH_USER(ui) {
556 if (!ui->table)
557 continue;
558 printf("%u\t%llu\t%llu\t%llu\n", (unsigned)ui->uid,
559 (long long unsigned)ui->dirs,
560 (long long unsigned)ui->files,
561 (long long unsigned)ui->bytes);
562 }
563 }
564
565 enum user_stats_flags {
566 USF_PRINT_DIRNAME = 1,
567 USF_PRINT_BYTES = 2,
568 USF_PRINT_FILES = 4,
569 USF_COMPUTE_SUMMARY = 8,
570 };
571
572 struct user_stats_info {
573 uint32_t count;
574 enum user_stats_flags flags;
575 struct user_info *ui;
576 };
577
578 static int user_stats_loop_function(struct osl_row *row, void *data)
579 {
580 struct user_stats_info *usi = data;
581 struct osl_row *dir_row;
582 struct osl_object obj;
583 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
584
585 if (!usi->count && !summary)
586 return -E_LOOP_COMPLETE;
587 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
588 char *dirname;
589 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
590 if (ret < 0)
591 return ret;
592 ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
593 if (ret < 0)
594 return ret;
595 ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
596 if (ret < 0)
597 return ret;
598 dirname = obj.data;
599 printf("%s%s",
600 dirname,
601 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
602 "\t" : "\n"
603 );
604 }
605 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
606 uint64_t files;
607 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
608 if (ret < 0)
609 return ret;
610 files = *(uint64_t *)obj.data;
611 if (usi->count && (usi->flags & USF_PRINT_FILES))
612 printf("%llu%s",
613 (long long unsigned)files,
614 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
615 );
616 if (summary)
617 usi->ui->files += files;
618 }
619 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
620 uint64_t bytes;
621 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
622 if (ret < 0)
623 return ret;
624 bytes = *(uint64_t *)obj.data;
625 if (usi->count && (usi->flags & USF_PRINT_BYTES))
626 printf("%llu\n", (long long unsigned)bytes);
627 if (summary) {
628 usi->ui->bytes += bytes;
629 usi->ui->dirs++;
630 }
631
632 }
633 if (usi->count)
634 usi->count--;
635 return 1;
636 }
637
638 static void print_user_stats(void)
639 {
640 struct user_info *ui;
641
642 FOR_EACH_USER(ui) {
643 struct user_stats_info usi = {
644 .count = 10,
645 .ui = ui
646 };
647 if (!ui->table)
648 continue;
649 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
650 printf("************************************************ uid %u\n",
651 (unsigned) ui->uid);
652 if (!ui->table)
653 continue;
654 printf("----------------- Largest dirs -------------------\n");
655 osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
656 user_stats_loop_function);
657 printf("---------- dirs containing most files ------------\n");
658 usi.count = 10;
659 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
660 osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
661 user_stats_loop_function);
662 }
663 }
664
665 static int print_statistics(void)
666 {
667 int ret;
668 struct global_stats_info gsi = {
669 .count = 10,
670 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
671 };
672
673 printf("----------------- Largest dirs -------------------\n");
674 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
675 global_stats_loop_function);
676 if (ret < 0 && ret != -E_LOOP_COMPLETE)
677 return ret;
678 gsi.count = 10;
679
680 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
681 printf("---------- dirs containing most files ------------\n");
682 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
683 global_stats_loop_function);
684 if (ret < 0 && ret != -E_LOOP_COMPLETE)
685 return ret;
686
687 printf("------------------ Global summary (dirs/files/bytes)\n"
688 "%llu\t%llu\t%llu\n",
689 (long long unsigned)num_dirs, (long long unsigned)num_files,
690 (long long unsigned)num_bytes);
691 print_user_stats();
692 print_id_stats();
693 return 1;
694 }
695
696 static int write_uid_list(void)
697 {
698 char *buf;
699 uint32_t count = 0;
700 struct user_info *ui;
701 size_t size = num_uids * sizeof(uint32_t);
702 int ret;
703
704 if (!num_uids)
705 return 0;
706 buf = para_malloc(size);
707 FOR_EACH_USER(ui) {
708 if (!ui->table)
709 continue;
710 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
711 }
712 ret = para_write_file(UID_LIST, buf, size);
713 free(buf);
714 return ret;
715 }
716
717 static int open_dir_table(void)
718 {
719 return osl_open_table(&dir_table_desc, &dir_table);
720 }
721
722 static void close_dir_table(void)
723 {
724 int ret;
725
726 if (!dir_table)
727 return;
728 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
729 if (ret < 0)
730 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
731 dir_table = NULL;
732 }
733
734 static void close_user_table(struct user_info *ui)
735 {
736 int ret;
737
738 if (!ui || !ui->table)
739 return;
740 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
741 if (ret < 0)
742 ERROR_LOG("failed to close user table %u: %s\n",
743 (unsigned) ui->uid, error_txt(-ret));
744 free((char *)ui->desc->name);
745 ui->desc->name = NULL;
746 free((char *)ui->desc->dir);
747 ui->desc->dir = NULL;
748 free(ui->desc);
749 ui->desc = NULL;
750 ui->table = NULL;
751 }
752
753 static void close_user_tables(void)
754 {
755 struct user_info *ui;
756
757 FOR_EACH_USER(ui)
758 close_user_table(ui);
759 }
760
761 static void close_all_tables(void)
762 {
763 close_dir_table();
764 close_user_tables();
765 free_hash_table();
766 }
767
768 static int com_create()
769 {
770 int ret = create_tables();
771 if (ret < 0)
772 return ret;
773 ret = open_dir_table();
774 if (ret < 0)
775 return ret;
776 ret = scan_dir(conf.base_dir_arg);
777 if (ret < 0)
778 goto out;
779 ret = write_uid_list();
780 out:
781 close_all_tables();
782 return ret;
783 }
784
785 static int read_uid_file(void)
786 {
787 char *map;
788 size_t size;
789 int ret = mmap_full_file(UID_LIST, O_RDONLY, (void **)&map, &size, NULL);
790 uint32_t n;
791
792 if (ret < 0)
793 return ret;
794 num_uids = size / 4;
795 /* hash table size should be a power of two and larger than the number of uids */
796 uid_hash_table_size = 4;
797 while (uid_hash_table_size < num_uids)
798 uid_hash_table_size *= 2;
799 create_hash_table();
800 for (n = 0; n < num_uids; n++) {
801 uint32_t uid = read_u32(map + n * sizeof(uid));
802 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
803 if (ret < 0)
804 goto out;
805 }
806 out:
807 para_munmap(map, size);
808 return ret;
809 }
810
811 static int com_select(void)
812 {
813 int ret;
814
815 ret = open_dir_table();
816 if (ret < 0)
817 return ret;
818 ret = read_uid_file();
819 if (ret < 0)
820 return ret;
821 print_statistics();
822 close_all_tables();
823 return 1;
824 }
825
826 int main(int argc, char **argv)
827 {
828 int ret;
829 struct cmdline_parser_params params = {
830 .override = 0,
831 .initialize = 1,
832 .check_required = 0,
833 .check_ambiguity = 0,
834 .print_errors = 1
835 };
836
837 cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
838 ret = -E_SYNTAX;
839 if (conf.select_given)
840 ret = com_select();
841 else
842 ret = com_create();
843 if (ret < 0)
844 goto out;
845 out:
846 if (ret < 0) {
847 ERROR_LOG("%s\n", error_txt(-ret));
848 return -EXIT_FAILURE;
849 }
850 return EXIT_SUCCESS;
851 }