#include "includes.h"
#include "system/filesys.h"
#include "smbd/smbd.h"
+#include "source3/smbd/dir.h"
/*
/*
fsync a directory by name
*/
-static void syncops_sync_directory(const char *dname)
+static void syncops_sync_directory(connection_struct *conn,
+ char *dname)
{
-#ifdef O_DIRECTORY
- int fd = open(dname, O_DIRECTORY|O_RDONLY);
- if (fd != -1) {
- fsync(fd);
- close(fd);
- }
-#else
- DIR *d = opendir(dname);
- if (d != NULL) {
- fsync(dirfd(d));
- closedir(d);
+ struct smb_Dir *dir_hnd = NULL;
+ struct files_struct *dirfsp = NULL;
+ struct smb_filename smb_dname = { .base_name = dname };
+ NTSTATUS status;
+
+ status = OpenDir(talloc_tos(),
+ conn,
+ &smb_dname,
+ "*",
+ 0,
+ &dir_hnd);
+ if (!NT_STATUS_IS_OK(status)) {
+ errno = map_errno_from_nt_status(status);
+ return;
}
-#endif
+
+ dirfsp = dir_hnd_fetch_fsp(dir_hnd);
+
+ smb_vfs_fsync_sync(dirfsp);
+
+ TALLOC_FREE(dir_hnd);
}
/*
sync two meta data changes for 2 names
*/
-static void syncops_two_names(const char *name1, const char *name2)
+static void syncops_two_names(connection_struct *conn,
+ const struct smb_filename *name1,
+ const struct smb_filename *name2)
{
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
char *parent1, *parent2;
- parent1 = parent_dir(tmp_ctx, name1);
- parent2 = parent_dir(tmp_ctx, name2);
+ parent1 = parent_dir(tmp_ctx, name1->base_name);
+ parent2 = parent_dir(tmp_ctx, name2->base_name);
if (!parent1 || !parent2) {
talloc_free(tmp_ctx);
return;
}
- syncops_sync_directory(parent1);
+ syncops_sync_directory(conn, parent1);
if (strcmp(parent1, parent2) != 0) {
- syncops_sync_directory(parent2);
+ syncops_sync_directory(conn, parent2);
}
talloc_free(tmp_ctx);
}
/*
sync two meta data changes for 1 names
*/
-static void syncops_smb_fname(const struct smb_filename *smb_fname)
+static void syncops_smb_fname(connection_struct *conn,
+ const struct smb_filename *smb_fname)
{
char *parent = NULL;
if (smb_fname != NULL) {
parent = parent_dir(NULL, smb_fname->base_name);
if (parent != NULL) {
- syncops_sync_directory(parent);
+ syncops_sync_directory(conn, parent);
talloc_free(parent);
}
}
{
int ret;
+ struct smb_filename *full_fname_src = NULL;
+ struct smb_filename *full_fname_dst = NULL;
struct syncops_config_data *config;
SMB_VFS_HANDLE_GET_DATA(handle, config,
smb_fname_src,
dstfsp,
smb_fname_dst);
- if (ret == 0 && config->onmeta && !config->disable) {
- syncops_two_names(smb_fname_src->base_name,
- smb_fname_dst->base_name);
+ if (ret == -1) {
+ return ret;
+ }
+ if (config->disable) {
+ return ret;
+ }
+ if (!config->onmeta) {
+ return ret;
+ }
+
+ full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
+ srcfsp,
+ smb_fname_src);
+ if (full_fname_src == NULL) {
+ errno = ENOMEM;
+ return ret;
+ }
+ full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
+ dstfsp,
+ smb_fname_dst);
+ if (full_fname_dst == NULL) {
+ TALLOC_FREE(full_fname_src);
+ errno = ENOMEM;
+ return ret;
}
+ syncops_two_names(handle->conn,
+ full_fname_src,
+ full_fname_dst);
+ TALLOC_FREE(full_fname_src);
+ TALLOC_FREE(full_fname_dst);
return ret;
}
#define SYNCOPS_NEXT_SMB_FNAME(op, fname, args) do { \
int ret; \
+ struct smb_filename *full_fname = NULL; \
struct syncops_config_data *config; \
SMB_VFS_HANDLE_GET_DATA(handle, config, \
struct syncops_config_data, \
return -1); \
ret = SMB_VFS_NEXT_ ## op args; \
- if (ret == 0 \
- && config->onmeta && !config->disable \
- && fname) syncops_smb_fname(fname); \
+ if (ret != 0) { \
+ return ret; \
+ } \
+ if (config->disable) { \
+ return ret; \
+ } \
+ if (!config->onmeta) { \
+ return ret; \
+ } \
+ full_fname = full_path_from_dirfsp_atname(talloc_tos(), \
+ dirfsp, \
+ smb_fname); \
+ if (full_fname == NULL) { \
+ return ret; \
+ } \
+ syncops_smb_fname(dirfsp->conn, full_fname); \
+ TALLOC_FREE(full_fname); \
return ret; \
} while (0)
-static int syncops_symlink(vfs_handle_struct *handle,
- const char *link_contents,
- const struct smb_filename *new_smb_fname)
+static int syncops_symlinkat(vfs_handle_struct *handle,
+ const struct smb_filename *link_contents,
+ struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname)
{
- int ret;
- struct syncops_config_data *config;
-
- SMB_VFS_HANDLE_GET_DATA(handle, config,
- struct syncops_config_data,
- return -1);
-
- ret = SMB_VFS_NEXT_SYMLINK(handle, link_contents, new_smb_fname);
- if (ret == 0 && config->onmeta && !config->disable) {
- syncops_two_names(link_contents,
- new_smb_fname->base_name);
- }
- return ret;
+ SYNCOPS_NEXT_SMB_FNAME(SYMLINKAT,
+ smb_fname,
+ (handle,
+ link_contents,
+ dirfsp,
+ smb_fname));
}
static int syncops_linkat(vfs_handle_struct *handle,
{
int ret;
struct syncops_config_data *config;
+ struct smb_filename *old_full_fname = NULL;
+ struct smb_filename *new_full_fname = NULL;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct syncops_config_data,
return -1);
- SMB_ASSERT(srcfsp == srcfsp->conn->cwd_fsp);
- SMB_ASSERT(dstfsp == dstfsp->conn->cwd_fsp);
-
ret = SMB_VFS_NEXT_LINKAT(handle,
srcfsp,
old_smb_fname,
new_smb_fname,
flags);
- if (ret == 0 && config->onmeta && !config->disable) {
- syncops_two_names(old_smb_fname->base_name,
- new_smb_fname->base_name);
+ if (ret == -1) {
+ return ret;
}
+ if (config->disable) {
+ return ret;
+ }
+ if (!config->onmeta) {
+ return ret;
+ }
+
+ old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
+ srcfsp,
+ old_smb_fname);
+ if (old_full_fname == NULL) {
+ return ret;
+ }
+ new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
+ dstfsp,
+ new_smb_fname);
+ if (new_full_fname == NULL) {
+ TALLOC_FREE(old_full_fname);
+ return ret;
+ }
+ syncops_two_names(handle->conn,
+ old_full_fname,
+ new_full_fname);
+ TALLOC_FREE(old_full_fname);
+ TALLOC_FREE(new_full_fname);
return ret;
}
-
-static int syncops_open(vfs_handle_struct *handle,
- struct smb_filename *smb_fname, files_struct *fsp,
- int flags, mode_t mode)
+static int syncops_openat(struct vfs_handle_struct *handle,
+ const struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ struct files_struct *fsp,
+ const struct vfs_open_how *how)
{
- SYNCOPS_NEXT_SMB_FNAME(OPEN, (flags&O_CREAT?smb_fname:NULL),
- (handle, smb_fname, fsp, flags, mode));
+ SYNCOPS_NEXT_SMB_FNAME(OPENAT, (how->flags & O_CREAT ? smb_fname : NULL),
+ (handle, dirfsp, smb_fname, fsp, how));
}
-static int syncops_unlink(vfs_handle_struct *handle,
- const struct smb_filename *smb_fname)
+static int syncops_unlinkat(vfs_handle_struct *handle,
+ files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ int flags)
{
- SYNCOPS_NEXT_SMB_FNAME(UNLINK, smb_fname, (handle, smb_fname));
+ SYNCOPS_NEXT_SMB_FNAME(UNLINKAT,
+ smb_fname,
+ (handle,
+ dirfsp,
+ smb_fname,
+ flags));
}
-static int syncops_mknod(vfs_handle_struct *handle,
+static int syncops_mknodat(vfs_handle_struct *handle,
+ files_struct *dirfsp,
const struct smb_filename *smb_fname,
mode_t mode,
SMB_DEV_T dev)
{
- SYNCOPS_NEXT_SMB_FNAME(MKNOD,
- smb_fname, (handle, smb_fname, mode, dev));
+ SYNCOPS_NEXT_SMB_FNAME(MKNODAT,
+ smb_fname,
+ (handle,
+ dirfsp,
+ smb_fname,
+ mode,
+ dev));
}
-static int syncops_mkdir(vfs_handle_struct *handle,
+static int syncops_mkdirat(vfs_handle_struct *handle,
+ struct files_struct *dirfsp,
const struct smb_filename *smb_fname,
mode_t mode)
{
- SYNCOPS_NEXT_SMB_FNAME(MKDIR, smb_fname, (handle, smb_fname, mode));
-}
-
-static int syncops_rmdir(vfs_handle_struct *handle,
- const struct smb_filename *smb_fname)
-{
- SYNCOPS_NEXT_SMB_FNAME(RMDIR, smb_fname, (handle, smb_fname));
+ SYNCOPS_NEXT_SMB_FNAME(MKDIRAT,
+ full_fname,
+ (handle,
+ dirfsp,
+ smb_fname,
+ mode));
}
/* close needs to be handled specially */
struct syncops_config_data,
return -1);
- if (fsp->can_write && config->onclose) {
+ if (fsp->fsp_flags.can_write && config->onclose) {
/* ideally we'd only do this if we have written some
data, but there is no flag for that in fsp yet. */
- fsync(fsp->fh->fd);
+ fsync(fsp_get_io_fd(fsp));
}
return SMB_VFS_NEXT_CLOSE(handle, fsp);
}
static struct vfs_fn_pointers vfs_syncops_fns = {
.connect_fn = syncops_connect,
- .mkdir_fn = syncops_mkdir,
- .rmdir_fn = syncops_rmdir,
- .open_fn = syncops_open,
+ .mkdirat_fn = syncops_mkdirat,
+ .openat_fn = syncops_openat,
.renameat_fn = syncops_renameat,
- .unlink_fn = syncops_unlink,
- .symlink_fn = syncops_symlink,
+ .unlinkat_fn = syncops_unlinkat,
+ .symlinkat_fn = syncops_symlinkat,
.linkat_fn = syncops_linkat,
- .mknod_fn = syncops_mknod,
+ .mknodat_fn = syncops_mknodat,
.close_fn = syncops_close,
};