]> git.tuebingen.mpg.de Git - adu.git/blob - select.c
Use only one copy of struct select_conf.
[adu.git] / select.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file select.c The select mode of adu. */
8
9 #include <dirent.h> /* readdir() */
10 #include "adu.h"
11 #include "gcc-compat.h"
12 #include "cmdline.h"
13 #include "fd.h"
14 #include "string.h"
15 #include "error.h"
16 #include "portable_io.h"
17 #include "select.cmdline.h"
18
19 /** Global dir count. */
20 static uint64_t num_dirs;
21 /** Global files count. */
22 static uint64_t num_files;
23 /** Global bytes count. */
24 static uint64_t num_bytes;
25
26 /** The decimal representation of an uint64_t never exceeds that size. */
27 #define FORMATED_VALUE_SIZE 25
28
29 /* these get filled in by the select command. */
30 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
31
32 enum global_stats_flags {
33         GSF_PRINT_DIRNAME = 1,
34         GSF_PRINT_BYTES = 2,
35         GSF_PRINT_FILES = 4,
36         GSF_COMPUTE_SUMMARY = 8,
37 };
38
39 struct global_stats_info {
40         uint32_t count;
41         int ret;
42         int osl_errno;
43         enum global_stats_flags flags;
44 };
45
46 enum user_stats_flags {
47         USF_PRINT_DIRNAME = 1,
48         USF_PRINT_BYTES = 2,
49         USF_PRINT_FILES = 4,
50         USF_COMPUTE_SUMMARY = 8,
51 };
52
53 struct user_stats_info {
54         uint32_t count;
55         enum user_stats_flags flags;
56         int ret;
57         int osl_errno;
58         struct user_info *ui;
59 };
60
61 static const uint64_t size_unit_divisors[] = {
62         [size_unit_arg_b] = 1ULL,
63         [size_unit_arg_k] = 1024ULL,
64         [size_unit_arg_m] = 1024ULL * 1024ULL,
65         [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
66         [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
67 };
68
69 static const uint64_t count_unit_divisors[] = {
70
71         [count_unit_arg_n] = 1ULL,
72         [count_unit_arg_k] = 1000ULL,
73         [count_unit_arg_m] = 1000ULL * 1000ULL,
74         [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
75         [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
76 };
77
78 static const char size_unit_abbrevs[] = " BKMGT";
79 static const char count_unit_abbrevs[] = "  kmgt";
80 static struct uid_range *admissible_uids;
81
82 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
83                 uint64_t value, int print_unit, char *result)
84 {
85         enum enum_size_unit u = unit;
86         char unit_buf[2] = "\0\0";
87
88         if (unit == size_unit_arg_h) /* human readable */
89                 for (u = size_unit_arg_b; u < size_unit_arg_t &&
90                                 value > size_unit_divisors[u + 1]; u++)
91                         ; /* nothing */
92         if (print_unit)
93                 unit_buf[0] = size_unit_abbrevs[u];
94         sprintf(result, "%llu%s",
95                 (long long unsigned)value / size_unit_divisors[u], unit_buf);
96         return u;
97 }
98
99 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
100                 uint64_t value, int print_unit, char *result)
101 {
102         enum enum_count_unit u = unit;
103         char unit_buf[2] = "\0\0";
104
105         if (unit == count_unit_arg_h) /* human readable */
106                 for (u = count_unit_arg_n; u < count_unit_arg_t &&
107                                 value > count_unit_divisors[u + 1]; u++)
108                         ; /* nothing */
109         if (print_unit)
110                 unit_buf[0] = count_unit_abbrevs[u];
111         sprintf(result, "%llu%s",
112                 (long long unsigned)value / count_unit_divisors[u], unit_buf);
113         return u;
114 }
115
116 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
117 {
118         char *result = NULL, *tmp;
119         struct osl_row *row;
120         uint64_t val = *dirnum;
121         struct osl_object obj;
122         int ret;
123         char *pfx;
124
125 again:
126         obj.data = &val;
127         obj.size = sizeof(val);
128         ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
129         if (ret < 0)
130                 goto out;
131         ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
132         if (ret < 0)
133                 goto out;
134         val = *(uint64_t *)obj.data;
135         ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
136         if (ret < 0)
137                 goto out;
138         pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data :  ".";
139         tmp = make_message("%s/%s", pfx, result? result : "");
140         free(result);
141         result = tmp;
142         if (val)
143                 goto again;
144 out:
145         if (ret < 0) {
146                 free(result);
147                 *name = NULL;
148         } else {
149                 assert(result);
150                 *name = result;
151         }
152         return ret;
153 }
154
155 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
156 {
157         struct osl_object obj;
158         int ret;
159
160         *name = NULL;
161         ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
162         if (ret < 0)
163                 return ret;
164         return get_dir_name_by_number((uint64_t *)obj.data, name);
165 }
166
167 static int user_stats_loop_function(struct osl_row *row, void *data)
168 {
169         struct user_stats_info *usi = data;
170         struct osl_object obj;
171         int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
172         char formated_value[FORMATED_VALUE_SIZE];
173
174         check_signals();
175         if (!usi->count && !summary) {
176                 ret = -E_LOOP_COMPLETE;
177                 goto err;
178         }
179         if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
180                 uint64_t files;
181                 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
182                 if (ret < 0)
183                         goto err;
184                 files = *(uint64_t *)obj.data;
185                 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
186                         format_count_value(select_conf.count_unit_arg, files,
187                                 select_conf.count_unit_arg == count_unit_arg_h,
188                                 formated_value);
189                         printf("\t%s%s", formated_value,
190                                 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
191                                         "\t" : "\n"
192                         );
193                 }
194                 if (summary)
195                         usi->ui->files += files;
196         }
197         if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
198                 uint64_t bytes;
199                 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
200                 if (ret < 0)
201                         goto err;
202                 bytes = *(uint64_t *)obj.data;
203                 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
204                         format_size_value(select_conf.size_unit_arg, bytes,
205                                 select_conf.size_unit_arg == size_unit_arg_h,
206                                 formated_value);
207                         printf("%s%s%s",
208                                 (usi->flags & USF_PRINT_FILES)? "" : "\t",
209                                 formated_value,
210                                 usi->flags & USF_PRINT_DIRNAME?  "\t" : "\n"
211                         );
212                 }
213                 if (summary) {
214                         usi->ui->bytes += bytes;
215                         usi->ui->dirs++;
216                 }
217
218         }
219         if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
220                 char *dirname;
221                 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
222                 if (ret < 0)
223                         goto err;
224                 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
225                 if (ret < 0)
226                         goto err;
227                 printf("%s%s\n",
228                         (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
229                         dirname);
230                 free(dirname);
231         }
232         if (usi->count > 0)
233                 usi->count--;
234         return 1;
235 err:
236         usi->ret = ret;
237         usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
238         return -1;
239 }
240
241 static int global_stats_loop_function(struct osl_row *row, void *data)
242 {
243         struct global_stats_info *gsi = data;
244         struct osl_object obj;
245         char *dirname, formated_value[FORMATED_VALUE_SIZE];
246         int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
247
248         check_signals();
249         if (!gsi->count && !summary) {
250                 ret = -E_LOOP_COMPLETE;
251                 goto err;
252         }
253         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
254                 uint64_t files;
255                 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
256                 if (ret < 0)
257                         goto err;
258                 files = *(uint64_t *)obj.data;
259                 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
260                         format_count_value(select_conf.count_unit_arg, files,
261                                 select_conf.count_unit_arg == count_unit_arg_h,
262                                 formated_value);
263                         printf("\t%s%s", formated_value,
264                                 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
265                                 "\t" : "\n");
266                 }
267                 if (summary)
268                         num_files += files;
269         }
270         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
271                 uint64_t bytes;
272                 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
273                 if (ret < 0)
274                         goto err;
275                 bytes = *(uint64_t *)obj.data;
276                 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
277                         format_size_value(select_conf.size_unit_arg, bytes,
278                                 select_conf.size_unit_arg == size_unit_arg_h,
279                                 formated_value);
280                         printf("%s%s%s",
281                                 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
282                                 formated_value,
283                                 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
284                         );
285                 }
286                 if (summary) {
287                         num_bytes += bytes;
288                         num_dirs++;
289                 }
290         }
291         if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
292                 ret = get_dir_name_of_row(row, &dirname);
293                 if (ret < 0)
294                         goto err;
295                 printf("%s%s\n",
296                         (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
297                         dirname);
298                 free(dirname);
299         }
300         if (gsi->count > 0)
301                 gsi->count--;
302         return 1;
303 err:
304         gsi->ret = ret;
305         gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
306         return -1;
307 }
308
309 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
310 {
311         if (ret >= 0)
312                 return ret;
313         assert(ret == -E_OSL);
314         if (osl_errno != E_OSL_LOOP)
315                 /* error not caused by loop function returning negative. */
316                 return ret;
317         assert(loop_ret < 0);
318         if (loop_ret == -E_LOOP_COMPLETE) /* no error */
319                 return 1;
320         if (loop_ret == -E_OSL) { /* osl error in loop function */
321                 assert(loop_osl_errno);
322                 osl_errno = loop_osl_errno;
323         }
324         return loop_ret;
325 }
326
327 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
328                 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
329 {
330         int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
331         return check_loop_return(ret, *loop_ret, *loop_osl_errno);
332 }
333
334 static void print_global_summary(void)
335 {
336         char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
337                 s[FORMATED_VALUE_SIZE];
338         enum enum_count_unit ud, uf;
339         enum enum_size_unit us;
340
341         if (select_conf.no_global_summary_given)
342                 return;
343         ud = format_count_value(select_conf.count_unit_arg, num_dirs, 0, d);
344         uf = format_count_value(select_conf.count_unit_arg, num_files, 0, f);
345         us = format_size_value(select_conf.size_unit_arg, num_bytes, 0, s);
346
347         if (!select_conf.no_headers_given)
348                 printf("Global summary "
349                         "(dirs(%c)/files(%c)/size(%c))\n",
350                         count_unit_abbrevs[ud],
351                         count_unit_abbrevs[uf],
352                         size_unit_abbrevs[us]
353                 );
354         printf("\t%s\t%s\t%s\n\n", d, f, s);
355 }
356
357 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
358 {
359         char formated_dir_count[FORMATED_VALUE_SIZE],
360                 formated_file_count[FORMATED_VALUE_SIZE],
361                 formated_bytes[FORMATED_VALUE_SIZE ];
362
363         format_count_value(select_conf.count_unit_arg, ui->dirs,
364                 select_conf.count_unit_arg == count_unit_arg_h,
365                 formated_dir_count);
366         format_count_value(select_conf.count_unit_arg, ui->files,
367                 select_conf.count_unit_arg == count_unit_arg_h,
368                 formated_file_count);
369         format_size_value(select_conf.size_unit_arg, ui->bytes,
370                 select_conf.size_unit_arg == size_unit_arg_h,
371                 formated_bytes);
372         printf("\t%s\t%u\t%s\t%s\t%s\n",
373                 ui->pw_name? ui->pw_name : "?",
374                 (unsigned)ui->uid,
375                 formated_dir_count,
376                 formated_file_count,
377                 formated_bytes
378         );
379         return 1;
380 }
381
382 static int name_comp(const void *a, const void *b)
383 {
384         char *x = ((struct user_info *)a)->pw_name;
385         char *y = ((struct user_info *)b)->pw_name;
386
387         if (!x)
388                 return 1;
389         if (!y)
390                 return -1;
391         return strcmp(x, y);
392 }
393
394 static int uid_comp(const void *a, const void *b)
395 {
396         return -NUM_COMPARE(((struct user_info *)a)->uid,
397                 ((struct user_info *)b)->uid);
398 }
399
400 static int dir_count_comp(const void *a, const void *b)
401 {
402         return NUM_COMPARE(((struct user_info *)a)->dirs,
403                 ((struct user_info *)b)->dirs);
404 }
405
406 static int file_count_comp(const void *a, const void *b)
407 {
408         return NUM_COMPARE(((struct user_info *)a)->files,
409                 ((struct user_info *)b)->files);
410 }
411
412 static int size_comp(const void *a, const void *b)
413 {
414         return NUM_COMPARE(((struct user_info *)a)->bytes,
415                 ((struct user_info *)b)->bytes);
416 }
417
418 /*
419  * The comparators for sorting the user summary.
420  *
421  * This is an array of pointers to functions taking two constant void *
422  * pointers and returning an int.
423  */
424 static int (*summary_comparators[])(const void *, const void *) = {
425         [user_summary_sort_arg_name] = name_comp,
426         [user_summary_sort_arg_uid] = uid_comp,
427         [user_summary_sort_arg_dir_count] = dir_count_comp,
428         [user_summary_sort_arg_file_count] = file_count_comp,
429         [user_summary_sort_arg_size] = size_comp,
430 };
431
432 static void print_user_summary(void)
433 {
434         if (select_conf.no_user_summary_given)
435                 return;
436         if (!select_conf.no_headers_given)
437                 printf("User summary "
438                         "(pw_name/uid/dirs%s/files%s/size%s):\n",
439                         count_unit_buf, count_unit_buf, size_unit_buf);
440         sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
441         for_each_admissible_user(print_user_summary_line, NULL);
442 }
443
444 static int print_user_list(struct user_info *ui, __a_unused void *data)
445 {
446         int ret;
447         struct user_stats_info usi;
448         enum enum_user_list ula = select_conf.user_list_arg;
449         int print_size_list = (ula == user_list_arg_size
450                 || ula == user_list_arg_both);
451
452         if (print_size_list) {
453                 usi.count = select_conf.limit_arg;
454                 usi.ui = ui;
455                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
456                 if (!select_conf.no_headers_given)
457                         printf("%s (uid %u), by size%s:\n",
458                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
459                                 size_unit_buf);
460                 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
461                         &usi.ret, &usi.osl_errno);
462                 if (ret < 0)
463                         return ret;
464                 printf("\n");
465         }
466         if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
467                 if (!select_conf.no_headers_given)
468                         printf("%s (uid %u), by file count%s:\n",
469                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
470                                 count_unit_buf);
471                 usi.count = select_conf.limit_arg,
472                 usi.ui = ui;
473                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
474                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
475                         &usi.ret, &usi.osl_errno);
476                 if (ret < 0)
477                         return ret;
478                 printf("\n");
479         }
480         if (ula == user_list_arg_none && !select_conf.no_user_summary_given) {
481                 usi.count = select_conf.limit_arg;
482                 usi.ui = ui;
483                 usi.flags = USF_COMPUTE_SUMMARY;
484                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
485                         &usi.ret, &usi.osl_errno);
486                 if (ret < 0)
487                         return ret;
488         }
489         return 1;
490 }
491
492 static int print_user_lists(void)
493 {
494         return for_each_admissible_user(print_user_list, NULL);
495 }
496
497 static int print_global_lists(void)
498 {
499         struct global_stats_info gsi;
500         int ret;
501         enum enum_global_list gla = select_conf.global_list_arg;
502         int print_size_list = (gla == global_list_arg_size
503                 || gla == global_list_arg_both);
504
505         if (print_size_list) {
506                 gsi.count = select_conf.limit_arg;
507                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
508                 if (!select_conf.no_headers_given)
509                         printf("By size%s:\n", size_unit_buf);
510                 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
511                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
512                 if (ret < 0)
513                         return ret;
514                 printf("\n");
515         }
516         if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
517                 gsi.count = select_conf.limit_arg;
518                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
519                 if (!print_size_list)
520                         gsi.flags |= GSF_COMPUTE_SUMMARY;
521                 if (!select_conf.no_headers_given)
522                         printf("By file count%s:\n", count_unit_buf);
523                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
524                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
525                 if (ret < 0)
526                         return ret;
527                 printf("\n");
528         }
529         if (gla == global_list_arg_none && !select_conf.no_global_summary_given) {
530                 /* must compute summary */
531                 gsi.count = select_conf.limit_arg;
532                 gsi.flags = GSF_COMPUTE_SUMMARY;
533                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
534                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
535                 if (ret < 0)
536                         return ret;
537         }
538         return 1;
539 }
540
541 static int print_statistics(void)
542 {
543         int ret;
544
545         ret = print_global_lists();
546         if (ret < 0)
547                 return ret;
548         print_global_summary();
549         print_user_lists();
550         print_user_summary();
551         return 1;
552 }
553
554 static int read_uid_file(void)
555 {
556         size_t size;
557         uint32_t n;
558         char *filename = get_uid_list_name(), *map;
559         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
560         unsigned bits;
561
562         if (ret < 0) {
563                 INFO_LOG("failed to map %s\n", filename);
564                 free(filename);
565                 return ret;
566         }
567         num_uids = size / 4;
568         INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
569         free(filename);
570         /*
571          * Compute number of hash table bits. The hash table size must be a
572          * power of two and larger than the number of uids.
573          */
574         bits = 2;
575         while (1 << bits < num_uids)
576                 bits++;
577         create_hash_table(bits);
578         for (n = 0; n < num_uids; n++) {
579                 uint32_t uid = read_u32(map + n * sizeof(uid));
580                 ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
581                 if (ret < 0)
582                         goto out;
583         }
584 out:
585         adu_munmap(map, size);
586         return ret;
587 }
588
589 int com_select(void)
590 {
591         int ret;
592         if (conf.select_options_given) {
593                 struct select_cmdline_parser_params params = {
594                         .override = 1,
595                         .initialize = 1,
596                         .check_required = 1,
597                         .check_ambiguity = 1,
598                         .print_errors = 1
599                 };
600
601                 if (select_cmdline_parser_string_ext(conf.select_options_arg,
602                         &select_conf, "select", &params))
603                         return -E_SYNTAX;
604                 ret = parse_uid_arg(select_conf.uid_arg, &admissible_uids);
605                 if (ret < 0)
606                         return ret;
607         }
608
609         if (select_conf.count_unit_arg != count_unit_arg_h)
610                 count_unit_buf[1] = count_unit_abbrevs[select_conf.count_unit_arg];
611         else
612                 count_unit_buf[0] = '\0';
613         if (select_conf.size_unit_arg != size_unit_arg_h)
614                 size_unit_buf[1] = size_unit_abbrevs[select_conf.size_unit_arg];
615         else
616                 size_unit_buf[0] = '\0';
617
618         ret = open_dir_table(0);
619         if (ret < 0)
620                 return ret;
621         check_signals();
622         ret = read_uid_file();
623         if (ret < 0)
624                 return ret;
625         check_signals();
626         ret = print_statistics();
627         close_all_tables();
628         return ret;
629 }