Convert all uint8/16/32 to _t in all modules.
[kamenim/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         unsigned int i = 0;
69         unsigned int total_count = 0;
70
71         data->number_of_entries = 0;
72
73         if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
74                 return false;
75         }
76
77         while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
78                != NULL) {
79                 total_count++;
80         }
81
82         if (total_count == 0) {
83                 return false;
84         }
85
86         /* Open the underlying directory and count the number of entries
87            Skip back to the beginning as we'll read it again */
88         SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
89
90         /* Set up an array and read the directory entries into it */
91         TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
92         data->directory_list = talloc_zero_array(data,
93                                         struct dirent,
94                                         total_count);
95         if (!data->directory_list) {
96                 return false;
97         }
98         for (i = 0; i < total_count; i++) {
99                 struct dirent *dp = SMB_VFS_NEXT_READDIR(handle,
100                                                 data->source_directory,
101                                                 NULL);
102                 if (dp == NULL) {
103                         break;
104                 }
105                 data->directory_list[i] = *dp;
106         }
107
108         data->number_of_entries = i;
109
110         /* Sort the directory entries by name */
111         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
112         return true;
113 }
114
115 static DIR *dirsort_opendir(vfs_handle_struct *handle,
116                                        const char *fname, const char *mask,
117                                        uint32_t attr)
118 {
119         struct dirsort_privates *list_head = NULL;
120         struct dirsort_privates *data = NULL;
121
122         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
123                 /* Find the list head of all open directories. */
124                 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
125                                 return NULL);
126         }
127
128         /* set up our private data about this directory */
129         data = talloc_zero(handle->conn, struct dirsort_privates);
130         if (!data) {
131                 return NULL;
132         }
133
134         data->smb_fname = synthetic_smb_fname(data, fname, NULL, NULL);
135         if (data->smb_fname == NULL) {
136                 TALLOC_FREE(data);
137                 return NULL;
138         }
139
140         /* Open the underlying directory and count the number of entries */
141         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
142                                                       attr);
143
144         if (data->source_directory == NULL) {
145                 TALLOC_FREE(data);
146                 return NULL;
147         }
148
149         if (!open_and_sort_dir(handle, data)) {
150                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
151                 TALLOC_FREE(data);
152                 return NULL;
153         }
154
155         /* Add to the private list of all open directories. */
156         DLIST_ADD(list_head, data);
157         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
158                                 struct dirsort_privates, return NULL);
159
160         return data->source_directory;
161 }
162
163 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
164                                         files_struct *fsp,
165                                         const char *mask,
166                                         uint32_t attr)
167 {
168         struct dirsort_privates *list_head = NULL;
169         struct dirsort_privates *data = NULL;
170
171         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
172                 /* Find the list head of all open directories. */
173                 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
174                                 return NULL);
175         }
176
177         /* set up our private data about this directory */
178         data = talloc_zero(handle->conn, struct dirsort_privates);
179         if (!data) {
180                 return NULL;
181         }
182
183         data->fsp = fsp;
184
185         /* Open the underlying directory and count the number of entries */
186         data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
187                                                       attr);
188
189         if (data->source_directory == NULL) {
190                 TALLOC_FREE(data);
191                 return NULL;
192         }
193
194         if (!open_and_sort_dir(handle, data)) {
195                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
196                 TALLOC_FREE(data);
197                 /* fd is now closed. */
198                 fsp->fh->fd = -1;
199                 return NULL;
200         }
201
202         /* Add to the private list of all open directories. */
203         DLIST_ADD(list_head, data);
204         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
205                                 struct dirsort_privates, return NULL);
206
207         return data->source_directory;
208 }
209
210 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
211                                           DIR *dirp,
212                                           SMB_STRUCT_STAT *sbuf)
213 {
214         struct dirsort_privates *data = NULL;
215         struct timespec current_mtime;
216
217         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
218                                 return NULL);
219
220         while(data && (data->source_directory != dirp)) {
221                 data = data->next;
222         }
223         if (data == NULL) {
224                 return NULL;
225         }
226
227         if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
228                 return NULL;
229         }
230
231         /* throw away cache and re-read the directory if we've changed */
232         if (timespec_compare(&current_mtime, &data->mtime)) {
233                 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
234                 open_and_sort_dir(handle, data);
235         }
236
237         if (data->pos >= data->number_of_entries) {
238                 return NULL;
239         }
240
241         return &data->directory_list[data->pos++];
242 }
243
244 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
245                             long offset)
246 {
247         struct timespec current_mtime;
248         struct dirsort_privates *data = NULL;
249
250         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
251
252         /* Find the entry holding dirp. */
253         while(data && (data->source_directory != dirp)) {
254                 data = data->next;
255         }
256         if (data == NULL) {
257                 return;
258         }
259         if (offset >= data->number_of_entries) {
260                 return;
261         }
262         data->pos = offset;
263
264         if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
265                 return;
266         }
267
268         if (timespec_compare(&current_mtime, &data->mtime)) {
269                 /* Directory changed. We must re-read the
270                    cache and search for the name that was
271                    previously stored at the offset being
272                    requested, otherwise after the re-sort
273                    we will point to the wrong entry. The
274                    OS/2 incremental delete code relies on
275                    this. */
276                 unsigned int i;
277                 char *wanted_name = talloc_strdup(handle->conn,
278                                         data->directory_list[offset].d_name);
279                 if (wanted_name == NULL) {
280                         return;
281                 }
282                 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
283                 open_and_sort_dir(handle, data);
284                 /* Now search for where we were. */
285                 data->pos = 0;
286                 for (i = 0; i < data->number_of_entries; i++) {
287                         if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
288                                 data->pos = i;
289                                 break;
290                         }
291                 }
292                 TALLOC_FREE(wanted_name);
293         }
294 }
295
296 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
297 {
298         struct dirsort_privates *data = NULL;
299         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
300                                 return -1);
301
302         /* Find the entry holding dirp. */
303         while(data && (data->source_directory != dirp)) {
304                 data = data->next;
305         }
306         if (data == NULL) {
307                 return -1;
308         }
309         return data->pos;
310 }
311
312 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
313 {
314         struct dirsort_privates *data = NULL;
315         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
316
317         /* Find the entry holding dirp. */
318         while(data && (data->source_directory != dirp)) {
319                 data = data->next;
320         }
321         if (data == NULL) {
322                 return;
323         }
324         data->pos = 0;
325 }
326
327 static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
328 {
329         struct dirsort_privates *list_head = NULL;
330         struct dirsort_privates *data = NULL;
331         int ret;
332
333         SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
334         /* Find the entry holding dirp. */
335         for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
336                 ;
337         }
338         if (data == NULL) {
339                 return -1;
340         }
341         /* Remove from the list and re-store the list head. */
342         DLIST_REMOVE(list_head, data);
343         SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
344                                 struct dirsort_privates, return -1);
345
346         ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
347         TALLOC_FREE(data);
348         return ret;
349 }
350
351 static struct vfs_fn_pointers vfs_dirsort_fns = {
352         .opendir_fn = dirsort_opendir,
353         .fdopendir_fn = dirsort_fdopendir,
354         .readdir_fn = dirsort_readdir,
355         .seekdir_fn = dirsort_seekdir,
356         .telldir_fn = dirsort_telldir,
357         .rewind_dir_fn = dirsort_rewinddir,
358         .closedir_fn = dirsort_closedir,
359 };
360
361 NTSTATUS vfs_dirsort_init(void)
362 {
363         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
364                                 &vfs_dirsort_fns);
365 }