Change source3/modules/vfs_dirsort.c from MALLOC -> TALLOC.
[Samba.git] / source3 / modules / vfs_dirsort.c
blob88191b04f7afc2dec6e05f02010d3b79227aeb9b
1 /*
2 * VFS module to provide a sorted directory list.
4 * Copyright (C) Andy Kelk (andy@mopoke.co.uk), 2009
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.
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.
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/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
25 static int compare_dirent (const SMB_STRUCT_DIRENT *da, const SMB_STRUCT_DIRENT *db)
27 return StrCaseCmp(da->d_name, db->d_name);
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;
39 static void free_dirsort_privates(void **datap) {
40 TALLOC_FREE(*datap);
43 static bool open_and_sort_dir (vfs_handle_struct *handle)
45 SMB_STRUCT_DIRENT *dp;
46 struct stat dir_stat;
47 long current_pos;
48 struct dirsort_privates *data = NULL;
50 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
51 return false);
53 data->number_of_entries = 0;
55 if (fstat(data->fd, &dir_stat) == 0) {
56 data->mtime = dir_stat.st_mtime;
59 while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
60 != NULL) {
61 data->number_of_entries++;
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);
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;
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;
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;
89 static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
90 const char *fname, const char *mask,
91 uint32 attr)
93 struct dirsort_privates *data = NULL;
95 /* set up our private data about this directory */
96 data = talloc_zero(handle->conn, struct dirsort_privates);
97 if (!data) {
98 return NULL;
101 data->directory_list = NULL;
102 data->pos = 0;
104 /* Open the underlying directory and count the number of entries */
105 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
106 attr);
108 data->fd = dirfd(data->source_directory);
110 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
111 struct dirsort_privates, return NULL);
113 if (!open_and_sort_dir(handle)) {
114 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
115 return NULL;
118 return data->source_directory;
121 static SMB_STRUCT_DIR *dirsort_fdopendir(vfs_handle_struct *handle,
122 files_struct *fsp,
123 const char *mask,
124 uint32 attr)
126 struct dirsort_privates *data = NULL;
128 /* set up our private data about this directory */
129 data = talloc_zero(handle->conn, struct dirsort_privates);
130 if (!data) {
131 return NULL;
134 data->directory_list = NULL;
135 data->pos = 0;
137 /* Open the underlying directory and count the number of entries */
138 data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
139 attr);
141 if (data->source_directory == NULL) {
142 TALLOC_FREE(data);
143 return NULL;
146 data->fd = dirfd(data->source_directory);
148 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
149 struct dirsort_privates, return NULL);
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;
158 return data->source_directory;
161 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
162 SMB_STRUCT_DIR *dirp,
163 SMB_STRUCT_STAT *sbuf)
165 struct dirsort_privates *data = NULL;
166 time_t current_mtime;
167 struct stat dir_stat;
169 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
170 return NULL);
172 if (fstat(data->fd, &dir_stat) == -1) {
173 return NULL;
176 current_mtime = dir_stat.st_mtime;
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);
183 if (data->pos >= data->number_of_entries) {
184 return NULL;
187 return &data->directory_list[data->pos++];
190 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
191 long offset)
193 struct dirsort_privates *data = NULL;
194 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
196 data->pos = offset;
199 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
201 struct dirsort_privates *data = NULL;
202 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
203 return -1);
205 return data->pos;
208 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
210 struct dirsort_privates *data = NULL;
211 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
213 data->pos = 0;
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,
225 NTSTATUS vfs_dirsort_init(void)
227 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
228 &vfs_dirsort_fns);