smbd: Fix a typo in a few places
[samba.git] / source3 / modules / vfs_dirsort.c
index f6fc9256d07bba844d1a16ed2a4a01446b43ad56..c4baf819b0384cee94d5099dad7e730a1a002bf1 100644 (file)
  */
 
 #include "includes.h"
+#include "smbd/smbd.h"
+#include "system/filesys.h"
 
-static int compare_dirent (const void *a, const void *b) {
-       const SMB_STRUCT_DIRENT *da = (const SMB_STRUCT_DIRENT *) a;
-       const SMB_STRUCT_DIRENT *db = (const SMB_STRUCT_DIRENT *) b;
-       return StrCaseCmp(da->d_name, db->d_name);
+static int compare_dirent (const struct dirent *da, const struct dirent *db)
+{
+       return strcasecmp_m(da->d_name, db->d_name);
 }
 
 struct dirsort_privates {
+       struct dirsort_privates *prev, *next;
        long pos;
-       SMB_STRUCT_DIRENT *directory_list;
-       long number_of_entries;
-       time_t mtime;
-       SMB_STRUCT_DIR *source_directory;
-       int fd;
+       struct dirent *directory_list;
+       unsigned int number_of_entries;
+       struct timespec mtime;
+       DIR *source_directory;
+       files_struct *fsp; /* If open via FDOPENDIR. */
+       struct smb_filename *smb_fname; /* If open via OPENDIR */
 };
 
-static void free_dirsort_privates(void **datap) {
-       struct dirsort_privates *data = (struct dirsort_privates *) *datap;
-       SAFE_FREE(data->directory_list);
-       SAFE_FREE(data);
-       *datap = NULL;
+static bool get_sorted_dir_mtime(vfs_handle_struct *handle,
+                               struct dirsort_privates *data,
+                               struct timespec *ret_mtime)
+{
+       int ret;
+       struct timespec mtime;
+       NTSTATUS status;
+
+       if (data->fsp) {
+               status = vfs_stat_fsp(data->fsp);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return false;
+               }
+               mtime = data->fsp->fsp_name->st.st_ex_mtime;
+       } else {
+               ret = SMB_VFS_STAT(handle->conn, data->smb_fname);
+               if (ret == -1) {
+                       return false;
+               }
+               mtime = data->smb_fname->st.st_ex_mtime;
+       }
+
+       *ret_mtime = mtime;
 
-       return;
+       return true;
 }
 
-static void open_and_sort_dir (vfs_handle_struct *handle)
+static bool open_and_sort_dir(vfs_handle_struct *handle,
+                               struct dirsort_privates *data)
 {
-       SMB_STRUCT_DIRENT *dp;
-       struct stat dir_stat;
-       long current_pos;
-       struct dirsort_privates *data = NULL;
-
-       SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
+       uint32_t total_count = 0;
+       /* This should be enough for most use cases */
+       uint32_t dirent_allocated = 64;
+       struct dirent *dp;
 
        data->number_of_entries = 0;
 
-       if (fstat(data->fd, &dir_stat) == 0) {
-               data->mtime = dir_stat.st_mtime;
+       if (get_sorted_dir_mtime(handle, data, &data->mtime) == false) {
+               return false;
        }
 
-       while (SMB_VFS_NEXT_READDIR(handle, data->source_directory, NULL)
-              != NULL) {
-               data->number_of_entries++;
+       dp = SMB_VFS_NEXT_READDIR(handle, data->fsp, data->source_directory);
+       if (dp == NULL) {
+               return false;
        }
 
-       /* Open the underlying directory and count the number of entries
-          Skip back to the beginning as we'll read it again */
-       SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
-
        /* Set up an array and read the directory entries into it */
-       SAFE_FREE(data->directory_list); /* destroy previous cache if needed */
-       data->directory_list = (SMB_STRUCT_DIRENT *)SMB_MALLOC(
-               data->number_of_entries * sizeof(SMB_STRUCT_DIRENT));
-       current_pos = data->pos;
-       data->pos = 0;
-       while ((dp = SMB_VFS_NEXT_READDIR(handle, data->source_directory,
-                                         NULL)) != NULL) {
-               data->directory_list[data->pos++] = *dp;
+       TALLOC_FREE(data->directory_list); /* destroy previous cache if needed */
+       data->directory_list = talloc_zero_array(data,
+                                                struct dirent,
+                                                dirent_allocated);
+       if (data->directory_list == NULL) {
+               return false;
        }
 
+       do {
+               if (total_count >= dirent_allocated) {
+                       struct dirent *dlist;
+
+                       /*
+                        * Be memory friendly.
+                        *
+                        * We should not double the amount of memory. With a lot
+                        * of files we reach easily 50MB, and doubling will
+                        * get much bigger just for a few files more.
+                        *
+                        * For 200k files this means 50 memory reallocations.
+                        */
+                       dirent_allocated += 4096;
+
+                       dlist = talloc_realloc(data,
+                                              data->directory_list,
+                                              struct dirent,
+                                              dirent_allocated);
+                       if (dlist == NULL) {
+                               break;
+                       }
+                       data->directory_list = dlist;
+               }
+               data->directory_list[total_count] = *dp;
+
+               total_count++;
+               dp = SMB_VFS_NEXT_READDIR(handle,
+                                         data->fsp,
+                                         data->source_directory);
+       } while (dp != NULL);
+
+       data->number_of_entries = total_count;
+
        /* Sort the directory entries by name */
-       data->pos = current_pos;
-       qsort(data->directory_list, data->number_of_entries,
-             sizeof(SMB_STRUCT_DIRENT), compare_dirent);
+       TYPESAFE_QSORT(data->directory_list, data->number_of_entries, compare_dirent);
+       return true;
 }
 
-static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
-                                      const char *fname, const char *mask,
-                                      uint32 attr)
+static DIR *dirsort_fdopendir(vfs_handle_struct *handle,
+                                       files_struct *fsp,
+                                       const char *mask,
+                                       uint32_t attr)
 {
+       struct dirsort_privates *list_head = NULL;
        struct dirsort_privates *data = NULL;
 
+       if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
+               /* Find the list head of all open directories. */
+               SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates,
+                               return NULL);
+       }
+
        /* set up our private data about this directory */
-       data = (struct dirsort_privates *)SMB_MALLOC(
-               sizeof(struct dirsort_privates));
+       data = talloc_zero(handle->conn, struct dirsort_privates);
+       if (!data) {
+               return NULL;
+       }
 
-       data->directory_list = NULL;
-       data->pos = 0;
+       data->fsp = fsp;
 
        /* Open the underlying directory and count the number of entries */
-       data->source_directory = SMB_VFS_NEXT_OPENDIR(handle, fname, mask,
+       data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
                                                      attr);
 
-       data->fd = dirfd(data->source_directory);
+       if (data->source_directory == NULL) {
+               TALLOC_FREE(data);
+               return NULL;
+       }
 
-       SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
-                               struct dirsort_privates, return NULL);
+       if (!open_and_sort_dir(handle, data)) {
+               SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
+               TALLOC_FREE(data);
+               /* fd is now closed. */
+               fsp_set_fd(fsp, -1);
+               return NULL;
+       }
 
-       open_and_sort_dir(handle);
+       /* Add to the private list of all open directories. */
+       DLIST_ADD(list_head, data);
+       SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
+                               struct dirsort_privates, return NULL);
 
        return data->source_directory;
 }
 
