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
{
31 struct dirsort_privates
*prev
, *next
;
33 struct dirent
*directory_list
;
34 unsigned int number_of_entries
;
35 struct timespec mtime
;
36 DIR *source_directory
;
37 files_struct
*fsp
; /* If open via FDOPENDIR. */
38 struct smb_filename
*smb_fname
; /* If open via OPENDIR */
41 static bool get_sorted_dir_mtime(vfs_handle_struct
*handle
,
42 struct dirsort_privates
*data
,
43 struct timespec
*ret_mtime
)
46 struct timespec mtime
;
50 status
= vfs_stat_fsp(data
->fsp
);
51 if (!NT_STATUS_IS_OK(status
)) {
54 mtime
= data
->fsp
->fsp_name
->st
.st_ex_mtime
;
56 ret
= SMB_VFS_STAT(handle
->conn
, data
->smb_fname
);
60 mtime
= data
->smb_fname
->st
.st_ex_mtime
;
68 static bool open_and_sort_dir(vfs_handle_struct
*handle
,
69 struct dirsort_privates
*data
)
71 uint32_t total_count
= 0;
72 /* This should be enough for most use cases */
73 uint32_t dirent_allocated
= 64;
76 data
->number_of_entries
= 0;
78 if (get_sorted_dir_mtime(handle
, data
, &data
->mtime
) == false) {
82 dp
= SMB_VFS_NEXT_READDIR(handle
, data
->fsp
, data
->source_directory
);
87 /* Set up an array and read the directory entries into it */
88 TALLOC_FREE(data
->directory_list
); /* destroy previous cache if needed */
89 data
->directory_list
= talloc_zero_array(data
,
92 if (data
->directory_list
== NULL
) {
97 if (total_count
>= dirent_allocated
) {
101 * Be memory friendly.
103 * We should not double the amount of memory. With a lot
104 * of files we reach easily 50MB, and doubling will
105 * get much bigger just for a few files more.
107 * For 200k files this means 50 memory reallocations.
109 dirent_allocated
+= 4096;
111 dlist
= talloc_realloc(data
,
112 data
->directory_list
,
118 data
->directory_list
= dlist
;
120 data
->directory_list
[total_count
] = *dp
;
123 dp
= SMB_VFS_NEXT_READDIR(handle
,
125 data
->source_directory
);
126 } while (dp
!= NULL
);
128 data
->number_of_entries
= total_count
;
130 /* Sort the directory entries by name */
131 TYPESAFE_QSORT(data
->directory_list
, data
->number_of_entries
, compare_dirent
);
135 static DIR *dirsort_fdopendir(vfs_handle_struct
*handle
,
140 struct dirsort_privates
*list_head
= NULL
;
141 struct dirsort_privates
*data
= NULL
;
143 if (SMB_VFS_HANDLE_TEST_DATA(handle
)) {
144 /* Find the list head of all open directories. */
145 SMB_VFS_HANDLE_GET_DATA(handle
, list_head
, struct dirsort_privates
,
149 /* set up our private data about this directory */
150 data
= talloc_zero(handle
->conn
, struct dirsort_privates
);
157 /* Open the underlying directory and count the number of entries */
158 data
->source_directory
= SMB_VFS_NEXT_FDOPENDIR(handle
, fsp
, mask
,
161 if (data
->source_directory
== NULL
) {
166 if (!open_and_sort_dir(handle
, data
)) {
167 SMB_VFS_NEXT_CLOSEDIR(handle
,data
->source_directory
);
169 /* fd is now closed. */
174 /* Add to the private list of all open directories. */
175 DLIST_ADD(list_head
, data
);
176 SMB_VFS_HANDLE_SET_DATA(handle
, list_head
, NULL
,
177 struct dirsort_privates
, return NULL
);
179 return data
->source_directory
;
182 static struct dirent
*dirsort_readdir(vfs_handle_struct
*handle
,
183 struct files_struct
*dirfsp
,
186 struct dirsort_privates
*data
= NULL
;
187 struct timespec current_mtime
;
189 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
,
192 while(data
&& (data
->source_directory
!= dirp
)) {
199 if (get_sorted_dir_mtime(handle
, data
, ¤t_mtime
) == false) {
203 /* throw away cache and re-read the directory if we've changed */
204 if (timespec_compare(¤t_mtime
, &data
->mtime
)) {
205 SMB_VFS_NEXT_REWINDDIR(handle
, data
->source_directory
);
206 open_and_sort_dir(handle
, data
);
209 if (data
->pos
>= data
->number_of_entries
) {
213 return &data
->directory_list
[data
->pos
++];
216 static void dirsort_rewinddir(vfs_handle_struct
*handle
, DIR *dirp
)
218 struct dirsort_privates
*data
= NULL
;
219 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
, return);
221 /* Find the entry holding dirp. */
222 while(data
&& (data
->source_directory
!= dirp
)) {
231 static int dirsort_closedir(vfs_handle_struct
*handle
, DIR *dirp
)
233 struct dirsort_privates
*list_head
= NULL
;
234 struct dirsort_privates
*data
= NULL
;
237 SMB_VFS_HANDLE_GET_DATA(handle
, list_head
, struct dirsort_privates
, return -1);
238 /* Find the entry holding dirp. */
239 for(data
= list_head
; data
&& (data
->source_directory
!= dirp
); data
= data
->next
) {
245 /* Remove from the list and re-store the list head. */
246 DLIST_REMOVE(list_head
, data
);
247 SMB_VFS_HANDLE_SET_DATA(handle
, list_head
, NULL
,
248 struct dirsort_privates
, return -1);
250 ret
= SMB_VFS_NEXT_CLOSEDIR(handle
, dirp
);
255 static struct vfs_fn_pointers vfs_dirsort_fns
= {
256 .fdopendir_fn
= dirsort_fdopendir
,
257 .readdir_fn
= dirsort_readdir
,
258 .rewind_dir_fn
= dirsort_rewinddir
,
259 .closedir_fn
= dirsort_closedir
,
263 NTSTATUS
vfs_dirsort_init(TALLOC_CTX
*ctx
)
265 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "dirsort",