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/>.
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 {
32 struct dirent *directory_list;
33 unsigned int number_of_entries;
35 DIR *source_directory;
39 static void free_dirsort_privates(void **datap) {
43 static bool open_and_sort_dir(vfs_handle_struct *handle,
44 struct dirsort_privates *data)
48 unsigned int total_count = 0;
49 struct dirsort_privates *data = NULL;
51 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
54 data->number_of_entries = 0;
56 if (fstat(data->fd, &dir_stat) == 0) {
57 data->mtime = dir_stat.st_mtime;
60 while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
65 if (total_count == 0) {
69 /* Open the underlying directory and count the number of entries
70 Skip back to the beginning as we'll read it again */
71 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
73 /* Set up an array and read the directory entries into it */
74 TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
75 data->directory_list = talloc_zero_array(data,
78 if (!data->directory_list) {
81 for (i = 0; i < total_count; i++) {
82 struct dirent *dp = SMB_VFS_NEXT_READDIR(handle,
83 data->source_directory,
88 data->directory_list[i] = *dp;
91 data->number_of_entries = i;
93 /* Sort the directory entries by name */
94 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
98 static DIR *dirsort_opendir(vfs_handle_struct *handle,
99 const char *fname, const char *mask,
102 struct dirsort_privates *data = NULL;
104 /* set up our private data about this directory */
105 data = talloc_zero(handle->conn, struct dirsort_privates);
110 data->directory_list = NULL;
113 /* Open the underlying directory and count the number of entries */
114 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
117 data->fd = dirfd(data->source_directory);
119 if (!open_and_sort_dir(handle, data)) {
120 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
125 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
126 struct dirsort_privates, return NULL);
128 return data->source_directory;
131 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
136 struct dirsort_privates *data = NULL;
138 /* set up our private data about this directory */
139 data = talloc_zero(handle->conn, struct dirsort_privates);
144 data->directory_list = NULL;
147 /* Open the underlying directory and count the number of entries */
148 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
151 if (data->source_directory == NULL) {
156 data->fd = dirfd(data->source_directory);
158 if (!open_and_sort_dir(handle, data)) {
159 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
161 /* fd is now closed. */
166 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
167 struct dirsort_privates, return NULL);
169 return data->source_directory;
172 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
174 SMB_STRUCT_STAT *sbuf)
176 struct dirsort_privates *data = NULL;
177 time_t current_mtime;
178 struct stat dir_stat;
180 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
183 if (fstat(data->fd, &dir_stat) == -1) {
187 current_mtime = dir_stat.st_mtime;
189 /* throw away cache and re-read the directory if we've changed */
190 if (current_mtime > data->mtime) {
191 open_and_sort_dir(handle, data);
194 if (data->pos >= data->number_of_entries) {
198 return &data->directory_list[data->pos++];
201 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
204 struct dirsort_privates *data = NULL;
205 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
210 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
212 struct dirsort_privates *data = NULL;
213 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
219 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
221 struct dirsort_privates *data = NULL;
222 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
227 static struct vfs_fn_pointers vfs_dirsort_fns = {
228 .opendir_fn = dirsort_opendir,
229 .fdopendir_fn = dirsort_fdopendir,
230 .readdir_fn = dirsort_readdir,
231 .seekdir_fn = dirsort_seekdir,
232 .telldir_fn = dirsort_telldir,
233 .rewind_dir_fn = dirsort_rewinddir,
236 NTSTATUS vfs_dirsort_init(void)
238 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",