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/>.
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
{
31 SMB_STRUCT_DIRENT
*directory_list
;
32 long number_of_entries
;
34 SMB_STRUCT_DIR
*source_directory
;
38 static void free_dirsort_privates(void **datap
) {
39 struct dirsort_privates
*data
= (struct dirsort_privates
*) *datap
;
40 SAFE_FREE(data
->directory_list
);
47 static void open_and_sort_dir (vfs_handle_struct
*handle
)
49 SMB_STRUCT_DIRENT
*dp
;
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
)
64 data
->number_of_entries
++;
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
);
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 current_pos
= data
->pos
;
77 while ((dp
= SMB_VFS_NEXT_READDIR(handle
, data
->source_directory
,
79 data
->directory_list
[data
->pos
++] = *dp
;
82 /* Sort the directory entries by name */
83 data
->pos
= current_pos
;
84 qsort(data
->directory_list
, data
->number_of_entries
,
85 sizeof(SMB_STRUCT_DIRENT
), compare_dirent
);
88 static SMB_STRUCT_DIR
*dirsort_opendir(vfs_handle_struct
*handle
,
89 const char *fname
, const char *mask
,
92 struct dirsort_privates
*data
= NULL
;
94 /* set up our private data about this directory */
95 data
= (struct dirsort_privates
*)SMB_MALLOC(
96 sizeof(struct dirsort_privates
));
98 data
->directory_list
= NULL
;
101 /* Open the underlying directory and count the number of entries */
102 data
->source_directory
= SMB_VFS_NEXT_OPENDIR(handle
, fname
, mask
,
105 data
->fd
= dirfd(data
->source_directory
);
107 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_dirsort_privates
,
108 struct dirsort_privates
, return NULL
);
110 open_and_sort_dir(handle
);
112 return data
->source_directory
;
115 static SMB_STRUCT_DIRENT
*dirsort_readdir(vfs_handle_struct
*handle
,
116 SMB_STRUCT_DIR
*dirp
)
118 struct dirsort_privates
*data
= NULL
;
119 time_t current_mtime
;
120 struct stat dir_stat
;
122 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
,
125 if (fstat(data
->fd
, &dir_stat
) == -1) {
129 current_mtime
= dir_stat
.st_mtime
;
131 /* throw away cache and re-read the directory if we've changed */
132 if (current_mtime
> data
->mtime
) {
133 open_and_sort_dir(handle
);
136 if (data
->pos
>= data
->number_of_entries
) {
140 return &data
->directory_list
[data
->pos
++];
143 static void dirsort_seekdir(vfs_handle_struct
*handle
, SMB_STRUCT_DIR
*dirp
,
146 struct dirsort_privates
*data
= NULL
;
147 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
, return);
152 static long dirsort_telldir(vfs_handle_struct
*handle
, SMB_STRUCT_DIR
*dirp
)
154 struct dirsort_privates
*data
= NULL
;
155 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
,
161 static void dirsort_rewinddir(vfs_handle_struct
*handle
, SMB_STRUCT_DIR
*dirp
)
163 struct dirsort_privates
*data
= NULL
;
164 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct dirsort_privates
, return);
169 /* VFS operations structure */
171 static vfs_op_tuple dirsort_op_tuples
[] = {
173 /* Directory operations */
175 {SMB_VFS_OP(dirsort_opendir
), SMB_VFS_OP_OPENDIR
,
176 SMB_VFS_LAYER_TRANSPARENT
},
177 {SMB_VFS_OP(dirsort_readdir
), SMB_VFS_OP_READDIR
,
178 SMB_VFS_LAYER_TRANSPARENT
},
179 {SMB_VFS_OP(dirsort_seekdir
), SMB_VFS_OP_SEEKDIR
,
180 SMB_VFS_LAYER_TRANSPARENT
},
181 {SMB_VFS_OP(dirsort_telldir
), SMB_VFS_OP_TELLDIR
,
182 SMB_VFS_LAYER_TRANSPARENT
},
183 {SMB_VFS_OP(dirsort_rewinddir
), SMB_VFS_OP_REWINDDIR
,
184 SMB_VFS_LAYER_TRANSPARENT
},
186 {NULL
, SMB_VFS_OP_NOOP
,
190 NTSTATUS
vfs_dirsort_init(void)
192 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "dirsort",