Check all SMB_MALLOC returns correctly. Found by Andreas Moroder <andreas.moroder...
[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
23 static int compare_dirent (const SMB_STRUCT_DIRENT *da, const SMB_STRUCT_DIRENT *db)
24 {
25         return StrCaseCmp(da->d_name, db->d_name);
26 }
27
28 struct dirsort_privates {
29         long pos;
30         SMB_STRUCT_DIRENT *directory_list;
31         long number_of_entries;
32         time_t mtime;
33         SMB_STRUCT_DIR *source_directory;
34         int fd;
35 };
36
37 static void free_dirsort_privates(void **datap) {
38         struct dirsort_privates *data = (struct dirsort_privates *) *datap;
39         SAFE_FREE(data->directory_list);
40         SAFE_FREE(data);
41         *datap = NULL;
42
43         return;
44 }
45
46 static bool open_and_sort_dir (vfs_handle_struct *handle)
47 {
48         SMB_STRUCT_DIRENT *dp;
49         struct stat dir_stat;
50         long current_pos;
51         struct dirsort_privates *data = NULL;
52
53         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
54                                 return false);
55
56         data->number_of_entries = 0;
57
58         if (fstat(data->fd, &dir_stat) == 0) {
59                 data->mtime = dir_stat.st_mtime;
60         }
61
62         while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
63                != NULL) {
64                 data->number_of_entries++;
65         }
66
67         /* Open the underlying directory and count the number of entries
68            Skip back to the beginning as we'll read it again */
69         SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
70
71         /* Set up an array and read the directory entries into it */
72         SAFE_FREE(data->directory_list); /* destroy previous cache if needed */
73         data->directory_list = (SMB_STRUCT_DIRENT *)SMB_MALLOC(
74                 data->number_of_entries * sizeof(SMB_STRUCT_DIRENT));
75         if (!data->directory_list) {
76                 return false;
77         }
78         current_pos = data->pos;
79         data->pos = 0;
80         while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
81                                           NULL)) != NULL) {
82                 data->directory_list[data->pos++] = *dp;
83         }
84
85         /* Sort the directory entries by name */
86         data->pos = current_pos;
87         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
88         return true;
89 }
90
91 static SMB_STRUCT_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 = (struct dirsort_privates *)SMB_MALLOC(
99                 sizeof(struct dirsort_privates));
100
101         if (!data) {
102                 return NULL;
103         }
104
105         data->directory_list = NULL;
106         data->pos = 0;
107
108         /* Open the underlying directory and count the number of entries */
109         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
110                                                       attr);
111
112         data->fd = dirfd(data->source_directory);
113
114         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
115                                 struct dirsort_privates, return NULL);
116
117         if (!open_and_sort_dir(handle)) {
118                 SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
119                 return NULL;
120         }
121
122         return data->source_directory;
123 }
124
125 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
126                                           SMB_STRUCT_DIR *dirp,
127                                           SMB_STRUCT_STAT *sbuf)
128 {
129         struct dirsort_privates *data = NULL;
130         time_t current_mtime;
131         struct stat dir_stat;
132
133         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
134                                 return NULL);
135
136         if (fstat(data->fd, &dir_stat) == -1) {
137                 return NULL;
138         }
139
140         current_mtime = dir_stat.st_mtime;
141
142         /* throw away cache and re-read the directory if we've changed */
143         if (current_mtime > data->mtime) {
144                 open_and_sort_dir(handle);
145         }
146
147         if (data->pos >= data->number_of_entries) {
148                 return NULL;
149         }
150
151         return &data->directory_list[data->pos++];
152 }
153
154 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
155                             long offset)
156 {
157         struct dirsort_privates *data = NULL;
158         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
159
160         data->pos = offset;
161 }
162
163 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
164 {
165         struct dirsort_privates *data = NULL;
166         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
167                                 return -1);
168
169         return data->pos;
170 }
171
172 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
173 {
174         struct dirsort_privates *data = NULL;
175         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
176
177         data->pos = 0;
178 }
179
180 static struct vfs_fn_pointers vfs_dirsort_fns = {
181         .opendir = dirsort_opendir,
182         .readdir = dirsort_readdir,
183         .seekdir = dirsort_seekdir,
184         .telldir = dirsort_telldir,
185         .rewind_dir = dirsort_rewinddir,
186 };
187
188 NTSTATUS vfs_dirsort_init(void)
189 {
190         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
191                                 &vfs_dirsort_fns);
192 }