forked from coreutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdf.c
1847 lines (1586 loc) · 54 KB
/
df.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* df - summarize free disk space
Copyright (C) 1991-2020 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by David MacKenzie <[email protected]>.
--human-readable option added by [email protected].
--si and large file support added by [email protected]. */
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <getopt.h>
#include <assert.h>
#include <c-ctype.h>
#include <wchar.h>
#include <wctype.h>
#include "system.h"
#include "canonicalize.h"
#include "die.h"
#include "error.h"
#include "fsusage.h"
#include "human.h"
#include "mbsalign.h"
#include "mbswidth.h"
#include "mountlist.h"
#include "quote.h"
#include "find-mount-point.h"
#include "hash.h"
#include "xstrtol-error.h"
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "df"
#define AUTHORS \
proper_name ("Torbjorn Granlund"), \
proper_name ("David MacKenzie"), \
proper_name ("Paul Eggert")
struct devlist
{
dev_t dev_num;
struct mount_entry *me;
struct devlist *next;
};
/* Filled with device numbers of examined file systems to avoid
duplicates in output. */
static Hash_table *devlist_table;
/* If true, show even file systems with zero size or
uninteresting types. */
static bool show_all_fs;
/* If true, show only local file systems. */
static bool show_local_fs;
/* If true, output data for each file system corresponding to a
command line argument -- even if it's a dummy (automounter) entry. */
static bool show_listed_fs;
/* Human-readable options for output. */
static int human_output_opts;
/* The units to use when printing sizes. */
static uintmax_t output_block_size;
/* True if a file system has been processed for output. */
static bool file_systems_processed;
/* If true, invoke the 'sync' system call before getting any usage data.
Using this option can make df very slow, especially with many or very
busy disks. Note that this may make a difference on some systems --
SunOS 4.1.3, for one. It is *not* necessary on GNU/Linux. */
static bool require_sync;
/* Desired exit status. */
static int exit_status;
/* A file system type to display. */
struct fs_type_list
{
char *fs_name;
struct fs_type_list *fs_next;
};
/* Linked list of file system types to display.
If 'fs_select_list' is NULL, list all types.
This table is generated dynamically from command-line options,
rather than hardcoding into the program what it thinks are the
valid file system types; let the user specify any file system type
they want to, and if there are any file systems of that type, they
will be shown.
Some file system types:
4.2 4.3 ufs nfs swap ignore io vm efs dbg */
static struct fs_type_list *fs_select_list;
/* Linked list of file system types to omit.
If the list is empty, don't exclude any types. */
static struct fs_type_list *fs_exclude_list;
/* Linked list of mounted file systems. */
static struct mount_entry *mount_list;
/* If true, print file system type as well. */
static bool print_type;
/* If true, print a grand total at the end. */
static bool print_grand_total;
/* Grand total data. */
static struct fs_usage grand_fsu;
/* Display modes. */
enum
{
DEFAULT_MODE,
INODES_MODE,
HUMAN_MODE,
POSIX_MODE,
OUTPUT_MODE
};
static int header_mode = DEFAULT_MODE;
/* Displayable fields. */
typedef enum
{
SOURCE_FIELD, /* file system */
FSTYPE_FIELD, /* FS type */
SIZE_FIELD, /* FS size */
USED_FIELD, /* FS size used */
AVAIL_FIELD, /* FS size available */
PCENT_FIELD, /* percent used */
ITOTAL_FIELD, /* inode total */
IUSED_FIELD, /* inodes used */
IAVAIL_FIELD, /* inodes available */
IPCENT_FIELD, /* inodes used in percent */
TARGET_FIELD, /* mount point */
FILE_FIELD, /* specified file name */
INVALID_FIELD /* validation marker */
} display_field_t;
/* Flag if a field contains a block, an inode or another value. */
typedef enum
{
BLOCK_FLD, /* Block values field */
INODE_FLD, /* Inode values field */
OTHER_FLD /* Neutral field, e.g. target */
} field_type_t;
/* Attributes of a display field. */
struct field_data_t
{
display_field_t field;
char const *arg;
field_type_t field_type;
const char *caption;/* NULL means to use the default header of this field. */
size_t width; /* Auto adjusted (up) widths used to align columns. */
mbs_align_t align; /* Alignment for this field. */
bool used;
};
/* Header strings, minimum width and alignment for the above fields. */
static struct field_data_t field_data[] = {
[SOURCE_FIELD] = { SOURCE_FIELD,
"source", OTHER_FLD, N_("Filesystem"), 14, MBS_ALIGN_LEFT, false },
[FSTYPE_FIELD] = { FSTYPE_FIELD,
"fstype", OTHER_FLD, N_("Type"), 4, MBS_ALIGN_LEFT, false },
[SIZE_FIELD] = { SIZE_FIELD,
"size", BLOCK_FLD, N_("blocks"), 5, MBS_ALIGN_RIGHT, false },
[USED_FIELD] = { USED_FIELD,
"used", BLOCK_FLD, N_("Used"), 5, MBS_ALIGN_RIGHT, false },
[AVAIL_FIELD] = { AVAIL_FIELD,
"avail", BLOCK_FLD, N_("Available"), 5, MBS_ALIGN_RIGHT, false },
[PCENT_FIELD] = { PCENT_FIELD,
"pcent", BLOCK_FLD, N_("Use%"), 4, MBS_ALIGN_RIGHT, false },
[ITOTAL_FIELD] = { ITOTAL_FIELD,
"itotal", INODE_FLD, N_("Inodes"), 5, MBS_ALIGN_RIGHT, false },
[IUSED_FIELD] = { IUSED_FIELD,
"iused", INODE_FLD, N_("IUsed"), 5, MBS_ALIGN_RIGHT, false },
[IAVAIL_FIELD] = { IAVAIL_FIELD,
"iavail", INODE_FLD, N_("IFree"), 5, MBS_ALIGN_RIGHT, false },
[IPCENT_FIELD] = { IPCENT_FIELD,
"ipcent", INODE_FLD, N_("IUse%"), 4, MBS_ALIGN_RIGHT, false },
[TARGET_FIELD] = { TARGET_FIELD,
"target", OTHER_FLD, N_("Mounted on"), 0, MBS_ALIGN_LEFT, false },
[FILE_FIELD] = { FILE_FIELD,
"file", OTHER_FLD, N_("File"), 0, MBS_ALIGN_LEFT, false }
};
static char const *all_args_string =
"source,fstype,itotal,iused,iavail,ipcent,size,"
"used,avail,pcent,file,target";
/* Storage for the definition of output columns. */
static struct field_data_t **columns;
/* The current number of output columns. */
static size_t ncolumns;
/* Field values. */
struct field_values_t
{
uintmax_t input_units;
uintmax_t output_units;
uintmax_t total;
uintmax_t available;
bool negate_available;
uintmax_t available_to_root;
uintmax_t used;
bool negate_used;
};
/* Storage for pointers for each string (cell of table). */
static char ***table;
/* The current number of processed rows (including header). */
static size_t nrows;
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{
NO_SYNC_OPTION = CHAR_MAX + 1,
SYNC_OPTION,
TOTAL_OPTION,
OUTPUT_OPTION
};
static struct option const long_options[] =
{
{"all", no_argument, NULL, 'a'},
{"block-size", required_argument, NULL, 'B'},
{"inodes", no_argument, NULL, 'i'},
{"human-readable", no_argument, NULL, 'h'},
{"si", no_argument, NULL, 'H'},
{"local", no_argument, NULL, 'l'},
{"output", optional_argument, NULL, OUTPUT_OPTION},
{"portability", no_argument, NULL, 'P'},
{"print-type", no_argument, NULL, 'T'},
{"sync", no_argument, NULL, SYNC_OPTION},
{"no-sync", no_argument, NULL, NO_SYNC_OPTION},
{"total", no_argument, NULL, TOTAL_OPTION},
{"type", required_argument, NULL, 't'},
{"exclude-type", required_argument, NULL, 'x'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
/* Replace problematic chars with '?'.
Since only control characters are currently considered,
this should work in all encodings. */
static void
replace_control_chars (char *cell)
{
char *p = cell;
while (*p)
{
if (c_iscntrl (to_uchar (*p)))
*p = '?';
p++;
}
}
/* Replace problematic chars with '?'. */
static void
replace_invalid_chars (char *cell)
{
char *srcend = cell + strlen (cell);
char *dst = cell;
mbstate_t mbstate = { 0, };
size_t n;
for (char *src = cell; src != srcend; src += n)
{
wchar_t wc;
size_t srcbytes = srcend - src;
n = mbrtowc (&wc, src, srcbytes, &mbstate);
bool ok = n <= srcbytes;
if (ok)
ok = !iswcntrl (wc);
else
n = 1;
if (ok)
{
memmove (dst, src, n);
dst += n;
}
else
{
*dst++ = '?';
memset (&mbstate, 0, sizeof mbstate);
}
}
*dst = '\0';
}
static void
replace_problematic_chars (char *cell)
{
static int tty_out = -1;
if (tty_out < 0)
tty_out = isatty (STDOUT_FILENO);
(tty_out ? replace_invalid_chars : replace_control_chars) (cell) ;
}
/* Dynamically allocate a row of pointers in TABLE, which
can then be accessed with standard 2D array notation. */
static void
alloc_table_row (void)
{
nrows++;
table = xnrealloc (table, nrows, sizeof (char **));
table[nrows - 1] = xnmalloc (ncolumns, sizeof (char *));
}
/* Output each cell in the table, accounting for the
alignment and max width of each column. */
static void
print_table (void)
{
size_t row;
for (row = 0; row < nrows; row++)
{
size_t col;
for (col = 0; col < ncolumns; col++)
{
char *cell = table[row][col];
/* Note the SOURCE_FIELD used to be displayed on it's own line
if (!posix_format && mbswidth (cell) > 20), but that
functionality was probably more problematic than helpful,
hence changed in commit v8.10-40-g99679ff. */
if (col != 0)
putchar (' ');
int flags = 0;
if (col == ncolumns - 1) /* The last one. */
flags = MBA_NO_RIGHT_PAD;
size_t width = columns[col]->width;
cell = ambsalign (cell, &width, columns[col]->align, flags);
/* When ambsalign fails, output unaligned data. */
fputs (cell ? cell : table[row][col], stdout);
free (cell);
IF_LINT (free (table[row][col]));
}
putchar ('\n');
IF_LINT (free (table[row]));
}
IF_LINT (free (table));
}
/* Dynamically allocate a struct field_t in COLUMNS, which
can then be accessed with standard array notation. */
static void
alloc_field (int f, const char *c)
{
ncolumns++;
columns = xnrealloc (columns, ncolumns, sizeof (struct field_data_t *));
columns[ncolumns - 1] = &field_data[f];
if (c != NULL)
columns[ncolumns - 1]->caption = c;
if (field_data[f].used)
assert (!"field used");
/* Mark field as used. */
field_data[f].used = true;
}
/* Given a string, ARG, containing a comma-separated list of arguments
to the --output option, add the appropriate fields to columns. */
static void
decode_output_arg (char const *arg)
{
char *arg_writable = xstrdup (arg);
char *s = arg_writable;
do
{
/* find next comma */
char *comma = strchr (s, ',');
/* If we found a comma, put a NUL in its place and advance. */
if (comma)
*comma++ = 0;
/* process S. */
display_field_t field = INVALID_FIELD;
for (unsigned int i = 0; i < ARRAY_CARDINALITY (field_data); i++)
{
if (STREQ (field_data[i].arg, s))
{
field = i;
break;
}
}
if (field == INVALID_FIELD)
{
error (0, 0, _("option --output: field %s unknown"), quote (s));
usage (EXIT_FAILURE);
}
if (field_data[field].used)
{
/* Prevent the fields from being used more than once. */
error (0, 0, _("option --output: field %s used more than once"),
quote (field_data[field].arg));
usage (EXIT_FAILURE);
}
switch (field)
{
case SOURCE_FIELD:
case FSTYPE_FIELD:
case USED_FIELD:
case PCENT_FIELD:
case ITOTAL_FIELD:
case IUSED_FIELD:
case IAVAIL_FIELD:
case IPCENT_FIELD:
case TARGET_FIELD:
case FILE_FIELD:
alloc_field (field, NULL);
break;
case SIZE_FIELD:
alloc_field (field, N_("Size"));
break;
case AVAIL_FIELD:
alloc_field (field, N_("Avail"));
break;
default:
assert (!"invalid field");
}
s = comma;
}
while (s);
free (arg_writable);
}
/* Get the appropriate columns for the mode. */
static void
get_field_list (void)
{
switch (header_mode)
{
case DEFAULT_MODE:
alloc_field (SOURCE_FIELD, NULL);
if (print_type)
alloc_field (FSTYPE_FIELD, NULL);
alloc_field (SIZE_FIELD, NULL);
alloc_field (USED_FIELD, NULL);
alloc_field (AVAIL_FIELD, NULL);
alloc_field (PCENT_FIELD, NULL);
alloc_field (TARGET_FIELD, NULL);
break;
case HUMAN_MODE:
alloc_field (SOURCE_FIELD, NULL);
if (print_type)
alloc_field (FSTYPE_FIELD, NULL);
alloc_field (SIZE_FIELD, N_("Size"));
alloc_field (USED_FIELD, NULL);
alloc_field (AVAIL_FIELD, N_("Avail"));
alloc_field (PCENT_FIELD, NULL);
alloc_field (TARGET_FIELD, NULL);
break;
case INODES_MODE:
alloc_field (SOURCE_FIELD, NULL);
if (print_type)
alloc_field (FSTYPE_FIELD, NULL);
alloc_field (ITOTAL_FIELD, NULL);
alloc_field (IUSED_FIELD, NULL);
alloc_field (IAVAIL_FIELD, NULL);
alloc_field (IPCENT_FIELD, NULL);
alloc_field (TARGET_FIELD, NULL);
break;
case POSIX_MODE:
alloc_field (SOURCE_FIELD, NULL);
if (print_type)
alloc_field (FSTYPE_FIELD, NULL);
alloc_field (SIZE_FIELD, NULL);
alloc_field (USED_FIELD, NULL);
alloc_field (AVAIL_FIELD, NULL);
alloc_field (PCENT_FIELD, N_("Capacity"));
alloc_field (TARGET_FIELD, NULL);
break;
case OUTPUT_MODE:
if (!ncolumns)
{
/* Add all fields if --output was given without a field list. */
decode_output_arg (all_args_string);
}
break;
default:
assert (!"invalid header_mode");
}
}
/* Obtain the appropriate header entries. */
static void
get_header (void)
{
size_t col;
alloc_table_row ();
for (col = 0; col < ncolumns; col++)
{
char *cell = NULL;
char const *header = _(columns[col]->caption);
if (columns[col]->field == SIZE_FIELD
&& (header_mode == DEFAULT_MODE
|| (header_mode == OUTPUT_MODE
&& !(human_output_opts & human_autoscale))))
{
char buf[LONGEST_HUMAN_READABLE + 1];
int opts = (human_suppress_point_zero
| human_autoscale | human_SI
| (human_output_opts
& (human_group_digits | human_base_1024 | human_B)));
/* Prefer the base that makes the human-readable value more exact,
if there is a difference. */
uintmax_t q1000 = output_block_size;
uintmax_t q1024 = output_block_size;
bool divisible_by_1000;
bool divisible_by_1024;
do
{
divisible_by_1000 = q1000 % 1000 == 0; q1000 /= 1000;
divisible_by_1024 = q1024 % 1024 == 0; q1024 /= 1024;
}
while (divisible_by_1000 & divisible_by_1024);
if (divisible_by_1000 < divisible_by_1024)
opts |= human_base_1024;
if (divisible_by_1024 < divisible_by_1000)
opts &= ~human_base_1024;
if (! (opts & human_base_1024))
opts |= human_B;
char *num = human_readable (output_block_size, buf, opts, 1, 1);
/* Reset the header back to the default in OUTPUT_MODE. */
header = _("blocks");
/* TRANSLATORS: this is the "1K-blocks" header in "df" output. */
if (asprintf (&cell, _("%s-%s"), num, header) == -1)
cell = NULL;
}
else if (header_mode == POSIX_MODE && columns[col]->field == SIZE_FIELD)
{
char buf[INT_BUFSIZE_BOUND (uintmax_t)];
char *num = umaxtostr (output_block_size, buf);
/* TRANSLATORS: this is the "1024-blocks" header in "df -P". */
if (asprintf (&cell, _("%s-%s"), num, header) == -1)
cell = NULL;
}
else
cell = strdup (header);
if (!cell)
xalloc_die ();
replace_problematic_chars (cell);
table[nrows - 1][col] = cell;
size_t cell_width = mbswidth (cell, 0);
columns[col]->width = MAX (columns[col]->width, cell_width);
}
}
/* Is FSTYPE a type of file system that should be listed? */
static bool _GL_ATTRIBUTE_PURE
selected_fstype (const char *fstype)
{
const struct fs_type_list *fsp;
if (fs_select_list == NULL || fstype == NULL)
return true;
for (fsp = fs_select_list; fsp; fsp = fsp->fs_next)
if (STREQ (fstype, fsp->fs_name))
return true;
return false;
}
/* Is FSTYPE a type of file system that should be omitted? */
static bool _GL_ATTRIBUTE_PURE
excluded_fstype (const char *fstype)
{
const struct fs_type_list *fsp;
if (fs_exclude_list == NULL || fstype == NULL)
return false;
for (fsp = fs_exclude_list; fsp; fsp = fsp->fs_next)
if (STREQ (fstype, fsp->fs_name))
return true;
return false;
}
static size_t
devlist_hash (void const *x, size_t table_size)
{
struct devlist const *p = x;
return (uintmax_t) p->dev_num % table_size;
}
static bool
devlist_compare (void const *x, void const *y)
{
struct devlist const *a = x;
struct devlist const *b = y;
return a->dev_num == b->dev_num;
}
static struct devlist *
devlist_for_dev (dev_t dev)
{
if (devlist_table == NULL)
return NULL;
struct devlist dev_entry;
dev_entry.dev_num = dev;
return hash_lookup (devlist_table, &dev_entry);
}
static void
devlist_free (void *p)
{
free (p);
}
/* Filter mount list by skipping duplicate entries.
In the case of duplicates - based on the device number - the mount entry
with a '/' in its me_devname (i.e., not pseudo name like tmpfs) wins.
If both have a real devname (e.g. bind mounts), then that with the shorter
me_mountdir wins. With DEVICES_ONLY == true (set with df -a), only update
the global devlist_table, rather than filtering the global mount_list. */
static void
filter_mount_list (bool devices_only)
{
struct mount_entry *me;
/* Temporary list to keep entries ordered. */
struct devlist *device_list = NULL;
int mount_list_size = 0;
for (me = mount_list; me; me = me->me_next)
mount_list_size++;
devlist_table = hash_initialize (mount_list_size, NULL,
devlist_hash,
devlist_compare,
devlist_free);
if (devlist_table == NULL)
xalloc_die ();
/* Sort all 'wanted' entries into the list device_list. */
for (me = mount_list; me;)
{
struct stat buf;
struct mount_entry *discard_me = NULL;
/* Avoid stating remote file systems as that may hang.
On Linux we probably have me_dev populated from /proc/self/mountinfo,
however we still stat() in case another device was mounted later. */
if ((me->me_remote && show_local_fs)
|| (me->me_dummy && !show_all_fs && !show_listed_fs)
|| (!selected_fstype (me->me_type) || excluded_fstype (me->me_type))
|| -1 == stat (me->me_mountdir, &buf))
{
/* If remote, and showing just local, or FS type is excluded,
add ME for filtering later.
If stat failed; add ME to be able to complain about it later. */
buf.st_dev = me->me_dev;
}
else
{
/* If we've already seen this device... */
struct devlist *seen_dev = devlist_for_dev (buf.st_dev);
if (seen_dev)
{
bool target_nearer_root = strlen (seen_dev->me->me_mountdir)
> strlen (me->me_mountdir);
/* With bind mounts, prefer items nearer the root of the source */
bool source_below_root = seen_dev->me->me_mntroot != NULL
&& me->me_mntroot != NULL
&& (strlen (seen_dev->me->me_mntroot)
< strlen (me->me_mntroot));
if (! print_grand_total
&& me->me_remote && seen_dev->me->me_remote
&& ! STREQ (seen_dev->me->me_devname, me->me_devname))
{
/* Don't discard remote entries with different locations,
as these are more likely to be explicitly mounted.
However avoid this when producing a total to give
a more accurate value in that case. */
}
else if ((strchr (me->me_devname, '/')
/* let "real" devices with '/' in the name win. */
&& ! strchr (seen_dev->me->me_devname, '/'))
/* let points towards the root of the device win. */
|| (target_nearer_root && ! source_below_root)
/* let an entry overmounted on a new device win... */
|| (! STREQ (seen_dev->me->me_devname, me->me_devname)
/* ... but only when matching an existing mnt point,
to avoid problematic replacement when given
inaccurate mount lists, seen with some chroot
environments for example. */
&& STREQ (me->me_mountdir,
seen_dev->me->me_mountdir)))
{
/* Discard mount entry for existing device. */
discard_me = seen_dev->me;
seen_dev->me = me;
}
else
{
/* Discard mount entry currently being processed. */
discard_me = me;
}
}
}
if (discard_me)
{
me = me->me_next;
if (! devices_only)
free_mount_entry (discard_me);
}
else
{
/* Add the device number to the device_table. */
struct devlist *devlist = xmalloc (sizeof *devlist);
devlist->me = me;
devlist->dev_num = buf.st_dev;
devlist->next = device_list;
device_list = devlist;
if (hash_insert (devlist_table, devlist) == NULL)
xalloc_die ();
me = me->me_next;
}
}
/* Finally rebuild the mount_list from the devlist. */
if (! devices_only) {
mount_list = NULL;
while (device_list)
{
/* Add the mount entry. */
me = device_list->me;
me->me_next = mount_list;
mount_list = me;
device_list = device_list->next;
}
hash_free (devlist_table);
devlist_table = NULL;
}
}
/* Search a mount entry list for device id DEV.
Return the corresponding mount entry if found or NULL if not. */
static struct mount_entry const * _GL_ATTRIBUTE_PURE
me_for_dev (dev_t dev)
{
struct devlist *dl = devlist_for_dev (dev);
if (dl)
return dl->me;
return NULL;
}
/* Return true if N is a known integer value. On many file systems,
UINTMAX_MAX represents an unknown value; on AIX, UINTMAX_MAX - 1
represents unknown. Use a rule that works on AIX file systems, and
that almost-always works on other types. */
static bool
known_value (uintmax_t n)
{
return n < UINTMAX_MAX - 1;
}
/* Like human_readable (N, BUF, human_output_opts, INPUT_UNITS, OUTPUT_UNITS),
except:
- If NEGATIVE, then N represents a negative number,
expressed in two's complement.
- Otherwise, return "-" if N is unknown. */
static char const *
df_readable (bool negative, uintmax_t n, char *buf,
uintmax_t input_units, uintmax_t output_units)
{
if (! known_value (n) && !negative)
return "-";
else
{
char *p = human_readable (negative ? -n : n, buf + negative,
human_output_opts, input_units, output_units);
if (negative)
*--p = '-';
return p;
}
}
/* Logical equivalence */
#define LOG_EQ(a, b) (!(a) == !(b))
/* Add integral value while using uintmax_t for value part and separate
negation flag. It adds value of SRC and SRC_NEG to DEST and DEST_NEG.
The result will be in DEST and DEST_NEG. See df_readable to understand
how the negation flag is used. */
static void
add_uint_with_neg_flag (uintmax_t *dest, bool *dest_neg,
uintmax_t src, bool src_neg)
{
if (LOG_EQ (*dest_neg, src_neg))
{
*dest += src;
return;
}
if (*dest_neg)
*dest = -*dest;
if (src_neg)
src = -src;
if (src < *dest)
*dest -= src;
else
{
*dest = src - *dest;
*dest_neg = src_neg;
}
if (*dest_neg)
*dest = -*dest;
}
/* Return true if S ends in a string that may be a 36-byte UUID,
i.e., of the form HHHHHHHH-HHHH-HHHH-HHHH-HHHHHHHHHHHH, where
each H is an upper or lower case hexadecimal digit. */
static bool _GL_ATTRIBUTE_PURE
has_uuid_suffix (char const *s)
{
size_t len = strlen (s);
return (36 < len
&& strspn (s + len - 36, "-0123456789abcdefABCDEF") == 36);
}
/* Obtain the block values BV and inode values IV
from the file system usage FSU. */
static void
get_field_values (struct field_values_t *bv,
struct field_values_t *iv,
const struct fs_usage *fsu)
{
/* Inode values. */
iv->input_units = iv->output_units = 1;
iv->total = fsu->fsu_files;
iv->available = iv->available_to_root = fsu->fsu_ffree;
iv->negate_available = false;
iv->used = UINTMAX_MAX;
iv->negate_used = false;
if (known_value (iv->total) && known_value (iv->available_to_root))
{
iv->used = iv->total - iv->available_to_root;
iv->negate_used = (iv->total < iv->available_to_root);
}
/* Block values. */
bv->input_units = fsu->fsu_blocksize;
bv->output_units = output_block_size;
bv->total = fsu->fsu_blocks;
bv->available = fsu->fsu_bavail;
bv->available_to_root = fsu->fsu_bfree;
bv->negate_available = (fsu->fsu_bavail_top_bit_set
&& known_value (fsu->fsu_bavail));
bv->used = UINTMAX_MAX;
bv->negate_used = false;
if (known_value (bv->total) && known_value (bv->available_to_root))
{
bv->used = bv->total - bv->available_to_root;
bv->negate_used = (bv->total < bv->available_to_root);
}
}
/* Add block and inode values to grand total. */
static void
add_to_grand_total (struct field_values_t *bv, struct field_values_t *iv)
{
if (known_value (iv->total))
grand_fsu.fsu_files += iv->total;
if (known_value (iv->available))
grand_fsu.fsu_ffree += iv->available;
if (known_value (bv->total))
grand_fsu.fsu_blocks += bv->input_units * bv->total;
if (known_value (bv->available_to_root))
grand_fsu.fsu_bfree += bv->input_units * bv->available_to_root;
if (known_value (bv->available))
add_uint_with_neg_flag (&grand_fsu.fsu_bavail,
&grand_fsu.fsu_bavail_top_bit_set,
bv->input_units * bv->available,
bv->negate_available);
}
/* Obtain a space listing for the disk device with absolute file name DISK.
If MOUNT_POINT is non-NULL, it is the name of the root of the
file system on DISK.
If STAT_FILE is non-null, it is the name of a file within the file
system that the user originally asked for; this provides better
diagnostics, and sometimes it provides better results on networked
file systems that give different free-space results depending on
where in the file system you probe.
If FSTYPE is non-NULL, it is the type of the file system on DISK.
If MOUNT_POINT is non-NULL, then DISK may be NULL -- certain systems may
not be able to produce statistics in this case.
ME_DUMMY and ME_REMOTE are the mount entry flags.
Caller must set PROCESS_ALL to true when iterating over all entries, as
when df is invoked with no non-option argument. See below for details. */
static void
get_dev (char const *disk, char const *mount_point, char const* file,
char const *stat_file, char const *fstype,
bool me_dummy, bool me_remote,
const struct fs_usage *force_fsu,
bool process_all)
{
if (me_remote && show_local_fs)