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