7150e258886ac4a80daebf243edda86f5b74bbf2
[dragonfly.git] / usr.bin / du / du.c
1 /*
2  * Copyright (c) 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Newcomb.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
37  * @(#)du.c     8.5 (Berkeley) 5/4/95
38  * $FreeBSD: src/usr.bin/du/du.c,v 1.17.2.4 2002/12/12 16:29:39 trhodes Exp $
39  * $DragonFly: src/usr.bin/du/du.c,v 1.9 2006/01/12 13:43:10 corecode Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/stat.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fnmatch.h>
49 #include <fts.h>
50 #include <math.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sysexits.h>
55 #include <unistd.h>
56
57 #define KILO_SZ(n) (n)
58 #define MEGA_SZ(n) ((n) * (n))
59 #define GIGA_SZ(n) ((n) * (n) * (n))
60 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
61 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
62
63 #define KILO_2_SZ (KILO_SZ(1024ULL))
64 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
65 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
66 #define TERA_2_SZ (TERA_SZ(1024ULL))
67 #define PETA_2_SZ (PETA_SZ(1024ULL))
68
69 #define KILO_SI_SZ (KILO_SZ(1000ULL))
70 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
71 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
72 #define TERA_SI_SZ (TERA_SZ(1000ULL))
73 #define PETA_SI_SZ (PETA_SZ(1000ULL))
74
75 #define HASHSIZE        256             /* power of 2 only */
76 #define HASHMASK        (HASHSIZE - 1)
77
78 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
79 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
80 unsigned long long *valp;
81
82 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
83
84 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
85
86 SLIST_HEAD(ignhead, ignentry) ignores;
87 struct ignentry {
88         char                    *mask;
89         SLIST_ENTRY(ignentry)   next;
90 };
91
92 static int      linkchk(FTSENT *);
93 static void     usage(void);
94 void            prthumanval(double);
95 unit_t          unit_adjust(double *);
96 void            ignoreadd(const char *);
97 void            ignoreclean(void);
98 int             ignorep(FTSENT *);
99
100 static char period[] = ".";
101
102 int
103 main(int argc, char **argv)
104 {
105         FTS             *fts;
106         FTSENT          *p;
107         long            blocksize, savednumber = 0;
108         int             ftsoptions;
109         int             listall;
110         int             depth;
111         int             Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
112         char            **save;
113
114         Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
115         
116         save = argv;
117         ftsoptions = 0;
118         depth = INT_MAX;
119         SLIST_INIT(&ignores);
120         
121         while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1)
122                 switch (ch) {
123                         case 'H':
124                                 Hflag = 1;
125                                 break;
126                         case 'I':
127                                 ignoreadd(optarg);
128                                 break;
129                         case 'L':
130                                 if (Pflag)
131                                         usage();
132                                 Lflag = 1;
133                                 break;
134                         case 'P':
135                                 if (Lflag)
136                                         usage();
137                                 Pflag = 1;
138                                 break;
139                         case 'a':
140                                 aflag = 1;
141                                 break;
142                         case 's':
143                                 sflag = 1;
144                                 break;
145                         case 'd':
146                                 dflag = 1;
147                                 errno = 0;
148                                 depth = atoi(optarg);
149                                 if (errno == ERANGE || depth < 0) {
150                                         warnx("invalid argument to option d: %s", optarg);
151                                         usage();
152                                 }
153                                 break;
154                         case 'c':
155                                 cflag = 1;
156                                 break;
157                         case 'h':
158                                 if (putenv("BLOCKSIZE=512") == -1)
159                                         warn("putenv: cannot set BLOCKSIZE=512");
160                                 hflag = 1;
161                                 valp = vals_base2;
162                                 break;
163                         case 'k':
164                                 hflag = 0;
165                                 if (putenv("BLOCKSIZE=1024") == -1)
166                                         warn("putenv: cannot set BLOCKSIZE=1024");
167                                 break;
168                         case 'r':                /* Compatibility. */
169                                 break;
170                         case 'x':
171                                 ftsoptions |= FTS_XDEV;
172                                 break;
173                         case '?':
174                         default:
175                                 usage();
176                 }
177
178         argc -= optind;
179         argv += optind;
180
181         /*
182          * XXX
183          * Because of the way that fts(3) works, logical walks will not count
184          * the blocks actually used by symbolic links.  We rationalize this by
185          * noting that users computing logical sizes are likely to do logical
186          * copies, so not counting the links is correct.  The real reason is
187          * that we'd have to re-implement the kernel's symbolic link traversing
188          * algorithm to get this right.  If, for example, you have relative
189          * symbolic links referencing other relative symbolic links, it gets
190          * very nasty, very fast.  The bottom line is that it's documented in
191          * the man page, so it's a feature.
192          */
193
194         if (Hflag + Lflag + Pflag > 1)
195                 usage();
196
197         if (Hflag + Lflag + Pflag == 0)
198                 Pflag = 1;                      /* -P (physical) is default */
199
200         if (Hflag)
201                 ftsoptions |= FTS_COMFOLLOW;
202
203         if (Lflag)
204                 ftsoptions |= FTS_LOGICAL;
205
206         if (Pflag)
207                 ftsoptions |= FTS_PHYSICAL;
208
209         listall = 0;
210
211         if (aflag) {
212                 if (sflag || dflag)
213                         usage();
214                 listall = 1;
215         } else if (sflag) {
216                 if (dflag)
217                         usage();
218                 depth = 0;
219         }
220
221         if (!*argv) {
222                 argv = save;
223                 argv[0] = period;
224                 argv[1] = NULL;
225         }
226
227         (void) getbsize(&notused, &blocksize);
228         blocksize /= 512;
229
230         rval = 0;
231         
232         if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
233                 err(1, "fts_open");
234
235         while ((p = fts_read(fts)) != NULL) {
236                 switch (p->fts_info) {
237                         case FTS_D:                     /* Ignore. */
238                                 if (ignorep(p))
239                                         fts_set(fts, p, FTS_SKIP);
240                                 break;
241                         case FTS_DP:
242                                 if (ignorep(p))
243                                         break;
244
245                                 p->fts_parent->fts_number +=
246                                     p->fts_number += p->fts_statp->st_blocks;
247                                 
248                                 if (p->fts_level <= depth) {
249                                         if (hflag) {
250                                                 (void) prthumanval(howmany(p->fts_number, blocksize));
251                                                 (void) printf("\t%s\n", p->fts_path);
252                                         } else {
253                                         (void) printf("%ld\t%s\n",
254                                             howmany(p->fts_number, blocksize),
255                                             p->fts_path);
256                                         }
257                                 }
258                                 break;
259                         case FTS_DC:                    /* Ignore. */
260                                 break;
261                         case FTS_DNR:                   /* Warn, continue. */
262                         case FTS_ERR:
263                         case FTS_NS:
264                                 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
265                                 rval = 1;
266                                 break;
267                         default:
268                                 if (ignorep(p))
269                                         break;
270
271                                 if (p->fts_statp->st_nlink > 1 && linkchk(p))
272                                         break;
273                                 
274                                 if (listall || p->fts_level == 0) {
275                                         if (hflag) {
276                                                 (void) prthumanval(howmany(p->fts_statp->st_blocks,
277                                                         blocksize));
278                                                 (void) printf("\t%s\n", p->fts_path);
279                                         } else {
280                                                 (void) printf("%qd\t%s\n",
281                                                         howmany(p->fts_statp->st_blocks, blocksize),
282                                                         p->fts_path);
283                                         }
284                                 }
285
286                                 p->fts_parent->fts_number += p->fts_statp->st_blocks;
287                 }
288                 savednumber = p->fts_parent->fts_number;
289         }
290
291         if (errno)
292                 err(1, "fts_read");
293
294         if (cflag) {
295                 if (hflag) {
296                         (void) prthumanval(howmany(savednumber, blocksize));
297                         (void) printf("\ttotal\n");
298                 } else {
299                         (void) printf("%ld\ttotal\n", howmany(savednumber, blocksize));
300                 }
301         }
302
303         ignoreclean();
304         exit(rval);
305 }
306
307 static int
308 linkchk(FTSENT *p)
309 {
310         struct links_entry {
311                 struct links_entry *next;
312                 struct links_entry *previous;
313                 int             links;
314                 dev_t           dev;
315                 ino_t           ino;
316         };
317
318         static const size_t links_hash_initial_size = 8192;
319         static struct links_entry **buckets;
320         static struct links_entry *free_list;
321         static size_t number_buckets;
322         static unsigned long number_entries;
323         static char stop_allocating;
324         struct links_entry *le, **new_buckets;
325         struct stat *st;
326         size_t i, new_size;
327         int hash;
328
329         st = p->fts_statp;
330
331         /* If necessary, initialize the hash table. */
332         if (buckets == NULL) {
333                 number_buckets = links_hash_initial_size;
334                 buckets = malloc(number_buckets * sizeof(buckets[0]));
335                 if (buckets == NULL)
336                         errx(1, "No memory for hardlink detection");
337                 for (i = 0; i < number_buckets; i++)
338                         buckets[i] = NULL;
339         }
340
341         /* If the hash table is getting too full, enlarge it. */
342         if (number_entries > number_buckets * 10 && !stop_allocating) {
343                 new_size = number_buckets * 2;
344                 new_buckets = malloc(new_size * sizeof(struct links_entry *));
345
346                 /* Try releasing the free list to see if that helps. */
347                 if (new_buckets == NULL && free_list != NULL) {
348                         while (free_list != NULL) {
349                                 le = free_list;
350                                 free_list = le->next;
351                                 free(le);
352                         }
353                         new_buckets = malloc(new_size * sizeof(new_buckets[0]));
354                 }
355
356                 if (new_buckets == NULL) {
357                         stop_allocating = 1;
358                         warnx("No more memory for tracking hard links");
359                 } else {
360                         memset(new_buckets, 0, new_size * sizeof(struct links_entry *));
361                         for (i = 0; i < number_buckets; i++) {
362                                 while (buckets[i] != NULL) {
363                                         /* Remove entry from old bucket. */
364                                         le = buckets[i];
365                                         buckets[i] = le->next;
366         
367                                         /* Add entry to new bucket. */
368                                         hash = (le->dev ^ le->ino) % new_size;
369         
370                                         if (new_buckets[hash] != NULL)
371                                                 new_buckets[hash]->previous = le;
372                                         le->next = new_buckets[hash];
373                                         le->previous = NULL;
374                                         new_buckets[hash] = le;
375                                 }
376                         }
377                         free(buckets);
378                         buckets = new_buckets;
379                         number_buckets = new_size;
380                 }
381         }
382
383         /* Try to locate this entry in the hash table. */
384         hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
385         for (le = buckets[hash]; le != NULL; le = le->next) {
386                 if (le->dev == st->st_dev && le->ino == st->st_ino) {
387                         /*
388                          * Save memory by releasing an entry when we've seen
389                          * all of it's links.
390                          */
391                         if (--le->links <= 0) {
392                                 if (le->previous != NULL)
393                                         le->previous->next = le->next;
394                                 if (le->next != NULL)
395                                         le->next->previous = le->previous;
396                                 if (buckets[hash] == le)
397                                         buckets[hash] = le->next;
398                                 number_entries--;
399                                 /* Recycle this node through the free list */
400                                 if (stop_allocating) {
401                                         free(le);
402                                 } else {
403                                         le->next = free_list;
404                                         free_list = le;
405                                 }
406                         }
407                         return (1);
408                 }
409         }
410
411         if (stop_allocating)
412                 return (0);
413
414         /* Add this entry to the links cache. */
415         if (free_list != NULL) {
416                 /* Pull a node from the free list if we can. */
417                 le = free_list;
418                 free_list = le->next;
419         } else
420                 /* Malloc one if we have to. */
421                 le = malloc(sizeof(struct links_entry));
422         if (le == NULL) {
423                 stop_allocating = 1;
424                 warnx("No more memory for tracking hard links");
425                 return (0);
426         }
427         le->dev = st->st_dev;
428         le->ino = st->st_ino;
429         le->links = st->st_nlink - 1;
430         number_entries++;
431         le->next = buckets[hash];
432         le->previous = NULL;
433         if (buckets[hash] != NULL)
434                 buckets[hash]->previous = le;
435         buckets[hash] = le;
436         return (0);
437 }
438
439 /*
440  * Output in "human-readable" format.  Uses 3 digits max and puts
441  * unit suffixes at the end.  Makes output compact and easy to read,
442  * especially on huge disks.
443  *
444  */
445 unit_t
446 unit_adjust(double *val)
447 {
448         double abval;
449         unit_t unit;
450         unsigned int unit_sz;
451
452         abval = fabs(*val);
453
454         unit_sz = abval ? ilogb(abval) / 10 : 0;
455
456         if (unit_sz >= UNIT_MAX) {
457                 unit = NONE;
458         } else {
459                 unit = unitp[unit_sz];
460                 *val /= (double)valp[unit_sz];
461         }
462
463         return (unit);
464 }
465
466 void
467 prthumanval(double bytes)
468 {
469         unit_t unit;
470
471         bytes *= 512;
472         unit = unit_adjust(&bytes);
473
474         if (bytes == 0)
475                 (void)printf("  0B");
476         else if (bytes > 10)
477                 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
478         else
479                 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
480 }
481
482 static void
483 usage(void)
484 {
485         (void)fprintf(stderr,
486                 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
487         exit(EX_USAGE);
488 }
489
490 void
491 ignoreadd(const char *mask)
492 {
493         struct ignentry *ign;
494
495         ign = calloc(1, sizeof(*ign));
496         if (ign == NULL)
497                 errx(1, "cannot allocate memory");
498         ign->mask = strdup(mask);
499         if (ign->mask == NULL)
500                 errx(1, "cannot allocate memory");
501         SLIST_INSERT_HEAD(&ignores, ign, next);
502 }
503
504 void
505 ignoreclean(void)
506 {
507         struct ignentry *ign;
508         
509         while (!SLIST_EMPTY(&ignores)) {
510                 ign = SLIST_FIRST(&ignores);
511                 SLIST_REMOVE_HEAD(&ignores, next);
512                 free(ign->mask);
513                 free(ign);
514         }
515 }
516
517 int
518 ignorep(FTSENT *ent)
519 {
520         struct ignentry *ign;
521
522         SLIST_FOREACH(ign, &ignores, next)
523                 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
524                         return 1;
525         return 0;
526 }