s3: Plumb smb_filename through SMB_VFS_UNLINK
[nivanova/samba-autobuild/.git] / source3 / modules / onefs_streams.c
index 284e199fc5537a7d1b52b69aa9143cea341a4b53..c22fc711a0e74093a31a652d6da1e3c1081dd6a3 100644 (file)
@@ -54,6 +54,52 @@ NTSTATUS onefs_split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
        return NT_STATUS_OK;
 }
 
+NTSTATUS onefs_stream_prep_smb_fname(TALLOC_CTX *ctx,
+                                    const struct smb_filename *smb_fname_in,
+                                    struct smb_filename **smb_fname_out)
+{
+       char *stream_name = NULL;
+       NTSTATUS status;
+
+       /*
+        * Only attempt to strip off the trailing :$DATA if there is an actual
+        * stream there.  If it is the default stream, the smb_fname_out will
+        * just have a NULL stream so the base file is opened.
+        */
+       if (smb_fname_in->stream_name &&
+           !is_ntfs_default_stream_smb_fname(smb_fname_in)) {
+               char *str_tmp = smb_fname_in->stream_name;
+
+               /* First strip off the leading ':' */
+               if (str_tmp[0] == ':') {
+                       str_tmp++;
+               }
+
+               /* Create a new copy of the stream_name. */
+               stream_name = talloc_strdup(ctx, str_tmp);
+               if (stream_name == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               /* Strip off the :$DATA if one exists. */
+               str_tmp = strrchr_m(stream_name, ':');
+               if (str_tmp) {
+                       str_tmp[0] = '\0';
+               }
+       }
+
+       /*
+        * If there was a stream that wasn't the default stream the leading
+        * colon and trailing :$DATA has now been stripped off.  Create a new
+        * smb_filename to pass back.
+        */
+       status = create_synthetic_smb_fname(ctx, smb_fname_in->base_name,
+                                           stream_name, &smb_fname_in->st,
+                                           smb_fname_out);
+       TALLOC_FREE(stream_name);
+       return status;
+}
+
 int onefs_is_stream(const char *path, char **pbase, char **pstream,
                    bool *is_stream)
 {
@@ -149,68 +195,80 @@ static int get_stream_dir_fd(connection_struct *conn, const char *base,
        return dir_fd;
 }
 
-int onefs_rename(vfs_handle_struct *handle, const char *oldname,
-                const char *newname)
+int onefs_rename(vfs_handle_struct *handle,
+                const struct smb_filename *smb_fname_src,
+                const struct smb_filename *smb_fname_dst)
 {
-       TALLOC_CTX *frame = NULL;
-       int ret = -1;
-       int dir_fd = -1;
+       struct smb_filename *smb_fname_src_onefs = NULL;
+       struct smb_filename *smb_fname_dst_onefs = NULL;
+       NTSTATUS status;
        int saved_errno;
-       bool old_is_stream;
-       bool new_is_stream;
-       char *obase = NULL;
-       char *osname = NULL;
-       char *nbase = NULL;
-       char *nsname = NULL;
+       int dir_fd = -1;
+       int ret = -1;
 
        START_PROFILE(syscall_rename_at);
 
-       frame = talloc_stackframe();
-
-       ret = onefs_is_stream(oldname, &obase, &osname, &old_is_stream);
-       if (ret) {
-               END_PROFILE(syscall_rename_at);
-               return ret;
+       if (!is_ntfs_stream_smb_fname(smb_fname_src) &&
+           !is_ntfs_stream_smb_fname(smb_fname_dst)) {
+               ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
+                                         smb_fname_dst);
+               goto done;
        }
 
-       ret = onefs_is_stream(newname, &nbase, &nsname, &new_is_stream);
-       if (ret) {
-               END_PROFILE(syscall_rename_at);
-               return ret;
+       /* For now don't allow renames from or to the default stream. */
+       if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
+           is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
+               errno = ENOSYS;
+               goto done;
        }
 
-       if (!old_is_stream && !new_is_stream) {
-               ret = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
-               END_PROFILE(syscall_rename_at);
-               return ret;
+       /* prep stream smb_filename structs. */
+       status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_src,
+                                            &smb_fname_src_onefs);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
+               goto done;
+       }
+       status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_dst,
+                                            &smb_fname_dst_onefs);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
+               goto done;
        }
 
