Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/du.c
Parent Directory | Revision Log
Revision 1123 -
(hide annotations)
(download)
Wed Aug 18 21:56:57 2010 UTC (13 years, 8 months ago) by niro
File MIME type: text/plain
File size: 5809 byte(s)
Wed Aug 18 21:56:57 2010 UTC (13 years, 8 months ago) by niro
File MIME type: text/plain
File size: 5809 byte(s)
-updated to busybox-1.17.1
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Mini du implementation for busybox | ||
4 | * | ||
5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu | ||
6 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> | ||
7 | * Copyright (C) 2002 Edward Betts <edward@debian.org> | ||
8 | * | ||
9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
10 | */ | ||
11 | |||
12 | /* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */ | ||
13 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */ | ||
14 | |||
15 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) | ||
16 | * | ||
17 | * Mostly rewritten for SUSv3 compliance and to fix bugs/defects. | ||
18 | * 1) Added support for SUSv3 -a, -H, -L, gnu -c, and (busybox) -d options. | ||
19 | * The -d option allows setting of max depth (similar to gnu --max-depth). | ||
20 | * 2) Fixed incorrect size calculations for links and directories, especially | ||
21 | * when errors occurred. Calculates sizes should now match gnu du output. | ||
22 | * 3) Added error checking of output. | ||
23 | * 4) Fixed busybox bug #1284 involving long overflow with human_readable. | ||
24 | */ | ||
25 | |||
26 | niro | 816 | #include "libbb.h" |
27 | niro | 532 | |
28 | niro | 984 | enum { |
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 | |||
42 | niro | 816 | struct globals { |
43 | niro | 532 | #if ENABLE_FEATURE_HUMAN_READABLE |
44 | niro | 816 | unsigned long disp_hr; |
45 | niro | 532 | #else |
46 | niro | 816 | unsigned disp_k; |
47 | niro | 532 | #endif |
48 | niro | 816 | int max_print_depth; |
49 | bool status; | ||
50 | int slink_depth; | ||
51 | int du_depth; | ||
52 | dev_t dir_dev; | ||
53 | niro | 1123 | } FIX_ALIASING; |
54 | niro | 816 | #define G (*(struct globals*)&bb_common_bufsiz1) |
55 | niro | 532 | |
56 | |||
57 | niro | 816 | static void print(unsigned long size, const char *filename) |
58 | niro | 532 | { |
59 | /* TODO - May not want to defer error checking here. */ | ||
60 | #if ENABLE_FEATURE_HUMAN_READABLE | ||
61 | niro | 984 | 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), | ||
65 | niro | 532 | filename); |
66 | #else | ||
67 | niro | 816 | if (G.disp_k) { |
68 | niro | 532 | size++; |
69 | size >>= 1; | ||
70 | } | ||
71 | niro | 984 | printf("%lu\t%s\n", size, filename); |
72 | niro | 532 | #endif |
73 | } | ||
74 | |||
75 | /* tiny recursive du */ | ||
76 | niro | 816 | static unsigned long du(const char *filename) |
77 | niro | 532 | { |
78 | struct stat statbuf; | ||
79 | niro | 816 | unsigned long sum; |
80 | niro | 532 | |
81 | if (lstat(filename, &statbuf) != 0) { | ||
82 | niro | 816 | bb_simple_perror_msg(filename); |
83 | G.status = EXIT_FAILURE; | ||
84 | niro | 532 | return 0; |
85 | } | ||
86 | |||
87 | niro | 984 | if (option_mask32 & OPT_x_one_FS) { |
88 | niro | 816 | if (G.du_depth == 0) { |
89 | G.dir_dev = statbuf.st_dev; | ||
90 | } else if (G.dir_dev != statbuf.st_dev) { | ||
91 | niro | 532 | return 0; |
92 | } | ||
93 | } | ||
94 | |||
95 | sum = statbuf.st_blocks; | ||
96 | |||
97 | if (S_ISLNK(statbuf.st_mode)) { | ||
98 | niro | 984 | if (G.slink_depth > G.du_depth) { /* -H or -L */ |
99 | niro | 532 | if (stat(filename, &statbuf) != 0) { |
100 | niro | 816 | bb_simple_perror_msg(filename); |
101 | G.status = EXIT_FAILURE; | ||
102 | niro | 532 | return 0; |
103 | } | ||
104 | sum = statbuf.st_blocks; | ||
105 | niro | 816 | if (G.slink_depth == 1) { |
106 | niro | 984 | /* Convert -H to -L */ |
107 | G.slink_depth = INT_MAX; | ||
108 | niro | 532 | } |
109 | } | ||
110 | } | ||
111 | |||
112 | niro | 984 | if (!(option_mask32 & OPT_l_hardlinks) |
113 | && statbuf.st_nlink > 1 | ||
114 | ) { | ||
115 | niro | 532 | /* Add files/directories with links only once */ |
116 | niro | 816 | if (is_in_ino_dev_hashtable(&statbuf)) { |
117 | niro | 532 | return 0; |
118 | } | ||
119 | add_to_ino_dev_hashtable(&statbuf, NULL); | ||
120 | } | ||
121 | |||
122 | if (S_ISDIR(statbuf.st_mode)) { | ||
123 | DIR *dir; | ||
124 | struct dirent *entry; | ||
125 | char *newfile; | ||
126 | |||
127 | dir = warn_opendir(filename); | ||
128 | if (!dir) { | ||
129 | niro | 816 | G.status = EXIT_FAILURE; |
130 | niro | 532 | return sum; |
131 | } | ||
132 | |||
133 | while ((entry = readdir(dir))) { | ||
134 | niro | 984 | newfile = concat_subpath_file(filename, entry->d_name); |
135 | niro | 532 | if (newfile == NULL) |
136 | continue; | ||
137 | niro | 816 | ++G.du_depth; |
138 | niro | 532 | sum += du(newfile); |
139 | niro | 816 | --G.du_depth; |
140 | niro | 532 | free(newfile); |
141 | } | ||
142 | closedir(dir); | ||
143 | niro | 984 | } else { |
144 | if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0) | ||
145 | return sum; | ||
146 | niro | 532 | } |
147 | niro | 816 | if (G.du_depth <= G.max_print_depth) { |
148 | niro | 532 | print(sum, filename); |
149 | } | ||
150 | return sum; | ||
151 | } | ||
152 | |||
153 | niro | 816 | int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
154 | int du_main(int argc UNUSED_PARAM, char **argv) | ||
155 | niro | 532 | { |
156 | niro | 816 | unsigned long total; |
157 | niro | 532 | int slink_depth_save; |
158 | unsigned opt; | ||
159 | |||
160 | #if ENABLE_FEATURE_HUMAN_READABLE | ||
161 | niro | 984 | IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;) |
162 | IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;) | ||
163 | niro | 816 | if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */ |
164 | G.disp_hr = 512; | ||
165 | niro | 532 | #else |
166 | niro | 984 | 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 | niro | 532 | #endif |
169 | niro | 816 | G.max_print_depth = INT_MAX; |
170 | niro | 532 | |
171 | /* Note: SUSv3 specifies that -a and -s options cannot be used together | ||
172 | * in strictly conforming applications. However, it also says that some | ||
173 | * du implementations may produce output when -a and -s are used together. | ||
174 | * gnu du exits with an error code in this case. We choose to simply | ||
175 | * ignore -a. This is consistent with -s being equivalent to -d 0. | ||
176 | */ | ||
177 | #if ENABLE_FEATURE_HUMAN_READABLE | ||
178 | niro | 816 | 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 | niro | 984 | if (opt & OPT_h_for_humans) { |
182 | niro | 816 | G.disp_hr = 0; |
183 | niro | 532 | } |
184 | niro | 984 | if (opt & OPT_m_mbytes) { |
185 | niro | 816 | G.disp_hr = 1024*1024; |
186 | niro | 532 | } |
187 | niro | 984 | if (opt & OPT_k_kbytes) { |
188 | niro | 816 | G.disp_hr = 1024; |
189 | niro | 532 | } |
190 | #else | ||
191 | niro | 816 | 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; | ||
194 | niro | 532 | #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K |
195 | niro | 984 | if (opt & OPT_k_kbytes) { |
196 | niro | 816 | G.disp_k = 1; |
197 | niro | 532 | } |
198 | #endif | ||
199 | #endif | ||
200 | niro | 984 | if (opt & OPT_H_follow_links) { |
201 | niro | 816 | G.slink_depth = 1; |
202 | niro | 532 | } |
203 | niro | 984 | if (opt & OPT_L_follow_links) { |
204 | niro | 816 | G.slink_depth = INT_MAX; |
205 | niro | 532 | } |
206 | niro | 984 | if (opt & OPT_s_total_norecurse) { |
207 | niro | 816 | G.max_print_depth = 0; |
208 | niro | 532 | } |
209 | |||
210 | /* go through remaining args (if any) */ | ||
211 | niro | 816 | if (!*argv) { |
212 | *--argv = (char*)"."; | ||
213 | if (G.slink_depth == 1) { | ||
214 | G.slink_depth = 0; | ||
215 | niro | 532 | } |
216 | } | ||
217 | |||
218 | niro | 816 | slink_depth_save = G.slink_depth; |
219 | niro | 532 | total = 0; |
220 | do { | ||
221 | total += du(*argv); | ||
222 | niro | 984 | /* otherwise du /dir /dir won't show /dir twice: */ |
223 | reset_ino_dev_hashtable(); | ||
224 | niro | 816 | G.slink_depth = slink_depth_save; |
225 | niro | 532 | } while (*++argv); |
226 | |||
227 | niro | 984 | if (opt & OPT_c_total) |
228 | niro | 532 | print(total, "total"); |
229 | |||
230 | niro | 816 | fflush_stdout_and_exit(G.status); |
231 | niro | 532 | } |