70322ae3 |
1 | /* |
2 | * du.c: implementation of du.h. |
3 | */ |
4 | |
50e82fdc |
5 | #include "agedu.h" |
70322ae3 |
6 | #include "du.h" |
995db599 |
7 | #include "alloc.h" |
70322ae3 |
8 | |
9c6e61f2 |
9 | #if !defined __linux__ || defined HAVE_FDOPENDIR |
704fafa3 |
10 | |
50e82fdc |
11 | #ifdef HAVE_DIRENT_H |
12 | # include <dirent.h> |
13 | #endif |
14 | #ifdef HAVE_NDIR_H |
15 | # include <ndir.h> |
16 | #endif |
17 | #ifdef HAVE_SYS_DIR_H |
18 | # include <sys/dir.h> |
19 | #endif |
20 | #ifdef HAVE_SYS_NDIR_H |
21 | # include <sys/ndir.h> |
22 | #endif |
23 | |
704fafa3 |
24 | /* |
9c6e61f2 |
25 | * Wrappers around POSIX opendir, readdir and closedir, which |
26 | * permit me to replace them with different wrappers in special |
27 | * circumstances. |
704fafa3 |
28 | */ |
29 | |
9c6e61f2 |
30 | typedef DIR *dirhandle; |
704fafa3 |
31 | |
32 | int open_dir(const char *path, dirhandle *dh) |
33 | { |
9c6e61f2 |
34 | #if defined O_NOATIME && defined HAVE_FDOPENDIR |
35 | |
36 | /* |
37 | * On Linux, we have the O_NOATIME flag. This means we can |
38 | * read the contents of directories without affecting their |
39 | * atimes, which enables us to at least try to include them in |
40 | * the age display rather than exempting them. |
41 | * |
42 | * Unfortunately, opendir() doesn't let us open a directory |
43 | * with O_NOATIME. So instead, we have to open the directory |
44 | * with vanilla open(), and then use fdopendir() to translate |
45 | * the fd into a POSIX dir handle. |
46 | */ |
47 | int fd; |
48 | |
49 | fd = open(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_LARGEFILE | |
50 | O_NOATIME | O_DIRECTORY); |
51 | if (fd < 0) { |
704fafa3 |
52 | /* |
53 | * Opening a file with O_NOATIME is not unconditionally |
54 | * permitted by the Linux kernel. As far as I can tell, |
55 | * it's permitted only for files on which the user would |
56 | * have been able to call utime(2): in other words, files |
57 | * for which the user could have deliberately set the |
58 | * atime back to its original value after finishing with |
59 | * it. Hence, O_NOATIME has no security implications; it's |
60 | * simply a cleaner, faster and more race-condition-free |
61 | * alternative to stat(), a normal open(), and a utimes() |
62 | * when finished. |
63 | * |
64 | * The upshot of all of which, for these purposes, is that |
65 | * we must be prepared to try again without O_NOATIME if |
66 | * we receive EPERM. |
67 | */ |
68 | if (errno == EPERM) |
9c6e61f2 |
69 | fd = open(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | |
70 | O_LARGEFILE | O_DIRECTORY); |
71 | if (fd < 0) |
72 | return -1; |
73 | } |
74 | |
75 | *dh = fdopendir(fd); |
76 | #else |
77 | *dh = opendir(path); |
78 | #endif |
79 | |
80 | if (!*dh) |
81 | return -1; |
82 | return 0; |
83 | } |
84 | |
85 | const char *read_dir(dirhandle *dh) |
86 | { |
87 | struct dirent *de = readdir(*dh); |
88 | return de ? de->d_name : NULL; |
89 | } |
90 | |
91 | void close_dir(dirhandle *dh) |
92 | { |
93 | closedir(*dh); |
94 | } |
95 | |
96 | #else /* defined __linux__ && !defined HAVE_FDOPENDIR */ |
97 | |
98 | /* |
99 | * Earlier versions of glibc do not have fdopendir(). Therefore, |
100 | * if we are on Linux and still wish to make use of O_NOATIME, we |
101 | * have no option but to talk directly to the kernel system call |
102 | * interface which underlies the POSIX opendir/readdir machinery. |
103 | */ |
104 | |
105 | #define __KERNEL__ |
9c6e61f2 |
106 | #include <linux/types.h> |
107 | #include <linux/dirent.h> |
108 | #include <linux/unistd.h> |
109 | |
110 | _syscall3(int, getdents, uint, fd, struct dirent *, dirp, uint, count) |
111 | |
112 | typedef struct { |
113 | int fd; |
114 | struct dirent data[32]; |
115 | struct dirent *curr; |
116 | int pos, endpos; |
117 | } dirhandle; |
118 | |
119 | int open_dir(const char *path, dirhandle *dh) |
120 | { |
121 | /* |
122 | * As above, we try with O_NOATIME and then fall back to |
123 | * trying without it. |
124 | */ |
125 | dh->fd = open(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_LARGEFILE | |
126 | O_NOATIME | O_DIRECTORY); |
127 | if (dh->fd < 0) { |
128 | if (errno == EPERM) |
129 | dh->fd = open(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | |
130 | O_LARGEFILE | O_DIRECTORY); |
704fafa3 |
131 | if (dh->fd < 0) |
132 | return -1; |
133 | } |
134 | |
135 | dh->pos = dh->endpos = 0; |
136 | |
137 | return 0; |
138 | } |
139 | |
140 | const char *read_dir(dirhandle *dh) |
141 | { |
142 | const char *ret; |
143 | |
144 | if (dh->pos >= dh->endpos) { |
145 | dh->curr = dh->data; |
146 | dh->pos = 0; |
147 | dh->endpos = getdents(dh->fd, dh->data, sizeof(dh->data)); |
148 | if (dh->endpos <= 0) |
149 | return NULL; |
150 | } |
151 | |
152 | ret = dh->curr->d_name; |
153 | |
154 | dh->pos += dh->curr->d_reclen; |
155 | dh->curr = (struct dirent *)((char *)dh->data + dh->pos); |
156 | |
157 | return ret; |
158 | } |
159 | |
160 | void close_dir(dirhandle *dh) |
161 | { |
162 | close(dh->fd); |
163 | } |
164 | |
9c6e61f2 |
165 | #endif /* !defined __linux__ || defined HAVE_FDOPENDIR */ |
70322ae3 |
166 | |
167 | static int str_cmp(const void *av, const void *bv) |
168 | { |
169 | return strcmp(*(const char **)av, *(const char **)bv); |
170 | } |
171 | |
172 | static void du_recurse(char **path, size_t pathlen, size_t *pathsize, |
09fd7619 |
173 | gotdata_fn_t gotdata, err_fn_t err, void *gotdata_ctx) |
70322ae3 |
174 | { |
704fafa3 |
175 | const char *name; |
70322ae3 |
176 | dirhandle d; |
9c6e61f2 |
177 | STRUCT_STAT st; |
70322ae3 |
178 | char **names; |
179 | size_t i, nnames, namesize; |
180 | |
9c6e61f2 |
181 | if (LSTAT(*path, &st) < 0) { |
09fd7619 |
182 | err(gotdata_ctx, "%s: lstat: %s\n", *path, strerror(errno)); |
70322ae3 |
183 | return; |
184 | } |
185 | |
186 | if (!gotdata(gotdata_ctx, *path, &st)) |
187 | return; |
188 | |
189 | if (!S_ISDIR(st.st_mode)) |
190 | return; |
191 | |
192 | names = NULL; |
193 | nnames = namesize = 0; |
194 | |
704fafa3 |
195 | if (open_dir(*path, &d) < 0) { |
09fd7619 |
196 | err(gotdata_ctx, "%s: opendir: %s\n", *path, strerror(errno)); |
70322ae3 |
197 | return; |
198 | } |
704fafa3 |
199 | while ((name = read_dir(&d)) != NULL) { |
200 | if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2]))) { |
201 | /* do nothing - we skip "." and ".." */ |
202 | } else { |
203 | if (nnames >= namesize) { |
204 | namesize = nnames * 3 / 2 + 64; |
205 | names = sresize(names, namesize, char *); |
70322ae3 |
206 | } |
704fafa3 |
207 | names[nnames++] = dupstr(name); |
208 | } |
70322ae3 |
209 | } |
704fafa3 |
210 | close_dir(&d); |
70322ae3 |
211 | |
212 | if (nnames == 0) |
213 | return; |
214 | |
215 | qsort(names, nnames, sizeof(*names), str_cmp); |
216 | |
217 | for (i = 0; i < nnames; i++) { |
218 | size_t newpathlen = pathlen + 1 + strlen(names[i]); |
219 | if (*pathsize <= newpathlen) { |
220 | *pathsize = newpathlen * 3 / 2 + 256; |
221 | *path = sresize(*path, *pathsize, char); |
222 | } |
256c29a2 |
223 | /* |
224 | * Avoid duplicating a slash if we got a trailing one to |
225 | * begin with (i.e. if we're starting the scan in '/' itself). |
226 | */ |
227 | if (pathlen > 0 && (*path)[pathlen-1] == '/') { |
228 | strcpy(*path + pathlen, names[i]); |
229 | newpathlen--; |
230 | } else { |
231 | sprintf(*path + pathlen, "/%s", names[i]); |
232 | } |
70322ae3 |
233 | |
09fd7619 |
234 | du_recurse(path, newpathlen, pathsize, gotdata, err, gotdata_ctx); |
70322ae3 |
235 | |
236 | sfree(names[i]); |
237 | } |
238 | sfree(names); |
239 | } |
240 | |
09fd7619 |
241 | void du(const char *inpath, gotdata_fn_t gotdata, err_fn_t err, |
242 | void *gotdata_ctx) |
70322ae3 |
243 | { |
244 | char *path; |
245 | size_t pathlen, pathsize; |
246 | |
247 | pathlen = strlen(inpath); |
248 | pathsize = pathlen + 256; |
249 | path = snewn(pathsize, char); |
250 | strcpy(path, inpath); |
251 | |
09fd7619 |
252 | du_recurse(&path, pathlen, &pathsize, gotdata, err, gotdata_ctx); |
70322ae3 |
253 | } |