-static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
-                                         SMB_STRUCT_DIR *dirp,
-                                         SMB_STRUCT_STAT *sbuf)
+static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
+                                     struct files_struct *dirfsp,
+                                     DIR *dirp)
 {
        struct dirsort_privates *data = NULL;
-       time_t current_mtime;
-       struct stat dir_stat;
+       struct timespec current_mtime;
 
        SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
                                return NULL);
 
-       if (fstat(data->fd, &dir_stat) == -1) {
+       while(data && (data->source_directory != dirp)) {
+               data = data->next;
+       }
+       if (data == NULL) {
                return NULL;
        }
 
-       current_mtime = dir_stat.st_mtime;
+       if (get_sorted_dir_mtime(handle, data, &current_mtime) == false) {
+               return NULL;
+       }
 
        /* throw away cache and re-read the directory if we've changed */
-       if (current_mtime > data->mtime) {
-               open_and_sort_dir(handle);
+       if (timespec_compare(&current_mtime, &data->mtime)) {
+               SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
+               open_and_sort_dir(handle, data);
        }
 
        if (data->pos >= data->number_of_entries) {
@@ -141,55 +213,55 @@ static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
        return &data->directory_list[data->pos++];
 }
 
-static void dirsort_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
-                           long offset)
+static void dirsort_rewinddir(vfs_handle_struct *handle, DIR *dirp)
 {
        struct dirsort_privates *data = NULL;
        SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
 
-       data->pos = offset;
-}
-
-static long dirsort_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
-{
-       struct dirsort_privates *data = NULL;
-       SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
-                               return -1);
-
-       return data->pos;
+       /* Find the entry holding dirp. */
+       while(data && (data->source_directory != dirp)) {
+               data = data->next;
+       }
+       if (data == NULL) {
+               return;
+       }
+       data->pos = 0;
 }
 
