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