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 37 #ifndef lint 38 static const char copyright[] = 39 "@(#) Copyright (c) 1989, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95"; 46 #endif 47 #endif /* not lint */ 48 #include <sys/cdefs.h> 49 __FBSDID("$FreeBSD$"); 50 51 #include <sys/param.h> 52 #include <sys/queue.h> 53 #include <sys/stat.h> 54 55 #include <err.h> 56 #include <errno.h> 57 #include <fnmatch.h> 58 #include <fts.h> 59 #include <libutil.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <sysexits.h> 64 #include <unistd.h> 65 66 SLIST_HEAD(ignhead, ignentry) ignores; 67 struct ignentry { 68 char *mask; 69 SLIST_ENTRY(ignentry) next; 70 }; 71 72 static int linkchk(FTSENT *); 73 static void usage(void); 74 void prthumanval(int64_t); 75 void ignoreadd(const char *); 76 void ignoreclean(void); 77 int ignorep(FTSENT *); 78 79 int 80 main(int argc, char *argv[]) 81 { 82 FTS *fts; 83 FTSENT *p; 84 long blocksize, savednumber = 0; 85 int ftsoptions; 86 int listall; 87 int depth; 88 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval; 89 char **save; 90 static char dot[] = "."; 91 92 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0; 93 94 save = argv; 95 ftsoptions = 0; 96 depth = INT_MAX; 97 SLIST_INIT(&ignores); 98 99 while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1) 100 switch (ch) { 101 case 'H': 102 Hflag = 1; 103 break; 104 case 'I': 105 ignoreadd(optarg); 106 break; 107 case 'L': 108 if (Pflag) 109 usage(); 110 Lflag = 1; 111 break; 112 case 'P': 113 if (Lflag) 114 usage(); 115 Pflag = 1; 116 break; 117 case 'a': 118 aflag = 1; 119 break; 120 case 's': 121 sflag = 1; 122 break; 123 case 'd': 124 dflag = 1; 125 errno = 0; 126 depth = atoi(optarg); 127 if (errno == ERANGE || depth < 0) { 128 warnx("invalid argument to option d: %s", optarg); 129 usage(); 130 } 131 break; 132 case 'c': 133 cflag = 1; 134 break; 135 case 'h': 136 putenv("BLOCKSIZE=512"); 137 hflag = 1; 138 break; 139 case 'k': 140 hflag = 0; 141 putenv("BLOCKSIZE=1024"); 142 break; 143 case 'r': /* Compatibility. */ 144 break; 145 case 'x': 146 ftsoptions |= FTS_XDEV; 147 break; 148 case '?': 149 default: 150 usage(); 151 } 152 153 argc -= optind; 154 argv += optind; 155 156 /* 157 * XXX 158 * Because of the way that fts(3) works, logical walks will not count 159 * the blocks actually used by symbolic links. We rationalize this by 160 * noting that users computing logical sizes are likely to do logical 161 * copies, so not counting the links is correct. The real reason is 162 * that we'd have to re-implement the kernel's symbolic link traversing 163 * algorithm to get this right. If, for example, you have relative 164 * symbolic links referencing other relative symbolic links, it gets 165 * very nasty, very fast. The bottom line is that it's documented in 166 * the man page, so it's a feature. 167 */ 168 169 if (Hflag + Lflag + Pflag > 1) 170 usage(); 171 172 if (Hflag + Lflag + Pflag == 0) 173 Pflag = 1; /* -P (physical) is default */ 174 175 if (Hflag) 176 ftsoptions |= FTS_COMFOLLOW; 177 178 if (Lflag) 179 ftsoptions |= FTS_LOGICAL; 180 181 if (Pflag) 182 ftsoptions |= FTS_PHYSICAL; 183 184 listall = 0; 185 186 if (aflag) { 187 if (sflag || dflag) 188 usage(); 189 listall = 1; 190 } else if (sflag) { 191 if (dflag) 192 usage(); 193 depth = 0; 194 } 195 196 if (!*argv) { 197 argv = save; 198 argv[0] = dot; 199 argv[1] = NULL; 200 } 201 202 (void) getbsize(¬used, &blocksize); 203 blocksize /= 512; 204 205 rval = 0; 206 207 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) 208 err(1, "fts_open"); 209 210 while ((p = fts_read(fts)) != NULL) { 211 switch (p->fts_info) { 212 case FTS_D: /* Ignore. */ 213 if (ignorep(p)) 214 fts_set(fts, p, FTS_SKIP); 215 break; 216 case FTS_DP: 217 if (ignorep(p)) 218 break; 219 220 p->fts_parent->fts_number += 221 p->fts_number += p->fts_statp->st_blocks; 222 223 if (p->fts_level <= depth) { 224 if (hflag) { 225 (void) prthumanval(howmany(p->fts_number, blocksize)); 226 (void) printf("\t%s\n", p->fts_path); 227 } else { 228 (void) printf("%ld\t%s\n", 229 howmany(p->fts_number, blocksize), 230 p->fts_path); 231 } 232 } 233 break; 234 case FTS_DC: /* Ignore. */ 235 break; 236 case FTS_DNR: /* Warn, continue. */ 237 case FTS_ERR: 238 case FTS_NS: 239 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 240 rval = 1; 241 break; 242 default: 243 if (ignorep(p)) 244 break; 245 246 if (p->fts_statp->st_nlink > 1 && linkchk(p)) 247 break; 248 249 if (listall || p->fts_level == 0) { 250 if (hflag) { 251 (void) prthumanval(howmany(p->fts_statp->st_blocks, 252 blocksize)); 253 (void) printf("\t%s\n", p->fts_path); 254 } else { 255 (void) printf("%qd\t%s\n", 256 (long long)howmany(p->fts_statp->st_blocks, blocksize), 257 p->fts_path); 258 } 259 } 260 261 p->fts_parent->fts_number += p->fts_statp->st_blocks; 262 } 263 savednumber = p->fts_parent->fts_number; 264 } 265 266 if (errno) 267 err(1, "fts_read"); 268 269 if (cflag) { 270 if (hflag) { 271 (void) prthumanval(howmany(savednumber, blocksize)); 272 (void) printf("\ttotal\n"); 273 } else { 274 (void) printf("%ld\ttotal\n", howmany(savednumber, blocksize)); 275 } 276 } 277 278 ignoreclean(); 279 exit(rval); 280 } 281 282 static int 283 linkchk(FTSENT *p) 284 { 285 struct links_entry { 286 struct links_entry *next; 287 struct links_entry *previous; 288 int links; 289 dev_t dev; 290 ino_t ino; 291 }; 292 static const size_t links_hash_initial_size = 8192; 293 static struct links_entry **buckets; 294 static struct links_entry *free_list; 295 static size_t number_buckets; 296 static unsigned long number_entries; 297 static char stop_allocating; 298 struct links_entry *le, **new_buckets; 299 struct stat *st; 300 size_t i, new_size; 301 int count, hash; 302 303 st = p->fts_statp; 304 305 /* If necessary, initialize the hash table. */ 306 if (buckets == NULL) { 307 number_buckets = links_hash_initial_size; 308 buckets = malloc(number_buckets * sizeof(buckets[0])); 309 if (buckets == NULL) 310 errx(1, "No memory for hardlink detection"); 311 for (i = 0; i < number_buckets; i++) 312 buckets[i] = NULL; 313 } 314 315 /* If the hash table is getting too full, enlarge it. */ 316 if (number_entries > number_buckets * 10 && !stop_allocating) { 317 new_size = number_buckets * 2; 318 new_buckets = malloc(new_size * sizeof(struct links_entry *)); 319 count = 0; 320 321 /* Try releasing the free list to see if that helps. */ 322 if (new_buckets == NULL && free_list != NULL) { 323 while (free_list != NULL) { 324 le = free_list; 325 free_list = le->next; 326 free(le); 327 } 328 new_buckets = malloc(new_size * sizeof(new_buckets[0])); 329 } 330 331 if (new_buckets == NULL) { 332 stop_allocating = 1; 333 warnx("No more memory for tracking hard links"); 334 } else { 335 memset(new_buckets, 0, 336 new_size * sizeof(struct links_entry *)); 337 for (i = 0; i < number_buckets; i++) { 338 while (buckets[i] != NULL) { 339 /* Remove entry from old bucket. */ 340 le = buckets[i]; 341 buckets[i] = le->next; 342 343 /* Add entry to new bucket. */ 344 hash = (le->dev ^ le->ino) % new_size; 345 346 if (new_buckets[hash] != NULL) 347 new_buckets[hash]->previous = 348 le; 349 le->next = new_buckets[hash]; 350 le->previous = NULL; 351 new_buckets[hash] = le; 352 } 353 } 354 free(buckets); 355 buckets = new_buckets; 356 number_buckets = new_size; 357 } 358 } 359 360 /* Try to locate this entry in the hash table. */ 361 hash = ( st->st_dev ^ st->st_ino ) % number_buckets; 362 for (le = buckets[hash]; le != NULL; le = le->next) { 363 if (le->dev == st->st_dev && le->ino == st->st_ino) { 364 /* 365 * Save memory by releasing an entry when we've seen 366 * all of it's links. 367 */ 368 if (--le->links <= 0) { 369 if (le->previous != NULL) 370 le->previous->next = le->next; 371 if (le->next != NULL) 372 le->next->previous = le->previous; 373 if (buckets[hash] == le) 374 buckets[hash] = le->next; 375 number_entries--; 376 /* Recycle this node through the free list */ 377 if (stop_allocating) { 378 free(le); 379 } else { 380 le->next = free_list; 381 free_list = le; 382 } 383 } 384 return (1); 385 } 386 } 387 388 if (stop_allocating) 389 return (0); 390 391 /* Add this entry to the links cache. */ 392 if (free_list != NULL) { 393 /* Pull a node from the free list if we can. */ 394 le = free_list; 395 free_list = le->next; 396 } else 397 /* Malloc one if we have to. */ 398 le = malloc(sizeof(struct links_entry)); 399 if (le == NULL) { 400 stop_allocating = 1; 401 warnx("No more memory for tracking hard links"); 402 return (0); 403 } 404 le->dev = st->st_dev; 405 le->ino = st->st_ino; 406 le->links = st->st_nlink - 1; 407 number_entries++; 408 le->next = buckets[hash]; 409 le->previous = NULL; 410 if (buckets[hash] != NULL) 411 buckets[hash]->previous = le; 412 buckets[hash] = le; 413 return (0); 414 } 415 416 void 417 prthumanval(int64_t bytes) 418 { 419 char buf[5]; 420 421 bytes *= DEV_BSIZE; 422 423 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, 424 HN_B | HN_NOSPACE | HN_DECIMAL); 425 426 (void)printf("%4s", buf); 427 } 428 429 static void 430 usage(void) 431 { 432 (void)fprintf(stderr, 433 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n"); 434 exit(EX_USAGE); 435 } 436 437 void 438 ignoreadd(const char *mask) 439 { 440 struct ignentry *ign; 441 442 ign = calloc(1, sizeof(*ign)); 443 if (ign == NULL) 444 errx(1, "cannot allocate memory"); 445 ign->mask = strdup(mask); 446 if (ign->mask == NULL) 447 errx(1, "cannot allocate memory"); 448 SLIST_INSERT_HEAD(&ignores, ign, next); 449 } 450 451 void 452 ignoreclean(void) 453 { 454 struct ignentry *ign; 455 456 while (!SLIST_EMPTY(&ignores)) { 457 ign = SLIST_FIRST(&ignores); 458 SLIST_REMOVE_HEAD(&ignores, next); 459 free(ign->mask); 460 free(ign); 461 } 462 } 463 464 int 465 ignorep(FTSENT *ent) 466 { 467 struct ignentry *ign; 468 469 SLIST_FOREACH(ign, &ignores, next) 470 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH) 471 return 1; 472 return 0; 473 } 474