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