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
|
|
|
*
|
2006-04-12 07:35:12 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
1999-12-09 18:23:54 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.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
|
2007-09-03 20:05:58 +00:00
|
|
|
unsigned long disp_hr;
|
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;
|
2007-09-03 20:05:58 +00:00
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
1999-12-09 18:23:54 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2007-09-03 20:05:58 +00:00
|
|
|
static void print(unsigned 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
|
2009-10-12 23:25:09 +00:00
|
|
|
printf("%s\t%s\n",
|
|
|
|
/* size x 512 / G.disp_hr, show one fractional,
|
|
|
|
* use suffixes if G.disp_hr == 0 */
|
|
|
|
make_human_readable_str(size, 512, G.disp_hr),
|
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;
|
|
|
|
}
|
2009-10-12 23:25:09 +00:00
|
|
|
printf("%lu\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 */
|
2007-09-03 20:05:58 +00:00
|
|
|
static unsigned long du(const char *filename)
|
1999-12-09 18:23:54 +00:00
|
|
|
{
|
2000-02-08 19:58:47 +00:00
|
|
|
struct stat statbuf;
|
2007-09-03 20:05:58 +00:00
|
|
|
unsigned 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
|
|
|
{
|
2007-09-03 20:05:58 +00:00
|
|
|
unsigned 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
|
|
|
|
2006-12-18 21:22:16 +00:00
|
|
|
#if ENABLE_FEATURE_HUMAN_READABLE
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
|
|
|
|
IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
|
2007-09-03 20:05:58 +00:00
|
|
|
if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
|
|
|
|
G.disp_hr = 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
|
2008-03-17 09:09:09 +00:00
|
|
|
opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s:d+";
|
|
|
|
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) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.disp_hr = 0;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_m_mbytes) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.disp_hr = 1024*1024;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
2008-11-11 21:15:56 +00:00
|
|
|
if (opt & OPT_k_kbytes) {
|
2007-09-03 20:05:58 +00:00
|
|
|
G.disp_hr = 1024;
|
2003-06-20 09:01:58 +00:00
|
|
|
}
|
|
|
|
#else
|
2008-03-17 09:09:09 +00:00
|
|
|
opt_complementary = "H-L:L-H:s-d:d-s:d+";
|
|
|
|
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
|
|
|
}
|