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