2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-09 18:23:54 +00:00
|
|
|
/*
|
|
|
|
* Mini du implementation for busybox
|
|
|
|
*
|
2001-10-24 05:00:29 +00:00
|
|
|
* Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu
|
|
|
|
* Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
|
2002-04-06 23:16:44 +00:00
|
|
|
* Copyright (C) 2002 Edward Betts <edward@debian.org>
|
1999-12-09 18:23:54 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-12-09 18:23:54 +00:00
|
|
|
*/
|
2003-03-19 09:13:01 +00:00
|
|
|
/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
|
|
|
|
*
|
|
|
|
* Mostly rewritten for SUSv3 compliance and to fix bugs/defects.
|
|
|
|
* 1) Added support for SUSv3 -a, -H, -L, gnu -c, and (busybox) -d options.
|
|
|
|
* The -d option allows setting of max depth (similar to gnu --max-depth).
|
|
|
|
* 2) Fixed incorrect size calculations for links and directories, especially
|
|
|
|
* when errors occurred. Calculates sizes should now match gnu du output.
|
|
|
|
* 3) Added error checking of output.
|
|
|
|
* 4) Fixed busybox bug #1284 involving long overflow with human_readable.
|
|
|
|
*/
|
2016-11-23 13:46:56 +00:00
|
|
|
//config:config DU
|
|
|
|
//config: bool "du (default blocksize of 512 bytes)"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: du is used to report the amount of disk space used
|
|
|
|
//config: for specified files.
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DU_DEFAULT_BLOCKSIZE_1K
|
|
|
|
//config: bool "Use a default blocksize of 1024 bytes (1K)"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on DU
|
|
|
|
//config: help
|
|
|
|
//config: Use a blocksize of (1K) instead of the default 512b.
|
|
|
|
|
|
|
|
//applet:IF_DU(APPLET(du, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_DU) += du.o
|
|
|
|
|
|
|
|
/* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
|
|
|
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage:#define du_trivial_usage
|
|
|
|
//usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..."
|
|
|
|
//usage:#define du_full_usage "\n\n"
|
2012-03-05 08:59:56 +00:00
|
|
|
//usage: "Summarize disk space used for each FILE and/or directory\n"
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: "\n -a Show file sizes too"
|
|
|
|
//usage: "\n -L Follow all symlinks"
|
|
|
|
//usage: "\n -H Follow symlinks on command line"
|
|
|
|
//usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
|
|
|
|
//usage: "\n -c Show grand total"
|
|
|
|
//usage: "\n -l Count sizes many times if hard linked"
|
|
|
|
//usage: "\n -s Display only a total for each argument"
|
|
|
|
//usage: "\n -x Skip directories on different filesystems"
|
|
|
|
//usage: IF_FEATURE_HUMAN_READABLE(
|
2012-03-05 08:52:19 +00:00
|
|
|
//usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)"
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: "\n -m Sizes in megabytes"
|
|
|
|
//usage: )
|
2012-03-05 08:59:56 +00:00
|
|
|
//usage: "\n -k Sizes in kilobytes" IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(" (default)")
|
|
|
|
//usage: IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(
|
|
|
|
//usage: "\n Default unit is 512 bytes"
|
|
|
|
//usage: )
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage:
|
|
|
|
//usage:#define du_example_usage
|
|
|
|
//usage: "$ du\n"
|
|
|
|
//usage: "16 ./CVS\n"
|
|
|
|
//usage: "12 ./kernel-patches/CVS\n"
|
|
|
|
//usage: "80 ./kernel-patches\n"
|
|
|
|
//usage: "12 ./tests/CVS\n"
|
|
|
|
//usage: "36 ./tests\n"
|
|
|
|
//usage: "12 ./scripts/CVS\n"
|
|
|
|
//usage: "16 ./scripts\n"
|
|
|
|
//usage: "12 ./docs/CVS\n"
|
|
|
|
//usage: "104 ./docs\n"
|
|
|
|
//usage: "2417 .\n"
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2016-04-21 14:26:30 +00:00
|
|
|
#include "common_bufsiz.h"
|
2001-02-20 06:14:08 +00:00
|
|
|
|
2008-11-11 21:15:56 +00:00
|
|
|
enum {
|
|
|
|
OPT_a_files_too = (1 << 0),
|
|
|
|
OPT_H_follow_links = (1 << 1),
|
|
|
|
OPT_k_kbytes = (1 << 2),
|
|
|
|
OPT_L_follow_links = (1 << 3),
|
|
|
|
OPT_s_total_norecurse = (1 << 4),
|
|
|
|
OPT_x_one_FS = (1 << 5),
|
|
|
|
OPT_d_maxdepth = (1 << 6),
|
|
|
|
OPT_l_hardlinks = (1 << 7),
|
|
|
|
OPT_c_total = (1 << 8),
|
|
|
|
OPT_h_for_humans = (1 << 9),
|
|
|
|
OPT_m_mbytes = (1 << 10),
|
|
|
|
};
|
|
|
|
|
2007-09-03 20:05:58 +00:00
|
|
|
struct globals {
|
2006-12-18 21:22:16 +00:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2015-10-15 19:33:34 +00:00
|
|
|
unsigned long disp_unit;
|
2003-03-19 09:13:01 +00:00
|
|
|
#else
|
2007-09-03 20:05:58 +00:00
|
|
|
unsigned disp_k;
|
2001-01-22 22:35:38 +00:00
|
|
|
#endif
|
2007-09-03 20:05:58 +00:00
|
|
|
int max_print_depth;
|
|
|
|
bool status;
|
|
|
|
int slink_depth;
|
|
|
|
int du_depth;
|
|
|
|
dev_t dir_dev;
|
2010-02-04 14:00:15 +00:00
|
|
|
} FIX_ALIASING;
|
2016-04-21 14:26:30 +00:00
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2016-04-21 16:18:48 +00:00
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
1999-12-09 18:23:54 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2012-02-28 02:10:31 +00:00
|
|
|
static void print(unsigned long long size, const char *filename)
|
1999-12-09 18:23:54 +00:00
|
|
|
{
|
2003-03-19 09:13:01 +00:00
|
|
|
/* TODO - May not want to defer error checking here. */
|
2006-12-18 21:22:16 +00:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2015-10-15 19:33:34 +00:00
|
|
|
# if ENABLE_DESKTOP
|
|
|
|
/* ~30 bytes of code for extra comtat:
|
|
|
|
* coreutils' du rounds sizes up:
|
|
|
|
* for example, 1025k file is shown as "2" by du -m.
|
|
|
|
* We round to nearest if human-readable [too hard to fix],
|
|
|
|
* else (fixed scale such as -m), we round up. To that end,
|
|
|
|
* add yet another half of the unit before displaying:
|
|
|
|
*/
|
|
|
|
if (G.disp_unit)
|
|
|
|
size += (G.disp_unit-1) / (unsigned)(512 * 2);
|
|
|
|
# endif
|
2009-10-12 23:25:09 +00:00
|
|
|
printf("%s\t%s\n",
|
2015-10-15 19:33:34 +00:00
|
|
|
/* size x 512 / G.disp_unit.
|
|
|
|
* If G.disp_unit == 0, show one fractional
|
|
|
|
* and use suffixes
|
|
|
|
*/
|
|
|
|
make_human_readable_str(size, 512, G.disp_unit),
|
2006-12-18 21:22:16 +00:00
|
|
|
filename);
|
2001-01-22 22:35:38 +00:00
|
|
|
#else
|
2007-09-03 20:05:58 +00:00
|
|
|
if (G.disp_k) {
|
2004-03-10 09:58:51 +00:00
|
|
|
size++;
|
|
|
|
size >>= 1;
|
|
|
|
}
|
2012-02-28 02:10:31 +00:00
|
|
|
printf("%llu\t%s\n", size, filename);
|
2001-01-22 22:35:38 +00:00
|
|
|
#endif
|
1999-12-09 18:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tiny recursive du */
|
2012-02-28 02:10:31 +00:00
|
|
|
static unsigned long long du(const char *filename)
|
1999-12-09 18:23:54 +00:00
|
|
|
{
|
2000-02-08 19:58:47 +00:00
|
|
|
struct stat statbuf;
|
2012-02-28 02:10:31 +00:00
|
|
|
unsigned long long sum;
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2006-12-18 21:22:16 +00:00
|
|
|
if (lstat(filename, &statbuf) != 0) {
|
2007-10-01 11:58:38 +00:00
|
|
|
bb_simple_perror_msg(filename);
|
2007-09-03 20:05:58 +00:00
|
|
|
G.status = EXIT_FAILURE;
|
2001-04-09 22:48:12 +00:00
|
|
|
return 0;
|
1999-12-09 18:23:54 +00:00
|
|
|
}
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2008-11-11 21:15:56 +00:00
|
|
|
if (option_mask32 & OPT_x_one_FS) {
|
2007-09-03 20:05:58 +00:00
|
|
|
if (G.du_depth == 0) {
|
|
|
|
G.dir_dev = statbuf.st_dev;
|
|
|
|
} else if (G.dir_dev != statbuf.st_dev) {
|
2003-03-19 09:13:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = statbuf.st_blocks;
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2000-02-11 21:55:04 +00:00
|
|
|
if (S_ISLNK(statbuf.st_mode)) {
|
2008-11-11 21:15:56 +00:00
|
|
|
if (G.slink_depth > G.du_depth) { /* -H or -L */
|
2006-12-18 21:22:16 +00:00
|
|
|
if (stat(filename, &statbuf) != 0) {
|
2007-10-01 11:58:38 +00:00
|
|
|
bb_simple_perror_msg(filename);
|
2007-09-03 20:05:58 +00:00
|
|
|
G.status = EXIT_FAILURE;
|
2003-03-19 09:13:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sum = statbuf.st_blocks;
|
2007-09-03 20:05:58 +00:00
|
|
|
if (G.slink_depth == 1) {
|
2008-11-11 21:15:56 +00:00
|
|
|
/* Convert -H to -L */
|
|
|
|
G.slink_depth = INT_MAX;
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
2001-06-30 17:54:20 +00:00
|
|
|
}
|
2000-02-11 21:55:04 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2008-11-11 21:15:56 +00:00
|
|
|
if (!(option_mask32 & OPT_l_hardlinks)
|
|
|
|
&& statbuf.st_nlink > 1
|
|
|
|
) {
|
2003-03-19 09:13:01 +00:00
|
|
|
/* Add files/directories with links only once */
|
2007-03-14 22:06:57 +00:00
|
|
|
if (is_in_ino_dev_hashtable(&statbuf)) {
|
2003-03-19 09:13:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
add_to_ino_dev_hashtable(&statbuf, NULL);
|
|
|
|
}
|
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2001-05-07 22:49:43 +00:00
|
|
|
char *newfile;
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2006-08-03 17:58:17 +00:00
|
|
|
dir = warn_opendir(filename);
|
2000-02-08 19:58:47 +00:00
|
|
|
if (!dir) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.status = EXIT_FAILURE;
|
2003-03-19 09:13:01 +00:00
|
|
|
return sum;
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
2000-02-21 17:27:17 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
while ((entry = readdir(dir))) {
|
2008-11-11 21:15:56 +00:00
|
|
|
newfile = concat_subpath_file(filename, entry->d_name);
|
2006-12-18 21:22:16 +00:00
|
|
|
if (newfile == NULL)
|
2000-02-08 19:58:47 +00:00
|
|
|
continue;
|
2007-09-03 20:05:58 +00:00
|
|
|
++G.du_depth;
|
2000-02-08 19:58:47 +00:00
|
|
|
sum += du(newfile);
|
2007-09-03 20:05:58 +00:00
|
|
|
--G.du_depth;
|
2001-04-09 22:48:12 +00:00
|
|
|
free(newfile);
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
|
|
|
closedir(dir);
|
2008-11-11 21:15:56 +00:00
|
|
|
} else {
|
|
|
|
if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
|
|
|
|
return sum;
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
2007-09-03 20:05:58 +00:00
|
|
|
if (G.du_depth <= G.max_print_depth) {
|
2000-02-08 19:58:47 +00:00
|
|
|
print(sum, filename);
|
2000-02-19 18:16:49 +00:00
|
|
|
}
|
2000-02-08 19:58:47 +00:00
|
|
|
return sum;
|
1999-12-09 18:23:54 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int du_main(int argc UNUSED_PARAM, char **argv)
|
1999-12-09 18:23:54 +00:00
|
|
|
{
|
2012-02-28 02:10:31 +00:00
|
|
|
unsigned long long total;
|
2003-03-19 09:13:01 +00:00
|
|
|
int slink_depth_save;
|
2006-10-03 21:00:06 +00:00
|
|
|
unsigned opt;
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2011-09-20 23:59:15 +00:00
|
|
|
INIT_G();
|
|
|
|
|
2006-12-18 21:22:16 +00:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2015-10-15 19:33:34 +00:00
|
|
|
IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
|
|
|
|
IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
|
2007-09-03 20:05:58 +00:00
|
|
|
if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
|
2015-10-15 19:33:34 +00:00
|
|
|
G.disp_unit = 512;
|
2003-03-19 09:13:01 +00:00
|
|
|
#else
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
|
|
|
|
/* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
|
2003-03-19 09:13:01 +00:00
|
|
|
#endif
|
2007-09-03 20:05:58 +00:00
|
|
|
G.max_print_depth = INT_MAX;
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2006-10-20 13:28:22 +00:00
|
|
|
/* Note: SUSv3 specifies that -a and -s options cannot be used together
|
2003-03-19 09:13:01 +00:00
|
|
|
* in strictly conforming applications. However, it also says that some
|
|
|
|
* du implementations may produce output when -a and -s are used together.
|
|
|
|
* gnu du exits with an error code in this case. We choose to simply
|
|
|
|
* ignore -a. This is consistent with -s being equivalent to -d 0.
|
|
|
|
*/
|
2006-12-18 21:22:16 +00:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2016-07-06 19:58:02 +00:00
|
|
|
opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
|
|
|
|
opt = getopt32(argv, "aHkLsx" "d:+" "lc" "hm", &G.max_print_depth);
|
2007-09-03 20:05:58 +00:00
|
|
|
argv += optind;
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_h_for_humans) {
|
2015-10-15 19:33:34 +00:00
|
|
|
G.disp_unit = 0;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_m_mbytes) {
|
2015-10-15 19:33:34 +00:00
|
|
|
G.disp_unit = 1024*1024;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_k_kbytes) {
|
2015-10-15 19:33:34 +00:00
|
|
|
G.disp_unit = 1024;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
|
|
|
#else
|
2016-07-06 19:58:02 +00:00
|
|
|
opt_complementary = "H-L:L-H:s-d:d-s";
|
|
|
|
opt = getopt32(argv, "aHkLsx" "d:+" "lc", &G.max_print_depth);
|
2007-09-03 20:05:58 +00:00
|
|
|
argv += optind;
|
2006-12-18 21:22:16 +00:00
|
|
|
#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_k_kbytes) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.disp_k = 1;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
2003-03-19 09:13:01 +00:00
|
|
|
#endif
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_H_follow_links) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.slink_depth = 1;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_L_follow_links) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.slink_depth = INT_MAX;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_s_total_norecurse) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.max_print_depth = 0;
|
2006-12-18 21:22:16 +00:00
|
|
|
}
|
1999-12-10 06:15:27 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
/* go through remaining args (if any) */
|
2007-09-03 20:05:58 +00:00
|
|
|
if (!*argv) {
|
2007-01-29 22:51:00 +00:00
|
|
|
*--argv = (char*)".";
|
2007-09-03 20:05:58 +00:00
|
|
|
if (G.slink_depth == 1) {
|
|
|
|
G.slink_depth = 0;
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
1999-12-10 06:15:27 +00:00
|
|
|
}
|
|
|
|
|
2007-09-03 20:05:58 +00:00
|
|
|
slink_depth_save = G.slink_depth;
|
2003-03-19 09:13:01 +00:00
|
|
|
total = 0;
|
|
|
|
do {
|
|
|
|
total += du(*argv);
|
2008-11-11 21:15:56 +00:00
|
|
|
/* otherwise du /dir /dir won't show /dir twice: */
|
|
|
|
reset_ino_dev_hashtable();
|
2007-09-03 20:05:58 +00:00
|
|
|
G.slink_depth = slink_depth_save;
|
2003-03-19 09:13:01 +00:00
|
|
|
} while (*++argv);
|
2007-09-03 20:05:58 +00:00
|
|
|
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_c_total)
|
2003-03-19 09:13:01 +00:00
|
|
|
print(total, "total");
|
|
|
|
|
2007-09-03 20:05:58 +00:00
|
|
|
fflush_stdout_and_exit(G.status);
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|