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, '-');
84 if (p == arg) /* -42 */
86 ret = check_uid_arg(p, &ur->high);
89 ur->low = p? 0 : ur->high;
96 ret = check_uid_arg(arg, &ur->low);
100 if (*p) { /* 42-4711 */
101 ret = check_uid_arg(p, &ur->high);
105 if (ur->low > ur->high)
106 ret = -ERRNO_TO_ERROR(EINVAL);
109 ERROR_LOG("bad uid option: %s\n", orig_arg);
111 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
118 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
119 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
124 * \param ll Loglevel.
125 * \param fml Usual format string.
127 * All XXX_LOG() macros use this function.
129 __printf_2_3 void __log(int ll, const char* fmt,...)
137 if (ll < conf.loglevel_arg)
142 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
143 fprintf(outfd, "%s ", str);
145 vfprintf(outfd, fmt, argp);
150 * Compare the size of two directories
152 * \param obj1 Pointer to the first object.
153 * \param obj2 Pointer to the second object.
155 * This function first compares the size values as usual integers. If they compare as
156 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
157 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
159 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
161 uint64_t d1 = *(uint64_t *)obj1->data;
162 uint64_t d2 = *(uint64_t *)obj2->data;
163 int ret = NUM_COMPARE(d2, d1);
167 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
168 return NUM_COMPARE(obj2->data, obj1->data);
172 * Compare two osl objects pointing to unsigned integers of 64 bit size.
174 * \param obj1 Pointer to the first integer.
175 * \param obj2 Pointer to the second integer.
177 * \return The values required for an osl compare function.
179 * \sa osl_compare_func, osl_hash_compare().
181 static int uint64_compare(const struct osl_object *obj1,
182 const struct osl_object *obj2)
184 uint64_t d1 = read_u64((const char *)obj1->data);
185 uint64_t d2 = read_u64((const char *)obj2->data);
194 /** The columns of the directory table. */
195 enum dir_table_columns {
196 /** The name of the directory. */
198 /** The dir count number. */
200 /** The number of the parent directory. */
202 /** The number of bytes of all regular files. */
204 /** The number of all regular files. */
206 /** Number of columns in this table. */
210 static struct osl_column_description dir_table_cols[] = {
212 .storage_type = OSL_MAPPED_STORAGE,
217 .storage_type = OSL_MAPPED_STORAGE,
218 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
220 .compare_function = uint64_compare,
221 .data_size = sizeof(uint64_t)
224 .storage_type = OSL_MAPPED_STORAGE,
225 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
226 .name = "parent_num",
227 .compare_function = size_compare,
228 .data_size = sizeof(uint64_t)
231 .storage_type = OSL_MAPPED_STORAGE,
232 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
233 .compare_function = size_compare,
235 .data_size = sizeof(uint64_t)
238 .storage_type = OSL_MAPPED_STORAGE,
239 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
240 .compare_function = size_compare,
242 .data_size = sizeof(uint64_t)
246 static struct osl_table_description dir_table_desc = {
248 .num_columns = NUM_DT_COLUMNS,
250 .column_descriptions = dir_table_cols,
253 /** The columns of the id table. */
254 enum user_table_columns {
255 /** The numer of the directory. */
257 /** The number of bytes of all regular files in this dir owned by this id. */
259 /** The number of files in this dir owned by this id. */
261 /** Number of columns in this table. */
265 static struct osl_column_description user_table_cols[] = {
267 .storage_type = OSL_MAPPED_STORAGE,
268 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
270 .compare_function = uint64_compare,
271 .data_size = sizeof(uint64_t)
274 .storage_type = OSL_MAPPED_STORAGE,
275 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
276 .compare_function = size_compare,
278 .data_size = sizeof(uint64_t)
281 .storage_type = OSL_MAPPED_STORAGE,
282 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
283 .compare_function = size_compare,
285 .data_size = sizeof(uint64_t)
289 static struct osl_table *dir_table;
291 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
292 uint64_t *dir_size, uint64_t *dir_files)
294 struct osl_object dir_objects[NUM_DT_COLUMNS];
296 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
297 dir_objects[DT_NAME].data = dirname;
298 dir_objects[DT_NAME].size = strlen(dirname) + 1;
299 dir_objects[DT_NUM].data = dir_num;
300 dir_objects[DT_NUM].size = sizeof(*dir_num);
301 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
302 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
303 dir_objects[DT_BYTES].data = dir_size;
304 dir_objects[DT_BYTES].size = sizeof(*dir_size);
305 dir_objects[DT_FILES].data = dir_files;
306 dir_objects[DT_FILES].size = sizeof(*dir_files);
307 return osl(osl_add_row(dir_table, dir_objects));
310 static uint32_t num_uids;
312 static int open_user_table(struct user_info *ui, int create)
316 ui->desc = para_malloc(sizeof(*ui->desc));
317 ui->desc->num_columns = NUM_UT_COLUMNS;
319 ui->desc->column_descriptions = user_table_cols;
320 ui->desc->dir = para_strdup(conf.database_dir_arg);
321 ui->desc->name = make_message("%u", (unsigned)ui->uid);
322 INFO_LOG(".............................uid #%u: %u\n",
323 (unsigned)num_uids, (unsigned)ui->uid);
325 ret = osl(osl_create_table(ui->desc));
330 ret = osl(osl_open_table(ui->desc, &ui->table));
335 free((char *)ui->desc->name);
336 free((char *)ui->desc->dir);
338 ui->desc->name = NULL;
339 ui->desc->dir = NULL;
346 #define uid_hash_bits 8
347 static uint32_t uid_hash_table_size = 1 << uid_hash_bits;
348 #define PRIME1 0x811c9dc5
349 #define PRIME2 0x01000193
351 static void create_hash_table(void)
353 uid_hash_table = para_calloc(uid_hash_table_size
354 * sizeof(struct user_info));
357 static void free_hash_table(void)
359 free(uid_hash_table);
360 uid_hash_table = NULL;
363 static int create_tables(void)
367 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
368 ret = osl(osl_create_table(&dir_table_desc));
376 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
377 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
378 * unused slots in the table are used to store different uids that hash to the
381 * If a hash collision occurs, different slots are successively probed in order
382 * to find an unused slot for the new uid. Probing is implemented via a second
383 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
386 * An odd number is sufficient to make sure each entry of the hash table gets
387 * probed for probe_num between 0 and s-1 because s is a power of two, hence
388 * the second hash value has never a common divisor with the hash table size.
389 * IOW: h is invertible in the ring [0..s].
391 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
393 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
394 % uid_hash_table_size;
397 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
398 + uid_hash_table_size; ui++)
400 enum search_uid_flags {
402 CREATE_USER_TABLE = 2,
405 static int uid_is_admissible(uint32_t uid)
409 for (i = 0; i < conf.uid_given; i++) {
410 struct uid_range *ur = admissible_uids + i;
412 if (ur->low <= uid && ur->high >= uid)
415 i = !conf.uid_given || i < conf.uid_given;
416 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
421 static int search_uid(uint32_t uid, enum search_uid_flags flags,
422 struct user_info **ui_ptr)
426 for (p = 0; p < uid_hash_table_size; p++) {
427 struct user_info *ui = uid_hash_table + double_hash(uid, p);
434 ui->flags |= UI_FL_SLOT_USED;
435 if (!uid_is_admissible(uid))
437 ui->flags |= UI_FL_ADMISSIBLE;
438 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
452 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
455 static int update_user_row(struct osl_table *t, uint64_t dir_num,
459 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
461 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
463 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
465 if (ret < 0) { /* this is the first file we add */
466 struct osl_object objects[NUM_UT_COLUMNS];
467 uint64_t num_files = 1;
469 objects[UT_DIR_NUM].data = &dir_num;
470 objects[UT_DIR_NUM].size = sizeof(dir_num);
471 objects[UT_BYTES].data = add;
472 objects[UT_BYTES].size = sizeof(*add);
473 objects[UT_FILES].data = &num_files;
474 objects[UT_FILES].size = sizeof(num_files);
475 INFO_LOG("######################### ret: %d\n", ret);
476 ret = osl(osl_add_row(t, objects));
477 INFO_LOG("######################### ret: %d\n", ret);
479 } else { /* add size and increment file count */
481 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
483 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
486 num = *(uint64_t *)obj1.data + *add;
487 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
490 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
493 num = *(uint64_t *)obj1.data + 1;
494 return osl(osl_update_object(t, row, UT_FILES, &obj2));
498 static uint64_t num_dirs;
499 static uint64_t num_files;
500 static uint64_t num_bytes;
502 int scan_dir(char *dirname, uint64_t *parent_dir_num)
505 struct dirent *entry;
506 int ret, cwd_fd, ret2;
507 uint64_t dir_size = 0, dir_files = 0;
508 uint64_t this_dir_num = ++num_dirs;
510 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
511 ret = para_opendir(dirname, &dir, &cwd_fd);
513 if (ret != -ERRNO_TO_ERROR(EACCES))
515 WARNING_LOG("permission denied for %s\n", dirname);
518 while ((entry = readdir(dir))) {
523 struct user_info *ui;
525 if (!strcmp(entry->d_name, "."))
527 if (!strcmp(entry->d_name, ".."))
529 if (lstat(entry->d_name, &s) == -1) {
530 WARNING_LOG("lstat error for %s/%s\n", dirname,
535 if (!S_ISREG(m) && !S_ISDIR(m))
538 ret = scan_dir(entry->d_name, &this_dir_num);
550 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
555 ret = update_user_row(ui->table, this_dir_num, &size);
559 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
560 &dir_size, &dir_files);
563 ret2 = para_fchdir(cwd_fd);
564 if (ret2 < 0 && ret >= 0)
570 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
572 char *result = NULL, *tmp;
574 uint64_t val = *dirnum;
575 struct osl_object obj = {.data = &val, .size = sizeof(val)};
579 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
582 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
586 tmp = make_message("%s/%s", (char *)obj.data, result);
590 result = para_strdup((char *)obj.data);
591 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
594 val = *(uint64_t *)obj.data;
606 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
608 struct osl_object obj;
610 char *this_dir, *prefix = NULL;
613 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
616 this_dir = para_strdup((char *)obj.data);
617 ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
620 if (!*(uint64_t *)obj.data) {
624 ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
627 *name = make_message("%s/%s", prefix, this_dir);
635 const uint64_t size_unit_divisors[] = {
636 [size_unit_arg_b] = 1ULL,
637 [size_unit_arg_k] = 1024ULL,
638 [size_unit_arg_m] = 1024ULL * 1024ULL,
639 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
640 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
643 const uint64_t count_unit_divisors[] = {
645 [count_unit_arg_n] = 1ULL,
646 [count_unit_arg_k] = 1000ULL,
647 [count_unit_arg_m] = 1000ULL * 1000ULL,
648 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
649 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
652 const char size_unit_abbrevs[] = " BKMGT";
653 const char count_unit_abbrevs[] = " KMGT";
655 static void format_size_value(enum enum_size_unit unit, uint64_t value, char *result)
657 if (unit == size_unit_arg_h) /* human readable */
658 for (unit = size_unit_arg_b; unit < size_unit_arg_t && value > size_unit_divisors[unit + 1]; unit++)
660 sprintf(result, "%llu%c", (long long unsigned)value / size_unit_divisors[unit], size_unit_abbrevs[unit]);
663 static void format_count_value(enum enum_count_unit unit, uint64_t value, char *result)
665 if (unit == count_unit_arg_h) /* human readable */
666 for (unit = count_unit_arg_n; unit < count_unit_arg_t && value > count_unit_divisors[unit + 1]; unit++)
668 sprintf(result, "%llu%c", (long long unsigned)value / count_unit_divisors[unit], count_unit_abbrevs[unit]);
671 enum global_stats_flags {
672 GSF_PRINT_DIRNAME = 1,
675 GSF_COMPUTE_SUMMARY = 8,
678 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 return -E_LOOP_COMPLETE;
692 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
693 ret = get_dir_name_of_row(row, &dirname);
696 printf("%s%s", dirname,
697 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
701 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
703 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
706 files = *(uint64_t *)obj.data;
707 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
708 format_size_value(conf.size_unit_arg, files,
710 printf("%s%s", formated_value,
711 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
716 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
718 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
721 bytes = *(uint64_t *)obj.data;
722 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
723 format_size_value(conf.size_unit_arg, bytes,
725 printf("%s\n", formated_value);
737 static void print_id_stats(void)
739 struct user_info *ui;
741 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
743 char formated_dir_count[25], formated_file_count[25],
747 format_count_value(conf.count_unit_arg, ui->dirs,
749 format_count_value(conf.count_unit_arg, ui->files,
750 formated_file_count);
751 format_size_value(conf.size_unit_arg, ui->bytes,
753 printf("%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
761 enum user_stats_flags {
762 USF_PRINT_DIRNAME = 1,
765 USF_COMPUTE_SUMMARY = 8,
768 struct user_stats_info {
770 enum user_stats_flags flags;
771 struct user_info *ui;
774 static int user_stats_loop_function(struct osl_row *row, void *data)
776 struct user_stats_info *usi = data;
777 struct osl_object obj;
778 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
779 char formated_value[25];
781 if (!usi->count && !summary)
782 return -E_LOOP_COMPLETE;
783 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
785 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
788 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
793 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
797 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
799 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
802 files = *(uint64_t *)obj.data;
803 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
804 format_size_value(conf.size_unit_arg, files,
806 printf("%s%s", formated_value,
807 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
811 usi->ui->files += files;
813 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
815 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
818 bytes = *(uint64_t *)obj.data;
819 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
820 format_size_value(conf.size_unit_arg, bytes,
822 printf("%s\n", formated_value);
825 usi->ui->bytes += bytes;
835 static void print_user_stats(void)
837 struct user_info *ui;
840 struct user_stats_info usi = {
841 .count = conf.limit_arg,
844 if (!ui_used(ui) || !ui_admissible(ui))
846 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
847 printf("************************************************ uid %u\n",
849 printf("----------------- Largest dirs -------------------\n");
850 osl(osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
851 user_stats_loop_function));
852 printf("---------- dirs containing most files ------------\n");
853 usi.count = conf.limit_arg,
854 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
855 osl(osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
856 user_stats_loop_function));
860 static int print_statistics(void)
863 struct global_stats_info gsi = {
864 .count = conf.limit_arg,
865 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
868 printf("----------------- Largest dirs -------------------\n");
869 ret = osl(osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
870 global_stats_loop_function));
871 if (ret < 0 && ret != -E_LOOP_COMPLETE)
873 gsi.count = conf.limit_arg;
875 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
876 printf("---------- dirs containing most files ------------\n");
877 ret = osl(osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
878 global_stats_loop_function));
879 if (ret < 0 && ret != -E_LOOP_COMPLETE)
882 printf("------------------ Global summary (dirs/files/bytes)\n"
883 "%llu\t%llu\t%llu\n",
884 (long long unsigned)num_dirs, (long long unsigned)num_files,
885 (long long unsigned)num_bytes);
891 static char *get_uid_list_name(void)
893 return make_message("%s/uid_list", conf.database_dir_arg);
896 static int write_uid_list(void)
898 char *buf, *filename;
900 struct user_info *ui;
901 size_t size = num_uids * sizeof(uint32_t);
906 buf = para_malloc(size);
908 if (!ui_used(ui) || !ui_admissible(ui))
910 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
911 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
913 filename = get_uid_list_name();
914 ret = para_write_file(filename, buf, size);
920 static int open_dir_table(void)
922 if (!dir_table_desc.dir) /* we did not create the table */
923 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
924 return osl(osl_open_table(&dir_table_desc, &dir_table));
927 static void close_dir_table(void)
933 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
935 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
936 free((char *)dir_table_desc.dir);
940 static void close_user_table(struct user_info *ui)
944 if (!ui || !ui_used(ui) || !ui_admissible(ui))
946 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
948 ERROR_LOG("failed to close user table %u: %s\n",
949 (unsigned) ui->uid, adu_strerror(-ret));
950 free((char *)ui->desc->name);
951 ui->desc->name = NULL;
952 free((char *)ui->desc->dir);
953 ui->desc->dir = NULL;
960 static void close_user_tables(void)
962 struct user_info *ui;
965 close_user_table(ui);
968 static void close_all_tables(void)
975 static int com_create()
977 uint64_t zero = 0ULL;
978 int ret = create_tables();
982 ret = open_dir_table();
985 ret = scan_dir(conf.base_dir_arg, &zero);
988 ret = write_uid_list();
994 static int read_uid_file(void)
998 char *filename = get_uid_list_name(), *map;
999 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
1002 INFO_LOG("failed to map %s\n", filename);
1006 num_uids = size / 4;
1007 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
1009 /* hash table size should be a power of two and larger than the number of uids */
1010 uid_hash_table_size = 4;
1011 while (uid_hash_table_size < num_uids)
1012 uid_hash_table_size *= 2;
1013 create_hash_table();
1014 for (n = 0; n < num_uids; n++) {
1015 uint32_t uid = read_u32(map + n * sizeof(uid));
1016 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
1021 para_munmap(map, size);
1025 static int com_select(void)
1029 ret = open_dir_table();
1032 ret = read_uid_file();
1035 ret = print_statistics();
1040 static int check_args(void)
1044 /* remove trailing slashes from base-dir arg */
1045 if (conf.base_dir_given) {
1046 size_t len = strlen(conf.base_dir_arg);
1048 if (!len) /* empty string */
1049 return -ERRNO_TO_ERROR(EINVAL);
1050 if (!--len) /* length 1 is always OK */
1052 if (conf.base_dir_arg[len] != '/')
1053 break; /* no trailing slash, also OK */
1054 conf.base_dir_arg[len] = '\0';
1057 if (!conf.uid_given)
1059 admissible_uids = para_malloc(conf.uid_given * sizeof(*admissible_uids));
1060 for (i = 0; i < conf.uid_given; i++) {
1061 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
1067 free(admissible_uids);
1068 admissible_uids = NULL;
1072 int main(int argc, char **argv)
1075 struct cmdline_parser_params params = {
1078 .check_required = 0,
1079 .check_ambiguity = 0,
1083 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1088 if (conf.select_given)
1095 free(admissible_uids);
1097 ERROR_LOG("%s\n", adu_strerror(-ret));
1098 return -EXIT_FAILURE;
1100 return EXIT_SUCCESS;