-static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
+static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
 {
+       struct dirsort_privates *list_head = NULL;
        struct dirsort_privates *data = NULL;
-       SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
+       int ret;
 
-       data->pos = 0;
+       SMB_VFS_HANDLE_GET_DATA(handle, list_head, struct dirsort_privates, return -1);
+       /* Find the entry holding dirp. */
+       for(data = list_head; data && (data->source_directory != dirp); data = data->next) {
+               ;
+       }
+       if (data == NULL) {
+               return -1;
+       }
+       /* Remove from the list and re-store the list head. */
+       DLIST_REMOVE(list_head, data);
+       SMB_VFS_HANDLE_SET_DATA(handle, list_head, NULL,
+                               struct dirsort_privates, return -1);
+
+       ret = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
+       TALLOC_FREE(data);
+       return ret;
 }
 
-/* VFS operations structure */
-
-static vfs_op_tuple dirsort_op_tuples[] = {
-
-    /* Directory operations */
-
-    {SMB_VFS_OP(dirsort_opendir),           SMB_VFS_OP_OPENDIR,
-     SMB_VFS_LAYER_TRANSPARENT},
-    {SMB_VFS_OP(dirsort_readdir),           SMB_VFS_OP_READDIR,
-     SMB_VFS_LAYER_TRANSPARENT},
-    {SMB_VFS_OP(dirsort_seekdir),           SMB_VFS_OP_SEEKDIR,
-     SMB_VFS_LAYER_TRANSPARENT},
-    {SMB_VFS_OP(dirsort_telldir),           SMB_VFS_OP_TELLDIR,
-     SMB_VFS_LAYER_TRANSPARENT},
-    {SMB_VFS_OP(dirsort_rewinddir),         SMB_VFS_OP_REWINDDIR,
-     SMB_VFS_LAYER_TRANSPARENT},
-
-    {NULL,                                  SMB_VFS_OP_NOOP,
-     SMB_VFS_LAYER_NOOP}
+static struct vfs_fn_pointers vfs_dirsort_fns = {
+       .fdopendir_fn = dirsort_fdopendir,
+       .readdir_fn = dirsort_readdir,
+       .rewind_dir_fn = dirsort_rewinddir,
+       .closedir_fn = dirsort_closedir,
 };
 
-NTSTATUS vfs_dirsort_init(void)
+static_decl_vfs;
+NTSTATUS vfs_dirsort_init(TALLOC_CTX *ctx)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dirsort",
-                               dirsort_op_tuples);
+                               &vfs_dirsort_fns);
 }