du.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * du - print disk usage
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <String.h>
  15. extern int64_t du(char*, Dir*);
  16. extern void err(char*);
  17. extern int64_t blkmultiple(int64_t);
  18. extern int seen(Dir*);
  19. extern int warn(char*);
  20. enum {
  21. Vkilo = 1024LL,
  22. };
  23. /* rounding up, how many units does amt occupy? */
  24. #define HOWMANY(amt, unit) (((amt)+(unit)-1) / (unit))
  25. #define ROUNDUP(amt, unit) (HOWMANY(amt, unit) * (unit))
  26. int aflag;
  27. int autoscale;
  28. int fflag;
  29. int fltflag;
  30. int qflag;
  31. int readflg;
  32. int sflag;
  33. int tflag;
  34. int uflag;
  35. char *fmt = "%llu\t%q\n";
  36. char *readbuf;
  37. int64_t blocksize = Vkilo; /* actually more likely to be 4K or 8K */
  38. int64_t unit; /* scale factor for output */
  39. static char *pfxes[] = { /* SI prefixes for units > 1 */
  40. "",
  41. "k", "M", "G",
  42. "T", "P", "E",
  43. "Z", "Y",
  44. nil,
  45. };
  46. void
  47. usage(void)
  48. {
  49. fprint(2, "usage: du [-aefhnqstu] [-b size] [-p si-pfx] [file ...]\n");
  50. exits("usage");
  51. }
  52. void
  53. printamt(int64_t amt, char *name)
  54. {
  55. if (readflg)
  56. return;
  57. if (autoscale) {
  58. int scale = 0;
  59. double val = (double)amt/unit;
  60. while (fabs(val) >= 1024 && scale < nelem(pfxes)-1) {
  61. scale++;
  62. val /= 1024;
  63. }
  64. print("%.6g%s\t%q\n", val, pfxes[scale], name);
  65. } else if (fltflag)
  66. print("%.6g\t%q\n", (double)amt/unit, name);
  67. else
  68. print(fmt, HOWMANY(amt, unit), name);
  69. }
  70. void
  71. main(int argc, char *argv[])
  72. {
  73. int i, scale;
  74. char *s, *ss, *name;
  75. doquote = needsrcquote;
  76. quotefmtinstall();
  77. ARGBEGIN {
  78. case 'a': /* all files */
  79. aflag = 1;
  80. break;
  81. case 'b': /* block size */
  82. s = ARGF();
  83. if(s) {
  84. blocksize = strtoul(s, &ss, 0);
  85. if(s == ss)
  86. blocksize = 1;
  87. while(*ss++ == 'k')
  88. blocksize *= 1024;
  89. }
  90. break;
  91. case 'e': /* print in %g notation */
  92. fltflag = 1;
  93. break;
  94. case 'f': /* don't print warnings */
  95. fflag = 1;
  96. break;
  97. case 'h': /* similar to -h in bsd but more precise */
  98. autoscale = 1;
  99. break;
  100. case 'n': /* all files, number of bytes */
  101. aflag = 1;
  102. blocksize = 1;
  103. unit = 1;
  104. break;
  105. case 'p':
  106. s = ARGF();
  107. if(s) {
  108. for (scale = 0; pfxes[scale] != nil; scale++)
  109. if (cistrcmp(s, pfxes[scale]) == 0)
  110. break;
  111. if (pfxes[scale] == nil)
  112. sysfatal("unknown suffix %s", s);
  113. unit = 1;
  114. while (scale-- > 0)
  115. unit *= Vkilo;
  116. }
  117. break;
  118. case 'q': /* qid */
  119. fmt = "%.16llux\t%q\n";
  120. qflag = 1;
  121. break;
  122. case 'r':
  123. /* undocumented: just read & ignore every block of every file */
  124. readflg = 1;
  125. break;
  126. case 's': /* only top level */
  127. sflag = 1;
  128. break;
  129. case 't': /* return modified/accessed time */
  130. tflag = 1;
  131. break;
  132. case 'u': /* accessed time */
  133. uflag = 1;
  134. break;
  135. default:
  136. usage();
  137. } ARGEND
  138. if (unit == 0)
  139. if (qflag || tflag || uflag || autoscale)
  140. unit = 1;
  141. else
  142. unit = Vkilo;
  143. if (blocksize < 1)
  144. blocksize = 1;
  145. if (readflg) {
  146. readbuf = malloc(blocksize);
  147. if (readbuf == nil)
  148. sysfatal("out of memory");
  149. }
  150. if(argc==0)
  151. printamt(du(".", dirstat(".")), ".");
  152. else
  153. for(i=0; i<argc; i++) {
  154. name = argv[i];
  155. printamt(du(name, dirstat(name)), name);
  156. }
  157. exits(0);
  158. }
  159. int64_t
  160. dirval(Dir *d, int64_t size)
  161. {
  162. if(qflag)
  163. return d->qid.path;
  164. else if(tflag) {
  165. if(uflag)
  166. return d->atime;
  167. return d->mtime;
  168. } else
  169. return size;
  170. }
  171. void
  172. readfile(char *name)
  173. {
  174. int n, fd = open(name, OREAD);
  175. if(fd < 0) {
  176. warn(name);
  177. return;
  178. }
  179. while ((n = read(fd, readbuf, blocksize)) > 0)
  180. continue;
  181. if (n < 0)
  182. warn(name);
  183. close(fd);
  184. }
  185. int64_t
  186. dufile(char *name, Dir *d)
  187. {
  188. int64_t t = blkmultiple(d->length);
  189. if(aflag || readflg) {
  190. String *file = s_copy(name);
  191. s_append(file, "/");
  192. s_append(file, d->name);
  193. if (readflg)
  194. readfile(s_to_c(file));
  195. t = dirval(d, t);
  196. printamt(t, s_to_c(file));
  197. s_free(file);
  198. }
  199. return t;
  200. }
  201. int64_t
  202. du(char *name, Dir *dir)
  203. {
  204. int fd, i, n;
  205. Dir *buf, *d;
  206. String *file;
  207. int64_t nk, t;
  208. if(dir == nil)
  209. return warn(name);
  210. if((dir->qid.type&QTDIR) == 0)
  211. return dirval(dir, blkmultiple(dir->length));
  212. fd = open(name, OREAD);
  213. if(fd < 0)
  214. return warn(name);
  215. nk = 0;
  216. while((n=dirread(fd, &buf)) > 0) {
  217. d = buf;
  218. for(i = n; i > 0; i--, d++) {
  219. if((d->qid.type&QTDIR) == 0) {
  220. nk += dufile(name, d);
  221. continue;
  222. }
  223. if(strcmp(d->name, ".") == 0 ||
  224. strcmp(d->name, "..") == 0 ||
  225. /* !readflg && */ seen(d))
  226. continue; /* don't get stuck */
  227. file = s_copy(name);
  228. s_append(file, "/");
  229. s_append(file, d->name);
  230. t = du(s_to_c(file), d);
  231. nk += t;
  232. t = dirval(d, t);
  233. if(!sflag)
  234. printamt(t, s_to_c(file));
  235. s_free(file);
  236. }
  237. free(buf);
  238. }
  239. if(n < 0)
  240. warn(name);
  241. close(fd);
  242. return dirval(dir, nk);
  243. }
  244. #define NCACHE 256 /* must be power of two */
  245. typedef struct
  246. {
  247. Dir* cache;
  248. int n;
  249. int max;
  250. } Cache;
  251. Cache cache[NCACHE];
  252. int
  253. seen(Dir *dir)
  254. {
  255. Dir *dp;
  256. int i;
  257. Cache *c;
  258. c = &cache[dir->qid.path&(NCACHE-1)];
  259. dp = c->cache;
  260. for(i=0; i<c->n; i++, dp++)
  261. if(dir->qid.path == dp->qid.path &&
  262. dir->type == dp->type &&
  263. dir->dev == dp->dev)
  264. return 1;
  265. if(c->n == c->max){
  266. if (c->max == 0)
  267. c->max = 8;
  268. else
  269. c->max += c->max/2;
  270. c->cache = realloc(c->cache, c->max*sizeof(Dir));
  271. if(c->cache == nil)
  272. err("malloc failure");
  273. }
  274. c->cache[c->n++] = *dir;
  275. return 0;
  276. }
  277. void
  278. err(char *s)
  279. {
  280. fprint(2, "du: %s: %r\n", s);
  281. exits(s);
  282. }
  283. int
  284. warn(char *s)
  285. {
  286. if(fflag == 0)
  287. fprint(2, "du: %s: %r\n", s);
  288. return 0;
  289. }
  290. /* round up n to nearest block */
  291. int64_t
  292. blkmultiple(int64_t n)
  293. {
  294. if(blocksize == 1) /* no quantization */
  295. return n;
  296. return ROUNDUP(n, blocksize);
  297. }