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
;
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
) {
44 static bool get_sorted_dir_mtime(vfs_handle_struct
*handle
,
45 struct dirsort_privates
*data
,
46 struct timespec
*ret_mtime
)
49 struct timespec mtime
;
52 ret
= fsp_stat(data
->fsp
);
53 mtime
= data
->fsp
->fsp_name
->st
.st_ex_mtime
;
55 ret
= SMB_VFS_STAT(handle
->conn
, data
->smb_fname
);
56 mtime
= data
->smb_fname
->st
.st_ex_mtime
;
68 static bool open_and_sort_dir(vfs_handle_struct
*handle
,
69 struct dirsort_privates
*data
)
72 unsigned int total_count
= 0;
74 data
->number_of_entries
= 0;
76 if (get_sorted_dir_mtime(handle
, data
, &data
->mtime
) == false) {
80 while (SMB_VFS_NEXT_READDIR(handle
, data
->source_directory
, NULL
)
85 if (total_count
== 0) {
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
,
98 if (!data
->directory_list
) {
101 for (i
= 0; i
< total_count
; i
++) {
102 struct dirent
*dp
= SMB_VFS_NEXT_READDIR(handle
,
103 data
->source_directory
,
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
);
118 static DIR *dirsort_opendir(vfs_handle_struct
*handle
,
119 const char *fname
, const char *mask
,
123 struct dirsort_privates
*data
= NULL
;
125 /* set up our private data about this directory */
126 data
= talloc_zero(handle
->conn
, struct dirsort_privates
);
131 data
->directory_list
= NULL
;
134 status
= create_synthetic_smb_fname(data
,
139 if (!NT_STATUS_IS_OK(status
)) {
144 /* Open the underlying directory and count the number of entries */
145 data
->source_directory
= SMB_VFS_NEXT_OPENDIR(handle
, fname
, mask
,
148 if (data
->source_directory
== NULL
) {
153 if (!open_and_sort_dir(handle
, data
)) {
154 SMB_VFS_NEXT_CLOSEDIR(handle
,data
->source_directory
);
159 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_dirsort_privates
,
160 struct dirsort_privates
, return NULL
);
162 return data
->source_directory
;
165 static DIR *dirsort_fdopendir(vfs_handle_struct
*handle
,
170 struct dirsort_privates
*data
= NULL
;
172 /* set up our private data about this directory */
173 data
= talloc_zero(handle
->conn
, struct dirsort_privates
);
178 data
->directory_list
= NULL
;
182 /* Open the underlying directory and count the number of entries */
183 data
->source_directory
= SMB_VFS_NEXT_FDOPENDIR(handle
, fsp
, mask
,
186 if (data
->source_directory
== NULL
) {
191 if (!open_and_sort_dir(handle
, data
)) {
192 SMB_VFS_NEXT_CLOSEDIR(handle
,data
->source_directory
);
194 /* fd is now closed. */
199 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_dirsort_privates
,
200 struct dirsort_privates
, return NULL
);
202 return data
->source_directory
;
205 static struct dirent
*dirsort_readdir(vfs_handle_struct
*handle
,
207 SMB_STRUCT_STAT
*sbuf
)
209 struct dirsort_privates
*data
= NULL
;
210 struct timespec current_mtime
;
212 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
,
215 if (get_sorted_dir_mtime(handle
, data
, ¤t_mtime
) == false) {
219 /* throw away cache and re-read the directory if we've changed */
220 if (timespec_compare(¤t_mtime
, &data
->mtime
) > 1) {
221 open_and_sort_dir(handle
, data
);
224 if (data
->pos
>= data
->number_of_entries
) {
228 return &data
->directory_list
[data
->pos
++];
231 static void dirsort_seekdir(vfs_handle_struct
*handle
, DIR *dirp
,
234 struct dirsort_privates
*data
= NULL
;
235 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
, return);
240 static long dirsort_telldir(vfs_handle_struct
*handle
, DIR *dirp
)
242 struct dirsort_privates
*data
= NULL
;
243 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
,
249 static void dirsort_rewinddir(vfs_handle_struct
*handle
, DIR *dirp
)
251 struct dirsort_privates
*data
= NULL
;
252 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
, return);
257 static struct vfs_fn_pointers vfs_dirsort_fns
= {
258 .opendir_fn
= dirsort_opendir
,
259 .fdopendir_fn
= dirsort_fdopendir
,
260 .readdir_fn
= dirsort_readdir
,
261 .seekdir_fn
= dirsort_seekdir
,
262 .telldir_fn
= dirsort_telldir
,
263 .rewind_dir_fn
= dirsort_rewinddir
,
266 NTSTATUS
vfs_dirsort_init(void)
268 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "dirsort",