Fix bug #7669.
[Samba.git] / source / modules / vfs_dirsort.c
blobd7f2b03a3222a09c197abf13125fbd7d1ae72bc8
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"
23 static int compare_dirent (const void *a, const void *b) {
24 const SMB_STRUCT_DIRENT *da = (const SMB_STRUCT_DIRENT *) a;
25 const SMB_STRUCT_DIRENT *db = (const SMB_STRUCT_DIRENT *) b;
26 return StrCaseCmp(da->d_name, db->d_name);
29 struct dirsort_privates {
30 long pos;
31 SMB_STRUCT_DIRENT *directory_list;
32 long number_of_entries;
33 time_t mtime;
34 SMB_STRUCT_DIR *source_directory;
35 int fd;
38 static void free_dirsort_privates(void **datap) {
39 struct dirsort_privates *data = (struct dirsort_privates *) *datap;
40 SAFE_FREE(data->directory_list);
41 SAFE_FREE(data);
42 *datap = NULL;
44 return;
47 static void open_and_sort_dir (vfs_handle_struct *handle)
49 SMB_STRUCT_DIRENT *dp;
50 struct stat dir_stat;
51 long current_pos;
52 struct dirsort_privates *data = NULL;
54 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
56 data->number_of_entries = 0;
58 if (fstat(data->fd, &dir_stat) == 0) {
59 data->mtime = dir_stat.st_mtime;
62 while (SMB_VFS_NEXT_READDIR(handle, data->source_directory) != NULL) {
63 data->number_of_entries++;
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);
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) {
78 data->directory_list[data->pos++] = *dp;
81 /* Sort the directory entries by name */
82 data->pos = current_pos;
83 qsort(data->directory_list, data->number_of_entries,
84 sizeof(SMB_STRUCT_DIRENT), compare_dirent);
87 static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
88 const char *fname, const char *mask,
89 uint32 attr)
91 struct dirsort_privates *data = NULL;
93 /* set up our private data about this directory */
94 data = (struct dirsort_privates *)SMB_MALLOC(
95 sizeof(struct dirsort_privates));
97 data->directory_list = NULL;
98 data->pos = 0;
100 /* Open the underlying directory and count the number of entries */
101 data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
102 attr);
104 data->fd = dirfd(data->source_directory);
106 SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
107 struct dirsort_privates, return NULL);
109 open_and_sort_dir(handle);
111 return data->source_directory;
114 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
115 SMB_STRUCT_DIR *dirp)
117 struct dirsort_privates *data = NULL;
118 time_t current_mtime;
119 struct stat dir_stat;
121 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
122 return NULL);
124 if (fstat(data->fd, &dir_stat) == -1) {
125 return NULL;
128 current_mtime = dir_stat.st_mtime;
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);
135 if (data->pos >= data->number_of_entries) {
136 return NULL;
139 return &data->directory_list[data->pos++];
142 static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
143 long offset)
145 struct dirsort_privates *data = NULL;
146 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
148 data->pos = offset;
151 static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
153 struct dirsort_privates *data = NULL;
154 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
155 return -1);
157 return data->pos;
160 static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
162 struct dirsort_privates *data = NULL;
163 SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
165 data->pos = 0;
168 /* VFS operations structure */
170 static vfs_op_tuple dirsort_op_tuples[] = {
172 /* Directory operations */
174 {SMB_VFS_OP(dirsort_opendir), SMB_VFS_OP_OPENDIR,
175 SMB_VFS_LAYER_TRANSPARENT},
176 {SMB_VFS_OP(dirsort_readdir), SMB_VFS_OP_READDIR,
177 SMB_VFS_LAYER_TRANSPARENT},
178 {SMB_VFS_OP(dirsort_seekdir), SMB_VFS_OP_SEEKDIR,
179 SMB_VFS_LAYER_TRANSPARENT},
180 {SMB_VFS_OP(dirsort_telldir), SMB_VFS_OP_TELLDIR,
181 SMB_VFS_LAYER_TRANSPARENT},
182 {SMB_VFS_OP(dirsort_rewinddir), SMB_VFS_OP_REWINDDIR,
183 SMB_VFS_LAYER_TRANSPARENT},
185 {NULL, SMB_VFS_OP_NOOP,
186 SMB_VFS_LAYER_NOOP}
189 NTSTATUS vfs_dirsort_init(void)
191 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
192 dirsort_op_tuples);