s3-vfs: use TYPESAFE_QSORT() in s3 VFS modules
[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
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 void 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, return);
54
55         data->number_of_entries = 0;
56
57         if (fstat(data->fd, &dir_stat) == 0) {
58                 data->mtime = dir_stat.st_mtime;
59         }
60
61         while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
62                != NULL) {
63                 data->number_of_entries++;
64         }
65
66         /* Open the underlying directory and count the number of entries
67            Skip back to the beginning as we'll read it again */
68         SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
69
70         /* Set up an array and read the directory entries into it */
71         SAFE_FREE(data->directory_list); /* destroy previous cache if needed */
72         data->directory_list = (SMB_STRUCT_DIRENT *)SMB_MALLOC(
73                 data->number_of_entries * sizeof(SMB_STRUCT_DIRENT));
74         current_pos = data->pos;
75         data->pos = 0;
76         while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
77                                           NULL)) != NULL) {
78                 data->directory_list[data->pos++] = *dp;
79         }
80
81         /* Sort the directory entries by name */
82         data->pos = current_pos;
83         TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
84 }
85
86 static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
87                                        const char *fname, const char *mask,
88                                        uint32 attr)
89 {
90         struct dirsort_privates *data = NULL;
91
92         /* set up our private data about this directory */
93         data = (struct dirsort_privates *)SMB_MALLOC(
94                 sizeof(struct dirsort_privates));
95
96         data->directory_list = NULL;
97         data->pos = 0;
98
99         /* Open the underlying directory and count the number of entries */
100         data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
101                                                       attr);
102
103         data->fd = dirfd(data->source_directory);
104
105         SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
106                                 struct dirsort_privates, return NULL);
107
108         open_and_sort_dir(handle);
109
110         return data->source_directory;
111 }
112
113 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
114                                           SMB_STRUCT_DIR *dirp,
115                                           SMB_STRUCT_STAT *sbuf)
116 {
117         struct dirsort_privates *data = NULL;
118         time_t current_mtime;
119         struct stat dir_stat;
120
121         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
122                                 return NULL);
123
124         if (fstat(data->fd, &dir_stat) == -1) {
125                 return NULL;
126         }
127
128         current_mtime = dir_stat.st_mtime;
129
130         /* throw away cache and re-read the directory if we've changed */
131         if (current_mtime > data->mtime) {
132                 open_and_sort_dir(handle);
133         }
134
135         if (data->pos >= data->number_of_entries) {
136                 return NULL;
137         }
138
139         return &data->directory_list[data->pos++];
140 }
141
142 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
143                             long offset)
144 {
145         struct dirsort_privates *data = NULL;
146         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
147
148         data->pos = offset;
149 }
150
151 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
152 {
153         struct dirsort_privates *data = NULL;
154         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
155                                 return -1);
156
157         return data->pos;
158 }
159
160 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
161 {
162         struct dirsort_privates *data = NULL;
163         SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
164
165         data->pos = 0;
166 }
167
168 static struct vfs_fn_pointers vfs_dirsort_fns = {
169         .opendir = dirsort_opendir,
170         .readdir = dirsort_readdir,
171         .seekdir = dirsort_seekdir,
172         .telldir = dirsort_telldir,
173         .rewind_dir = dirsort_rewinddir,
174 };
175
176 NTSTATUS vfs_dirsort_init(void)
177 {
178         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
179                                 &vfs_dirsort_fns);
180 }