Protect against early error in SMB_VFS_NEXT_READDIR.
[Samba.git] / source3 / modules / vfs_dirsort.c
blob6fe7c184146185a109d20eb23de7805b34cb0aa0
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 long pos;
32 struct dirent *directory_list;
33 long number_of_entries;
34 time_t mtime;
35 DIR *source_directory;
36 int fd;
39 static void free_dirsort_privates(void **datap) {
40 TALLOC_FREE(*datap);
43 static bool open_and_sort_dir (vfs_handle_struct *handle)
45 struct dirent *dp;
46 struct stat dir_stat;
47 long current_pos;
48 struct dirsort_privates *data = NULL;
50 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
51 return false);
53 data->number_of_entries = 0;
55 if (fstat(data->fd, &dir_stat) == 0) {
56 data->mtime = dir_stat.st_mtime;
59 while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
60 != NULL) {
61 data->number_of_entries++;
64 if (data->number_of_entries == 0) {
65 return false;
68 /* Open the underlying directory and count the number of entries
69 Skip back to the beginning as we'll read it again */
70 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
72 /* Set up an array and read the directory entries into it */
73 TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
74 data->directory_list = talloc_zero_array(data,
75 struct dirent,
76 data->number_of_entries);
77 if (!data->directory_list) {
78 return false;
80 current_pos = data->pos;
81 data->pos = 0;
82 while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
83 NULL)) != NULL) {
84 data->directory_list[data->pos++] = *dp;
87 /* Sort the directory entries by name */
88 data->pos = current_pos;
89 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
90 return true;
93 static DIR *dirsort_opendir(vfs_handle_struct *handle,
94 const char *fname, const char *mask,
95 uint32 attr)
97 struct dirsort_privates *data = NULL;
99 /* set up our private data about this directory */
100 data = talloc_zero(handle->conn, struct dirsort_privates);
101 if (!data) {
102 return NULL;
105 data->directory_list = NULL;
106 data->pos = 0;
108 /* Open the underlying directory and count the number of entries */
109 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
110 attr);
112 data->fd = dirfd(data->source_directory);
114 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
115 struct dirsort_privates, return NULL);
117 if (!open_and_sort_dir(handle)) {
118 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
119 return NULL;
122 return data->source_directory;
125 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
126 files_struct *fsp,
127 const char *mask,
128 uint32 attr)
130 struct dirsort_privates *data = NULL;
132 /* set up our private data about this directory */
133 data = talloc_zero(handle->conn, struct dirsort_privates);
134 if (!data) {
135 return NULL;
138 data->directory_list = NULL;
139 data->pos = 0;
141 /* Open the underlying directory and count the number of entries */
142 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
143 attr);
145 if (data->source_directory == NULL) {
146 TALLOC_FREE(data);
147 return NULL;
150 data->fd = dirfd(data->source_directory);
152 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
153 struct dirsort_privates, return NULL);
155 if (!open_and_sort_dir(handle)) {
156 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
157 /* fd is now closed. */
158 fsp->fh->fd = -1;
159 return NULL;
162 return data->source_directory;
165 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
166 DIR *dirp,
167 SMB_STRUCT_STAT *sbuf)
169 struct dirsort_privates *data = NULL;
170 time_t current_mtime;
171 struct stat dir_stat;
173 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
174 return NULL);
176 if (fstat(data->fd, &dir_stat) == -1) {
177 return NULL;
180 current_mtime = dir_stat.st_mtime;
182 /* throw away cache and re-read the directory if we've changed */
183 if (current_mtime > data->mtime) {
184 open_and_sort_dir(handle);
187 if (data->pos >= data->number_of_entries) {
188 return NULL;
191 return &data->directory_list[data->pos++];
194 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
195 long offset)
197 struct dirsort_privates *data = NULL;
198 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
200 data->pos = offset;
203 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
205 struct dirsort_privates *data = NULL;
206 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
207 return -1);
209 return data->pos;
212 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
214 struct dirsort_privates *data = NULL;
215 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
217 data->pos = 0;
220 static struct vfs_fn_pointers vfs_dirsort_fns = {
221 .opendir_fn = dirsort_opendir,
222 .fdopendir_fn = dirsort_fdopendir,
223 .readdir_fn = dirsort_readdir,
224 .seekdir_fn = dirsort_seekdir,
225 .telldir_fn = dirsort_telldir,
226 .rewind_dir_fn = dirsort_rewinddir,
229 NTSTATUS vfs_dirsort_init(void)
231 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
232 &vfs_dirsort_fns);