2000-02-09 03:58:47 +08:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-10 02:23:54 +08:00
|
|
|
/*
|
|
|
|
* Mini du implementation for busybox
|
|
|
|
*
|
2001-10-24 13:00:29 +08: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-07 07:16:44 +08:00
|
|
|
* Copyright (C) 2002 Edward Betts <edward@debian.org>
|
1999-12-10 02:23:54 +08:00
|
|
|
*
|
2010-08-17 02:14:46 +08:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-12-10 02:23:54 +08:00
|
|
|
*/
|
2003-03-19 17:13:01 +08: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 21:46:56 +08:00
|
|
|
//config:config DU
|
|
|
|
//config: bool "du (default blocksize of 512 bytes)"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: du is used to report the amount of disk space used
|
|
|
|
//config: for specified files.
|
2016-11-23 21:46:56 +08:00
|
|
|
//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
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: Use a blocksize of (1K) instead of the default 512b.
|
2016-11-23 21:46:56 +08:00
|
|
|
|
|
|
|
//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 17:13:01 +08:00
|
|
|
|
2011-03-31 20:43:25 +08: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 16:59:56 +08:00
|
|
|
//usage: "Summarize disk space used for each FILE and/or directory\n"
|
2011-03-31 20:43:25 +08: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 16:52:19 +08:00
|
|
|
//usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)"
|
2011-03-31 20:43:25 +08:00
|
|
|
//usage: "\n -m Sizes in megabytes"
|
|
|
|
//usage: )
|
2012-03-05 16:59:56 +08: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 20:43:25 +08: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-27 03:00:18 +08:00
|
|
|
#include "libbb.h"
|
2016-04-21 22:26:30 +08:00
|
|
|
#include "common_bufsiz.h"
|
2001-02-20 14:14:08 +08:00
|
|
|
|
2008-11-12 05:15:56 +08: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-04 04:05:58 +08:00
|
|
|
struct globals {
|
2006-12-19 05:22:16 +08:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2015-10-16 03:33:34 +08:00
|
|
|
unsigned long disp_unit;
|
2003-03-19 17:13:01 +08:00
|
|
|
#else
|
2007-09-04 04:05:58 +08:00
|
|
|
unsigned disp_k;
|
2001-01-23 06:35:38 +08:00
|
|
|
#endif
|
2007-09-04 04:05:58 +08:00
|
|
|
int max_print_depth;
|
|
|
|
bool status;
|
|
|
|
int slink_depth;
|
|
|
|
int du_depth;
|
|
|
|
dev_t dir_dev;
|
2010-02-04 22:00:15 +08:00
|
|
|
} FIX_ALIASING;
|
2016-04-21 22:26:30 +08:00
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2016-04-22 00:18:48 +08:00
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
1999-12-10 02:23:54 +08:00
|
|
|
|
2000-02-09 03:58:47 +08:00
|
|
|
|
2012-02-28 10:10:31 +08:00
|
|
|
static void print(unsigned long long size, const char *filename)
|
1999-12-10 02:23:54 +08:00
|
|
|
{
|
2003-03-19 17:13:01 +08:00
|
|
|
/* TODO - May not want to defer error checking here. */
|
2006-12-19 05:22:16 +08:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2015-10-16 03:33:34 +08: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-13 07:25:09 +08:00
|
|
|
printf("%s\t%s\n",
|
2015-10-16 03:33:34 +08: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-19 05:22:16 +08:00
|
|
|
filename);
|
2001-01-23 06:35:38 +08:00
|
|
|
#else
|
2007-09-04 04:05:58 +08:00
|
|
|
if (G.disp_k) {
|
2004-03-10 17:58:51 +08:00
|
|
|
size++;
|
|
|
|
size >>= 1;
|
|
|
|
}
|
2012-02-28 10:10:31 +08:00
|
|
|
printf("%llu\t%s\n", size, filename);
|
2001-01-23 06:35:38 +08:00
|
|
|
#endif
|
1999-12-10 02:23:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tiny recursive du */
|
2012-02-28 10:10:31 +08:00
|
|
|
static unsigned long long du(const char *filename)
|
1999-12-10 02:23:54 +08:00
|
|
|
{
|
2000-02-09 03:58:47 +08:00
|
|
|
struct stat statbuf;
|
2012-02-28 10:10:31 +08:00
|
|
|
unsigned long long sum;
|
2000-02-09 03:58:47 +08:00
|
|
|
|
2006-12-19 05:22:16 +08:00
|
|
|
if (lstat(filename, &statbuf) != 0) {
|
2007-10-01 19:58:38 +08:00
|
|
|
bb_simple_perror_msg(filename);
|
2007-09-04 04:05:58 +08:00
|
|
|
G.status = EXIT_FAILURE;
|
2001-04-10 06:48:12 +08:00
|
|
|
return 0;
|
1999-12-10 02:23:54 +08:00
|
|
|
}
|
2000-02-09 03:58:47 +08:00
|
|
|
|
2008-11-12 05:15:56 +08:00
|
|
|
if (option_mask32 & OPT_x_one_FS) {
|
2007-09-04 04:05:58 +08:00
|
|
|
if (G.du_depth == 0) {
|
|
|
|
G.dir_dev = statbuf.st_dev;
|
|
|
|
} else if (G.dir_dev != statbuf.st_dev) {
|
2003-03-19 17:13:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = statbuf.st_blocks;
|
2000-02-09 03:58:47 +08:00
|
|
|
|
2000-02-12 05:55:04 +08:00
|
|
|
if (S_ISLNK(statbuf.st_mode)) {
|
2008-11-12 05:15:56 +08:00
|
|
|
if (G.slink_depth > G.du_depth) { /* -H or -L */
|
2006-12-19 05:22:16 +08:00
|
|
|
if (stat(filename, &statbuf) != 0) {
|
2007-10-01 19:58:38 +08:00
|
|
|
bb_simple_perror_msg(filename);
|
2007-09-04 04:05:58 +08:00
|
|
|
G.status = EXIT_FAILURE;
|
2003-03-19 17:13:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sum = statbuf.st_blocks;
|
2007-09-04 04:05:58 +08:00
|
|
|
if (G.slink_depth == 1) {
|
2008-11-12 05:15:56 +08:00
|
|
|
/* Convert -H to -L */
|
|
|
|
G.slink_depth = INT_MAX;
|
2003-03-19 17:13:01 +08:00
|
|
|
}
|
2001-07-01 01:54:20 +08:00
|
|
|
}
|
2000-02-12 05:55:04 +08:00
|
|
|
}
|
2003-03-19 17:13:01 +08:00
|
|
|
|
2008-11-12 05:15:56 +08:00
|
|
|
if (!(option_mask32 & OPT_l_hardlinks)
|
|
|
|
&& statbuf.st_nlink > 1
|
|
|
|
) {
|
2003-03-19 17:13:01 +08:00
|
|
|
/* Add files/directories with links only once */
|
2007-03-15 06:06:57 +08:00
|
|
|
if (is_in_ino_dev_hashtable(&statbuf)) {
|
2003-03-19 17:13:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
add_to_ino_dev_hashtable(&statbuf, NULL);
|
|
|
|
}
|
|
|
|
|
2000-02-09 03:58:47 +08:00
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2001-05-08 06:49:43 +08:00
|
|
|
char *newfile;
|
2000-02-09 03:58:47 +08:00
|
|
|
|
2006-08-04 01:58:17 +08:00
|
|
|
dir = warn_opendir(filename);
|
2000-02-09 03:58:47 +08:00
|
|
|
if (!dir) {
|
2007-09-04 04:05:58 +08:00
|
|
|
G.status = EXIT_FAILURE;
|
2003-03-19 17:13:01 +08:00
|
|
|
return sum;
|
2000-02-09 03:58:47 +08:00
|
|
|
}
|
2000-02-22 01:27:17 +08:00
|
|
|
|
2000-02-09 03:58:47 +08:00
|
|
|
while ((entry = readdir(dir))) {
|
2008-11-12 05:15:56 +08:00
|
|
|
newfile = concat_subpath_file(filename, entry->d_name);
|
2006-12-19 05:22:16 +08:00
|
|
|
if (newfile == NULL)
|
2000-02-09 03:58:47 +08:00
|
|
|
continue;
|
2007-09-04 04:05:58 +08:00
|
|
|
++G.du_depth;
|
2000-02-09 03:58:47 +08:00
|
|
|
sum += du(newfile);
|
2007-09-04 04:05:58 +08:00
|
|
|
--G.du_depth;
|
2001-04-10 06:48:12 +08:00
|
|
|
free(newfile);
|
2000-02-09 03:58:47 +08:00
|
|
|
}
|
|
|
|
closedir(dir);
|
2008-11-12 05:15:56 +08:00
|
|
|
} else {
|
|
|
|
if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
|
|
|
|
return sum;
|
2003-03-19 17:13:01 +08:00
|
|
|
}
|
2007-09-04 04:05:58 +08:00
|
|
|
if (G.du_depth <= G.max_print_depth) {
|
2000-02-09 03:58:47 +08:00
|
|
|
print(sum, filename);
|
2000-02-20 02:16:49 +08:00
|
|
|
}
|
2000-02-09 03:58:47 +08:00
|
|
|
return sum;
|
1999-12-10 02:23:54 +08:00
|
|
|
}
|
|
|
|
|
2007-10-11 18:05:36 +08:00
|
|
|
int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 17:18:54 +08:00
|
|
|
int du_main(int argc UNUSED_PARAM, char **argv)
|
1999-12-10 02:23:54 +08:00
|
|
|
{
|
2012-02-28 10:10:31 +08:00
|
|
|
unsigned long long total;
|
2003-03-19 17:13:01 +08:00
|
|
|
int slink_depth_save;
|
2006-10-04 05:00:06 +08:00
|
|
|
unsigned opt;
|
2000-02-09 03:58:47 +08:00
|
|
|
|
2011-09-21 07:59:15 +08:00
|
|
|
INIT_G();
|
|
|
|
|
2006-12-19 05:22:16 +08:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2015-10-16 03:33:34 +08:00
|
|
|
IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
|
|
|
|
IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
|
2007-09-04 04:05:58 +08:00
|
|
|
if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
|
2015-10-16 03:33:34 +08:00
|
|
|
G.disp_unit = 512;
|
2003-03-19 17:13:01 +08:00
|
|
|
#else
|
2009-04-21 19:09:40 +08: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 17:13:01 +08:00
|
|
|
#endif
|
2007-09-04 04:05:58 +08:00
|
|
|
G.max_print_depth = INT_MAX;
|
2003-03-19 17:13:01 +08:00
|
|
|
|
2006-10-20 21:28:22 +08:00
|
|
|
/* Note: SUSv3 specifies that -a and -s options cannot be used together
|
2003-03-19 17:13:01 +08: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-19 05:22:16 +08:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2017-08-09 03:55:02 +08:00
|
|
|
opt = getopt32(argv, "^"
|
|
|
|
"aHkLsxd:+lchm"
|
|
|
|
"\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
|
|
|
|
&G.max_print_depth
|
|
|
|
);
|
2007-09-04 04:05:58 +08:00
|
|
|
argv += optind;
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_h_for_humans) {
|
2015-10-16 03:33:34 +08:00
|
|
|
G.disp_unit = 0;
|
2003-06-20 17:01:58 +08:00
|
|
|
}
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_m_mbytes) {
|
2015-10-16 03:33:34 +08:00
|
|
|
G.disp_unit = 1024*1024;
|
2003-06-20 17:01:58 +08:00
|
|
|
}
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_k_kbytes) {
|
2015-10-16 03:33:34 +08:00
|
|
|
G.disp_unit = 1024;
|
2003-06-20 17:01:58 +08:00
|
|
|
}
|
|
|
|
#else
|
2017-08-09 03:55:02 +08:00
|
|
|
opt = getopt32(argv, "^"
|
|
|
|
"aHkLsxd:+lc"
|
|
|
|
"\0" "H-L:L-H:s-d:d-s",
|
|
|
|
&G.max_print_depth
|
|
|
|
);
|
2007-09-04 04:05:58 +08:00
|
|
|
argv += optind;
|
2006-12-19 05:22:16 +08:00
|
|
|
#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_k_kbytes) {
|
2007-09-04 04:05:58 +08:00
|
|
|
G.disp_k = 1;
|
2003-06-20 17:01:58 +08:00
|
|
|
}
|
|
|
|
#endif
|
2003-03-19 17:13:01 +08:00
|
|
|
#endif
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_H_follow_links) {
|
2007-09-04 04:05:58 +08:00
|
|
|
G.slink_depth = 1;
|
2003-06-20 17:01:58 +08:00
|
|
|
}
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_L_follow_links) {
|
2007-09-04 04:05:58 +08:00
|
|
|
G.slink_depth = INT_MAX;
|
2003-06-20 17:01:58 +08:00
|
|
|
}
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_s_total_norecurse) {
|
2007-09-04 04:05:58 +08:00
|
|
|
G.max_print_depth = 0;
|
2006-12-19 05:22:16 +08:00
|
|
|
}
|
1999-12-10 14:15:27 +08:00
|
|
|
|
2000-02-09 03:58:47 +08:00
|
|
|
/* go through remaining args (if any) */
|
2007-09-04 04:05:58 +08:00
|
|
|
if (!*argv) {
|
2007-01-30 06:51:00 +08:00
|
|
|
*--argv = (char*)".";
|
2007-09-04 04:05:58 +08:00
|
|
|
if (G.slink_depth == 1) {
|
|
|
|
G.slink_depth = 0;
|
2000-02-09 03:58:47 +08:00
|
|
|
}
|
1999-12-10 14:15:27 +08:00
|
|
|
}
|
|
|
|
|
2007-09-04 04:05:58 +08:00
|
|
|
slink_depth_save = G.slink_depth;
|
2003-03-19 17:13:01 +08:00
|
|
|
total = 0;
|
|
|
|
do {
|
|
|
|
total += du(*argv);
|
2008-11-12 05:15:56 +08:00
|
|
|
/* otherwise du /dir /dir won't show /dir twice: */
|
|
|
|
reset_ino_dev_hashtable();
|
2007-09-04 04:05:58 +08:00
|
|
|
G.slink_depth = slink_depth_save;
|
2003-03-19 17:13:01 +08:00
|
|
|
} while (*++argv);
|
2007-09-04 04:05:58 +08:00
|
|
|
|
2008-11-12 05:15:56 +08:00
|
|
|
if (opt & OPT_c_total)
|
2003-03-19 17:13:01 +08:00
|
|
|
print(total, "total");
|
|
|
|
|
2007-09-04 04:05:58 +08:00
|
|
|
fflush_stdout_and_exit(G.status);
|
2003-03-19 17:13:01 +08:00
|
|
|
}
|