Use an index i rather than re-using a state variable.
[metze/samba/wip.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 struct dirent *da, const struct dirent *db)
26 {
27         return strcasecmp_m(da->d_name, db->d_name);
28 }
29
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;
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         struct dirent *dp;
46         struct stat dir_stat;
47         unsigned int i;
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         if (data->number_of_entries == 0) {
65                 return false;
66         }
67
68         /* Open the underlying directory and count the number of entries
69            Skip back to the beginning as we'll read it again */
70         SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
71
72         /* Set up an array and read the directory entries into it */
73         TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
74         data->directory_list = talloc_zero_array(data,
75                                         struct dirent,
76                                         data->number_of_entries);
77         if (!data->directory_list) {
78                 return false;
79         }
80         i = 0;
81         while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
82                                           NULL)) != NULL) {
83                 data->directory_list[i++] = *dp;
84         }
85
86         /* Sort the directory entries by name */
87         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
88         return true;
89 }
90
91 static DIR *dirsort_opendir(vfs_handle_struct *handle,
92                                        const char *fname, const char *mask,
93                                        uint32 attr)
94 {
95         struct dirsort_privates *data = NULL;
96
97         /* set up our private data about this directory */
98         data = talloc_zero(handle->conn, struct dirsort_privates);
99         if (!data) {
100                 return NULL;
101         }
102
103         data->directory_list = NULL;
104         data->pos = 0;
105
106         /* Open the underlying directory and count the number of entries */
107         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
108                                                       attr);
109
110         data->fd = dirfd(data->source_directory);
111
112         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
113                                 struct dirsort_privates, return NULL);
114
115         if (!open_and_sort_dir(handle)) {
116                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
117                 return NULL;
118         }
119
120         return data->source_directory;
121 }
122
123 static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
124                                         files_struct *fsp,
125                                         const char *mask,
126                                         uint32 attr)
127 {
128         struct dirsort_privates *data = NULL;
129
130         /* set up our private data about this directory */
131         data = talloc_zero(handle->conn, struct dirsort_privates);
132         if (!data) {
133                 return NULL;
134         }
135
136         data->directory_list = NULL;
137         data->pos = 0;
138
139         /* Open the underlying directory and count the number of entries */
140         data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
141                                                       attr);
142
143         if (data->source_directory == NULL) {
144                 TALLOC_FREE(data);
145                 return NULL;
146         }
147
148         data->fd = dirfd(data->source_directory);
149
150         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
151                                 struct dirsort_privates, return NULL);
152
153         if (!open_and_sort_dir(handle)) {
154                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
155                 /* fd is now closed. */
156                 fsp->fh->fd = -1;
157                 return NULL;
158         }
159
160         return data->source_directory;
161 }
162
163 static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
164                                           DIR *dirp,
165                                           SMB_STRUCT_STAT *sbuf)
166 {
167         struct dirsort_privates *data = NULL;
168         time_t current_mtime;
169         struct stat dir_stat;
170
171         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
172                                 return NULL);
173
174         if (fstat(data->fd, &dir_stat) == -1) {
175                 return NULL;
176         }
177
178         current_mtime = dir_stat.st_mtime;
179
180         /* throw away cache and re-read the directory if we've changed */
181         if (current_mtime > data->mtime) {
182                 open_and_sort_dir(handle);
183         }
184
185         if (data->pos >= data->number_of_entries) {
186                 return NULL;
187         }
188
189         return &data->directory_list[data->pos++];
190 }
191
192 static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
193                             long offset)
194 {
195         struct dirsort_privates *data = NULL;
196         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
197
198         data->pos = offset;
199 }
200
201 static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
202 {
203         struct dirsort_privates *data = NULL;
204         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
205                                 return -1);
206
207         return data->pos;
208 }
209
210 static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
211 {
212         struct dirsort_privates *data = NULL;
213         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
214
215         data->pos = 0;
216 }
217
218 static struct vfs_fn_pointers vfs_dirsort_fns = {
219         .opendir_fn = dirsort_opendir,
220         .fdopendir_fn = dirsort_fdopendir,
221         .readdir_fn = dirsort_readdir,
222         .seekdir_fn = dirsort_seekdir,
223         .telldir_fn = dirsort_telldir,
224         .rewind_dir_fn = dirsort_rewinddir,
225 };
226
227 NTSTATUS vfs_dirsort_init(void)
228 {
229         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
230                                 &vfs_dirsort_fns);
231 }