Remove and unused variable.
[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 /** Command line and config file options. */
14 static struct gengetopt_args_info conf;
15
16 enum uid_info_flags {
17 /** whether this slot of the hash table is used. */
18 UI_FL_SLOT_USED = 1,
19 /** whether this uid should be taken into account. */
20 UI_FL_ADMISSIBLE = 2,
21 };
22
23 struct user_info {
24 uint32_t uid;
25 uint32_t flags;
26 struct osl_table *table;
27 uint64_t files;
28 uint64_t bytes;
29 uint64_t dirs;
30 struct osl_table_description *desc;
31 };
32
33 /**
34 * Contains info for each user that owns at least one regular file.
35 *
36 * Even users that are not taken into account because of the --uid
37 * option occupy a slot in this hash table. This allows to find out
38 * quicky whether a uid is admissible. And yes, this has to be fast.
39 */
40 static struct user_info *uid_hash_table;
41
42 static inline int ui_used(struct user_info *ui)
43 {
44 return ui->flags & UI_FL_SLOT_USED;
45 }
46
47 static inline int ui_admissible(struct user_info *ui)
48 {
49 return ui->flags & UI_FL_ADMISSIBLE;
50 }
51
52 struct uid_range {
53 uint32_t low;
54 uint32_t high;
55 };
56
57 static struct uid_range *admissible_uids;
58
59 static inline int check_uid_arg(const char *arg, uint32_t *uid)
60 {
61 const uint32_t max = ~0U;
62 /*
63 * we need an 64-bit int for string -> uid conversion because strtoll()
64 * returns a signed value.
65 */
66 int64_t val;
67 int ret = para_atoi64(arg, &val);
68
69 if (ret < 0)
70 return ret;
71 if (val < 0 || val > max)
72 return -ERRNO_TO_ERROR(EINVAL);
73 *uid = val;
74 return 1;
75 }
76
77 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
78 {
79 int ret;
80 char *arg = para_strdup(orig_arg), *p = strchr(arg, '-');
81
82 if (!p || p == arg) {
83 if (p == arg) /* -42 */
84 p++;
85 ret = check_uid_arg(p, &ur->high);
86 if (ret < 0)
87 goto out;
88 ur->low = p? 0 : ur->high;
89 ret = 1;
90 goto out;
91 }
92 /* 42- or 42-4711 */
93 *p = '\0';
94 p++;
95 ret = check_uid_arg(arg, &ur->low);
96 if (ret < 0)
97 goto out;
98 ur->high = ~0U;
99 if (*p) { /* 42-4711 */
100 ret = check_uid_arg(p, &ur->high);
101 if (ret < 0)
102 goto out;
103 }
104 if (ur->low > ur->high)
105 ret = -ERRNO_TO_ERROR(EINVAL);
106 out:
107 if (ret < 0)
108 ERROR_LOG("bad uid option: %s\n", orig_arg);
109 else
110 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
111 ur->high);
112 free(arg);
113 return ret;
114 }
115
116
117 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
118 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
119
120 /**
121 * The log function.
122 *
123 * \param ll Loglevel.
124 * \param fml Usual format string.
125 *
126 * All XXX_LOG() macros use this function.
127 */
128 __printf_2_3 void __log(int ll, const char* fmt,...)
129 {
130 va_list argp;
131 FILE *outfd;
132 struct tm *tm;
133 time_t t1;
134 char str[255] = "";
135
136 if (ll < conf.loglevel_arg)
137 return;
138 outfd = stderr;
139 time(&t1);
140 tm = localtime(&t1);
141 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
142 fprintf(outfd, "%s ", str);
143 va_start(argp, fmt);
144 vfprintf(outfd, fmt, argp);
145 va_end(argp);
146 }
147
148 /**
149 * Compare the size of two directories
150 *
151 * \param obj1 Pointer to the first object.
152 * \param obj2 Pointer to the second object.
153 *
154 * This function first compares the size values as usual integers. If they compare as
155 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
156 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
157 */
158 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
159 {
160 uint64_t d1 = *(uint64_t *)obj1->data;
161 uint64_t d2 = *(uint64_t *)obj2->data;
162 int ret = NUM_COMPARE(d2, d1);
163
164 if (ret)
165 return ret;
166 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
167 return NUM_COMPARE(obj2->data, obj1->data);
168 }
169
170 /**
171 * Compare two osl objects pointing to unsigned integers of 64 bit size.
172 *
173 * \param obj1 Pointer to the first integer.
174 * \param obj2 Pointer to the second integer.
175 *
176 * \return The values required for an osl compare function.
177 *
178 * \sa osl_compare_func, osl_hash_compare().
179 */
180 static int uint64_compare(const struct osl_object *obj1,
181 const struct osl_object *obj2)
182 {
183 uint64_t d1 = read_u64((const char *)obj1->data);
184 uint64_t d2 = read_u64((const char *)obj2->data);
185
186 if (d1 < d2)
187 return 1;
188 if (d1 > d2)
189 return -1;
190 return 0;
191 }
192
193 /** The columns of the directory table. */
194 enum dir_table_columns {
195 /** The name of the directory. */
196 DT_NAME,
197 /** The dir count number. */
198 DT_NUM,
199 /** The number of the parent directory. */
200 DT_PARENT_NUM,
201 /** The number of bytes of all regular files. */
202 DT_BYTES,
203 /** The number of all regular files. */
204 DT_FILES,
205 /** Number of columns in this table. */
206 NUM_DT_COLUMNS
207 };
208
209 static struct osl_column_description dir_table_cols[] = {
210 [DT_NAME] = {
211 .storage_type = OSL_MAPPED_STORAGE,
212 .storage_flags = 0,
213 .name = "dir",
214 },
215 [DT_NUM] = {
216 .storage_type = OSL_MAPPED_STORAGE,
217 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
218 .name = "num",
219 .compare_function = uint64_compare,
220 .data_size = sizeof(uint64_t)
221 },
222 [DT_PARENT_NUM] = {
223 .storage_type = OSL_MAPPED_STORAGE,
224 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
225 .name = "parent_num",
226 .compare_function = size_compare,
227 .data_size = sizeof(uint64_t)
228 },
229 [DT_BYTES] = {
230 .storage_type = OSL_MAPPED_STORAGE,
231 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
232 .compare_function = size_compare,
233 .name = "num_bytes",
234 .data_size = sizeof(uint64_t)
235 },
236 [DT_FILES] = {
237 .storage_type = OSL_MAPPED_STORAGE,
238 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
239 .compare_function = size_compare,
240 .name = "num_files",
241 .data_size = sizeof(uint64_t)
242 }
243 };
244
245 static struct osl_table_description dir_table_desc = {
246 .name = "dir_table",
247 .num_columns = NUM_DT_COLUMNS,
248 .flags = 0,
249 .column_descriptions = dir_table_cols,
250 };
251
252 /** The columns of the id table. */
253 enum user_table_columns {
254 /** The numer of the directory. */
255 UT_DIR_NUM,
256 /** The number of bytes of all regular files in this dir owned by this id. */
257 UT_BYTES,
258 /** The number of files in this dir owned by this id. */
259 UT_FILES,
260 /** Number of columns in this table. */
261 NUM_UT_COLUMNS
262 };
263
264 static struct osl_column_description user_table_cols[] = {
265 [UT_DIR_NUM] = {
266 .storage_type = OSL_MAPPED_STORAGE,
267 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
268 .name = "dir_num",
269 .compare_function = uint64_compare,
270 .data_size = sizeof(uint64_t)
271 },
272 [UT_BYTES] = {
273 .storage_type = OSL_MAPPED_STORAGE,
274 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
275 .compare_function = size_compare,
276 .name = "num_bytes",
277 .data_size = sizeof(uint64_t)
278 },
279 [UT_FILES] = {
280 .storage_type = OSL_MAPPED_STORAGE,
281 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
282 .compare_function = size_compare,
283 .name = "num_files",
284 .data_size = sizeof(uint64_t)
285 },
286 };
287
288 static struct osl_table *dir_table;
289
290 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
291 uint64_t *dir_size, uint64_t *dir_files)
292 {
293 struct osl_object dir_objects[NUM_DT_COLUMNS];
294
295 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
296 dir_objects[DT_NAME].data = dirname;
297 dir_objects[DT_NAME].size = strlen(dirname) + 1;
298 dir_objects[DT_NUM].data = dir_num;
299 dir_objects[DT_NUM].size = sizeof(*dir_num);
300 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
301 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
302 dir_objects[DT_BYTES].data = dir_size;
303 dir_objects[DT_BYTES].size = sizeof(*dir_size);
304 dir_objects[DT_FILES].data = dir_files;
305 dir_objects[DT_FILES].size = sizeof(*dir_files);
306
307 return osl_add_row(dir_table, dir_objects);
308 }
309
310 static uint32_t num_uids;
311
312 static int open_user_table(struct user_info *ui, int create)
313 {
314 int ret;
315
316 ui->desc = para_malloc(sizeof(*ui->desc));
317 ui->desc->num_columns = NUM_UT_COLUMNS;
318 ui->desc->flags = 0;
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);
324 if (create) {
325 ret = osl_create_table(ui->desc);
326 if (ret < 0)
327 goto err;
328 num_uids++;
329 }
330 ret = osl_open_table(ui->desc, &ui->table);
331 if (ret < 0)
332 goto err;
333 return 1;
334 err:
335 free((char *)ui->desc->name);
336 free((char *)ui->desc->dir);
337 free(ui->desc);
338 ui->desc->name = NULL;
339 ui->desc->dir = NULL;
340 ui->desc = NULL;
341 ui->table = NULL;
342 ui->flags = 0;
343 return ret;
344 }
345
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
350
351 static void create_hash_table(void)
352 {
353 uid_hash_table = para_calloc(uid_hash_table_size
354 * sizeof(struct user_info));
355 }
356
357 static void free_hash_table(void)
358 {
359 free(uid_hash_table);
360 uid_hash_table = NULL;
361 }
362
363 static int create_tables(void)
364 {
365 int ret;
366
367 dir_table_desc.dir = para_strdup(conf.database_dir_arg);
368 ret = osl_create_table(&dir_table_desc);
369 if (ret < 0)
370 return ret;
371 create_hash_table();
372 return 1;
373 }
374
375 /*
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
379 * same slot.
380 *
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
384 * odd number.
385 *
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].
390 */
391 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
392 {
393 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
394 % uid_hash_table_size;
395 }
396
397 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui && ui < uid_hash_table \
398 + uid_hash_table_size; ui++)
399
400 enum search_uid_flags {
401 OPEN_USER_TABLE = 1,
402 CREATE_USER_TABLE = 2,
403 };
404
405 static int uid_is_admissible(uint32_t uid)
406 {
407 int i;
408
409 for (i = 0; i < conf.uid_given; i++) {
410 struct uid_range *ur = admissible_uids + i;
411
412 if (ur->low <= uid && ur->high >= uid)
413 break;
414 }
415 i = !conf.uid_given || i < conf.uid_given;
416 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
417 i? "" : "not ");
418 return i;
419 }
420
421 static int search_uid(uint32_t uid, enum search_uid_flags flags,
422 struct user_info **ui_ptr)
423 {
424 uint32_t p;
425
426 for (p = 0; p < uid_hash_table_size; p++) {
427 struct user_info *ui = uid_hash_table + double_hash(uid, p);
428
429 if (!ui_used(ui)) {
430 int ret;
431 if (!flags)
432 return -E_BAD_UID;
433 ui->uid = uid;
434 ui->flags |= UI_FL_SLOT_USED;
435 if (!uid_is_admissible(uid))
436 return 0;
437 ui->flags |= UI_FL_ADMISSIBLE;
438 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
439 if (ret < 0)
440 return ret;
441
442 if (ui_ptr)
443 *ui_ptr = ui;
444 return 1;
445 }
446 if (ui->uid != uid)
447 continue;
448 if (ui_ptr)
449 *ui_ptr = ui;
450 return 0;
451 }
452 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
453 }
454
455 static int update_user_row(struct osl_table *t, uint64_t dir_num,
456 uint64_t *add)
457 {
458 struct osl_row *row;
459 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
460
461 int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
462
463 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
464 return ret;
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;
468
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_add_row(t, objects);
477 INFO_LOG("######################### ret: %d\n", ret);
478 return ret;
479 } else { /* add size and increment file count */
480 uint64_t num;
481 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
482
483 ret = osl_get_object(t, row, UT_BYTES, &obj1);
484 if (ret < 0)
485 return ret;
486 num = *(uint64_t *)obj1.data + *add;
487 ret = osl_update_object(t, row, UT_BYTES, &obj2);
488 if (ret < 0)
489 return ret;
490 ret = osl_get_object(t, row, UT_FILES, &obj1);
491 if (ret < 0)
492 return ret;
493 num = *(uint64_t *)obj1.data + 1;
494 return osl_update_object(t, row, UT_FILES, &obj2);
495 }
496 }
497
498 static uint64_t num_dirs;
499 static uint64_t num_files;
500 static uint64_t num_bytes;
501
502 int scan_dir(char *dirname, uint64_t *parent_dir_num)
503 {
504 DIR *dir;
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;
509
510 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
511 ret = para_opendir(dirname, &dir, &cwd_fd);
512 if (ret < 0) {
513 if (ret != -ERRNO_TO_ERROR(EACCES))
514 return ret;
515 WARNING_LOG("permission denied for %s\n", dirname);
516 return 1;
517 }
518 while ((entry = readdir(dir))) {
519 mode_t m;
520 struct stat s;
521 uint32_t uid;
522 uint64_t size;
523 struct user_info *ui;
524
525 if (!strcmp(entry->d_name, "."))
526 continue;
527 if (!strcmp(entry->d_name, ".."))
528 continue;
529 if (lstat(entry->d_name, &s) == -1) {
530 WARNING_LOG("lstat error for %s/%s\n", dirname,
531 entry->d_name);
532 continue;
533 }
534 m = s.st_mode;
535 if (!S_ISREG(m) && !S_ISDIR(m))
536 continue;
537 if (S_ISDIR(m)) {
538 ret = scan_dir(entry->d_name, &this_dir_num);
539 if (ret < 0)
540 goto out;
541 continue;
542 }
543 /* regular file */
544 size = s.st_size;
545 dir_size += size;
546 num_bytes += size;
547 dir_files++;
548 num_files++;
549 uid = s.st_uid;
550 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
551 if (ret < 0)
552 goto out;
553 ui->bytes += size;
554 ui->files++;
555 ret = update_user_row(ui->table, this_dir_num, &size);
556 if (ret < 0)
557 goto out;
558 }
559 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
560 &dir_size, &dir_files);
561 out:
562 closedir(dir);
563 ret2 = para_fchdir(cwd_fd);
564 if (ret2 < 0 && ret >= 0)
565 ret = ret2;
566 close(cwd_fd);
567 return ret;
568 }
569
570 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
571 {
572 char *result = NULL, *tmp;
573 struct osl_row *row;
574 uint64_t val = *dirnum;
575 struct osl_object obj = {.data = &val, .size = sizeof(val)};
576 int ret;
577
578 again:
579 ret = osl_get_row(dir_table, DT_NUM, &obj, &row);
580 if (ret < 0)
581 goto out;
582 ret = osl_get_object(dir_table, row, DT_NAME, &obj);
583 if (ret < 0)
584 goto out;
585 if (result) {
586 tmp = make_message("%s/%s", (char *)obj.data, result);
587 free(result);
588 result = tmp;
589 } else
590 result = para_strdup((char *)obj.data);
591 ret = osl_get_object(dir_table, row, DT_PARENT_NUM, &obj);
592 if (ret < 0)
593 goto out;
594 val = *(uint64_t *)obj.data;
595 if (val)
596 goto again;
597 out:
598 if (ret < 0) {
599 free(result);
600 *name = NULL;
601 } else
602 *name = result;
603 return ret;
604 }
605
606 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
607 {
608 struct osl_object obj;
609 int ret;
610 char *this_dir, *prefix = NULL;
611
612 *name = NULL;
613 ret = osl_get_object(dir_table, dir_table_row, DT_NAME, &obj);
614 if (ret < 0)
615 return ret;
616 this_dir = para_strdup((char *)obj.data);
617 ret = osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj);
618 if (ret < 0)
619 goto out;
620 if (!*(uint64_t *)obj.data) {
621 *name = this_dir;
622 return 1;
623 }
624 ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
625 if (ret < 0)
626 goto out;
627 *name = make_message("%s/%s", prefix, this_dir);
628 free(prefix);
629 ret = 1;
630 out:
631 free(this_dir);
632 return ret;
633 }
634
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,
641 };
642
643 const uint64_t count_unit_divisors[] = {
644
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,
650 };
651
652 const char size_unit_abbrevs[] = " BKMGT";
653 const char count_unit_abbrevs[] = " KMGT";
654
655 static void format_size_value(enum enum_size_unit unit, uint64_t value, char *result)
656 {
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++)
659 ; /* nothing */
660 sprintf(result, "%llu%c", (long long unsigned)value / size_unit_divisors[unit], size_unit_abbrevs[unit]);
661 }
662
663 static void format_count_value(enum enum_count_unit unit, uint64_t value, char *result)
664 {
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++)
667 ; /* nothing */
668 sprintf(result, "%llu%c", (long long unsigned)value / count_unit_divisors[unit], count_unit_abbrevs[unit]);
669 }
670
671 enum global_stats_flags {
672 GSF_PRINT_DIRNAME = 1,
673 GSF_PRINT_BYTES = 2,
674 GSF_PRINT_FILES = 4,
675 GSF_COMPUTE_SUMMARY = 8,
676 };
677
678 struct global_stats_info {
679 uint32_t count;
680 enum global_stats_flags flags;
681 };
682
683 static int global_stats_loop_function(struct osl_row *row, void *data)
684 {
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;
689
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);
694 if (ret < 0)
695 return ret;
696 printf("%s%s", dirname,
697 (gsi->flags & (GSF_PRINT_FILES | GSF_PRINT_BYTES))?
698 "\t" : "\n"
699 );
700 }
701 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
702 uint64_t files;
703 ret = osl_get_object(dir_table, row, DT_FILES, &obj);
704 if (ret < 0)
705 return ret;
706 files = *(uint64_t *)obj.data;
707 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
708 format_size_value(conf.size_unit_arg, files,
709 formated_value);
710 printf("%s%s", formated_value,
711 (gsi->flags & GSF_PRINT_BYTES)? "\t" : "\n");
712 }
713 if (summary)
714 num_files += files;
715 }
716 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
717 uint64_t bytes;
718 ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
719 if (ret < 0)
720 return ret;
721 bytes = *(uint64_t *)obj.data;
722 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
723 format_size_value(conf.size_unit_arg, bytes,
724 formated_value);
725 printf("%s\n", formated_value);
726 }
727 if (summary) {
728 num_bytes += bytes;
729 num_dirs++;
730 }
731 }
732 if (gsi->count > 0)
733 gsi->count--;
734 return 1;
735 }
736
737 static void print_id_stats(void)
738 {
739 struct user_info *ui;
740
741 printf("--------------------- user summary (uid/dirs/files/bytes):\n");
742 FOR_EACH_USER(ui) {
743 char formated_dir_count[25], formated_file_count[25],
744 formated_bytes[25];
745 if (!ui_used(ui))
746 continue;
747 format_count_value(conf.count_unit_arg, ui->dirs,
748 formated_dir_count);
749 format_count_value(conf.count_unit_arg, ui->files,
750 formated_file_count);
751 format_size_value(conf.size_unit_arg, ui->bytes,
752 formated_bytes);
753 printf("%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
754 formated_dir_count,
755 formated_file_count,
756 formated_bytes
757 );
758 }
759 }
760
761 enum user_stats_flags {
762 USF_PRINT_DIRNAME = 1,
763 USF_PRINT_BYTES = 2,
764 USF_PRINT_FILES = 4,
765 USF_COMPUTE_SUMMARY = 8,
766 };
767
768 struct user_stats_info {
769 uint32_t count;
770 enum user_stats_flags flags;
771 struct user_info *ui;
772 };
773
774 static int user_stats_loop_function(struct osl_row *row, void *data)
775 {
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];
780
781 if (!usi->count && !summary)
782 return -E_LOOP_COMPLETE;
783 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
784 char *dirname;
785 ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
786 if (ret < 0)
787 return ret;
788 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
789 if (ret < 0)
790 return ret;
791 printf("%s%s",
792 dirname,
793 (usi->flags & (USF_PRINT_FILES | USF_PRINT_BYTES))?
794 "\t" : "\n"
795 );
796 }
797 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
798 uint64_t files;
799 ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
800 if (ret < 0)
801 return ret;
802 files = *(uint64_t *)obj.data;
803 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
804 format_size_value(conf.size_unit_arg, files,
805 formated_value);
806 printf("%s%s", formated_value,
807 (usi->flags & USF_PRINT_BYTES)? "\t" : "\n"
808 );
809 }
810 if (summary)
811 usi->ui->files += files;
812 }
813 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
814 uint64_t bytes;
815 ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
816 if (ret < 0)
817 return ret;
818 bytes = *(uint64_t *)obj.data;
819 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
820 format_size_value(conf.size_unit_arg, bytes,
821 formated_value);
822 printf("%s\n", formated_value);
823 }
824 if (summary) {
825 usi->ui->bytes += bytes;
826 usi->ui->dirs++;
827 }
828
829 }
830 if (usi->count > 0)
831 usi->count--;
832 return 1;
833 }
834
835 static void print_user_stats(void)
836 {
837 struct user_info *ui;
838
839 FOR_EACH_USER(ui) {
840 struct user_stats_info usi = {
841 .count = conf.limit_arg,
842 .ui = ui
843 };
844 if (!ui_used(ui) || !ui_admissible(ui))
845 continue;
846 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
847 printf("************************************************ uid %u\n",
848 (unsigned) ui->uid);
849 printf("----------------- Largest dirs -------------------\n");
850 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_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
856 user_stats_loop_function);
857 }
858 }
859
860 static int print_statistics(void)
861 {
862 int ret;
863 struct global_stats_info gsi = {
864 .count = conf.limit_arg,
865 .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
866 };
867
868 printf("----------------- Largest dirs -------------------\n");
869 ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
870 global_stats_loop_function);
871 if (ret < 0 && ret != -E_LOOP_COMPLETE)
872 return ret;
873 gsi.count = conf.limit_arg;
874
875 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
876 printf("---------- dirs containing most files ------------\n");
877 ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
878 global_stats_loop_function);
879 if (ret < 0 && ret != -E_LOOP_COMPLETE)
880 return ret;
881
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);
886 print_user_stats();
887 print_id_stats();
888 return 1;
889 }
890
891 static char *get_uid_list_name(void)
892 {
893 return make_message("%s/uid_list", conf.database_dir_arg);
894 }
895
896 static int write_uid_list(void)
897 {
898 char *buf, *filename;
899 uint32_t count = 0;
900 struct user_info *ui;
901 size_t size = num_uids * sizeof(uint32_t);
902 int ret;
903
904 if (!num_uids)
905 return 0;
906 buf = para_malloc(size);
907 FOR_EACH_USER(ui) {
908 if (!ui_used(ui) || !ui_admissible(ui))
909 continue;
910 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
911 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
912 }
913 filename = get_uid_list_name();
914 ret = para_write_file(filename, buf, size);
915 free(filename);
916 free(buf);
917 return ret;
918 }
919
920 static int open_dir_table(void)
921 {
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_open_table(&dir_table_desc, &dir_table);
925 }
926
927 static void close_dir_table(void)
928 {
929 int ret;
930
931 if (!dir_table)
932 return;
933 ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
934 if (ret < 0)
935 ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
936 free((char *)dir_table_desc.dir);
937 dir_table = NULL;
938 }
939
940 static void close_user_table(struct user_info *ui)
941 {
942 int ret;
943
944 if (!ui || !ui_used(ui) || !ui_admissible(ui))
945 return;
946 ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
947 if (ret < 0)
948 ERROR_LOG("failed to close user table %u: %s\n",
949 (unsigned) ui->uid, error_txt(-ret));
950 free((char *)ui->desc->name);
951 ui->desc->name = NULL;
952 free((char *)ui->desc->dir);
953 ui->desc->dir = NULL;
954 free(ui->desc);
955 ui->desc = NULL;
956 ui->table = NULL;
957 ui->flags = 0;
958 }
959
960 static void close_user_tables(void)
961 {
962 struct user_info *ui;
963
964 FOR_EACH_USER(ui)
965 close_user_table(ui);
966 }
967
968 static void close_all_tables(void)
969 {
970 close_dir_table();
971 close_user_tables();
972 free_hash_table();
973 }
974
975 static int com_create()
976 {
977 uint64_t zero = 0ULL;
978 int ret = create_tables();
979
980 if (ret < 0)
981 return ret;
982 ret = open_dir_table();
983 if (ret < 0)
984 return ret;
985 ret = scan_dir(conf.base_dir_arg, &zero);
986 if (ret < 0)
987 goto out;
988 ret = write_uid_list();
989 out:
990 close_all_tables();
991 return ret;
992 }
993
994 static int read_uid_file(void)
995 {
996 size_t size;
997 uint32_t n;
998 char *filename = get_uid_list_name(), *map;
999 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
1000
1001 if (ret < 0) {
1002 INFO_LOG("failed to map %s\n", filename);
1003 free(filename);
1004 return ret;
1005 }
1006 num_uids = size / 4;
1007 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
1008 free(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);
1017 if (ret < 0)
1018 goto out;
1019 }
1020 out:
1021 para_munmap(map, size);
1022 return ret;
1023 }
1024
1025 static int com_select(void)
1026 {
1027 int ret;
1028
1029 ret = open_dir_table();
1030 if (ret < 0)
1031 return ret;
1032 ret = read_uid_file();
1033 if (ret < 0)
1034 return ret;
1035 ret = print_statistics();
1036 close_all_tables();
1037 return ret;
1038 }
1039
1040 static int check_args(void)
1041 {
1042 int i, ret;
1043
1044 if (!conf.uid_given)
1045 return 0;
1046
1047 admissible_uids = para_malloc(conf.uid_given * sizeof(*admissible_uids));
1048
1049 for (i = 0; i < conf.uid_given; i++) {
1050 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
1051 if (ret < 0)
1052 goto err;
1053 }
1054 return 1;
1055 err:
1056 free(admissible_uids);
1057 admissible_uids = NULL;
1058 return ret;
1059 }
1060
1061 int main(int argc, char **argv)
1062 {
1063 int ret;
1064 struct cmdline_parser_params params = {
1065 .override = 0,
1066 .initialize = 1,
1067 .check_required = 0,
1068 .check_ambiguity = 0,
1069 .print_errors = 1
1070 };
1071
1072 cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
1073 ret = check_args();
1074 if (ret < 0)
1075 goto out;
1076 ret = -E_SYNTAX;
1077 if (conf.select_given)
1078 ret = com_select();
1079 else
1080 ret = com_create();
1081 if (ret < 0)
1082 goto out;
1083 out:
1084 free(admissible_uids);
1085 if (ret < 0) {
1086 ERROR_LOG("%s\n", error_txt(-ret));
1087 return -EXIT_FAILURE;
1088 }
1089 return EXIT_SUCCESS;
1090 }