vfs: Convert dirsort_opendir to synthetic_smb_fname
[Samba.git] / source3 / modules / vfs_dirsort.c
blob2c257655bc814eca088c6a00de927e8bb24a71f9
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 unsigned int number_of_entries;
34 struct timespec mtime;
35 DIR *source_directory;
36 files_struct *fsp; /* If open via FDOPENDIR. */
37 struct smb_filename *smb_fname; /* If open via OPENDIR */
40 static void free_dirsort_privates(void **datap) {
41 TALLOC_FREE(*datap);
44 static bool get_sorted_dir_mtime(vfs_handle_struct *handle,
45 struct dirsort_privates *data,
46 struct timespec *ret_mtime)
48 int ret;
49 struct timespec mtime;
51 if (data->fsp) {
52 ret = fsp_stat(data->fsp);
53 mtime = data->fsp->fsp_name->st.st_ex_mtime;
54 } else {
55 ret = SMB_VFS_STAT(handle->conn, data->smb_fname);
56 mtime = data->smb_fname->st.st_ex_mtime;
59 if (ret == -1) {
60 return false;
63 *ret_mtime = mtime;
65 return true;
68 static bool open_and_sort_dir(vfs_handle_struct *handle,
69 struct dirsort_privates *data)
71 unsigned int i = 0;
72 unsigned int total_count = 0;
74 data->number_of_entries = 0;
76 if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
77 return false;
80 while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
81 != NULL) {
82 total_count++;
85 if (total_count == 0) {
86 return false;
89 /* Open the underlying directory and count the number of entries
90 Skip back to the beginning as we'll read it again */
91 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
93 /* Set up an array and read the directory entries into it */
94 TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
95 data->directory_list = talloc_zero_array(data,
96 struct dirent,
97 total_count);
98 if (!data->directory_list) {
99 return false;
101 for (i = 0; i < total_count; i++) {
102 struct dirent *dp = SMB_VFS_NEXT_READDIR(handle,
103 data->source_directory,
104 NULL);
105 if (dp == NULL) {
106 break;
108 data->directory_list[i] = *dp;
111 data->number_of_entries = i;
113 /* Sort the directory entries by name */
114 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
115 return true;
118 static DIR *dirsort_opendir(vfs_handle_struct *handle,
119 const char *fname, const char *mask,
120 uint32 attr)
122 struct dirsort_privates *data = NULL;
124 /* set up our private data about this directory */
125 data = talloc_zero(handle->conn, struct dirsort_privates);
126 if (!data) {
127 return NULL;
130 data->smb_fname = synthetic_smb_fname(data, fname, NULL, NULL);
131 if (data->smb_fname == NULL) {
132 TALLOC_FREE(data);
133 return NULL;
136 /* Open the underlying directory and count the number of entries */
137 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
138 attr);
140 if (data->source_directory == NULL) {
141 TALLOC_FREE(data);
142 return NULL;
145 if (!open_and_sort_dir(handle, data)) {
146 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
147 TALLOC_FREE(data);
148 return NULL;
151 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
152 struct dirsort_privates, return NULL);
154 return data->source_directory;
157 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
158 files_struct *fsp,
159 const char *mask,
160 uint32 attr)
162 struct dirsort_privates *data = NULL;
164 /* set up our private data about this directory */
165 data = talloc_zero(handle->conn, struct dirsort_privates);
166 if (!data) {
167 return NULL;
170 data->fsp = fsp;
172 /* Open the underlying directory and count the number of entries */
173 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
174 attr);
176 if (data->source_directory == NULL) {
177 TALLOC_FREE(data);
178 return NULL;
181 if (!open_and_sort_dir(handle, data)) {
182 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
183 TALLOC_FREE(data);
184 /* fd is now closed. */
185 fsp->fh->fd = -1;
186 return NULL;
189 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
190 struct dirsort_privates, return NULL);
192 return data->source_directory;
195 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
196 DIR *dirp,
197 SMB_STRUCT_STAT *sbuf)
199 struct dirsort_privates *data = NULL;
200 struct timespec current_mtime;
202 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
203 return NULL);
205 if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
206 return NULL;
209 /* throw away cache and re-read the directory if we've changed */
210 if (timespec_compare(&current_mtime, &data->mtime) > 1) {
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 dirsort_privates *data = NULL;
225 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
227 data->pos = offset;
230 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
232 struct dirsort_privates *data = NULL;
233 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
234 return -1);
236 return data->pos;
239 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
241 struct dirsort_privates *data = NULL;
242 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
244 data->pos = 0;
247 static struct vfs_fn_pointers vfs_dirsort_fns = {
248 .opendir_fn = dirsort_opendir,
249 .fdopendir_fn = dirsort_fdopendir,
250 .readdir_fn = dirsort_readdir,
251 .seekdir_fn = dirsort_seekdir,
252 .telldir_fn = dirsort_telldir,
253 .rewind_dir_fn = dirsort_rewinddir,
256 NTSTATUS vfs_dirsort_init(void)
258 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
259 &vfs_dirsort_fns);