Remove unneeded initializations (we already talloc_zero).
[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         unsigned int number_of_entries;
34         struct timespec mtime;
35         SMB_STRUCT_DIR *source_directory;
36         files_struct *fsp; /* If open via FDOPENDIR. */
37         struct smb_filename *smb_fname; /* If open via OPENDIR */
38 };
39
40 static void free_dirsort_privates(void **datap) {
41         TALLOC_FREE(*datap);
42 }
43
44 static bool get_sorted_dir_mtime(vfs_handle_struct *handle,
45                                 struct dirsort_privates *data,
46                                 struct timespec *ret_mtime)
47 {
48         int ret;
49         struct timespec mtime;
50
51         if (data->fsp) {
52                 ret = fsp_stat(data->fsp);
53                 mtime = data->fsp->fsp_name->st.st_ex_mtime;
54         } else {
55                 ret = SMB_VFS_STAT(handle->conn, data->smb_fname);
56                 mtime = data->smb_fname->st.st_ex_mtime;
57         }
58
59         if (ret == -1) {
60                 return false;
61         }
62
63         *ret_mtime = mtime;
64
65         return true;
66 }
67
68 static bool open_and_sort_dir(vfs_handle_struct *handle,
69                                 struct dirsort_privates *data)
70 {
71         unsigned int i = 0;
72         unsigned int total_count = 0;
73
74         data->number_of_entries = 0;
75
76         if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
77                 return false;
78         }
79
80         while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
81                != NULL) {
82                 total_count++;
83         }
84
85         if (total_count == 0) {
86                 return false;
87         }
88
89         /* Open the underlying directory and count the number of entries
90            Skip back to the beginning as we'll read it again */
91         SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
92
93         /* Set up an array and read the directory entries into it */
94         TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
95         data->directory_list = talloc_zero_array(data,
96                                                  SMB_STRUCT_DIRENT,
97                                                  total_count);
98         if (!data->directory_list) {
99                 return false;
100         }
101         for (i = 0; i < total_count; i++) {
102                 SMB_STRUCT_DIRENT *dp = SMB_VFS_NEXT_READDIR(handle,
103                                                 data->source_directory,
104                                                 NULL);
105                 if (dp == NULL) {
106                         break;
107                 }
108                 data->directory_list[i] = *dp;
109         }
110
111         data->number_of_entries = i;
112
113         /* Sort the directory entries by name */
114         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
115         return true;
116 }
117
118 static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
119                                        const char *fname, const char *mask,
120                                        uint32 attr)
121 {
122         NTSTATUS status;
123         struct dirsort_privates *data = NULL;
124
125         /* set up our private data about this directory */
126         data = talloc_zero(handle->conn, struct dirsort_privates);
127         if (!data) {
128                 return NULL;
129         }
130
131         status = create_synthetic_smb_fname(data,
132                                         fname,
133                                         NULL,
134                                         NULL,
135                                         &data->smb_fname);
136         if (!NT_STATUS_IS_OK(status)) {
137                 TALLOC_FREE(data);
138                 return NULL;
139         }
140
141         /* Open the underlying directory and count the number of entries */
142         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
143                                                       attr);
144
145         if (data->source_directory == NULL) {
146                 TALLOC_FREE(data);
147                 return NULL;
148         }
149
150         if (!open_and_sort_dir(handle, data)) {
151                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
152                 TALLOC_FREE(data);
153                 return NULL;
154         }
155
156         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
157                                 struct dirsort_privates, return NULL);
158
159         return data->source_directory;
160 }
161
162 static SMB_STRUCT_DIR *dirsort_fdopendir(vfs_handle_struct *handle,
163                                         files_struct *fsp,
164                                         const char *mask,
165                                         uint32 attr)
166 {
167         struct dirsort_privates *data = NULL;
168
169         /* set up our private data about this directory */
170         data = talloc_zero(handle->conn, struct dirsort_privates);
171         if (!data) {
172                 return NULL;
173         }
174
175         data->fsp = fsp;
176
177         /* Open the underlying directory and count the number of entries */
178         data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
179                                                       attr);
180
181         if (data->source_directory == NULL) {
182                 TALLOC_FREE(data);
183                 return NULL;
184         }
185
186         if (!open_and_sort_dir(handle, data)) {
187                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
188                 TALLOC_FREE(data);
189                 /* fd is now closed. */
190                 fsp->fh->fd = -1;
191                 return NULL;
192         }
193
194         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
195                                 struct dirsort_privates, return NULL);
196
197         return data->source_directory;
198 }
199
200 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
201                                           SMB_STRUCT_DIR *dirp,
202                                           SMB_STRUCT_STAT *sbuf)
203 {
204         struct dirsort_privates *data = NULL;
205         struct timespec current_mtime;
206
207         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
208                                 return NULL);
209
210         if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
211                 return NULL;
212         }
213
214         /* throw away cache and re-read the directory if we've changed */
215         if (timespec_compare(&current_mtime, &data->mtime) > 1) {
216                 open_and_sort_dir(handle, data);
217         }
218
219         if (data->pos >= data->number_of_entries) {
220                 return NULL;
221         }
222
223         return &data->directory_list[data->pos++];
224 }
225
226 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
227                             long offset)
228 {
229         struct dirsort_privates *data = NULL;
230         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
231
232         data->pos = offset;
233 }
234
235 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
236 {
237         struct dirsort_privates *data = NULL;
238         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
239                                 return -1);
240
241         return data->pos;
242 }
243
244 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
245 {
246         struct dirsort_privates *data = NULL;
247         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
248
249         data->pos = 0;
250 }
251
252 static struct vfs_fn_pointers vfs_dirsort_fns = {
253         .opendir = dirsort_opendir,
254         .fdopendir = dirsort_fdopendir,
255         .readdir = dirsort_readdir,
256         .seekdir = dirsort_seekdir,
257         .telldir = dirsort_telldir,
258         .rewind_dir = dirsort_rewinddir,
259 };
260
261 NTSTATUS vfs_dirsort_init(void)
262 {
263         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
264                                 &vfs_dirsort_fns);
265 }