2 #include <dirent.h> /* readdir() */
5 #include "gcc-compat.h"
10 #include "portable_io.h"
15 /** In case a signal is received, its number is stored here. */
18 /** Command line and config file options. */
19 struct gengetopt_args_info conf;
21 /** Global dir count. */
22 uint64_t num_dirs = 0;
23 /** Global files count. */
24 uint64_t num_files = 0;
25 /** Global bytes count. */
26 uint64_t num_bytes = 0;
28 /** The number of different uids found so far. */
29 uint32_t num_uids = 0;
32 * Contains info for each user that owns at least one regular file.
34 * Even users that are not taken into account because of the --uid
35 * option occupy a slot in this hash table. This allows to find out
36 * quicky whether a uid is admissible. And yes, this has to be fast.
38 struct user_info *uid_hash_table = NULL;
41 * The table containing the directory names and statistics.
43 struct osl_table *dir_table = NULL;
46 * The array of all uid ranges that were given at the command line.
48 struct uid_range *admissible_uids;
50 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
51 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
54 * Compare the size of two directories
56 * \param obj1 Pointer to the first object.
57 * \param obj2 Pointer to the second object.
59 * This function first compares the size values as usual integers. If they compare as
60 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
61 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
63 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
65 uint64_t d1 = *(uint64_t *)obj1->data;
66 uint64_t d2 = *(uint64_t *)obj2->data;
67 int ret = NUM_COMPARE(d2, d1);
71 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
72 return NUM_COMPARE(obj2->data, obj1->data);
76 * Compare two osl objects pointing to unsigned integers of 64 bit size.
78 * \param obj1 Pointer to the first integer.
79 * \param obj2 Pointer to the second integer.
81 * \return The values required for an osl compare function.
83 * \sa osl_compare_func, osl_hash_compare().
85 static int uint64_compare(const struct osl_object *obj1,
86 const struct osl_object *obj2)
88 uint64_t d1 = read_u64((const char *)obj1->data);
89 uint64_t d2 = read_u64((const char *)obj2->data);
98 static struct osl_column_description dir_table_cols[] = {
100 .storage_type = OSL_MAPPED_STORAGE,
105 .storage_type = OSL_MAPPED_STORAGE,
106 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
108 .compare_function = uint64_compare,
109 .data_size = sizeof(uint64_t)
112 .storage_type = OSL_MAPPED_STORAGE,
113 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
114 .name = "parent_num",
115 .compare_function = size_compare,
116 .data_size = sizeof(uint64_t)
119 .storage_type = OSL_MAPPED_STORAGE,
120 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
121 .compare_function = size_compare,
123 .data_size = sizeof(uint64_t)
126 .storage_type = OSL_MAPPED_STORAGE,
127 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
128 .compare_function = size_compare,
130 .data_size = sizeof(uint64_t)
134 static struct osl_table_description dir_table_desc = {
136 .num_columns = NUM_DT_COLUMNS,
138 .column_descriptions = dir_table_cols,
141 static struct osl_column_description user_table_cols[] = {
143 .storage_type = OSL_MAPPED_STORAGE,
144 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
146 .compare_function = uint64_compare,
147 .data_size = sizeof(uint64_t)
150 .storage_type = OSL_MAPPED_STORAGE,
151 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
152 .compare_function = size_compare,
154 .data_size = sizeof(uint64_t)
157 .storage_type = OSL_MAPPED_STORAGE,
158 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
159 .compare_function = size_compare,
161 .data_size = sizeof(uint64_t)
165 static int check_uid_arg(const char *arg, uint32_t *uid)
167 const uint32_t max = ~0U;
169 * we need an 64-bit int for string -> uid conversion because strtoll()
170 * returns a signed value.
173 int ret = atoi64(arg, &val);
177 if (val < 0 || val > max)
178 return -ERRNO_TO_ERROR(EINVAL);
183 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
186 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
188 if (!p || p == arg) { /* -42 or 42 */
189 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
192 ur->low = p? 0 : ur->high;
199 ret = check_uid_arg(arg, &ur->low);
203 if (*p) { /* 42-4711 */
204 ret = check_uid_arg(p, &ur->high);
208 if (ur->low > ur->high)
209 ret = -ERRNO_TO_ERROR(EINVAL);
212 ERROR_LOG("bad uid option: %s\n", orig_arg);
214 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
223 * \param ll Loglevel.
224 * \param fml Usual format string.
226 * All XXX_LOG() macros use this function.
228 __printf_2_3 void __log(int ll, const char* fmt,...)
236 if (ll < conf.loglevel_arg)
241 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
242 fprintf(outfd, "%s ", str);
244 vfprintf(outfd, fmt, argp);
248 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
249 uint64_t *dir_size, uint64_t *dir_files)
251 struct osl_object dir_objects[NUM_DT_COLUMNS];
253 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
254 dir_objects[DT_NAME].data = dirname;
255 dir_objects[DT_NAME].size = strlen(dirname) + 1;
256 dir_objects[DT_NUM].data = dir_num;
257 dir_objects[DT_NUM].size = sizeof(*dir_num);
258 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
259 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
260 dir_objects[DT_BYTES].data = dir_size;
261 dir_objects[DT_BYTES].size = sizeof(*dir_size);
262 dir_objects[DT_FILES].data = dir_files;
263 dir_objects[DT_FILES].size = sizeof(*dir_files);
264 return osl(osl_add_row(dir_table, dir_objects));
267 static int open_user_table(struct user_info *ui, int create)
272 ui->desc = adu_malloc(sizeof(*ui->desc));
273 ui->desc->num_columns = NUM_UT_COLUMNS;
275 ui->desc->column_descriptions = user_table_cols;
276 ui->desc->dir = adu_strdup(conf.database_dir_arg);
277 ui->desc->name = make_message("%u", (unsigned)ui->uid);
278 pw = getpwuid(ui->uid);
279 if (pw && pw->pw_name)
280 ui->pw_name = adu_strdup(pw->pw_name);
282 INFO_LOG(".............................uid #%u: %u\n",
283 (unsigned)num_uids, (unsigned)ui->uid);
285 ret = osl(osl_create_table(ui->desc));
290 ret = osl(osl_open_table(ui->desc, &ui->table));
295 free((char *)ui->desc->name);
296 free((char *)ui->desc->dir);
299 ui->desc->name = NULL;
300 ui->desc->dir = NULL;
307 #define uid_hash_bits 8
308 uint32_t uid_hash_table_size = 1 << uid_hash_bits;
309 #define PRIME1 0x811c9dc5
310 #define PRIME2 0x01000193
312 void create_hash_table(void)
314 uid_hash_table = adu_calloc(uid_hash_table_size
315 * sizeof(struct user_info));
318 static void free_hash_table(void)
320 free(uid_hash_table);
321 uid_hash_table = NULL;
324 static int create_tables(void)
328 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
329 ret = osl(osl_create_table(&dir_table_desc));
336 static void close_dir_table(void)
342 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
344 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
345 free((char *)dir_table_desc.dir);
349 static void close_user_table(struct user_info *ui)
353 if (!ui || !ui_used(ui) || !ui_admissible(ui))
355 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
357 ERROR_LOG("failed to close user table %u: %s\n",
358 (unsigned) ui->uid, adu_strerror(-ret));
359 free((char *)ui->desc->name);
360 ui->desc->name = NULL;
361 free((char *)ui->desc->dir);
362 ui->desc->dir = NULL;
371 static void close_user_tables(void)
373 struct user_info *ui;
376 close_user_table(ui);
379 void close_all_tables(void)
386 static void signal_handler(int s)
391 void check_signals(void)
395 EMERG_LOG("caught signal %d\n", signum);
400 static int init_signals(void)
402 if (signal(SIGINT, &signal_handler) == SIG_ERR)
403 return -E_SIGNAL_SIG_ERR;
404 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
405 return -E_SIGNAL_SIG_ERR;
410 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
411 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
412 * unused slots in the table are used to store different uids that hash to the
415 * If a hash collision occurs, different slots are successively probed in order
416 * to find an unused slot for the new uid. Probing is implemented via a second
417 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
420 * An odd number is sufficient to make sure each entry of the hash table gets
421 * probed for probe_num between 0 and s-1 because s is a power of two, hence
422 * the second hash value has never a common divisor with the hash table size.
423 * IOW: h is invertible in the ring [0..s].
425 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
427 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
428 % uid_hash_table_size;
431 static int uid_is_admissible(uint32_t uid)
435 for (i = 0; i < conf.uid_given; i++) {
436 struct uid_range *ur = admissible_uids + i;
438 if (ur->low <= uid && ur->high >= uid)
441 i = !conf.uid_given || i < conf.uid_given;
442 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
447 int search_uid(uint32_t uid, enum search_uid_flags flags,
448 struct user_info **ui_ptr)
452 for (p = 0; p < uid_hash_table_size; p++) {
453 struct user_info *ui = uid_hash_table + double_hash(uid, p);
460 ui->flags |= UI_FL_SLOT_USED;
461 if (!uid_is_admissible(uid))
463 ui->flags |= UI_FL_ADMISSIBLE;
464 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
478 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
481 static int update_user_row(struct osl_table *t, uint64_t dir_num,
485 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
487 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
489 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
491 if (ret < 0) { /* this is the first file we add */
492 struct osl_object objects[NUM_UT_COLUMNS];
493 uint64_t num_files = 1;
495 objects[UT_DIR_NUM].data = &dir_num;
496 objects[UT_DIR_NUM].size = sizeof(dir_num);
497 objects[UT_BYTES].data = add;
498 objects[UT_BYTES].size = sizeof(*add);
499 objects[UT_FILES].data = &num_files;
500 objects[UT_FILES].size = sizeof(num_files);
501 INFO_LOG("######################### ret: %d\n", ret);
502 ret = osl(osl_add_row(t, objects));
503 INFO_LOG("######################### ret: %d\n", ret);
505 } else { /* add size and increment file count */
507 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
509 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
512 num = *(uint64_t *)obj1.data + *add;
513 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
516 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
519 num = *(uint64_t *)obj1.data + 1;
520 return osl(osl_update_object(t, row, UT_FILES, &obj2));
524 /* id of the device containing the base dir. */
525 static dev_t device_id;
527 static int scan_dir(char *dirname, uint64_t *parent_dir_num)
530 struct dirent *entry;
531 int ret, cwd_fd, ret2;
532 uint64_t dir_size = 0, dir_files = 0;
533 uint64_t this_dir_num = ++num_dirs;
536 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
537 ret = adu_opendir(dirname, &dir, &cwd_fd);
539 if (ret != -ERRNO_TO_ERROR(EACCES))
541 WARNING_LOG("permission denied for %s\n", dirname);
544 while ((entry = readdir(dir))) {
549 struct user_info *ui;
551 if (!strcmp(entry->d_name, "."))
553 if (!strcmp(entry->d_name, ".."))
555 if (lstat(entry->d_name, &s) == -1) {
556 WARNING_LOG("lstat error for %s/%s\n", dirname,
561 if (!S_ISREG(m) && !S_ISDIR(m))
564 if (conf.one_file_system_given && s.st_dev != device_id)
566 ret = scan_dir(entry->d_name, &this_dir_num);
578 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
583 ret = update_user_row(ui->table, this_dir_num, &size);
587 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
588 &dir_size, &dir_files);
591 ret2 = adu_fchdir(cwd_fd);
592 if (ret2 < 0 && ret >= 0)
598 char *get_uid_list_name(void)
600 return make_message("%s/uid_list", conf.database_dir_arg);
603 static int write_uid_list(void)
605 char *buf, *filename;
607 struct user_info *ui;
608 size_t size = num_uids * sizeof(uint32_t);
613 buf = adu_malloc(size);
615 if (!ui_used(ui) || !ui_admissible(ui))
617 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
618 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
620 filename = get_uid_list_name();
621 ret = adu_write_file(filename, buf, size);
627 int open_dir_table(void)
629 if (!dir_table_desc.dir) /* we did not create the table */
630 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
631 return osl(osl_open_table(&dir_table_desc, &dir_table));
633 static int com_create()
635 uint64_t zero = 0ULL;
639 if (lstat(conf.base_dir_arg, &statbuf) == -1)
640 return -ERRNO_TO_ERROR(errno);
641 if (!S_ISDIR(statbuf.st_mode))
642 return -ERRNO_TO_ERROR(ENOTDIR);
643 device_id = statbuf.st_dev;
644 ret = create_tables();
648 ret = open_dir_table();
652 ret = scan_dir(conf.base_dir_arg, &zero);
655 ret = write_uid_list();
661 static int check_args(void)
665 /* remove trailing slashes from base-dir arg */
666 if (conf.base_dir_given) {
667 size_t len = strlen(conf.base_dir_arg);
669 if (!len) /* empty string */
670 return -ERRNO_TO_ERROR(EINVAL);
671 if (!--len) /* length 1 is always OK */
673 if (conf.base_dir_arg[len] != '/')
674 break; /* no trailing slash, also OK */
675 conf.base_dir_arg[len] = '\0';
680 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
681 for (i = 0; i < conf.uid_given; i++) {
682 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
688 free(admissible_uids);
689 admissible_uids = NULL;
693 int main(int argc, char **argv)
696 struct cmdline_parser_params params = {
700 .check_ambiguity = 0,
704 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
705 ret = init_signals();
712 if (conf.select_given)
719 free(admissible_uids);
721 ERROR_LOG("%s\n", adu_strerror(-ret));
722 return -EXIT_FAILURE;