}
struct dirsort_privates {
+ struct dirsort_privates *prev, *next;
long pos;
struct dirent *directory_list;
unsigned int number_of_entries;
struct smb_filename *smb_fname; /* If open via OPENDIR */
};
-static void free_dirsort_privates(void **datap) {
- TALLOC_FREE(*datap);
-}
-
static bool get_sorted_dir_mtime(vfs_handle_struct *handle,
struct dirsort_privates *data,
struct timespec *ret_mtime)
const char *fname, const char *mask,
uint32 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 = talloc_zero(handle->conn, struct dirsort_privates);
if (!data) {
return NULL;
}
- SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
+ /* 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;
const char *mask,
uint32 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 = talloc_zero(handle->conn, struct dirsort_privates);
if (!data) {
return NULL;
}
- SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
+ /* 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;
SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
return NULL);
+ while(data && (data->source_directory != dirp)) {
+ data = data->next;
+ }
+ if (data == NULL) {
+ return NULL;
+ }
+
if (get_sorted_dir_mtime(handle, data, ¤t_mtime) == false) {
return NULL;
}
/* throw away cache and re-read the directory if we've changed */
- if (timespec_compare(¤t_mtime, &data->mtime) > 1) {
+ if (timespec_compare(¤t_mtime, &data->mtime)) {
+ SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
open_and_sort_dir(handle, data);
}
static void dirsort_seekdir(vfs_handle_struct *handle, DIR *dirp,
long offset)
{
+ struct timespec current_mtime;
struct dirsort_privates *data = NULL;
+
SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
+ /* Find the entry holding dirp. */
+ while(data && (data->source_directory != dirp)) {
+ data = data->next;
+ }
+ if (data == NULL) {
+ return;
+ }
+ if (offset > data->number_of_entries) {
+ return;
+ }
data->pos = offset;
+
+ if (get_sorted_dir_mtime(handle, data, ¤t_mtime) == false) {
+ return;
+ }
+
+ if (timespec_compare(¤t_mtime, &data->mtime)) {
+ /* Directory changed. We must re-read the
+ cache and search for the name that was
+ previously stored at the offset being
+ requested, otherwise after the re-sort
+ we will point to the wrong entry. The
+ OS/2 incremental delete code relies on
+ this. */
+ unsigned int i;
+ char *wanted_name = talloc_strdup(handle->conn,
+ data->directory_list[offset].d_name);
+ if (wanted_name == NULL) {
+ return;
+ }
+ SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
+ open_and_sort_dir(handle, data);
+ /* Now search for where we were. */
+ data->pos = 0;
+ for (i = 0; i < data->number_of_entries; i++) {
+ if(strcmp(wanted_name, data->directory_list[i].d_name) == 0) {
+ data->pos = i;
+ break;
+ }
+ }
+ TALLOC_FREE(wanted_name);
+ }
}
static long dirsort_telldir(vfs_handle_struct *handle, DIR *dirp)
SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates,
return -1);
+ /* Find the entry holding dirp. */
+ while(data && (data->source_directory != dirp)) {
+ data = data->next;
+ }
+ if (data == NULL) {
+ return -1;
+ }
return data->pos;
}
struct dirsort_privates *data = NULL;
SMB_VFS_HANDLE_GET_DATA(handle, data, struct dirsort_privates, return);
+ /* Find the entry holding dirp. */
+ while(data && (data->source_directory != dirp)) {
+ data = data->next;
+ }
+ if (data == NULL) {
+ return;
+ }
data->pos = 0;
}
+static int dirsort_closedir(vfs_handle_struct *handle, DIR *dirp)
+{
+ struct dirsort_privates *list_head = NULL;
+ struct dirsort_privates *data = NULL;
+ int ret;
+
+ 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;
+}
+
static struct vfs_fn_pointers vfs_dirsort_fns = {
.opendir_fn = dirsort_opendir,
.fdopendir_fn = dirsort_fdopendir,
.seekdir_fn = dirsort_seekdir,
.telldir_fn = dirsort_telldir,
.rewind_dir_fn = dirsort_rewinddir,
+ .closedir_fn = dirsort_closedir,
};
NTSTATUS vfs_dirsort_init(void)