Having all security models implemented in one monolithic function is cumbersome. Especially when the need arises to fix something in the shared code, as it forces to change all the paths at the same time.
This doesn't fix any bug, it is just preparatory cleanup. Signed-off-by: Greg Kurz <[email protected]> --- hw/9pfs/9p-local.c | 134 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 95 insertions(+), 39 deletions(-) diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c index 7f513c5728f6..0d6869123094 100644 --- a/hw/9pfs/9p-local.c +++ b/hw/9pfs/9p-local.c @@ -626,8 +626,8 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, g_assert_not_reached(); } -static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, - const char *name, FsCred *credp) +static int local_mkdir_mapped(FsContext *fs_ctx, V9fsPath *dir_path, + const char *name, FsCred *credp) { char *path; int err = -1; @@ -639,43 +639,16 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); path = fullname.data; - /* Determine the security model */ - if (fs_ctx->export_flags & V9FS_SM_MAPPED) { - buffer = rpath(fs_ctx, path); - err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); - if (err == -1) { - goto out; - } - credp->fc_mode = credp->fc_mode|S_IFDIR; - err = local_set_xattr(buffer, credp); - if (err == -1) { - serrno = errno; - goto err_end; - } - } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { - buffer = rpath(fs_ctx, path); - err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); - if (err == -1) { - goto out; - } - credp->fc_mode = credp->fc_mode|S_IFDIR; - err = local_set_mapped_file_attr(fs_ctx, path, credp); - if (err == -1) { - serrno = errno; - goto err_end; - } - } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || - (fs_ctx->export_flags & V9FS_SM_NONE)) { - buffer = rpath(fs_ctx, path); - err = mkdir(buffer, credp->fc_mode); - if (err == -1) { - goto out; - } - err = local_post_create_passthrough(fs_ctx, path, credp); - if (err == -1) { - serrno = errno; - goto err_end; - } + buffer = rpath(fs_ctx, path); + err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); + if (err == -1) { + goto out; + } + credp->fc_mode = credp->fc_mode | S_IFDIR; + err = local_set_xattr(buffer, credp); + if (err == -1) { + serrno = errno; + goto err_end; } goto out; @@ -688,6 +661,89 @@ out: return err; } +static int local_mkdir_mapped_file(FsContext *fs_ctx, V9fsPath *dir_path, + const char *name, FsCred *credp) +{ + char *path; + int err = -1; + int serrno = 0; + V9fsString fullname; + char *buffer = NULL; + + v9fs_string_init(&fullname); + v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); + path = fullname.data; + + buffer = rpath(fs_ctx, path); + err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); + if (err == -1) { + goto out; + } + credp->fc_mode = credp->fc_mode | S_IFDIR; + err = local_set_mapped_file_attr(fs_ctx, path, credp); + if (err == -1) { + serrno = errno; + goto err_end; + } + goto out; + +err_end: + remove(buffer); + errno = serrno; +out: + g_free(buffer); + v9fs_string_free(&fullname); + return err; +} + +static int local_mkdir_passthrough(FsContext *fs_ctx, V9fsPath *dir_path, + const char *name, FsCred *credp) +{ + char *path; + int err = -1; + int serrno = 0; + V9fsString fullname; + char *buffer = NULL; + + v9fs_string_init(&fullname); + v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); + path = fullname.data; + + buffer = rpath(fs_ctx, path); + err = mkdir(buffer, credp->fc_mode); + if (err == -1) { + goto out; + } + err = local_post_create_passthrough(fs_ctx, path, credp); + if (err == -1) { + serrno = errno; + goto err_end; + } + goto out; + +err_end: + remove(buffer); + errno = serrno; +out: + g_free(buffer); + v9fs_string_free(&fullname); + return err; +} + +static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, + FsCred *credp) +{ + if (fs_ctx->export_flags & V9FS_SM_MAPPED) { + return local_mkdir_mapped(fs_ctx, dir_path, name, credp); + } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { + return local_mkdir_mapped_file(fs_ctx, dir_path, name, credp); + } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || + (fs_ctx->export_flags & V9FS_SM_NONE)) { + return local_mkdir_passthrough(fs_ctx, dir_path, name, credp); + } + g_assert_not_reached(); +} + static int local_fstat(FsContext *fs_ctx, int fid_type, V9fsFidOpenState *fs, struct stat *stbuf) {