-       dir_fd = get_stream_dir_fd(handle->conn, obase, NULL);
+       dir_fd = get_stream_dir_fd(handle->conn, smb_fname_src->base_name,
+                                  NULL);
        if (dir_fd < -1) {
                goto done;
        }
 
-       DEBUG(8,("onefs_rename called for %s : %s  => %s : %s\n",
-               obase, osname,  nbase, nsname));
+       DEBUG(8, ("onefs_rename called for %s => %s\n",
+                 smb_fname_str_dbg(smb_fname_src_onefs),
+                 smb_fname_str_dbg(smb_fname_dst_onefs)));
 
        /* Handle rename of stream to default stream specially. */
-       if (nsname == NULL) {
-               ret = enc_renameat(dir_fd, osname, ENC_DEFAULT, AT_FDCWD,
-                                  nbase, ENC_DEFAULT);
+       if (smb_fname_dst_onefs->stream_name == NULL) {
+               ret = enc_renameat(dir_fd, smb_fname_src_onefs->stream_name,
+                                  ENC_DEFAULT, AT_FDCWD,
+                                  smb_fname_dst_onefs->base_name,
+                                  ENC_DEFAULT);
        } else {
-               ret = enc_renameat(dir_fd, osname, ENC_DEFAULT, dir_fd, nsname,
+               ret = enc_renameat(dir_fd, smb_fname_src_onefs->stream_name,
+                                  ENC_DEFAULT, dir_fd,
+                                  smb_fname_dst_onefs->stream_name,
                                   ENC_DEFAULT);
        }
 
  done:
        END_PROFILE(syscall_rename_at);
+       TALLOC_FREE(smb_fname_src_onefs);
+       TALLOC_FREE(smb_fname_dst_onefs);
 
        saved_errno = errno;
        if (dir_fd >= 0) {
                close(dir_fd);
        }
        errno = saved_errno;
-       TALLOC_FREE(frame);
        return ret;
 }
 
@@ -312,28 +370,37 @@ static int stat_stream(struct connection_struct *conn, const char *base,
        return ret;
 }
 
-int onefs_stat(vfs_handle_struct *handle, const char *path,
-              SMB_STRUCT_STAT *sbuf)
+int onefs_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
 {
+       struct smb_filename *smb_fname_onefs = NULL;
+       NTSTATUS status;
        int ret;
-       bool is_stream;
-       char *base = NULL;
-       char *stream = NULL;
 
-       ret = onefs_is_stream(path, &base, &stream, &is_stream);
-       if (ret)
-               return ret;
+       status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
+                                            &smb_fname_onefs);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
+               return -1;
+       }
 
-       if (!is_stream) {
-               ret = onefs_sys_stat(path, sbuf);
-       } else if (!stream) {
-               /* If it's the ::$DATA stream just stat the base file name. */
-               ret = onefs_sys_stat(base, sbuf);
+       /*
+        * If the smb_fname has no stream or is :$DATA, then just stat the
+        * base stream. Otherwise stat the stream.
+        */
+       if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
+               ret = onefs_sys_stat(smb_fname_onefs->base_name,
+                                    &smb_fname->st);
        } else {
-               ret = stat_stream(handle->conn, base, stream, sbuf, 0);
+               ret = stat_stream(handle->conn, smb_fname_onefs->base_name,
+                                 smb_fname_onefs->stream_name, &smb_fname->st,
+                                 0);
        }
 
-       onefs_adjust_stat_time(handle->conn, path, sbuf);
+       onefs_adjust_stat_time(handle->conn, smb_fname->base_name,
+                              &smb_fname->st);
+
+       TALLOC_FREE(smb_fname_onefs);
+
        return ret;
 }
 
@@ -361,64 +428,83 @@ int onefs_fstat(vfs_handle_struct *handle, struct files_struct *fsp,
        return ret;
 }
 
