vfs_fruit: pass handle to ad_fset()
[metze/samba-autobuild/.git] / source3 / modules / vfs_dirsort.c
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 static int compare_dirent (const struct dirent *da, const struct dirent *db)
26 {
27         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 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
48         if (data->fsp) {
49                 ret = fsp_stat(data->fsp);
50                 mtime = data->fsp->fsp_name->st.st_ex_mtime;
51         } else {
52                 ret = SMB_VFS_STAT(handle->conn, data->smb_fname);
53                 mtime = data->smb_fname->st.st_ex_mtime;
54         }
55
56         if (ret == -1) {
57                 return false;
58         }
59
60         *ret_mtime = mtime;
61
62         return true;
63 }
64
65 static bool open_and_sort_dir(vfs_handle_struct *handle,
66                                 struct dirsort_privates *data)
67 {
68         uint32_t total_count = 0;
69         /* This should be enough for most use cases */
70         uint32_t dirent_allocated = 64;
71         struct dirent *dp;
72
73         data->number_of_entries = 0;
74
75         if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
76                 return false;
77         }
78
79         dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL);
80         if (dp == NULL) {
81                 return false;
82         }
83
84         /* Set up an array and read the directory entries into it */
85         TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
86         data->directory_list = talloc_zero_array(data,
87                                                  struct dirent,
88                                                  dirent_allocated);
89         if (data->directory_list == NULL) {
90                 return false;
91         }
92
93         do {
94                 if (total_count >= dirent_allocated) {
95                         struct dirent *dlist;
96
97                         /*
98                          * Be memory friendly.
99                          *
100                          * We should not double the amount of memory. With a lot
101                          * of files we reach easily 50MB, and doubling will
102                          * get much bigger just for a few files more.
103                          *
104                          * For 200k files this means 50 memory reallocations.
105                          */
106                         dirent_allocated += 4096;
107
108                         dlist = talloc_realloc(data,
109                                                data->directory_list,
110                                                struct dirent,
111                                                dirent_allocated);
112                         if (dlist == NULL) {
113                                 break;
114                         }
115                         data->directory_list = dlist;
116                 }
117                 data->directory_list[total_count] = *dp;
118
119                 total_count++;
120                 dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL);
121         } while (dp != NULL);
122
123         data->number_of_entries = total_count;
124
125         /* Sort the directory entries by name */
126         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
127         return true;
128 }
129
130 static DIR *dirsort_opendir(vfs_handle_struct *handle,
131                                 const struct smb_filename *smb_fname,
132                                 const char *mask,
133                                 uint32_t attr)
134 {
135         struct dirsort_privates *list_head = NULL;
136         struct dirsort_privates *data = NULL;
137
138         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
139                 /* Find the list head of all open directories. */
140                 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
141                                 return NULL);
142         }
143
144         /* set up our private data about this directory */
145         data = talloc_zero(handle->conn, struct dirsort_privates);
146         if (!data) {
147                 return NULL;
148         }
149
150         data->smb_fname = cp_smb_filename(data, smb_fname);
151         if (data->smb_fname == NULL) {
152                 TALLOC_FREE(data);
153                 return NULL;
154         }
155
156         if (ISDOT(data->smb_fname->base_name)) {
157                 struct smb_filename *cwd_fname = vfs_GetWd(data, handle->conn);
158                 if (cwd_fname == NULL) {
159                         TALLOC_FREE(data);
160                         return NULL;
161                 }
162                 TALLOC_FREE(data->smb_fname->base_name);
163                 data->smb_fname->base_name = talloc_move(data->smb_fname,
164                                                 &cwd_fname->base_name);
165                 TALLOC_FREE(cwd_fname);
166         }
167
168         /* Open the underlying directory and count the number of entries */
169         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask,
170                                                       attr);
171
172         if (data->source_directory == NULL) {
173                 TALLOC_FREE(data);
174                 return NULL;
175         }
176
177         if (!open_and_sort_dir(handle, data)) {
178                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
179                 TALLOC_FREE(data);
180                 return NULL;
181         }
182
183         /* Add to the private list of all open directories. */
184         DLIST_ADD(list_head, data);
185         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
186                                 struct dirsort_privates, return NULL);
187
188         return data->source_directory;
189 }
190
191 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
192                                         files_struct *fsp,
193                                         const char *mask,
194                                         uint32_t attr)
195 {
196         struct dirsort_privates *list_head = NULL;
197         struct dirsort_privates *data = NULL;
198
199         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
200                 /* Find the list head of all open directories. */
201                 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
202                                 return NULL);
203         }
204
205         /* set up our private data about this directory */
206         data = talloc_zero(handle->conn, struct dirsort_privates);
207         if (!data) {
208                 return NULL;
209         }
210
211         data->fsp = fsp;
212
213         /* Open the underlying directory and count the number of entries */
214         data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
215                                                       attr);
216
217         if (data->source_directory == NULL) {
218                 TALLOC_FREE(data);
219                 return NULL;
220         }
221
222         if (!open_and_sort_dir(handle, data)) {
223                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
224                 TALLOC_FREE(data);
225                 /* fd is now closed. */
226                 fsp->fh->fd = -1;
227                 return NULL;
228         }
229
230         /* Add to the private list of all open directories. */
231         DLIST_ADD(list_head, data);
232         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
233                                 struct dirsort_privates, return NULL);
234
235         return data->source_directory;
236 }
237
238 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
239                                           DIR *dirp,
240                                           SMB_STRUCT_STAT *sbuf)
241 {
242         struct dirsort_privates *data = NULL;
243         struct timespec current_mtime;
244
245         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
246                                 return NULL);
247
248         while(data && (data->source_directory != dirp)) {
249                 data = data->next;
250         }
251         if (data == NULL) {
252                 return NULL;
253         }
254
255         if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
256                 return NULL;
257         }
258
259         /* throw away cache and re-read the directory if we've changed */
260         if (timespec_compare(&current_mtime, &data->mtime)) {
261                 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
262                 open_and_sort_dir(handle, data);
263         }
264
265         if (data->pos >= data->number_of_entries) {
266                 return NULL;
267         }
268
269         return &data->directory_list[data->pos++];
270 }
271
272 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
273                             long offset)
274 {
275         struct timespec current_mtime;
276         struct dirsort_privates *data = NULL;
277
278         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
279
280         /* Find the entry holding dirp. */
281         while(data && (data->source_directory != dirp)) {
282                 data = data->next;
283         }
284         if (data == NULL) {
285                 return;
286         }
287         if (offset >= data->number_of_entries) {
288                 return;
289         }
290         data->pos = offset;
291
292         if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
293                 return;
294         }
295
296         if (timespec_compare(&current_mtime, &data->mtime)) {
297                 /* Directory changed. We must re-read the
298                    cache and search for the name that was
299                    previously stored at the offset being
300                    requested, otherwise after the re-sort
301                    we will point to the wrong entry. The
302                    OS/2 incremental delete code relies on
303                    this. */
304                 unsigned int i;
305                 char *wanted_name = talloc_strdup(handle->conn,
306                                         data->directory_list[offset].d_name);
307                 if (wanted_name == NULL) {
308                         return;
309                 }
310                 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
311                 open_and_sort_dir(handle, data);
312                 /* Now search for where we were. */
313                 data->pos = 0;
314                 for (i = 0; i < data->number_of_entries; i++) {
315                         if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
316                                 data->pos = i;
317                                 break;
318                         }
319                 }
320                 TALLOC_FREE(wanted_name);
321         }
322 }
323
324 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
325 {
326         struct dirsort_privates *data = NULL;
327         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
328                                 return -1);
329
330         /* Find the entry holding dirp. */
331         while(data && (data->source_directory != dirp)) {
332                 data = data->next;
333         }
334         if (data == NULL) {
335                 return -1;
336         }
337         return data->pos;
338 }
339
340 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
341 {
342         struct dirsort_privates *data = NULL;
343         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
344
345         /* Find the entry holding dirp. */
346         while(data && (data->source_directory != dirp)) {
347                 data = data->next;
348         }
349         if (data == NULL) {
350                 return;
351         }
352         data->pos = 0;
353 }
354
355 static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
356 {
357         struct dirsort_privates *list_head = NULL;
358         struct dirsort_privates *data = NULL;
359         int ret;
360
361         SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
362         /* Find the entry holding dirp. */
363         for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
364                 ;
365         }
366         if (data == NULL) {
367                 return -1;
368         }
369         /* Remove from the list and re-store the list head. */
370         DLIST_REMOVE(list_head, data);
371         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
372                                 struct dirsort_privates, return -1);
373
374         ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
375         TALLOC_FREE(data);
376         return ret;
377 }
378
379 static struct vfs_fn_pointers vfs_dirsort_fns = {
380         .opendir_fn = dirsort_opendir,
381         .fdopendir_fn = dirsort_fdopendir,
382         .readdir_fn = dirsort_readdir,
383         .seekdir_fn = dirsort_seekdir,
384         .telldir_fn = dirsort_telldir,
385         .rewind_dir_fn = dirsort_rewinddir,
386         .closedir_fn = dirsort_closedir,
387 };
388
389 static_decl_vfs;
390 NTSTATUS vfs_dirsort_init(TALLOC_CTX *ctx)
391 {
392         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
393                                 &vfs_dirsort_fns);
394 }