smbd: reject FILE_ATTRIBUTE_TEMPORARY on directories
[Samba.git] / source3 / modules / vfs_dirsort.c
blobd9cfcadacf84e0b89d6b83331725e93f29970fa6
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;
47 NTSTATUS status;
49 if (data->fsp) {
50 status = vfs_stat_fsp(data->fsp);
51 if (!NT_STATUS_IS_OK(status)) {
52 return false;
54 mtime = data->fsp->fsp_name->st.st_ex_mtime;
55 } else {
56 ret = SMB_VFS_STAT(handle->conn, data->smb_fname);
57 if (ret == -1) {
58 return false;
60 mtime = data->smb_fname->st.st_ex_mtime;
63 *ret_mtime = mtime;
65 return true;
68 static bool open_and_sort_dir(vfs_handle_struct *handle,
69 struct dirsort_privates *data)
71 uint32_t total_count = 0;
72 /* This should be enough for most use cases */
73 uint32_t dirent_allocated = 64;
74 struct dirent *dp;
76 data->number_of_entries = 0;
78 if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
79 return false;
82 dp = SMB_VFS_NEXT_READDIR(handle,
83 data->fsp,
84 data->source_directory,
85 NULL);
86 if (dp == NULL) {
87 return false;
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 dirent_allocated);
95 if (data->directory_list == NULL) {
96 return false;
99 do {
100 if (total_count >= dirent_allocated) {
101 struct dirent *dlist;
104 * Be memory friendly.
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.
110 * For 200k files this means 50 memory reallocations.
112 dirent_allocated += 4096;
114 dlist = talloc_realloc(data,
115 data->directory_list,
116 struct dirent,
117 dirent_allocated);
118 if (dlist == NULL) {
119 break;
121 data->directory_list = dlist;
123 data->directory_list[total_count] = *dp;
125 total_count++;
126 dp = SMB_VFS_NEXT_READDIR(handle,
127 data->fsp,
128 data->source_directory,
129 NULL);
130 } while (dp != NULL);
132 data->number_of_entries = total_count;
134 /* Sort the directory entries by name */
135 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
136 return true;
139 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
140 files_struct *fsp,
141 const char *mask,
142 uint32_t attr)
144 struct dirsort_privates *list_head = NULL;
145 struct dirsort_privates *data = NULL;
147 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
148 /* Find the list head of all open directories. */
149 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
150 return NULL);
153 /* set up our private data about this directory */
154 data = talloc_zero(handle->conn, struct dirsort_privates);
155 if (!data) {
156 return NULL;
159 data->fsp = fsp;
161 /* Open the underlying directory and count the number of entries */
162 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
163 attr);
165 if (data->source_directory == NULL) {
166 TALLOC_FREE(data);
167 return NULL;
170 if (!open_and_sort_dir(handle, data)) {
171 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
172 TALLOC_FREE(data);
173 /* fd is now closed. */
174 fsp_set_fd(fsp, -1);
175 return NULL;
178 /* Add to the private list of all open directories. */
179 DLIST_ADD(list_head, data);
180 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
181 struct dirsort_privates, return NULL);
183 return data->source_directory;
186 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
187 struct files_struct *dirfsp,
188 DIR *dirp,
189 SMB_STRUCT_STAT *sbuf)
191 struct dirsort_privates *data = NULL;
192 struct timespec current_mtime;
194 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
195 return NULL);
197 while(data && (data->source_directory != dirp)) {
198 data = data->next;
200 if (data == NULL) {
201 return NULL;
204 if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
205 return NULL;
208 /* throw away cache and re-read the directory if we've changed */
209 if (timespec_compare(&current_mtime, &data->mtime)) {
210 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
211 open_and_sort_dir(handle, data);
214 if (data->pos >= data->number_of_entries) {
215 return NULL;
218 return &data->directory_list[data->pos++];
221 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
222 long offset)
224 struct timespec current_mtime;
225 struct dirsort_privates *data = NULL;
227 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
229 /* Find the entry holding dirp. */
230 while(data && (data->source_directory != dirp)) {
231 data = data->next;
233 if (data == NULL) {
234 return;
236 if (offset >= data->number_of_entries) {
237 return;
239 data->pos = offset;
241 if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
242 return;
245 if (timespec_compare(&current_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 char *wanted_name = talloc_strdup(handle->conn,
255 data->directory_list[offset].d_name);
256 if (wanted_name == NULL) {
257 return;
259 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
260 open_and_sort_dir(handle, data);
261 /* Now search for where we were. */
262 data->pos = 0;
263 for (i = 0; i < data->number_of_entries; i++) {
264 if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
265 data->pos = i;
266 break;
269 TALLOC_FREE(wanted_name);
273 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
275 struct dirsort_privates *data = NULL;
276 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
277 return -1);
279 /* Find the entry holding dirp. */
280 while(data && (data->source_directory != dirp)) {
281 data = data->next;
283 if (data == NULL) {
284 return -1;
286 return data->pos;
289 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
291 struct dirsort_privates *data = NULL;
292 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
294 /* Find the entry holding dirp. */
295 while(data && (data->source_directory != dirp)) {
296 data = data->next;
298 if (data == NULL) {
299 return;
301 data->pos = 0;
304 static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
306 struct dirsort_privates *list_head = NULL;
307 struct dirsort_privates *data = NULL;
308 int ret;
310 SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
311 /* Find the entry holding dirp. */
312 for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
315 if (data == NULL) {
316 return -1;
318 /* Remove from the list and re-store the list head. */
319 DLIST_REMOVE(list_head, data);
320 SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
321 struct dirsort_privates, return -1);
323 ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
324 TALLOC_FREE(data);
325 return ret;
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,
337 static_decl_vfs;
338 NTSTATUS vfs_dirsort_init(TALLOC_CTX *ctx)
340 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
341 &vfs_dirsort_fns);