vfs: Convert stream_dir to synthetic_smb_fname
[samba.git] / source3 / modules / vfs_streams_depot.c
index a18827421a2a2736514811bcea07088e3a5f6b07..b74e774f08cbd45282e45b3b89ae3db4e7f7fe99 100644 (file)
@@ -67,15 +67,10 @@ static uint32_t hash_fn(DATA_BLOB key)
  * an option to put in a special ACL entry for a non-existing group.
  */
 
-static bool file_is_valid(vfs_handle_struct *handle, const char *path,
-                         bool check_valid)
+static bool file_is_valid(vfs_handle_struct *handle, const char *path)
 {
        char buf;
 
-       if (!check_valid) {
-               return true;
-       }
-
        DEBUG(10, ("file_is_valid (%s) called\n", path));
 
        if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
@@ -92,16 +87,11 @@ static bool file_is_valid(vfs_handle_struct *handle, const char *path,
        return true;
 }
 
-static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
-                           bool check_valid)
+static bool mark_file_valid(vfs_handle_struct *handle, const char *path)
 {
        char buf = '1';
        int ret;
 
-       if (!check_valid) {
-               return true;
-       }
-
        DEBUG(10, ("marking file %s as valid\n", path));
 
        ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
@@ -134,12 +124,11 @@ static char *stream_dir(vfs_handle_struct *handle,
        uint8 id_buf[16];
        bool check_valid;
        const char *rootdir;
-       NTSTATUS status;
 
        check_valid = lp_parm_bool(SNUM(handle->conn),
                      "streams_depot", "check_valid", true);
 
-       tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
+       tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->cwd);
 
        if (tmp == NULL) {
                errno = ENOMEM;
@@ -152,14 +141,12 @@ static char *stream_dir(vfs_handle_struct *handle,
 
        /* Stat the base file if it hasn't already been done. */
        if (base_sbuf == NULL) {
-               struct smb_filename *smb_fname_base = NULL;
+               struct smb_filename *smb_fname_base;
 
-               status = create_synthetic_smb_fname(talloc_tos(),
-                                                   smb_fname->base_name,
-                                                   NULL, NULL,
-                                                   &smb_fname_base);
-               if (!NT_STATUS_IS_OK(status)) {
-                       errno = map_errno_from_nt_status(status);
+               smb_fname_base = synthetic_smb_fname(
+                       talloc_tos(), smb_fname->base_name, NULL, NULL);
+               if (smb_fname_base == NULL) {
+                       errno = ENOMEM;
                        goto fail;
                }
                if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
@@ -198,58 +185,75 @@ static char *stream_dir(vfs_handle_struct *handle,
                return NULL;
        }
 
-       status = create_synthetic_smb_fname(talloc_tos(), result, NULL, NULL,
-                                           &smb_fname_hash);
-       if (!NT_STATUS_IS_OK(status)) {
-               errno = map_errno_from_nt_status(status);
+       smb_fname_hash = synthetic_smb_fname(talloc_tos(), result, NULL, NULL);
+       if (smb_fname_hash == NULL) {
+               errno = ENOMEM;
                goto fail;
        }
 
        if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
                struct smb_filename *smb_fname_new = NULL;
                char *newname;
+               bool delete_lost;
 
                if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
                        errno = EINVAL;
                        goto fail;
                }
 
-               if (file_is_valid(handle, smb_fname->base_name, check_valid)) {
+               if (!check_valid ||
+                   file_is_valid(handle, smb_fname->base_name)) {
                        return result;
                }
 
                /*
                 * Someone has recreated a file under an existing inode
-                * without deleting the streams directory. For now, just move
-                * it away.
+                * without deleting the streams directory.
+                * Move it away or remove if streams_depot:delete_lost is set.
                 */
 
        again:
-               newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
-               if (newname == NULL) {
-                       errno = ENOMEM;
-                       goto fail;
-               }
+               delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
+                                          "delete_lost", false);
+
+               if (delete_lost) {
+                       DEBUG(3, ("Someone has recreated a file under an "
+                             "existing inode. Removing: %s\n",
+                             smb_fname_hash->base_name));
+                       recursive_rmdir(talloc_tos(), handle->conn,
+                                       smb_fname_hash);
+                       SMB_VFS_NEXT_RMDIR(handle, smb_fname_hash->base_name);
+               } else {
+                       newname = talloc_asprintf(talloc_tos(), "lost-%lu",
+                                                 random());
+                       DEBUG(3, ("Someone has recreated a file under an "
+                             "existing inode. Renaming: %s to: %s\n",
+                             smb_fname_hash->base_name,
+                             newname));
+                       if (newname == NULL) {
+                               errno = ENOMEM;
+                               goto fail;
+                       }
 
-               status = create_synthetic_smb_fname(talloc_tos(), newname,
-                                                   NULL, NULL,
-                                                   &smb_fname_new);
-               TALLOC_FREE(newname);
-               if (!NT_STATUS_IS_OK(status)) {
-                       errno = map_errno_from_nt_status(status);
-                       goto fail;
-               }
+                       smb_fname_new = synthetic_smb_fname(
+                               talloc_tos(), newname, NULL, NULL);
+                       TALLOC_FREE(newname);
+                       if (smb_fname_new == NULL) {
+                               errno = ENOMEM;
+                               goto fail;
+                       }
 
-               if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
-                                       smb_fname_new) == -1) {
-                       TALLOC_FREE(smb_fname_new);
-                       if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
-                               goto again;
+                       if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
+                                               smb_fname_new) == -1) {
+                               TALLOC_FREE(smb_fname_new);
+                               if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
+                                       goto again;
+                               }
+                               goto fail;
                        }
-                       goto fail;
-               }
 
-               TALLOC_FREE(smb_fname_new);
+                       TALLOC_FREE(smb_fname_new);
+               }
        }
 
        if (!create_it) {
@@ -294,7 +298,7 @@ static char *stream_dir(vfs_handle_struct *handle,
                goto fail;
        }
 
-       if (!mark_file_valid(handle, smb_fname->base_name, check_valid)) {
+       if (check_valid && !mark_file_valid(handle, smb_fname->base_name)) {
                goto fail;
        }
 
@@ -354,7 +358,10 @@ static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
                }
        } else {
                /* Normalize the stream type to upercase. */
-               strupper_m(strrchr_m(stream_fname, ':') + 1);
+               if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
+                       status = NT_STATUS_INVALID_PARAMETER;
+                       goto fail;
+               }
        }
 
        DEBUG(10, ("stream filename = %s\n", stream_fname));
@@ -383,7 +390,7 @@ static NTSTATUS walk_streams(vfs_handle_struct *handle,
                             void *private_data)
 {
        char *dirname;
-       SMB_STRUCT_DIR *dirhandle = NULL;
+       DIR *dirhandle = NULL;
        const char *dirent = NULL;
        char *talloced = NULL;
 
@@ -757,8 +764,8 @@ done:
 
 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
                           struct stream_struct **streams,
-                          const char *name, SMB_OFF_T size,
-                          SMB_OFF_T alloc_size)
+                          const char *name, off_t size,
+                          off_t alloc_size)
 {
        struct stream_struct *tmp;