du.c revision 129678
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Chris Newcomb. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#ifndef lint 3841568Sarchiestatic const char copyright[] = 391590Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\ 401590Srgrimes The Regents of the University of California. All rights reserved.\n"; 411590Srgrimes#endif /* not lint */ 421590Srgrimes 431590Srgrimes#ifndef lint 4456597Smharo#if 0 4541568Sarchiestatic const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95"; 4656597Smharo#endif 471590Srgrimes#endif /* not lint */ 4899112Sobrien#include <sys/cdefs.h> 4999112Sobrien__FBSDID("$FreeBSD: head/usr.bin/du/du.c 129678 2004-05-24 22:22:29Z pjd $"); 501590Srgrimes 511590Srgrimes#include <sys/param.h> 5278158Sroam#include <sys/queue.h> 531590Srgrimes#include <sys/stat.h> 541590Srgrimes 551590Srgrimes#include <err.h> 561590Srgrimes#include <errno.h> 5778158Sroam#include <fnmatch.h> 581590Srgrimes#include <fts.h> 59129678Spjd#include <libutil.h> 601590Srgrimes#include <stdio.h> 611590Srgrimes#include <stdlib.h> 621590Srgrimes#include <string.h> 6356597Smharo#include <sysexits.h> 6423693Speter#include <unistd.h> 651590Srgrimes 6678158SroamSLIST_HEAD(ignhead, ignentry) ignores; 6778158Sroamstruct ignentry { 6878158Sroam char *mask; 6978158Sroam SLIST_ENTRY(ignentry) next; 7078158Sroam}; 7178158Sroam 72128772Skientzlestatic int linkchk(FTSENT *); 7392920Simpstatic void usage(void); 74129678Spjdvoid prthumanval(int64_t); 7592920Simpvoid ignoreadd(const char *); 7692920Simpvoid ignoreclean(void); 7792920Simpint ignorep(FTSENT *); 781590Srgrimes 791590Srgrimesint 80100822Sdwmalonemain(int argc, char *argv[]) 811590Srgrimes{ 8232097Sjkh FTS *fts; 8332097Sjkh FTSENT *p; 8437952Sdes long blocksize, savednumber = 0; 8532097Sjkh int ftsoptions; 8632097Sjkh int listall; 8732097Sjkh int depth; 88108453Smike int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval; 8932097Sjkh char **save; 9087216Smarkm static char dot[] = "."; 911590Srgrimes 9256597Smharo Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0; 93128772Skientzle 941590Srgrimes save = argv; 9532097Sjkh ftsoptions = 0; 9619120Sscrappy depth = INT_MAX; 9778158Sroam SLIST_INIT(&ignores); 98128772Skientzle 9978158Sroam while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1) 1001590Srgrimes switch (ch) { 10132097Sjkh case 'H': 10232097Sjkh Hflag = 1; 10332097Sjkh break; 10478158Sroam case 'I': 10578158Sroam ignoreadd(optarg); 10678158Sroam break; 10732097Sjkh case 'L': 10832097Sjkh if (Pflag) 10932097Sjkh usage(); 11032097Sjkh Lflag = 1; 11132097Sjkh break; 11232097Sjkh case 'P': 11332097Sjkh if (Lflag) 11432097Sjkh usage(); 11532097Sjkh Pflag = 1; 11632097Sjkh break; 11732097Sjkh case 'a': 11832097Sjkh aflag = 1; 11932097Sjkh break; 12032097Sjkh case 's': 12132097Sjkh sflag = 1; 12232097Sjkh break; 12332097Sjkh case 'd': 12432097Sjkh dflag = 1; 12532097Sjkh errno = 0; 12632097Sjkh depth = atoi(optarg); 12732097Sjkh if (errno == ERANGE || depth < 0) { 12858601Scharnier warnx("invalid argument to option d: %s", optarg); 12932097Sjkh usage(); 13032097Sjkh } 13132097Sjkh break; 13232097Sjkh case 'c': 13332097Sjkh cflag = 1; 13432097Sjkh break; 13556597Smharo case 'h': 13656597Smharo putenv("BLOCKSIZE=512"); 13756597Smharo hflag = 1; 13856597Smharo break; 13956597Smharo case 'k': 140112855Sobrien hflag = 0; 141112855Sobrien putenv("BLOCKSIZE=1024"); 14256597Smharo break; 14356597Smharo case 'r': /* Compatibility. */ 14456597Smharo break; 14556597Smharo case 'x': 14656597Smharo ftsoptions |= FTS_XDEV; 14756597Smharo break; 14832097Sjkh case '?': 14932097Sjkh default: 15019120Sscrappy usage(); 1511590Srgrimes } 15232097Sjkh 1531590Srgrimes argc -= optind; 1541590Srgrimes argv += optind; 1551590Srgrimes 1561590Srgrimes /* 1571590Srgrimes * XXX 1581590Srgrimes * Because of the way that fts(3) works, logical walks will not count 1591590Srgrimes * the blocks actually used by symbolic links. We rationalize this by 1601590Srgrimes * noting that users computing logical sizes are likely to do logical 1611590Srgrimes * copies, so not counting the links is correct. The real reason is 1621590Srgrimes * that we'd have to re-implement the kernel's symbolic link traversing 1631590Srgrimes * algorithm to get this right. If, for example, you have relative 1641590Srgrimes * symbolic links referencing other relative symbolic links, it gets 1651590Srgrimes * very nasty, very fast. The bottom line is that it's documented in 1661590Srgrimes * the man page, so it's a feature. 1671590Srgrimes */ 16832097Sjkh 16932097Sjkh if (Hflag + Lflag + Pflag > 1) 17032097Sjkh usage(); 17132097Sjkh 17232097Sjkh if (Hflag + Lflag + Pflag == 0) 17332097Sjkh Pflag = 1; /* -P (physical) is default */ 17432097Sjkh 1751590Srgrimes if (Hflag) 1761590Srgrimes ftsoptions |= FTS_COMFOLLOW; 17732097Sjkh 17832097Sjkh if (Lflag) 1791590Srgrimes ftsoptions |= FTS_LOGICAL; 1801590Srgrimes 18132097Sjkh if (Pflag) 18232097Sjkh ftsoptions |= FTS_PHYSICAL; 18332097Sjkh 18432097Sjkh listall = 0; 18532097Sjkh 1861590Srgrimes if (aflag) { 18719120Sscrappy if (sflag || dflag) 1881590Srgrimes usage(); 18932097Sjkh listall = 1; 19019120Sscrappy } else if (sflag) { 19119120Sscrappy if (dflag) 19219120Sscrappy usage(); 19332097Sjkh depth = 0; 1941590Srgrimes } 1951590Srgrimes 1961590Srgrimes if (!*argv) { 1971590Srgrimes argv = save; 19887216Smarkm argv[0] = dot; 1991590Srgrimes argv[1] = NULL; 2001590Srgrimes } 2011590Srgrimes 20232097Sjkh (void) getbsize(¬used, &blocksize); 2031590Srgrimes blocksize /= 512; 2041590Srgrimes 20532097Sjkh rval = 0; 206128772Skientzle 2071590Srgrimes if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) 20832097Sjkh err(1, "fts_open"); 2091590Srgrimes 21032097Sjkh while ((p = fts_read(fts)) != NULL) { 2111590Srgrimes switch (p->fts_info) { 21232097Sjkh case FTS_D: /* Ignore. */ 21378158Sroam if (ignorep(p)) 21478158Sroam fts_set(fts, p, FTS_SKIP); 2151590Srgrimes break; 21632097Sjkh case FTS_DP: 21778158Sroam if (ignorep(p)) 21878158Sroam break; 21978158Sroam 22032097Sjkh p->fts_parent->fts_number += 22132097Sjkh p->fts_number += p->fts_statp->st_blocks; 222128772Skientzle 22358601Scharnier if (p->fts_level <= depth) { 22456597Smharo if (hflag) { 22558522Smharo (void) prthumanval(howmany(p->fts_number, blocksize)); 22656597Smharo (void) printf("\t%s\n", p->fts_path); 22756597Smharo } else { 22832097Sjkh (void) printf("%ld\t%s\n", 22932097Sjkh howmany(p->fts_number, blocksize), 23032097Sjkh p->fts_path); 23156597Smharo } 23258601Scharnier } 23332097Sjkh break; 23432097Sjkh case FTS_DC: /* Ignore. */ 23532097Sjkh break; 23632097Sjkh case FTS_DNR: /* Warn, continue. */ 23732097Sjkh case FTS_ERR: 23832097Sjkh case FTS_NS: 23932097Sjkh warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 24032097Sjkh rval = 1; 24132097Sjkh break; 24232097Sjkh default: 24378158Sroam if (ignorep(p)) 24478158Sroam break; 24578158Sroam 24632097Sjkh if (p->fts_statp->st_nlink > 1 && linkchk(p)) 24732097Sjkh break; 248128772Skientzle 24958601Scharnier if (listall || p->fts_level == 0) { 25056597Smharo if (hflag) { 25156597Smharo (void) prthumanval(howmany(p->fts_statp->st_blocks, 25256597Smharo blocksize)); 25356597Smharo (void) printf("\t%s\n", p->fts_path); 25456597Smharo } else { 25556597Smharo (void) printf("%qd\t%s\n", 25690389Speter (long long)howmany(p->fts_statp->st_blocks, blocksize), 25756597Smharo p->fts_path); 25856597Smharo } 25958601Scharnier } 26032097Sjkh 26132097Sjkh p->fts_parent->fts_number += p->fts_statp->st_blocks; 2621590Srgrimes } 26339076Sdes savednumber = p->fts_parent->fts_number; 26432097Sjkh } 26532097Sjkh 2661590Srgrimes if (errno) 2671590Srgrimes err(1, "fts_read"); 26832097Sjkh 26958601Scharnier if (cflag) { 27056597Smharo if (hflag) { 27156597Smharo (void) prthumanval(howmany(savednumber, blocksize)); 27256597Smharo (void) printf("\ttotal\n"); 27356597Smharo } else { 27456597Smharo (void) printf("%ld\ttotal\n", howmany(savednumber, blocksize)); 27556597Smharo } 27658601Scharnier } 27732097Sjkh 27878158Sroam ignoreclean(); 27928891Swosch exit(rval); 2801590Srgrimes} 2811590Srgrimes 282128772Skientzlestatic int 283128772Skientzlelinkchk(FTSENT *p) 284128772Skientzle{ 285128772Skientzle struct links_entry { 286128806Skientzle struct links_entry *next; 287128806Skientzle struct links_entry *previous; 288128806Skientzle int links; 289128806Skientzle dev_t dev; 290128806Skientzle ino_t ino; 291128772Skientzle }; 292128806Skientzle static const size_t links_hash_initial_size = 8192; 293128806Skientzle static struct links_entry **buckets; 294128806Skientzle static struct links_entry *free_list; 295128806Skientzle static size_t number_buckets; 296128806Skientzle static unsigned long number_entries; 297128806Skientzle static char stop_allocating; 298128806Skientzle struct links_entry *le, **new_buckets; 299128806Skientzle struct stat *st; 300128806Skientzle size_t i, new_size; 301128834Skientzle int count, hash; 30232097Sjkh 303128772Skientzle st = p->fts_statp; 30432097Sjkh 305128772Skientzle /* If necessary, initialize the hash table. */ 306128772Skientzle if (buckets == NULL) { 307128772Skientzle number_buckets = links_hash_initial_size; 308128772Skientzle buckets = malloc(number_buckets * sizeof(buckets[0])); 309128772Skientzle if (buckets == NULL) 310128834Skientzle errx(1, "No memory for hardlink detection"); 311128772Skientzle for (i = 0; i < number_buckets; i++) 312128772Skientzle buckets[i] = NULL; 313128772Skientzle } 3141590Srgrimes 315128772Skientzle /* If the hash table is getting too full, enlarge it. */ 316128772Skientzle if (number_entries > number_buckets * 10 && !stop_allocating) { 317128772Skientzle new_size = number_buckets * 2; 318128772Skientzle new_buckets = malloc(new_size * sizeof(struct links_entry *)); 319128772Skientzle count = 0; 320128772Skientzle 321128772Skientzle /* Try releasing the free list to see if that helps. */ 322128772Skientzle if (new_buckets == NULL && free_list != NULL) { 323128772Skientzle while (free_list != NULL) { 324128772Skientzle le = free_list; 325128772Skientzle free_list = le->next; 326128772Skientzle free(le); 327128772Skientzle } 328128772Skientzle new_buckets = malloc(new_size * sizeof(new_buckets[0])); 329128772Skientzle } 330128772Skientzle 331128772Skientzle if (new_buckets == NULL) { 332128772Skientzle stop_allocating = 1; 333128834Skientzle warnx("No more memory for tracking hard links"); 334128772Skientzle } else { 335128772Skientzle memset(new_buckets, 0, 336128772Skientzle new_size * sizeof(struct links_entry *)); 337128772Skientzle for (i = 0; i < number_buckets; i++) { 338128772Skientzle while (buckets[i] != NULL) { 339128772Skientzle /* Remove entry from old bucket. */ 340128772Skientzle le = buckets[i]; 341128772Skientzle buckets[i] = le->next; 342128772Skientzle 343128772Skientzle /* Add entry to new bucket. */ 344128772Skientzle hash = (le->dev ^ le->ino) % new_size; 345128772Skientzle 346128772Skientzle if (new_buckets[hash] != NULL) 347128772Skientzle new_buckets[hash]->previous = 348128772Skientzle le; 349128772Skientzle le->next = new_buckets[hash]; 350128772Skientzle le->previous = NULL; 351128772Skientzle new_buckets[hash] = le; 352128772Skientzle } 353128772Skientzle } 354128772Skientzle free(buckets); 355128772Skientzle buckets = new_buckets; 356128772Skientzle number_buckets = new_size; 357128772Skientzle } 358128772Skientzle } 359128772Skientzle 360128772Skientzle /* Try to locate this entry in the hash table. */ 361128772Skientzle hash = ( st->st_dev ^ st->st_ino ) % number_buckets; 362128772Skientzle for (le = buckets[hash]; le != NULL; le = le->next) { 363128772Skientzle if (le->dev == st->st_dev && le->ino == st->st_ino) { 364128772Skientzle /* 365128772Skientzle * Save memory by releasing an entry when we've seen 366128772Skientzle * all of it's links. 367128772Skientzle */ 368128772Skientzle if (--le->links <= 0) { 369128772Skientzle if (le->previous != NULL) 370128772Skientzle le->previous->next = le->next; 371128772Skientzle if (le->next != NULL) 372128772Skientzle le->next->previous = le->previous; 373128772Skientzle if (buckets[hash] == le) 374128772Skientzle buckets[hash] = le->next; 375128772Skientzle number_entries--; 376128772Skientzle /* Recycle this node through the free list */ 377128772Skientzle if (stop_allocating) { 378128772Skientzle free(le); 379128772Skientzle } else { 380128772Skientzle le->next = free_list; 381128772Skientzle free_list = le; 382128772Skientzle } 383128772Skientzle } 384128772Skientzle return (1); 385128772Skientzle } 386128772Skientzle } 387128772Skientzle 388128772Skientzle if (stop_allocating) 389128772Skientzle return (0); 390128772Skientzle 391128772Skientzle /* Add this entry to the links cache. */ 392128772Skientzle if (free_list != NULL) { 393128772Skientzle /* Pull a node from the free list if we can. */ 394128772Skientzle le = free_list; 395128772Skientzle free_list = le->next; 396128772Skientzle } else 397128772Skientzle /* Malloc one if we have to. */ 398128772Skientzle le = malloc(sizeof(struct links_entry)); 399128772Skientzle if (le == NULL) { 400128772Skientzle stop_allocating = 1; 401128834Skientzle warnx("No more memory for tracking hard links"); 402128772Skientzle return (0); 403128772Skientzle } 404128772Skientzle le->dev = st->st_dev; 405128772Skientzle le->ino = st->st_ino; 406128772Skientzle le->links = st->st_nlink - 1; 407128772Skientzle number_entries++; 408128772Skientzle le->next = buckets[hash]; 409128772Skientzle le->previous = NULL; 410128772Skientzle if (buckets[hash] != NULL) 411128772Skientzle buckets[hash]->previous = le; 412128772Skientzle buckets[hash] = le; 4131590Srgrimes return (0); 4141590Srgrimes} 4151590Srgrimes 416129678Spjdvoid 417129678Spjdprthumanval(int64_t bytes) 41856597Smharo{ 419129678Spjd char buf[5]; 42056597Smharo 421129678Spjd bytes *= DEV_BSIZE; 42256597Smharo 423129678Spjd humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, 424129678Spjd HN_B | HN_NOSPACE | HN_DECIMAL); 42556597Smharo 426129678Spjd (void)printf("%4s", buf); 42756597Smharo} 42856597Smharo 42927099Scharnierstatic void 430100822Sdwmaloneusage(void) 4311590Srgrimes{ 4321590Srgrimes (void)fprintf(stderr, 43378158Sroam "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n"); 43456597Smharo exit(EX_USAGE); 4351590Srgrimes} 43678158Sroam 43778158Sroamvoid 438100822Sdwmaloneignoreadd(const char *mask) 43978158Sroam{ 44078158Sroam struct ignentry *ign; 44178158Sroam 44278158Sroam ign = calloc(1, sizeof(*ign)); 44378158Sroam if (ign == NULL) 44478158Sroam errx(1, "cannot allocate memory"); 44578158Sroam ign->mask = strdup(mask); 44678158Sroam if (ign->mask == NULL) 44778158Sroam errx(1, "cannot allocate memory"); 44878158Sroam SLIST_INSERT_HEAD(&ignores, ign, next); 44978158Sroam} 45078158Sroam 45178158Sroamvoid 452100822Sdwmaloneignoreclean(void) 45378158Sroam{ 45478158Sroam struct ignentry *ign; 45578158Sroam 45678158Sroam while (!SLIST_EMPTY(&ignores)) { 45778158Sroam ign = SLIST_FIRST(&ignores); 45878158Sroam SLIST_REMOVE_HEAD(&ignores, next); 45978158Sroam free(ign->mask); 46078158Sroam free(ign); 46178158Sroam } 46278158Sroam} 46378158Sroam 46478158Sroamint 465100822Sdwmaloneignorep(FTSENT *ent) 46678158Sroam{ 46778158Sroam struct ignentry *ign; 46878158Sroam 46978158Sroam SLIST_FOREACH(ign, &ignores, next) 47078158Sroam if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH) 47178158Sroam return 1; 47278158Sroam return 0; 47378158Sroam} 474