s3-vfs: include smbd/smbd.h in vfs modules.
[kamenim/samba-autobuild/.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         struct dirsort_privates *data = (struct dirsort_privates *) *datap;
41         SAFE_FREE(data->directory_list);
42         SAFE_FREE(data);
43         *datap = NULL;
44
45         return;
46 }
47
48 static bool open_and_sort_dir (vfs_handle_struct *handle)
49 {
50         SMB_STRUCT_DIRENT *dp;
51         struct stat dir_stat;
52         long current_pos;
53         struct dirsort_privates *data = NULL;
54
55         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
56                                 return false);
57
58         data->number_of_entries = 0;
59
60         if (fstat(data->fd, &dir_stat) == 0) {
61                 data->mtime = dir_stat.st_mtime;
62         }
63
64         while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
65                != NULL) {
66                 data->number_of_entries++;
67         }
68
69         /* Open the underlying directory and count the number of entries
70            Skip back to the beginning as we'll read it again */
71         SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
72
73         /* Set up an array and read the directory entries into it */
74         SAFE_FREE(data->directory_list); /* destroy previous cache if needed */
75         data->directory_list = (SMB_STRUCT_DIRENT *)SMB_MALLOC(
76                 data->number_of_entries * sizeof(SMB_STRUCT_DIRENT));
77         if (!data->directory_list) {
78                 return false;
79         }
80         current_pos = data->pos;
81         data->pos = 0;
82         while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
83                                           NULL)) != NULL) {
84                 data->directory_list[data->pos++] = *dp;
85         }
86
87         /* Sort the directory entries by name */
88         data->pos = current_pos;
89         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
90         return true;
91 }
92
93 static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
94                                        const char *fname, const char *mask,
95                                        uint32 attr)
96 {
97         struct dirsort_privates *data = NULL;
98
99         /* set up our private data about this directory */
100         data = (struct dirsort_privates *)SMB_MALLOC(
101                 sizeof(struct dirsort_privates));
102
103         if (!data) {
104                 return NULL;
105         }
106
107         data->directory_list = NULL;
108         data->pos = 0;
109
110         /* Open the underlying directory and count the number of entries */
111         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
112                                                       attr);
113
114         data->fd = dirfd(data->source_directory);
115
116         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
117                                 struct dirsort_privates, return NULL);
118
119         if (!open_and_sort_dir(handle)) {
120                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
121                 return NULL;
122         }
123
124         return data->source_directory;
125 }
126
127 static SMB_STRUCT_DIR *dirsort_fdopendir(vfs_handle_struct *handle,
128                                         files_struct *fsp,
129                                         const char *mask,
130                                         uint32 attr)
131 {
132         struct dirsort_privates *data = NULL;
133
134         /* set up our private data about this directory */
135         data = (struct dirsort_privates *)SMB_MALLOC(
136                 sizeof(struct dirsort_privates));
137
138         if (!data) {
139                 return NULL;
140         }
141
142         data->directory_list = NULL;
143         data->pos = 0;
144
145         /* Open the underlying directory and count the number of entries */
146         data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
147                                                       attr);
148
149         if (data->source_directory == NULL) {
150                 SAFE_FREE(data);
151                 return NULL;
152         }
153
154         data->fd = dirfd(data->source_directory);
155
156         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
157                                 struct dirsort_privates, return NULL);
158
159         if (!open_and_sort_dir(handle)) {
160                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
161                 /* fd is now closed. */
162                 fsp->fh->fd = -1;
163                 return NULL;
164         }
165
166         return data->source_directory;
167 }
168
169 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
170                                           SMB_STRUCT_DIR *dirp,
171                                           SMB_STRUCT_STAT *sbuf)
172 {
173         struct dirsort_privates *data = NULL;
174         time_t current_mtime;
175         struct stat dir_stat;
176
177         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
178                                 return NULL);
179
180         if (fstat(data->fd, &dir_stat) == -1) {
181                 return NULL;
182         }
183
184         current_mtime = dir_stat.st_mtime;
185
186         /* throw away cache and re-read the directory if we've changed */
187         if (current_mtime > data->mtime) {
188                 open_and_sort_dir(handle);
189         }
190
191         if (data->pos >= data->number_of_entries) {
192                 return NULL;
193         }
194
195         return &data->directory_list[data->pos++];
196 }
197
198 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
199                             long offset)
200 {
201         struct dirsort_privates *data = NULL;
202         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
203
204         data->pos = offset;
205 }
206
207 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
208 {
209         struct dirsort_privates *data = NULL;
210         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
211                                 return -1);
212
213         return data->pos;
214 }
215
216 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
217 {
218         struct dirsort_privates *data = NULL;
219         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
220
221         data->pos = 0;
222 }
223
224 static struct vfs_fn_pointers vfs_dirsort_fns = {
225         .opendir = dirsort_opendir,
226         .fdopendir = dirsort_fdopendir,
227         .readdir = dirsort_readdir,
228         .seekdir = dirsort_seekdir,
229         .telldir = dirsort_telldir,
230         .rewind_dir = dirsort_rewinddir,
231 };
232
233 NTSTATUS vfs_dirsort_init(void)
234 {
235         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
236                                 &vfs_dirsort_fns);
237 }