Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/du.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/coreutils/du.c

    r1765 r2725  
    77 * Copyright (C) 2002  Edward Betts <edward@debian.org>
    88 *
    9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    1010 */
    1111
     
    2626#include "libbb.h"
    2727
    28 #if ENABLE_FEATURE_HUMAN_READABLE
    29 # if ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
    30 static unsigned long disp_hr = 1024;
    31 # else
    32 static unsigned long disp_hr = 512;
    33 # endif
    34 #elif ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
    35 static unsigned disp_k = 1;
    36 #else
    37 static unsigned disp_k; /* bss inits to 0 */
    38 #endif
    39 
    40 static int max_print_depth = INT_MAX;
    41 static nlink_t count_hardlinks = 1;
    42 
    43 static int status;
    44 static int print_files;
    45 static int slink_depth;
    46 static int du_depth;
    47 static int one_file_system;
    48 static dev_t dir_dev;
    49 
    50 
    51 static void print(long size, const char *const filename)
     28enum {
     29    OPT_a_files_too    = (1 << 0),
     30    OPT_H_follow_links = (1 << 1),
     31    OPT_k_kbytes       = (1 << 2),
     32    OPT_L_follow_links = (1 << 3),
     33    OPT_s_total_norecurse = (1 << 4),
     34    OPT_x_one_FS       = (1 << 5),
     35    OPT_d_maxdepth     = (1 << 6),
     36    OPT_l_hardlinks    = (1 << 7),
     37    OPT_c_total        = (1 << 8),
     38    OPT_h_for_humans   = (1 << 9),
     39    OPT_m_mbytes       = (1 << 10),
     40};
     41
     42struct globals {
     43#if ENABLE_FEATURE_HUMAN_READABLE
     44    unsigned long disp_hr;
     45#else
     46    unsigned disp_k;
     47#endif
     48    int max_print_depth;
     49    bool status;
     50    int slink_depth;
     51    int du_depth;
     52    dev_t dir_dev;
     53} FIX_ALIASING;
     54#define G (*(struct globals*)&bb_common_bufsiz1)
     55
     56
     57static void print(unsigned long size, const char *filename)
    5258{
    5359    /* TODO - May not want to defer error checking here. */
    5460#if ENABLE_FEATURE_HUMAN_READABLE
    55     printf("%s\t%s\n", make_human_readable_str(size, 512, disp_hr),
     61    printf("%s\t%s\n",
     62            /* size x 512 / G.disp_hr, show one fractional,
     63             * use suffixes if G.disp_hr == 0 */
     64            make_human_readable_str(size, 512, G.disp_hr),
    5665            filename);
    5766#else
    58     if (disp_k) {
     67    if (G.disp_k) {
    5968        size++;
    6069        size >>= 1;
    6170    }
    62     printf("%ld\t%s\n", size, filename);
     71    printf("%lu\t%s\n", size, filename);
    6372#endif
    6473}
    6574
    6675/* tiny recursive du */
    67 static long du(const char *const filename)
     76static unsigned long du(const char *filename)
    6877{
    6978    struct stat statbuf;
    70     long sum;
     79    unsigned long sum;
    7180
    7281    if (lstat(filename, &statbuf) != 0) {
    73         bb_perror_msg("%s", filename);
    74         status = EXIT_FAILURE;
     82        bb_simple_perror_msg(filename);
     83        G.status = EXIT_FAILURE;
    7584        return 0;
    7685    }
    7786
    78     if (one_file_system) {
    79         if (du_depth == 0) {
    80             dir_dev = statbuf.st_dev;
    81         } else if (dir_dev != statbuf.st_dev) {
     87    if (option_mask32 & OPT_x_one_FS) {
     88        if (G.du_depth == 0) {
     89            G.dir_dev = statbuf.st_dev;
     90        } else if (G.dir_dev != statbuf.st_dev) {
    8291            return 0;
    8392        }
     
    8796
    8897    if (S_ISLNK(statbuf.st_mode)) {
    89         if (slink_depth > du_depth) {   /* -H or -L */
     98        if (G.slink_depth > G.du_depth) { /* -H or -L */
    9099            if (stat(filename, &statbuf) != 0) {
    91                 bb_perror_msg("%s", filename);
    92                 status = EXIT_FAILURE;
     100                bb_simple_perror_msg(filename);
     101                G.status = EXIT_FAILURE;
    93102                return 0;
    94103            }
    95104            sum = statbuf.st_blocks;
    96             if (slink_depth == 1) {
    97                 slink_depth = INT_MAX;  /* Convert -H to -L. */
     105            if (G.slink_depth == 1) {
     106                /* Convert -H to -L */
     107                G.slink_depth = INT_MAX;
    98108            }
    99109        }
    100110    }
    101111
    102     if (statbuf.st_nlink > count_hardlinks) {
     112    if (!(option_mask32 & OPT_l_hardlinks)
     113     && statbuf.st_nlink > 1
     114    ) {
    103115        /* Add files/directories with links only once */
    104116        if (is_in_ino_dev_hashtable(&statbuf)) {
     
    115127        dir = warn_opendir(filename);
    116128        if (!dir) {
    117             status = EXIT_FAILURE;
     129            G.status = EXIT_FAILURE;
    118130            return sum;
    119131        }
    120132
    121         newfile = last_char_is(filename, '/');
    122         if (newfile)
    123             *newfile = '\0';
    124 
    125133        while ((entry = readdir(dir))) {
    126             char *name = entry->d_name;
    127 
    128             newfile = concat_subpath_file(filename, name);
     134            newfile = concat_subpath_file(filename, entry->d_name);
    129135            if (newfile == NULL)
    130136                continue;
    131             ++du_depth;
     137            ++G.du_depth;
    132138            sum += du(newfile);
    133             --du_depth;
     139            --G.du_depth;
    134140            free(newfile);
    135141        }
    136142        closedir(dir);
    137     } else if (du_depth > print_files) {
    138         return sum;
    139     }
    140     if (du_depth <= max_print_depth) {
     143    } else {
     144        if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
     145            return sum;
     146    }
     147    if (G.du_depth <= G.max_print_depth) {
    141148        print(sum, filename);
    142149    }
     
    144151}
    145152
    146 int du_main(int argc, char **argv);
    147 int du_main(int argc, char **argv)
     153int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     154int du_main(int argc UNUSED_PARAM, char **argv)
    148155{
    149     long total;
     156    unsigned long total;
    150157    int slink_depth_save;
    151     int print_final_total;
    152     char *smax_print_depth;
    153158    unsigned opt;
    154159
    155 #if ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
    156     if (getenv("POSIXLY_CORRECT")) {    /* TODO - a new libbb function? */
    157 #if ENABLE_FEATURE_HUMAN_READABLE
    158         disp_hr = 512;
    159 #else
    160         disp_k = 0;
    161 #endif
    162     }
    163 #endif
     160#if ENABLE_FEATURE_HUMAN_READABLE
     161    IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
     162    IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
     163    if (getenv("POSIXLY_CORRECT"))  /* TODO - a new libbb function? */
     164        G.disp_hr = 512;
     165#else
     166    IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
     167    /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
     168#endif
     169    G.max_print_depth = INT_MAX;
    164170
    165171    /* Note: SUSv3 specifies that -a and -s options cannot be used together
     
    170176     */
    171177#if ENABLE_FEATURE_HUMAN_READABLE
    172     opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
    173     opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth);
    174     if (opt & (1 << 9)) {
    175         /* -h opt */
    176         disp_hr = 0;
    177     }
    178     if (opt & (1 << 10)) {
    179         /* -m opt */
    180         disp_hr = 1024*1024;
    181     }
    182     if (opt & (1 << 2)) {
    183         /* -k opt */
    184         disp_hr = 1024;
    185     }
    186 #else
    187     opt_complementary = "H-L:L-H:s-d:d-s";
    188     opt = getopt32(argv, "aHkLsx" "d:" "lc", &smax_print_depth);
     178    opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s:d+";
     179    opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth);
     180    argv += optind;
     181    if (opt & OPT_h_for_humans) {
     182        G.disp_hr = 0;
     183    }
     184    if (opt & OPT_m_mbytes) {
     185        G.disp_hr = 1024*1024;
     186    }
     187    if (opt & OPT_k_kbytes) {
     188        G.disp_hr = 1024;
     189    }
     190#else
     191    opt_complementary = "H-L:L-H:s-d:d-s:d+";
     192    opt = getopt32(argv, "aHkLsx" "d:" "lc", &G.max_print_depth);
     193    argv += optind;
    189194#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
    190     if (opt & (1 << 2)) {
    191         /* -k opt */
    192         disp_k = 1;
    193     }
    194 #endif
    195 #endif
    196     if (opt & (1 << 0)) {
    197         /* -a opt */
    198         print_files = INT_MAX;
    199     }
    200     if (opt & (1 << 1)) {
    201         /* -H opt */
    202         slink_depth = 1;
    203     }
    204     if (opt & (1 << 3)) {
    205         /* -L opt */
    206         slink_depth = INT_MAX;
    207     }
    208     if (opt & (1 << 4)) {
    209         /* -s opt */
    210         max_print_depth = 0;
    211     }
    212     one_file_system = opt & (1 << 5); /* -x opt */
    213     if (opt & (1 << 6)) {
    214         /* -d opt */
    215         max_print_depth = xatoi_u(smax_print_depth);
    216     }
    217     if (opt & (1 << 7)) {
    218         /* -l opt */
    219         count_hardlinks = MAXINT(nlink_t);
    220     }
    221     print_final_total = opt & (1 << 8); /* -c opt */
     195    if (opt & OPT_k_kbytes) {
     196        G.disp_k = 1;
     197    }
     198#endif
     199#endif
     200    if (opt & OPT_H_follow_links) {
     201        G.slink_depth = 1;
     202    }
     203    if (opt & OPT_L_follow_links) {
     204        G.slink_depth = INT_MAX;
     205    }
     206    if (opt & OPT_s_total_norecurse) {
     207        G.max_print_depth = 0;
     208    }
    222209
    223210    /* go through remaining args (if any) */
    224     argv += optind;
    225     if (optind >= argc) {
     211    if (!*argv) {
    226212        *--argv = (char*)".";
    227         if (slink_depth == 1) {
    228             slink_depth = 0;
    229         }
    230     }
    231 
    232     slink_depth_save = slink_depth;
     213        if (G.slink_depth == 1) {
     214            G.slink_depth = 0;
     215        }
     216    }
     217
     218    slink_depth_save = G.slink_depth;
    233219    total = 0;
    234220    do {
    235221        total += du(*argv);
    236         slink_depth = slink_depth_save;
     222        /* otherwise du /dir /dir won't show /dir twice: */
     223        reset_ino_dev_hashtable();
     224        G.slink_depth = slink_depth_save;
    237225    } while (*++argv);
    238     if (ENABLE_FEATURE_CLEAN_UP)
    239         reset_ino_dev_hashtable();
    240     if (print_final_total) {
     226
     227    if (opt & OPT_c_total)
    241228        print(total, "total");
    242     }
    243 
    244     fflush_stdout_and_exit(status);
     229
     230    fflush_stdout_and_exit(G.status);
    245231}
Note: See TracChangeset for help on using the changeset viewer.