Line data Source code
1 : /*
2 : * VFS module to provide a sorted directory list.
3 : *
4 : * Copyright (C) Andy Kelk (andy@mopoke.co.uk), 2009
5 : *
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "smbd/smbd.h"
23 : #include "system/filesys.h"
24 :
25 0 : static int compare_dirent (const struct dirent *da, const struct dirent *db)
26 : {
27 0 : return strcasecmp_m(da->d_name, db->d_name);
28 : }
29 :
30 : struct dirsort_privates {
31 : struct dirsort_privates *prev, *next;
32 : long pos;
33 : struct dirent *directory_list;
34 : unsigned int number_of_entries;
35 : struct timespec mtime;
36 : DIR *source_directory;
37 : files_struct *fsp; /* If open via FDOPENDIR. */
38 : struct smb_filename *smb_fname; /* If open via OPENDIR */
39 : };
40 :
41 0 : static bool get_sorted_dir_mtime(vfs_handle_struct *handle,
42 : struct dirsort_privates *data,
43 : struct timespec *ret_mtime)
44 : {
45 : int ret;
46 : struct timespec mtime;
47 : NTSTATUS status;
48 :
49 0 : if (data->fsp) {
50 0 : status = vfs_stat_fsp(data->fsp);
51 0 : if (!NT_STATUS_IS_OK(status)) {
52 0 : return false;
53 : }
54 0 : mtime = data->fsp->fsp_name->st.st_ex_mtime;
55 : } else {
56 0 : ret = SMB_VFS_STAT(handle->conn, data->smb_fname);
57 0 : if (ret == -1) {
58 0 : return false;
59 : }
60 0 : mtime = data->smb_fname->st.st_ex_mtime;
61 : }
62 :
63 0 : *ret_mtime = mtime;
64 :
65 0 : return true;
66 : }
67 :
68 0 : static bool open_and_sort_dir(vfs_handle_struct *handle,
69 : struct dirsort_privates *data)
70 : {
71 0 : uint32_t total_count = 0;
72 : /* This should be enough for most use cases */
73 0 : uint32_t dirent_allocated = 64;
74 : struct dirent *dp;
75 :
76 0 : data->number_of_entries = 0;
77 :
78 0 : if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
79 0 : return false;
80 : }
81 :
82 0 : dp = SMB_VFS_NEXT_READDIR(handle,
83 : data->fsp,
84 : data->source_directory,
85 : NULL);
86 0 : if (dp == NULL) {
87 0 : return false;
88 : }
89 :
90 : /* Set up an array and read the directory entries into it */
91 0 : TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
92 0 : data->directory_list = talloc_zero_array(data,
93 : struct dirent,
94 : dirent_allocated);
95 0 : if (data->directory_list == NULL) {
96 0 : return false;
97 : }
98 :
99 : do {
100 0 : if (total_count >= dirent_allocated) {
101 : struct dirent *dlist;
102 :
103 : /*
104 : * Be memory friendly.
105 : *
106 : * We should not double the amount of memory. With a lot
107 : * of files we reach easily 50MB, and doubling will
108 : * get much bigger just for a few files more.
109 : *
110 : * For 200k files this means 50 memory reallocations.
111 : */
112 0 : dirent_allocated += 4096;
113 :
114 0 : dlist = talloc_realloc(data,
115 : data->directory_list,
116 : struct dirent,
117 : dirent_allocated);
118 0 : if (dlist == NULL) {
119 0 : break;
120 : }
121 0 : data->directory_list = dlist;
122 : }
123 0 : data->directory_list[total_count] = *dp;
124 :
125 0 : total_count++;
126 0 : dp = SMB_VFS_NEXT_READDIR(handle,
127 : data->fsp,
128 : data->source_directory,
129 : NULL);
130 0 : } while (dp != NULL);
131 :
132 0 : data->number_of_entries = total_count;
133 :
134 : /* Sort the directory entries by name */
135 0 : TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
136 0 : return true;
137 : }
138 :
139 0 : static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
140 : files_struct *fsp,
141 : const char *mask,
142 : uint32_t attr)
143 : {
144 0 : struct dirsort_privates *list_head = NULL;
145 0 : struct dirsort_privates *data = NULL;
146 :
147 0 : if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
148 : /* Find the list head of all open directories. */
149 0 : SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
150 : return NULL);
151 : }
152 :
153 : /* set up our private data about this directory */
154 0 : data = talloc_zero(handle->conn, struct dirsort_privates);
155 0 : if (!data) {
156 0 : return NULL;
157 : }
158 :
159 0 : data->fsp = fsp;
160 :
161 : /* Open the underlying directory and count the number of entries */
162 0 : data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
163 : attr);
164 :
165 0 : if (data->source_directory == NULL) {
166 0 : TALLOC_FREE(data);
167 0 : return NULL;
168 : }
169 :
170 0 : if (!open_and_sort_dir(handle, data)) {
171 0 : SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
172 0 : TALLOC_FREE(data);
173 : /* fd is now closed. */
174 0 : fsp_set_fd(fsp, -1);
175 0 : return NULL;
176 : }
177 :
178 : /* Add to the private list of all open directories. */
179 0 : DLIST_ADD(list_head, data);
180 0 : SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
181 : struct dirsort_privates, return NULL);
182 :
183 0 : return data->source_directory;
184 : }
185 :
186 0 : static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
187 : struct files_struct *dirfsp,
188 : DIR *dirp,
189 : SMB_STRUCT_STAT *sbuf)
190 : {
191 0 : struct dirsort_privates *data = NULL;
192 : struct timespec current_mtime;
193 :
194 0 : SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
195 : return NULL);
196 :
197 0 : while(data && (data->source_directory != dirp)) {
198 0 : data = data->next;
199 : }
200 0 : if (data == NULL) {
201 0 : return NULL;
202 : }
203 :
204 0 : if (get_sorted_dir_mtime(handle, data, ¤t_mtime) == false) {
205 0 : return NULL;
206 : }
207 :
208 : /* throw away cache and re-read the directory if we've changed */
209 0 : if (timespec_compare(¤t_mtime, &data->mtime)) {
210 0 : SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
211 0 : open_and_sort_dir(handle, data);
212 : }
213 :
214 0 : if (data->pos >= data->number_of_entries) {
215 0 : return NULL;
216 : }
217 :
218 0 : return &data->directory_list[data->pos++];
219 : }
220 :
221 0 : static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
222 : long offset)
223 : {
224 : struct timespec current_mtime;
225 0 : struct dirsort_privates *data = NULL;
226 :
227 0 : SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
228 :
229 : /* Find the entry holding dirp. */
230 0 : while(data && (data->source_directory != dirp)) {
231 0 : data = data->next;
232 : }
233 0 : if (data == NULL) {
234 0 : return;
235 : }
236 0 : if (offset >= data->number_of_entries) {
237 0 : return;
238 : }
239 0 : data->pos = offset;
240 :
241 0 : if (get_sorted_dir_mtime(handle, data, ¤t_mtime) == false) {
242 0 : return;
243 : }
244 :
245 0 : if (timespec_compare(¤t_mtime, &data->mtime)) {
246 : /* Directory changed. We must re-read the
247 : cache and search for the name that was
248 : previously stored at the offset being
249 : requested, otherwise after the re-sort
250 : we will point to the wrong entry. The
251 : OS/2 incremental delete code relies on
252 : this. */
253 : unsigned int i;
254 0 : char *wanted_name = talloc_strdup(handle->conn,
255 0 : data->directory_list[offset].d_name);
256 0 : if (wanted_name == NULL) {
257 0 : return;
258 : }
259 0 : SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
260 0 : open_and_sort_dir(handle, data);
261 : /* Now search for where we were. */
262 0 : data->pos = 0;
263 0 : for (i = 0; i < data->number_of_entries; i++) {
264 0 : if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
265 0 : data->pos = i;
266 0 : break;
267 : }
268 : }
269 0 : TALLOC_FREE(wanted_name);
270 : }
271 : }
272 :
273 0 : static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
274 : {
275 0 : struct dirsort_privates *data = NULL;
276 0 : SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
277 : return -1);
278 :
279 : /* Find the entry holding dirp. */
280 0 : while(data && (data->source_directory != dirp)) {
281 0 : data = data->next;
282 : }
283 0 : if (data == NULL) {
284 0 : return -1;
285 : }
286 0 : return data->pos;
287 : }
288 :
289 0 : static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
290 : {
291 0 : struct dirsort_privates *data = NULL;
292 0 : SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
293 :
294 : /* Find the entry holding dirp. */
295 0 : while(data && (data->source_directory != dirp)) {
296 0 : data = data->next;
297 : }
298 0 : if (data == NULL) {
299 0 : return;
300 : }
301 0 : data->pos = 0;
302 : }
303 :
304 0 : static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
305 : {
306 0 : struct dirsort_privates *list_head = NULL;
307 0 : struct dirsort_privates *data = NULL;
308 : int ret;
309 :
310 0 : SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
311 : /* Find the entry holding dirp. */
312 0 : for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
313 : ;
314 : }
315 0 : if (data == NULL) {
316 0 : return -1;
317 : }
318 : /* Remove from the list and re-store the list head. */
319 0 : DLIST_REMOVE(list_head, data);
320 0 : SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
321 : struct dirsort_privates, return -1);
322 :
323 0 : ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
324 0 : TALLOC_FREE(data);
325 0 : return ret;
326 : }
327 :
328 : static struct vfs_fn_pointers vfs_dirsort_fns = {
329 : .fdopendir_fn = dirsort_fdopendir,
330 : .readdir_fn = dirsort_readdir,
331 : .seekdir_fn = dirsort_seekdir,
332 : .telldir_fn = dirsort_telldir,
333 : .rewind_dir_fn = dirsort_rewinddir,
334 : .closedir_fn = dirsort_closedir,
335 : };
336 :
337 : static_decl_vfs;
338 26 : NTSTATUS vfs_dirsort_init(TALLOC_CTX *ctx)
339 : {
340 26 : return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
341 : &vfs_dirsort_fns);
342 : }
|