du.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* $NetBSD: du.c,v 1.33 2008/07/30 22:03:40 dsl Exp $ */
  2. /*
  3. * Copyright (c) 1989, 1993, 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Chris Newcomb.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. #ifndef lint
  35. __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
  36. The Regents of the University of California. All rights reserved.");
  37. #endif /* not lint */
  38. #ifndef lint
  39. #if 0
  40. static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
  41. #else
  42. __RCSID("$NetBSD: du.c,v 1.33 2008/07/30 22:03:40 dsl Exp $");
  43. #endif
  44. #endif /* not lint */
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <dirent.h>
  48. #include <err.h>
  49. #include <errno.h>
  50. #include <fts.h>
  51. #include <stdint.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. #include <limits.h>
  57. int linkchk(dev_t, ino_t);
  58. void prstat(const char *, int64_t);
  59. static void usage(void);
  60. long blocksize;
  61. #define howmany(x, y) (((x)+((y)-1))/(y))
  62. int
  63. main(int argc, char *argv[])
  64. {
  65. FTS *fts;
  66. FTSENT *p;
  67. int64_t totalblocks;
  68. int ftsoptions, listfiles;
  69. int depth;
  70. int Hflag, Lflag, aflag, ch, cflag, dflag, gkmflag, rval, sflag;
  71. char *noargv[2];
  72. Hflag = Lflag = aflag = cflag = dflag = gkmflag = sflag = 0;
  73. totalblocks = 0;
  74. ftsoptions = FTS_PHYSICAL;
  75. depth = INT_MAX;
  76. while ((ch = getopt(argc, argv, "HLPacd:ghkmnrsx")) != -1)
  77. switch (ch) {
  78. case 'H':
  79. Hflag = 1;
  80. Lflag = 0;
  81. break;
  82. case 'L':
  83. Lflag = 1;
  84. Hflag = 0;
  85. break;
  86. case 'P':
  87. Hflag = Lflag = 0;
  88. break;
  89. case 'a':
  90. aflag = 1;
  91. break;
  92. case 'c':
  93. cflag = 1;
  94. break;
  95. case 'd':
  96. dflag = 1;
  97. depth = atoi(optarg);
  98. if (depth < 0 || depth > SHRT_MAX) {
  99. warnx("invalid argument to option d: %s",
  100. optarg);
  101. usage();
  102. }
  103. break;
  104. case 'g':
  105. blocksize = 1024 * 1024 * 1024;
  106. gkmflag = 1;
  107. break;
  108. case 'k':
  109. blocksize = 1024;
  110. gkmflag = 1;
  111. break;
  112. case 'm':
  113. blocksize = 1024 * 1024;
  114. gkmflag = 1;
  115. break;
  116. case 'r':
  117. break;
  118. case 's':
  119. sflag = 1;
  120. break;
  121. case 'x':
  122. ftsoptions |= FTS_XDEV;
  123. break;
  124. case '?':
  125. default:
  126. usage();
  127. }
  128. argc -= optind;
  129. argv += optind;
  130. /*
  131. * XXX
  132. * Because of the way that fts(3) works, logical walks will not count
  133. * the blocks actually used by symbolic links. We rationalize this by
  134. * noting that users computing logical sizes are likely to do logical
  135. * copies, so not counting the links is correct. The real reason is
  136. * that we'd have to re-implement the kernel's symbolic link traversing
  137. * algorithm to get this right. If, for example, you have relative
  138. * symbolic links referencing other relative symbolic links, it gets
  139. * very nasty, very fast. The bottom line is that it's documented in
  140. * the man page, so it's a feature.
  141. */
  142. if (Hflag)
  143. ftsoptions |= FTS_COMFOLLOW;
  144. if (Lflag) {
  145. ftsoptions &= ~FTS_PHYSICAL;
  146. ftsoptions |= FTS_LOGICAL;
  147. }
  148. listfiles = 0;
  149. if (aflag) {
  150. if (sflag || dflag)
  151. usage();
  152. listfiles = 1;
  153. } else if (sflag) {
  154. if (dflag)
  155. usage();
  156. depth = 0;
  157. }
  158. if (!*argv) {
  159. noargv[0] = strdup(".");
  160. noargv[1] = NULL;
  161. argv = noargv;
  162. }
  163. if (!gkmflag)
  164. blocksize = 512;
  165. blocksize /= 512;
  166. if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
  167. err(1, "fts_open `%s'", *argv);
  168. for (rval = 0; (p = fts_read(fts)) != NULL;) {
  169. switch (p->fts_info) {
  170. case FTS_D: /* Ignore. */
  171. break;
  172. case FTS_DP:
  173. p->fts_parent->fts_number +=
  174. p->fts_number += p->fts_statp->st_blocks;
  175. if (cflag)
  176. totalblocks += p->fts_statp->st_blocks;
  177. /*
  178. * If listing each directory, or not listing files
  179. * or directories and this is post-order of the
  180. * root of a traversal, display the total.
  181. */
  182. if (p->fts_level <= depth
  183. || (!listfiles && !p->fts_level))
  184. prstat(p->fts_path, p->fts_number);
  185. break;
  186. case FTS_DC: /* Ignore. */
  187. break;
  188. case FTS_DNR: /* Warn, continue. */
  189. case FTS_ERR:
  190. case FTS_NS:
  191. warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
  192. rval = 1;
  193. break;
  194. default:
  195. if (p->fts_statp->st_nlink > 1 &&
  196. linkchk(p->fts_statp->st_dev, p->fts_statp->st_ino))
  197. break;
  198. /*
  199. * If listing each file, or a non-directory file was
  200. * the root of a traversal, display the total.
  201. */
  202. if (listfiles || !p->fts_level)
  203. prstat(p->fts_path, p->fts_statp->st_blocks);
  204. p->fts_parent->fts_number += p->fts_statp->st_blocks;
  205. if (cflag)
  206. totalblocks += p->fts_statp->st_blocks;
  207. }
  208. }
  209. if (errno)
  210. err(1, "fts_read");
  211. if (cflag)
  212. prstat("total", totalblocks);
  213. exit(rval);
  214. }
  215. void
  216. prstat(const char *fname, int64_t blocks)
  217. {
  218. (void)printf("%lld\t%s\n",
  219. (long long)howmany(blocks, (int64_t)blocksize),
  220. fname);
  221. }
  222. int
  223. linkchk(dev_t dev, ino_t ino)
  224. {
  225. static struct entry {
  226. dev_t dev;
  227. ino_t ino;
  228. } *htable;
  229. static int htshift; /* log(allocated size) */
  230. static int htmask; /* allocated size - 1 */
  231. static int htused; /* 2*number of insertions */
  232. static int sawzero; /* Whether zero is in table or not */
  233. int h, h2;
  234. uint64_t tmp;
  235. /* this constant is (1<<64)/((1+sqrt(5))/2)
  236. * aka (word size)/(golden ratio)
  237. */
  238. const uint64_t HTCONST = 11400714819323198485ULL;
  239. const int HTBITS = CHAR_BIT * sizeof(tmp);
  240. /* Never store zero in hashtable */
  241. if (dev == 0 && ino == 0) {
  242. h = sawzero;
  243. sawzero = 1;
  244. return h;
  245. }
  246. /* Extend hash table if necessary, keep load under 0.5 */
  247. if (htused<<1 >= htmask) {
  248. struct entry *ohtable;
  249. if (!htable)
  250. htshift = 10; /* starting hashtable size */
  251. else
  252. htshift++; /* exponential hashtable growth */
  253. htmask = (1 << htshift) - 1;
  254. htused = 0;
  255. ohtable = htable;
  256. htable = calloc(htmask+1, sizeof(*htable));
  257. if (!htable)
  258. err(1, "calloc");
  259. /* populate newly allocated hashtable */
  260. if (ohtable) {
  261. int i;
  262. for (i = 0; i <= htmask>>1; i++)
  263. if (ohtable[i].ino || ohtable[i].dev)
  264. linkchk(ohtable[i].dev, ohtable[i].ino);
  265. free(ohtable);
  266. }
  267. }
  268. /* multiplicative hashing */
  269. tmp = dev;
  270. tmp <<= HTBITS>>1;
  271. tmp |= ino;
  272. tmp *= HTCONST;
  273. h = tmp >> (HTBITS - htshift);
  274. h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
  275. /* open address hashtable search with double hash probing */
  276. while (htable[h].ino || htable[h].dev) {
  277. if ((htable[h].ino == ino) && (htable[h].dev == dev))
  278. return 1;
  279. h = (h + h2) & htmask;
  280. }
  281. /* Insert the current entry into hashtable */
  282. htable[h].dev = dev;
  283. htable[h].ino = ino;
  284. htused++;
  285. return 0;
  286. }
  287. static void
  288. usage(void)
  289. {
  290. (void)fprintf(stderr,
  291. "usage: du [-H | -L | -P] [-a | -d depth | -s] [-cgkmrx] [file ...]\n");
  292. exit(1);
  293. }