pidl: Fix array range checks in python output
[Samba.git] / source3 / modules / vfs_dirsort.c
blobfc35186a4022933cf64544c74aadc125f4bfa29e
1 /*
2 * VFS module to provide a sorted directory list.
4 * Copyright (C) Andy Kelk (andy@mopoke.co.uk), 2009
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.
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.
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/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
25 static int compare_dirent (const struct dirent *da, const struct dirent *db)
27 return strcasecmp_m(da->d_name, db->d_name);
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 */
41 static bool get_sorted_dir_mtime(vfs_handle_struct *handle,
42 struct dirsort_privates *data,
43 struct timespec *ret_mtime)
45 int ret;
46 struct timespec mtime;
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;
56 if (ret == -1) {
57 return false;
60 *ret_mtime = mtime;
62 return true;
65 static bool open_and_sort_dir(vfs_handle_struct *handle,
66 struct dirsort_privates *data)
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;
73 data->number_of_entries = 0;
75 if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
76 return false;
79 dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL);
80 if (dp == NULL) {
81 return false;
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;
93 do {
94 if (total_count >= dirent_allocated) {
95 struct dirent *dlist;
98 * Be memory friendly.
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.
104 * For 200k files this means 50 memory reallocations.
106 dirent_allocated += 4096;
108 dlist = talloc_realloc(data,
109 data->directory_list,
110 struct dirent,
111 dirent_allocated);
112 if (dlist == NULL) {
113 break;
115 data->directory_list = dlist;
117 data->directory_list[total_count] = *dp;
119 total_count++;
120 dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL);
121 } while (dp != NULL);
123 data->number_of_entries = total_count;
125 /* Sort the directory entries by name */
126 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
127 return true;
130 static DIR *dirsort_opendir(vfs_handle_struct *handle,
131 const struct smb_filename *smb_fname,
132 const char *mask,
133 uint32_t attr)
135 struct dirsort_privates *list_head = NULL;
136 struct dirsort_privates *data = NULL;
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);
144 /* set up our private data about this directory */
145 data = talloc_zero(handle->conn, struct dirsort_privates);
146 if (!data) {
147 return NULL;
150 data->smb_fname = cp_smb_filename(data, smb_fname);
151 if (data->smb_fname == NULL) {
152 TALLOC_FREE(data);
153 return NULL;
156 if (ISDOT(data->smb_fname->base_name)) {
157 data->smb_fname->base_name = vfs_GetWd(data, handle->conn);
160 /* Open the underlying directory and count the number of entries */
161 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask,
162 attr);
164 if (data->source_directory == NULL) {
165 TALLOC_FREE(data);
166 return NULL;
169 if (!open_and_sort_dir(handle, data)) {
170 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
171 TALLOC_FREE(data);
172 return NULL;
175 /* Add to the private list of all open directories. */
176 DLIST_ADD(list_head, data);
177 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
178 struct dirsort_privates, return NULL);
180 return data->source_directory;
183 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
184 files_struct *fsp,
185 const char *mask,
186 uint32_t attr)
188 struct dirsort_privates *list_head = NULL;
189 struct dirsort_privates *data = NULL;
191 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
192 /* Find the list head of all open directories. */
193 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
194 return NULL);
197 /* set up our private data about this directory */
198 data = talloc_zero(handle->conn, struct dirsort_privates);
199 if (!data) {
200 return NULL;
203 data->fsp = fsp;
205 /* Open the underlying directory and count the number of entries */
206 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
207 attr);
209 if (data->source_directory == NULL) {
210 TALLOC_FREE(data);
211 return NULL;
214 if (!open_and_sort_dir(handle, data)) {
215 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
216 TALLOC_FREE(data);
217 /* fd is now closed. */
218 fsp->fh->fd = -1;
219 return NULL;
222 /* Add to the private list of all open directories. */
223 DLIST_ADD(list_head, data);
224 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
225 struct dirsort_privates, return NULL);
227 return data->source_directory;
230 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
231 DIR *dirp,
232 SMB_STRUCT_STAT *sbuf)
234 struct dirsort_privates *data = NULL;
235 struct timespec current_mtime;
237 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
238 return NULL);
240 while(data && (data->source_directory != dirp)) {
241 data = data->next;
243 if (data == NULL) {
244 return NULL;
247 if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
248 return NULL;
251 /* throw away cache and re-read the directory if we've changed */
252 if (timespec_compare(&current_mtime, &data->mtime)) {
253 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
254 open_and_sort_dir(handle, data);
257 if (data->pos >= data->number_of_entries) {
258 return NULL;
261 return &data->directory_list[data->pos++];
264 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
265 long offset)
267 struct timespec current_mtime;
268 struct dirsort_privates *data = NULL;
270 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
272 /* Find the entry holding dirp. */
273 while(data && (data->source_directory != dirp)) {
274 data = data->next;
276 if (data == NULL) {
277 return;
279 if (offset >= data->number_of_entries) {
280 return;
282 data->pos = offset;
284 if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
285 return;
288 if (timespec_compare(&current_mtime, &data->mtime)) {
289 /* Directory changed. We must re-read the
290 cache and search for the name that was
291 previously stored at the offset being
292 requested, otherwise after the re-sort
293 we will point to the wrong entry. The
294 OS/2 incremental delete code relies on
295 this. */
296 unsigned int i;
297 char *wanted_name = talloc_strdup(handle->conn,
298 data->directory_list[offset].d_name);
299 if (wanted_name == NULL) {
300 return;
302 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
303 open_and_sort_dir(handle, data);
304 /* Now search for where we were. */
305 data->pos = 0;
306 for (i = 0; i < data->number_of_entries; i++) {
307 if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
308 data->pos = i;
309 break;
312 TALLOC_FREE(wanted_name);
316 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
318 struct dirsort_privates *data = NULL;
319 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
320 return -1);
322 /* Find the entry holding dirp. */
323 while(data && (data->source_directory != dirp)) {
324 data = data->next;
326 if (data == NULL) {
327 return -1;
329 return data->pos;
332 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
334 struct dirsort_privates *data = NULL;
335 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
337 /* Find the entry holding dirp. */
338 while(data && (data->source_directory != dirp)) {
339 data = data->next;
341 if (data == NULL) {
342 return;
344 data->pos = 0;
347 static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
349 struct dirsort_privates *list_head = NULL;
350 struct dirsort_privates *data = NULL;
351 int ret;
353 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
354 /* Find the entry holding dirp. */
355 for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
358 if (data == NULL) {
359 return -1;
361 /* Remove from the list and re-store the list head. */
362 DLIST_REMOVE(list_head, data);
363 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
364 struct dirsort_privates, return -1);
366 ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
367 TALLOC_FREE(data);
368 return ret;
371 static struct vfs_fn_pointers vfs_dirsort_fns = {
372 .opendir_fn = dirsort_opendir,
373 .fdopendir_fn = dirsort_fdopendir,
374 .readdir_fn = dirsort_readdir,
375 .seekdir_fn = dirsort_seekdir,
376 .telldir_fn = dirsort_telldir,
377 .rewind_dir_fn = dirsort_rewinddir,
378 .closedir_fn = dirsort_closedir,
381 static_decl_vfs;
382 NTSTATUS vfs_dirsort_init(TALLOC_CTX *ctx)
384 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
385 &vfs_dirsort_fns);