-int onefs_lstat(vfs_handle_struct *handle, const char *path,
-               SMB_STRUCT_STAT *sbuf)
+int onefs_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
 {
+       struct smb_filename *smb_fname_onefs = NULL;
+       NTSTATUS status;
        int ret;
-       bool is_stream;
-       char *base = NULL;
-       char *stream = NULL;
 
-       ret = onefs_is_stream(path, &base, &stream, &is_stream);
-       if (ret)
-               return ret;
+       status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
+                                            &smb_fname_onefs);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
+               return -1;
+       }
 
-       if (!is_stream) {
-               ret = onefs_sys_lstat(path, sbuf);
-       } else if (!stream) {
-               /* If it's the ::$DATA stream just stat the base file name. */
-               ret = onefs_sys_lstat(base, sbuf);
+       /*
+        * If the smb_fname has no stream or is :$DATA, then just stat the
+        * base stream. Otherwise stat the stream.
+        */
+       if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
+               ret = onefs_sys_lstat(smb_fname_onefs->base_name,
+                                     &smb_fname->st);
        } else {
-               ret = stat_stream(handle->conn, base, stream, sbuf,
+               ret = stat_stream(handle->conn, smb_fname_onefs->base_name,
+                                 smb_fname_onefs->stream_name, &smb_fname->st,
                                  AT_SYMLINK_NOFOLLOW);
        }
 
-       onefs_adjust_stat_time(handle->conn, path, sbuf);
+       onefs_adjust_stat_time(handle->conn, smb_fname->base_name,
+                              &smb_fname->st);
+
+       TALLOC_FREE(smb_fname_onefs);
+
        return ret;
 }
 
-int onefs_unlink(vfs_handle_struct *handle, const char *path)
+int onefs_unlink(vfs_handle_struct *handle,
+                 const struct smb_filename *smb_fname)
 {
+       struct smb_filename *smb_fname_onefs = NULL;
        int ret;
        bool is_stream;
        char *base = NULL;
        char *stream = NULL;
        int dir_fd, saved_errno;
 
-       ret = onefs_is_stream(path, &base, &stream, &is_stream);
-       if (ret) {
-               return ret;
+       /* Not a stream. */
+       if (!is_ntfs_stream_smb_fname(smb_fname)) {
+               return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
        }
 
-       if (!is_stream) {
-               return SMB_VFS_NEXT_UNLINK(handle, path);
+       status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
+                                            &smb_fname_onefs);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
+               return -1;
        }
 
-       /* If it's the ::$DATA stream just unlink the base file name. */
-       if (!stream) {
-               return SMB_VFS_NEXT_UNLINK(handle, base);
+       /* Default stream (the ::$DATA was just stripped off). */
+       if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
+               ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_onefs);
+               goto out;
        }
 
-       dir_fd = get_stream_dir_fd(handle->conn, base, NULL);
+       dir_fd = get_stream_dir_fd(handle->conn, smb_fname_onefs->base_name,
+                                  NULL);
        if (dir_fd < 0) {
-               return -1;
+               ret = -1;
+               goto out;
        }
 
-       ret = enc_unlinkat(dir_fd, stream, ENC_DEFAULT, 0);
+       ret = enc_unlinkat(dir_fd, smb_fname_onefs->stream_name, ENC_DEFAULT,
+                          0);
 
        saved_errno = errno;
        close(dir_fd);
        errno = saved_errno;
+ out:
+       TALLOC_FREE(smb_fname_onefs);
        return ret;
 }
 
@@ -661,10 +747,22 @@ NTSTATUS onefs_streaminfo(vfs_handle_struct *handle,
                }
                ret = SMB_VFS_FSTAT(fsp, &sbuf);
        } else {
+               struct smb_filename *smb_fname = NULL;
+
                if (is_ntfs_stream_name(fname)) {
                        return NT_STATUS_INVALID_PARAMETER;
                }
-               ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
+
+               status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
+                                                   NULL, &smb_fname);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               ret = SMB_VFS_STAT(handle->conn, smb_fname);
+
+               sbuf = smb_fname->st;
+
+               TALLOC_FREE(smb_fname);
        }
 
        if (ret == -1) {