VERSION: Disable git snapshots for the 4.0.24 release.
[Samba.git] / source3 / modules / vfs_dirsort.c
blob1d46e4324bbc028318c51fb007f10a36c07f5465
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 unsigned int i = 0;
69 unsigned int total_count = 0;
71 data->number_of_entries = 0;
73 if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
74 return false;
77 while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
78 != NULL) {
79 total_count++;
82 if (total_count == 0) {
83 return false;
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);
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;
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;
105 data->directory_list[i] = *dp;
108 data->number_of_entries = i;
110 /* Sort the directory entries by name */
111 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
112 return true;
115 static DIR *dirsort_opendir(vfs_handle_struct *handle,
116 const char *fname, const char *mask,
117 uint32 attr)
119 NTSTATUS status;
120 struct dirsort_privates *list_head = NULL;
121 struct dirsort_privates *data = NULL;
123 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
124 /* Find the list head of all open directories. */
125 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
126 return NULL);
129 /* set up our private data about this directory */
130 data = talloc_zero(handle->conn, struct dirsort_privates);
131 if (!data) {
132 return NULL;
135 status = create_synthetic_smb_fname(data,
136 fname,
137 NULL,
138 NULL,
139 &data->smb_fname);
140 if (!NT_STATUS_IS_OK(status)) {
141 TALLOC_FREE(data);
142 return NULL;
145 /* Open the underlying directory and count the number of entries */
146 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
147 attr);
149 if (data->source_directory == NULL) {
150 TALLOC_FREE(data);
151 return NULL;
154 if (!open_and_sort_dir(handle, data)) {
155 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
156 TALLOC_FREE(data);
157 return NULL;
160 /* Add to the private list of all open directories. */
161 DLIST_ADD(list_head, data);
162 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
163 struct dirsort_privates, return NULL);
165 return data->source_directory;
168 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
169 files_struct *fsp,
170 const char *mask,
171 uint32 attr)
173 struct dirsort_privates *list_head = NULL;
174 struct dirsort_privates *data = NULL;
176 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
177 /* Find the list head of all open directories. */
178 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
179 return NULL);
182 /* set up our private data about this directory */
183 data = talloc_zero(handle->conn, struct dirsort_privates);
184 if (!data) {
185 return NULL;
188 data->fsp = fsp;
190 /* Open the underlying directory and count the number of entries */
191 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
192 attr);
194 if (data->source_directory == NULL) {
195 TALLOC_FREE(data);
196 return NULL;
199 if (!open_and_sort_dir(handle, data)) {
200 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
201 TALLOC_FREE(data);
202 /* fd is now closed. */
203 fsp->fh->fd = -1;
204 return NULL;
207 /* Add to the private list of all open directories. */
208 DLIST_ADD(list_head, data);
209 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
210 struct dirsort_privates, return NULL);
212 return data->source_directory;
215 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
216 DIR *dirp,
217 SMB_STRUCT_STAT *sbuf)
219 struct dirsort_privates *data = NULL;
220 struct timespec current_mtime;
222 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
223 return NULL);
225 while(data && (data->source_directory != dirp)) {
226 data = data->next;
228 if (data == NULL) {
229 return NULL;
232 if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
233 return NULL;
236 /* throw away cache and re-read the directory if we've changed */
237 if (timespec_compare(&current_mtime, &data->mtime)) {
238 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
239 open_and_sort_dir(handle, data);
242 if (data->pos >= data->number_of_entries) {
243 return NULL;
246 return &data->directory_list[data->pos++];
249 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
250 long offset)
252 struct timespec current_mtime;
253 struct dirsort_privates *data = NULL;
255 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
257 /* Find the entry holding dirp. */
258 while(data && (data->source_directory != dirp)) {
259 data = data->next;
261 if (data == NULL) {
262 return;
264 if (offset >= data->number_of_entries) {
265 return;
267 data->pos = offset;
269 if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
270 return;
273 if (timespec_compare(&current_mtime, &data->mtime)) {
274 /* Directory changed. We must re-read the
275 cache and search for the name that was
276 previously stored at the offset being
277 requested, otherwise after the re-sort
278 we will point to the wrong entry. The
279 OS/2 incremental delete code relies on
280 this. */
281 unsigned int i;
282 char *wanted_name = talloc_strdup(handle->conn,
283 data->directory_list[offset].d_name);
284 if (wanted_name == NULL) {
285 return;
287 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
288 open_and_sort_dir(handle, data);
289 /* Now search for where we were. */
290 data->pos = 0;
291 for (i = 0; i < data->number_of_entries; i++) {
292 if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
293 data->pos = i;
294 break;
297 TALLOC_FREE(wanted_name);
301 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
303 struct dirsort_privates *data = NULL;
304 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
305 return -1);
307 /* Find the entry holding dirp. */
308 while(data && (data->source_directory != dirp)) {
309 data = data->next;
311 if (data == NULL) {
312 return -1;
314 return data->pos;
317 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
319 struct dirsort_privates *data = NULL;
320 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
322 /* Find the entry holding dirp. */
323 while(data && (data->source_directory != dirp)) {
324 data = data->next;
326 if (data == NULL) {
327 return;
329 data->pos = 0;
332 static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
334 struct dirsort_privates *list_head = NULL;
335 struct dirsort_privates *data = NULL;
336 int ret;
338 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
339 /* Find the entry holding dirp. */
340 for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
343 if (data == NULL) {
344 return -1;
346 /* Remove from the list and re-store the list head. */
347 DLIST_REMOVE(list_head, data);
348 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
349 struct dirsort_privates, return -1);
351 ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
352 TALLOC_FREE(data);
353 return ret;
356 static struct vfs_fn_pointers vfs_dirsort_fns = {
357 .opendir_fn = dirsort_opendir,
358 .fdopendir_fn = dirsort_fdopendir,
359 .readdir_fn = dirsort_readdir,
360 .seekdir_fn = dirsort_seekdir,
361 .telldir_fn = dirsort_telldir,
362 .rewind_dir_fn = dirsort_rewinddir,
363 .closedir_fn = dirsort_closedir,
366 NTSTATUS vfs_dirsort_init(void)
368 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
369 &vfs_dirsort_fns);