/source3/modules/vfs_dirsort.c

https://github.com/theresahalloran/samba-tool-group-doc · C · 237 lines · 161 code · 47 blank · 29 comment · 17 complexity · 692056fe56ff288e8a179c68dccdab51 MD5 · raw file

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