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