s3:net_idmap_dump add missing braces
[Samba/gebeck_regimport.git] / source3 / modules / vfs_dirsort.c
blob98472f87a128ee142e313179ad8f08fffc071222
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 long number_of_entries;
34 time_t mtime;
35 DIR *source_directory;
36 int fd;
39 static void free_dirsort_privates(void **datap) {
40 struct dirsort_privates *data = (struct dirsort_privates *) *datap;
41 SAFE_FREE(data->directory_list);
42 SAFE_FREE(data);
43 *datap = NULL;
46 static bool open_and_sort_dir (vfs_handle_struct *handle)
48 struct dirent *dp;
49 struct stat dir_stat;
50 long current_pos;
51 struct dirsort_privates *data = NULL;
53 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
54 return false);
56 data->number_of_entries = 0;
58 if (fstat(data->fd, &dir_stat) == 0) {
59 data->mtime = dir_stat.st_mtime;
62 while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
63 != NULL) {
64 data->number_of_entries++;
67 /* Open the underlying directory and count the number of entries
68 Skip back to the beginning as we'll read it again */
69 SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
71 /* Set up an array and read the directory entries into it */
72 SAFE_FREE(data->directory_list); /* destroy previous cache if needed */
73 data->directory_list = (struct dirent *)SMB_MALLOC(
74 data->number_of_entries * sizeof(struct dirent));
75 if (!data->directory_list) {
76 return false;
78 current_pos = data->pos;
79 data->pos = 0;
80 while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
81 NULL)) != NULL) {
82 data->directory_list[data->pos++] = *dp;
85 /* Sort the directory entries by name */
86 data->pos = current_pos;
87 TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
88 return true;
91 static DIR *dirsort_opendir(vfs_handle_struct *handle,
92 const char *fname, const char *mask,
93 uint32 attr)
95 struct dirsort_privates *data = NULL;
97 /* set up our private data about this directory */
98 data = (struct dirsort_privates *)SMB_MALLOC(
99 sizeof(struct dirsort_privates));
101 if (!data) {
102 return NULL;
105 data->directory_list = NULL;
106 data->pos = 0;
108 /* Open the underlying directory and count the number of entries */
109 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
110 attr);
112 data->fd = dirfd(data->source_directory);
114 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
115 struct dirsort_privates, return NULL);
117 if (!open_and_sort_dir(handle)) {
118 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
119 return NULL;
122 return data->source_directory;
125 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
126 files_struct *fsp,
127 const char *mask,
128 uint32 attr)
130 struct dirsort_privates *data = NULL;
132 /* set up our private data about this directory */
133 data = (struct dirsort_privates *)SMB_MALLOC(
134 sizeof(struct dirsort_privates));
136 if (!data) {
137 return NULL;
140 data->directory_list = NULL;
141 data->pos = 0;
143 /* Open the underlying directory and count the number of entries */
144 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
145 attr);
147 if (data->source_directory == NULL) {
148 SAFE_FREE(data);
149 return NULL;
152 data->fd = dirfd(data->source_directory);
154 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
155 struct dirsort_privates, return NULL);
157 if (!open_and_sort_dir(handle)) {
158 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
159 /* fd is now closed. */
160 fsp->fh->fd = -1;
161 return NULL;
164 return data->source_directory;
167 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
168 DIR *dirp,
169 SMB_STRUCT_STAT *sbuf)
171 struct dirsort_privates *data = NULL;
172 time_t current_mtime;
173 struct stat dir_stat;
175 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
176 return NULL);
178 if (fstat(data->fd, &dir_stat) == -1) {
179 return NULL;
182 current_mtime = dir_stat.st_mtime;
184 /* throw away cache and re-read the directory if we've changed */
185 if (current_mtime > data->mtime) {
186 open_and_sort_dir(handle);
189 if (data->pos >= data->number_of_entries) {
190 return NULL;
193 return &data->directory_list[data->pos++];
196 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
197 long offset)
199 struct dirsort_privates *data = NULL;
200 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
202 data->pos = offset;
205 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
207 struct dirsort_privates *data = NULL;
208 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
209 return -1);
211 return data->pos;
214 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
216 struct dirsort_privates *data = NULL;
217 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
219 data->pos = 0;
222 static struct vfs_fn_pointers vfs_dirsort_fns = {
223 .opendir_fn = dirsort_opendir,
224 .fdopendir_fn = dirsort_fdopendir,
225 .readdir_fn = dirsort_readdir,
226 .seekdir_fn = dirsort_seekdir,
227 .telldir_fn = dirsort_telldir,
228 .rewind_dir_fn = dirsort_rewinddir,
231 NTSTATUS vfs_dirsort_init(void)
233 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
234 &vfs_dirsort_fns);