88191b04f7afc2dec6e05f02010d3b79227aeb9b
[samba.git] / source3 / modules / vfs_dirsort.c
1 /*
2  * VFS module to provide a sorted directory list.
3  *
4  * Copyright (C) Andy Kelk (andy@mopoke.co.uk), 2009
5  *
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24
25 static int compare_dirent (const SMB_STRUCT_DIRENT *da, const SMB_STRUCT_DIRENT *db)
26 {
27         return StrCaseCmp(da->d_name, db->d_name);
28 }
29
30 struct dirsort_privates {
31         long pos;
32         SMB_STRUCT_DIRENT *directory_list;
33         long number_of_entries;
34         time_t mtime;
35         SMB_STRUCT_DIR *source_directory;
36         int fd;
37 };
38
39 static void free_dirsort_privates(void **datap) {
40         TALLOC_FREE(*datap);
41 }
42
43 static bool open_and_sort_dir (vfs_handle_struct *handle)
44 {
45         SMB_STRUCT_DIRENT *dp;
46         struct stat dir_stat;
47         long current_pos;
48         struct dirsort_privates *data = NULL;
49
50         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
51                                 return false);
52
53         data->number_of_entries = 0;
54
55         if (fstat(data->fd, &dir_stat) == 0) {
56                 data->mtime = dir_stat.st_mtime;
57         }
58
59         while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
60                != NULL) {
61                 data->number_of_entries++;
62         }
63
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);
67
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,
71                                                  SMB_STRUCT_DIRENT,
72                                                  data->number_of_entries);
73         if (!data->directory_list) {
74                 return false;
75         }
76         current_pos = data->pos;
77         data->pos = 0;
78         while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
79                                           NULL)) != NULL) {
80                 data->directory_list[data->pos++] = *dp;
81         }
82
83         /* Sort the directory entries by name */
84         data->pos = current_pos;
85         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
86         return true;
87 }
88
89 static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
90                                        const char *fname, const char *mask,
91                                        uint32 attr)
92 {
93         struct dirsort_privates *data = NULL;
94
95         /* set up our private data about this directory */
96         data = talloc_zero(handle->conn, struct dirsort_privates);
97         if (!data) {
98                 return NULL;
99         }
100
101         data->directory_list = NULL;
102         data->pos = 0;
103
104         /* Open the underlying directory and count the number of entries */
105         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
106                                                       attr);
107
108         data->fd = dirfd(data->source_directory);
109
110         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
111                                 struct dirsort_privates, return NULL);
112
113         if (!open_and_sort_dir(handle)) {
114                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
115                 return NULL;
116         }
117
118         return data->source_directory;
119 }
120
121 static SMB_STRUCT_DIR *dirsort_fdopendir(vfs_handle_struct *handle,
122                                         files_struct *fsp,
123                                         const char *mask,
124                                         uint32 attr)
125 {
126         struct dirsort_privates *data = NULL;
127
128         /* set up our private data about this directory */
129         data = talloc_zero(handle->conn, struct dirsort_privates);
130         if (!data) {
131                 return NULL;
132         }
133
134         data->directory_list = NULL;
135         data->pos = 0;
136
137         /* Open the underlying directory and count the number of entries */
138         data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
139                                                       attr);
140
141         if (data->source_directory == NULL) {
142                 TALLOC_FREE(data);
143                 return NULL;
144         }
145
146         data->fd = dirfd(data->source_directory);
147
148         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
149                                 struct dirsort_privates, return NULL);
150
151         if (!open_and_sort_dir(handle)) {
152                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
153                 /* fd is now closed. */
154                 fsp->fh->fd = -1;
155                 return NULL;
156         }
157
158         return data->source_directory;
159 }
160
161 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
162                                           SMB_STRUCT_DIR *dirp,
163                                           SMB_STRUCT_STAT *sbuf)
164 {
165         struct dirsort_privates *data = NULL;
166         time_t current_mtime;
167         struct stat dir_stat;
168
169         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
170                                 return NULL);
171
172         if (fstat(data->fd, &dir_stat) == -1) {
173                 return NULL;
174         }
175
176         current_mtime = dir_stat.st_mtime;
177
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);
181         }
182
183         if (data->pos >= data->number_of_entries) {
184                 return NULL;
185         }
186
187         return &data->directory_list[data->pos++];
188 }
189
190 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
191                             long offset)
192 {
193         struct dirsort_privates *data = NULL;
194         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
195
196         data->pos = offset;
197 }
198
199 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
200 {
201         struct dirsort_privates *data = NULL;
202         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
203                                 return -1);
204
205         return data->pos;
206 }
207
208 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
209 {
210         struct dirsort_privates *data = NULL;
211         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
212
213         data->pos = 0;
214 }
215
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,
223 };
224
225 NTSTATUS vfs_dirsort_init(void)
226 {
227         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
228                                 &vfs_dirsort_fns);
229 }