2 #include <dirent.h> /* readdir() */
4 #include "gcc-compat.h"
9 #include "portable_io.h"
14 /** Command line and config file options. */
15 static struct gengetopt_args_info conf;
18 /** whether this slot of the hash table is used. */
20 /** whether this uid should be taken into account. */
27 struct osl_table *table;
31 struct osl_table_description *desc;
35 * Contains info for each user that owns at least one regular file.
37 * Even users that are not taken into account because of the --uid
38 * option occupy a slot in this hash table. This allows to find out
39 * quicky whether a uid is admissible. And yes, this has to be fast.
41 static struct user_info *uid_hash_table;
43 static inline int ui_used(struct user_info *ui)
45 return ui->flags & UI_FL_SLOT_USED;
48 static inline int ui_admissible(struct user_info *ui)
50 return ui->flags & UI_FL_ADMISSIBLE;
58 static struct uid_range *admissible_uids;
60 static inline int check_uid_arg(const char *arg, uint32_t *uid)
62 const uint32_t max = ~0U;
64 * we need an 64-bit int for string -> uid conversion because strtoll()
65 * returns a signed value.
68 int ret = para_atoi64(arg, &val);
72 if (val < 0 || val > max)
73 return -ERRNO_TO_ERROR(EINVAL);
78 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
81 char *arg = para_strdup(orig_arg), *p = strchr(arg, '-');
83 if (!p || p == arg) { /* -42 or 42 */
84 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
87 ur->low = p? 0 : ur->high;
94 ret = check_uid_arg(arg, &ur->low);
98 if (*p) { /* 42-4711 */
99 ret = check_uid_arg(p, &ur->high);
103 if (ur->low > ur->high)
104 ret = -ERRNO_TO_ERROR(EINVAL);
107 ERROR_LOG("bad uid option: %s\n", orig_arg);
109 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
116 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
117 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
122 * \param ll Loglevel.
123 * \param fml Usual format string.
125 * All XXX_LOG() macros use this function.
127 __printf_2_3 void __log(int ll, const char* fmt,...)
135 if (ll < conf.loglevel_arg)
140 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
141 fprintf(outfd, "%s ", str);
143 vfprintf(outfd, fmt, argp);
148 * Compare the size of two directories
150 * \param obj1 Pointer to the first object.
151 * \param obj2 Pointer to the second object.
153 * This function first compares the size values as usual integers. If they compare as
154 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
155 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
157 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
159 uint64_t d1 = *(uint64_t *)obj1->data;
160 uint64_t d2 = *(uint64_t *)obj2->data;
161 int ret = NUM_COMPARE(d2, d1);
165 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
166 return NUM_COMPARE(obj2->data, obj1->data);
170 * Compare two osl objects pointing to unsigned integers of 64 bit size.
172 * \param obj1 Pointer to the first integer.
173 * \param obj2 Pointer to the second integer.
175 * \return The values required for an osl compare function.
177 * \sa osl_compare_func, osl_hash_compare().
179 static int uint64_compare(const struct osl_object *obj1,
180 const struct osl_object *obj2)
182 uint64_t d1 = read_u64((const char *)obj1->data);
183 uint64_t d2 = read_u64((const char *)obj2->data);
192 /** The columns of the directory table. */
193 enum dir_table_columns {
194 /** The name of the directory. */
196 /** The dir count number. */
198 /** The number of the parent directory. */
200 /** The number of bytes of all regular files. */
202 /** The number of all regular files. */
204 /** Number of columns in this table. */
208 static struct osl_column_description dir_table_cols[] = {
210 .storage_type = OSL_MAPPED_STORAGE,
215 .storage_type = OSL_MAPPED_STORAGE,
216 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
218 .compare_function = uint64_compare,
219 .data_size = sizeof(uint64_t)
222 .storage_type = OSL_MAPPED_STORAGE,
223 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
224 .name = "parent_num",
225 .compare_function = size_compare,
226 .data_size = sizeof(uint64_t)
229 .storage_type = OSL_MAPPED_STORAGE,
230 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
231 .compare_function = size_compare,
233 .data_size = sizeof(uint64_t)
236 .storage_type = OSL_MAPPED_STORAGE,
237 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
238 .compare_function = size_compare,
240 .data_size = sizeof(uint64_t)
244 static struct osl_table_description dir_table_desc = {
246 .num_columns = NUM_DT_COLUMNS,
248 .column_descriptions = dir_table_cols,
251 /** The columns of the id table. */
252 enum user_table_columns {
253 /** The numer of the directory. */
255 /** The number of bytes of all regular files in this dir owned by this id. */
257 /** The number of files in this dir owned by this id. */
259 /** Number of columns in this table. */
263 static struct osl_column_description user_table_cols[] = {
265 .storage_type = OSL_MAPPED_STORAGE,
266 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
268 .compare_function = uint64_compare,
269 .data_size = sizeof(uint64_t)
272 .storage_type = OSL_MAPPED_STORAGE,
273 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
274 .compare_function = size_compare,
276 .data_size = sizeof(uint64_t)
279 .storage_type = OSL_MAPPED_STORAGE,
280 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
281 .compare_function = size_compare,
283 .data_size = sizeof(uint64_t)
287 static struct osl_table *dir_table;
289 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
290 uint64_t *dir_size, uint64_t *dir_files)
292 struct osl_object dir_objects[NUM_DT_COLUMNS];
294 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
295 dir_objects[DT_NAME].data = dirname;
296 dir_objects[DT_NAME].size = strlen(dirname) + 1;
297 dir_objects[DT_NUM].data = dir_num;
298 dir_objects[DT_NUM].size = sizeof(*dir_num);
299 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
300 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
301 dir_objects[DT_BYTES].data = dir_size;
302 dir_objects[DT_BYTES].size = sizeof(*dir_size);
303 dir_objects[DT_FILES].data = dir_files;
304 dir_objects[DT_FILES].size = sizeof(*dir_files);
305 return osl(osl_add_row(dir_table, dir_objects));
308 static uint32_t num_uids;
310 static int open_user_table(struct user_info *ui, int create)
314 ui->desc = para_malloc(sizeof(*ui->desc));
315 ui->desc->num_columns = NUM_UT_COLUMNS;
317 ui->desc->column_descriptions = user_table_cols;
318 ui->desc->dir = para_strdup(conf.database_dir_arg);
319 ui->desc->name = make_message("%u", (unsigned)ui->uid);
320 INFO_LOG(".............................uid #%u: %u\n",
321 (unsigned)num_uids, (unsigned)ui->uid);
323 ret = osl(osl_create_table(ui->desc));
328 ret = osl(osl_open_table(ui->desc, &ui->table));
333 free((char *)ui->desc->name);
334 free((char *)ui->desc->dir);
336 ui->desc->name = NULL;
337 ui->desc->dir = NULL;
344 #define uid_hash_bits 8
345 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
346 #define PRIME1 0x811c9dc5
347 #define PRIME2 0x01000193
349 static void create_hash_table(void)
351 uid_hash_table = para_calloc(uid_hash_table_size
352 * sizeof(struct user_info));
355 static void free_hash_table(void)
357 free(uid_hash_table);
358 uid_hash_table = NULL;
361 static int create_tables(void)
365 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
366 ret = osl(osl_create_table(&dir_table_desc));
374 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
375 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
376 * unused slots in the table are used to store different uids that hash to the
379 * If a hash collision occurs, different slots are successively probed in order
380 * to find an unused slot for the new uid. Probing is implemented via a second
381 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
384 * An odd number is sufficient to make sure each entry of the hash table gets
385 * probed for probe_num between 0 and s-1 because s is a power of two, hence
386 * the second hash value has never a common divisor with the hash table size.
387 * IOW: h is invertible in the ring [0..s].
389 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
391 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
392 % uid_hash_table_size;
395 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
396 + uid_hash_table_size; ui++)
398 enum search_uid_flags {
400 CREATE_USER_TABLE = 2,
403 static int uid_is_admissible(uint32_t uid)
407 for (i = 0; i < conf.uid_given; i++) {
408 struct uid_range *ur = admissible_uids + i;
410 if (ur->low <= uid && ur->high >= uid)
413 i = !conf.uid_given || i < conf.uid_given;
414 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
419 static int search_uid(uint32_t uid, enum search_uid_flags flags,
420 struct user_info **ui_ptr)
424 for (p = 0; p < uid_hash_table_size; p++) {
425 struct user_info *ui = uid_hash_table + double_hash(uid, p);
432 ui->flags |= UI_FL_SLOT_USED;
433 if (!uid_is_admissible(uid))
435 ui->flags |= UI_FL_ADMISSIBLE;
436 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
450 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
453 static int update_user_row(struct osl_table *t, uint64_t dir_num,
457 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
459 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
461 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
463 if (ret < 0) { /* this is the first file we add */
464 struct osl_object objects[NUM_UT_COLUMNS];
465 uint64_t num_files = 1;
467 objects[UT_DIR_NUM].data = &dir_num;
468 objects[UT_DIR_NUM].size = sizeof(dir_num);
469 objects[UT_BYTES].data = add;
470 objects[UT_BYTES].size = sizeof(*add);
471 objects[UT_FILES].data = &num_files;
472 objects[UT_FILES].size = sizeof(num_files);
473 INFO_LOG("######################### ret: %d\n", ret);
474 ret = osl(osl_add_row(t, objects));
475 INFO_LOG("######################### ret: %d\n", ret);
477 } else { /* add size and increment file count */
479 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
481 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
484 num = *(uint64_t *)obj1.data + *add;
485 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
488 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
491 num = *(uint64_t *)obj1.data + 1;
492 return osl(osl_update_object(t, row, UT_FILES, &obj2));
496 static uint64_t num_dirs;
497 static uint64_t num_files;
498 static uint64_t num_bytes;
500 int scan_dir(char *dirname, uint64_t *parent_dir_num)
503 struct dirent *entry;
504 int ret, cwd_fd, ret2;
505 uint64_t dir_size = 0, dir_files = 0;
506 uint64_t this_dir_num = ++num_dirs;
508 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
509 ret = para_opendir(dirname, &dir, &cwd_fd);
511 if (ret != -ERRNO_TO_ERROR(EACCES))
513 WARNING_LOG("permission denied for %s\n", dirname);
516 while ((entry = readdir(dir))) {
521 struct user_info *ui;
523 if (!strcmp(entry->d_name, "."))
525 if (!strcmp(entry->d_name, ".."))
527 if (lstat(entry->d_name, &s) == -1) {
528 WARNING_LOG("lstat error for %s/%s\n", dirname,
533 if (!S_ISREG(m) && !S_ISDIR(m))
536 ret = scan_dir(entry->d_name, &this_dir_num);
548 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
553 ret = update_user_row(ui->table, this_dir_num, &size);
557 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
558 &dir_size, &dir_files);
561 ret2 = para_fchdir(cwd_fd);
562 if (ret2 < 0 && ret >= 0)
568 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
570 char *result = NULL, *tmp;
572 uint64_t val = *dirnum;
573 struct osl_object obj = {.data = &val, .size = sizeof(val)};
577 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
580 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
584 tmp = make_message("%s/%s", (char *)obj.data, result);
588 result = para_strdup((char *)obj.data);
589 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
592 val = *(uint64_t *)obj.data;
604 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
606 struct osl_object obj;
608 char *this_dir, *prefix = NULL;
611 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
614 this_dir = para_strdup((char *)obj.data);
615 ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
618 if (!*(uint64_t *)obj.data) {
622 ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
625 *name = make_message("%s/%s", prefix, this_dir);
633 const uint64_t size_unit_divisors[] = {
634 [size_unit_arg_b] = 1ULL,
635 [size_unit_arg_k] = 1024ULL,
636 [size_unit_arg_m] = 1024ULL * 1024ULL,
637 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
638 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
641 const uint64_t count_unit_divisors[] = {
643 [count_unit_arg_n] = 1ULL,
644 [count_unit_arg_k] = 1000ULL,
645 [count_unit_arg_m] = 1000ULL * 1000ULL,
646 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
647 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
650 const char size_unit_abbrevs[] = " BKMGT";
651 const char count_unit_abbrevs[] = " KMGT";
653 static void format_size_value(enum enum_size_unit unit, uint64_t value, char *result)
655 if (unit == size_unit_arg_h) /* human readable */
656 for (unit = size_unit_arg_b; unit < size_unit_arg_t && value > size_unit_divisors[unit + 1]; unit++)
658 sprintf(result, "%llu%c", (long long unsigned)value / size_unit_divisors[unit], size_unit_abbrevs[unit]);
661 static void format_count_value(enum enum_count_unit unit, uint64_t value, char *result)
663 if (unit == count_unit_arg_h) /* human readable */
664 for (unit = count_unit_arg_n; unit < count_unit_arg_t && value > count_unit_divisors[unit + 1]; unit++)
666 sprintf(result, "%llu%c", (long long unsigned)value / count_unit_divisors[unit], count_unit_abbrevs[unit]);
669 enum global_stats_flags {
670 GSF_PRINT_DIRNAME = 1,
673 GSF_COMPUTE_SUMMARY = 8,
676 struct global_stats_info {
680 enum global_stats_flags flags;
683 static int global_stats_loop_function(struct osl_row *row, void *data)
685 struct global_stats_info *gsi = data;
686 struct osl_object obj;
687 char *dirname, formated_value[25];
688 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
690 if (!gsi->count && !summary) {
691 ret = -E_LOOP_COMPLETE;
694 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
695 ret = get_dir_name_of_row(row, &dirname);
698 printf("%s%s", dirname,
699 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
703 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
705 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
708 files = *(uint64_t *)obj.data;
709 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
710 format_size_value(conf.size_unit_arg, files,
712 printf("%s%s", formated_value,
713 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
718 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
720 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
723 bytes = *(uint64_t *)obj.data;
724 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
725 format_size_value(conf.size_unit_arg, bytes,
727 printf("%s\n", formated_value);
739 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
743 static void print_id_stats(void)
745 struct user_info *ui;
747 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
749 char formated_dir_count[25], formated_file_count[25],
751 if (!ui_used(ui) || !ui_admissible(ui))
753 format_count_value(conf.count_unit_arg, ui->dirs,
755 format_count_value(conf.count_unit_arg, ui->files,
756 formated_file_count);
757 format_size_value(conf.size_unit_arg, ui->bytes,
759 printf("%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
767 enum user_stats_flags {
768 USF_PRINT_DIRNAME = 1,
771 USF_COMPUTE_SUMMARY = 8,
774 struct user_stats_info {
776 enum user_stats_flags flags;
779 struct user_info *ui;
782 static int user_stats_loop_function(struct osl_row *row, void *data)
784 struct user_stats_info *usi = data;
785 struct osl_object obj;
786 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
787 char formated_value[25];
789 if (!usi->count && !summary) {
790 ret = -E_LOOP_COMPLETE;
793 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
795 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
798 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
803 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
807 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
809 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
812 files = *(uint64_t *)obj.data;
813 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
814 format_size_value(conf.size_unit_arg, files,
816 printf("%s%s", formated_value,
817 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
821 usi->ui->files += files;
823 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
825 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
828 bytes = *(uint64_t *)obj.data;
829 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
830 format_size_value(conf.size_unit_arg, bytes,
832 printf("%s\n", formated_value);
835 usi->ui->bytes += bytes;
845 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
849 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
853 assert(ret == -E_OSL);
854 if (osl_errno != E_OSL_LOOP)
855 /* error not caused by loop function returning negative. */
857 assert(loop_ret < 0);
858 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
860 if (loop_ret == -E_OSL) { /* osl error in loop function */
861 assert(loop_osl_errno);
862 osl_errno = loop_osl_errno;
867 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
868 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
870 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
871 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
874 static int print_user_stats(void)
876 struct user_info *ui;
880 struct user_stats_info usi = {
881 .count = conf.limit_arg,
884 if (!ui_used(ui) || !ui_admissible(ui))
886 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
887 printf("************************************************ uid %u\n",
889 printf("----------------- Largest dirs -------------------\n");
890 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
891 &usi.ret, &usi.osl_errno);
894 printf("---------- dirs containing most files ------------\n");
895 usi.count = conf.limit_arg,
896 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
897 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
898 &usi.ret, &usi.osl_errno);
905 static int print_statistics(void)
908 struct global_stats_info gsi = {
909 .count = conf.limit_arg,
910 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
913 printf("----------------- Largest dirs -------------------\n");
914 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
915 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
918 gsi.count = conf.limit_arg;
920 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
921 printf("---------- dirs containing most files ------------\n");
922 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
923 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
926 printf("------------------ Global summary (dirs/files/bytes)\n"
927 "%llu\t%llu\t%llu\n",
928 (long long unsigned)num_dirs, (long long unsigned)num_files,
929 (long long unsigned)num_bytes);
935 static char *get_uid_list_name(void)
937 return make_message("%s/uid_list", conf.database_dir_arg);
940 static int write_uid_list(void)
942 char *buf, *filename;
944 struct user_info *ui;
945 size_t size = num_uids * sizeof(uint32_t);
950 buf = para_malloc(size);
952 if (!ui_used(ui) || !ui_admissible(ui))
954 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
955 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
957 filename = get_uid_list_name();
958 ret = para_write_file(filename, buf, size);
964 static int open_dir_table(void)
966 if (!dir_table_desc.dir) /* we did not create the table */
967 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
968 return osl(osl_open_table(&dir_table_desc, &dir_table));
971 static void close_dir_table(void)
977 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
979 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
980 free((char *)dir_table_desc.dir);
984 static void close_user_table(struct user_info *ui)
988 if (!ui || !ui_used(ui) || !ui_admissible(ui))
990 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
992 ERROR_LOG("failed to close user table %u: %s\n",
993 (unsigned) ui->uid, adu_strerror(-ret));
994 free((char *)ui->desc->name);
995 ui->desc->name = NULL;
996 free((char *)ui->desc->dir);
997 ui->desc->dir = NULL;
1004 static void close_user_tables(void)
1006 struct user_info *ui;
1009 close_user_table(ui);
1012 static void close_all_tables(void)
1015 close_user_tables();
1019 static int com_create()
1021 uint64_t zero = 0ULL;
1022 int ret = create_tables();
1026 ret = open_dir_table();
1029 ret = scan_dir(conf.base_dir_arg, &zero);
1032 ret = write_uid_list();
1038 static int read_uid_file(void)
1042 char *filename = get_uid_list_name(), *map;
1043 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
1046 INFO_LOG("failed to map %s\n", filename);
1050 num_uids = size / 4;
1051 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
1053 /* hash table size should be a power of two and larger than the number of uids */
1054 uid_hash_table_size = 4;
1055 while (uid_hash_table_size < num_uids)
1056 uid_hash_table_size *= 2;
1057 create_hash_table();
1058 for (n = 0; n < num_uids; n++) {
1059 uint32_t uid = read_u32(map + n * sizeof(uid));
1060 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
1065 para_munmap(map, size);
1069 static int com_select(void)
1073 ret = open_dir_table();
1076 ret = read_uid_file();
1079 ret = print_statistics();
1084 static int check_args(void)
1088 /* remove trailing slashes from base-dir arg */
1089 if (conf.base_dir_given) {
1090 size_t len = strlen(conf.base_dir_arg);
1092 if (!len) /* empty string */
1093 return -ERRNO_TO_ERROR(EINVAL);
1094 if (!--len) /* length 1 is always OK */
1096 if (conf.base_dir_arg[len] != '/')
1097 break; /* no trailing slash, also OK */
1098 conf.base_dir_arg[len] = '\0';
1101 if (!conf.uid_given)
1103 admissible_uids = para_malloc(conf.uid_given * sizeof(*admissible_uids));
1104 for (i = 0; i < conf.uid_given; i++) {
1105 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
1111 free(admissible_uids);
1112 admissible_uids = NULL;
1116 int main(int argc, char **argv)
1119 struct cmdline_parser_params params = {
1122 .check_required = 0,
1123 .check_ambiguity = 0,
1127 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1132 if (conf.select_given)
1139 free(admissible_uids);
1141 ERROR_LOG("%s\n", adu_strerror(-ret));
1142 return -EXIT_FAILURE;
1144 return EXIT_SUCCESS;