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 SMB_STRUCT_DIRENT *da, const SMB_STRUCT_DIRENT *db)
27 return StrCaseCmp(da->d_name, db->d_name);
30 struct dirsort_privates {
32 SMB_STRUCT_DIRENT *directory_list;
33 long number_of_entries;
35 SMB_STRUCT_DIR *source_directory;
39 static void free_dirsort_privates(void **datap) {
43 static bool open_and_sort_dir (vfs_handle_struct *handle)
45 SMB_STRUCT_DIRENT *dp;
48 struct dirsort_privates *data = NULL;
50 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
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)
61 data->number_of_entries++;
64 /* Open the underlying directory and count the number of entries
65 Skip back to the beginning as we'll read it again */
66 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
68 /* Set up an array and read the directory entries into it */
69 TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
70 data->directory_list = talloc_zero_array(data,
72 data->number_of_entries);
73 if (!data->directory_list) {
76 current_pos = data->pos;
78 while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
80 data->directory_list[data->pos++] = *dp;
83 /* Sort the directory entries by name */
84 data->pos = current_pos;
85 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
89 static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
90 const char *fname, const char *mask,
93 struct dirsort_privates *data = NULL;
95 /* set up our private data about this directory */
96 data = talloc_zero(handle->conn, struct dirsort_privates);
101 data->directory_list = NULL;
104 /* Open the underlying directory and count the number of entries */
105 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
108 data->fd = dirfd(data->source_directory);
110 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
111 struct dirsort_privates, return NULL);
113 if (!open_and_sort_dir(handle)) {
114 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
118 return data->source_directory;
121 static SMB_STRUCT_DIR *dirsort_fdopendir(vfs_handle_struct *handle,
126 struct dirsort_privates *data = NULL;
128 /* set up our private data about this directory */
129 data = talloc_zero(handle->conn, struct dirsort_privates);
134 data->directory_list = NULL;
137 /* Open the underlying directory and count the number of entries */
138 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
141 if (data->source_directory == NULL) {
146 data->fd = dirfd(data->source_directory);
148 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
149 struct dirsort_privates, return NULL);
151 if (!open_and_sort_dir(handle)) {
152 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
153 /* fd is now closed. */
158 return data->source_directory;
161 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
162 SMB_STRUCT_DIR *dirp,
163 SMB_STRUCT_STAT *sbuf)
165 struct dirsort_privates *data = NULL;
166 time_t current_mtime;
167 struct stat dir_stat;
169 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
172 if (fstat(data->fd, &dir_stat) == -1) {
176 current_mtime = dir_stat.st_mtime;
178 /* throw away cache and re-read the directory if we've changed */
179 if (current_mtime > data->mtime) {
180 open_and_sort_dir(handle);
183 if (data->pos >= data->number_of_entries) {
187 return &data->directory_list[data->pos++];
190 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
193 struct dirsort_privates *data = NULL;
194 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
199 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
201 struct dirsort_privates *data = NULL;
202 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
208 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
210 struct dirsort_privates *data = NULL;
211 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
216 static struct vfs_fn_pointers vfs_dirsort_fns = {
217 .opendir = dirsort_opendir,
218 .fdopendir = dirsort_fdopendir,
219 .readdir = dirsort_readdir,
220 .seekdir = dirsort_seekdir,
221 .telldir = dirsort_telldir,
222 .rewind_dir = dirsort_rewinddir,
225 NTSTATUS vfs_dirsort_init(void)
227 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",