提交 3dbcf273 编写于 作者: G Greg Kurz

9pfs: local: simplify file opening

The logic to open a path currently sits between local_open_nofollow() and
the relative_openat_nofollow() helper, which has no other user.

For the sake of clarity, this patch moves all the code of the helper into
its unique caller. While here we also:
- drop the code to skip leading "/" because the backend isn't supposed to
  pass anything but relative paths without consecutive slashes. The assert()
  is kept because we really don't want a buggy backend to pass an absolute
  path to openat().
- use strchrnul() to get a simpler code. This is ok since virtfs is for
  linux+glibc hosts only.
- don't dup() the initial directory and add an assert() to ensure we don't
  return the global mountfd to the caller. BTW, this would mean that the
  caller passed an empty path, which isn't supposed to happen either.
Signed-off-by: NGreg Kurz <groug@kaod.org>
Reviewed-by: NEric Blake <eblake@redhat.com>
[groug: fixed typos in changelog]
上级 f57f5878
...@@ -53,13 +53,37 @@ int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags, ...@@ -53,13 +53,37 @@ int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
mode_t mode) mode_t mode)
{ {
LocalData *data = fs_ctx->private; LocalData *data = fs_ctx->private;
int fd = data->mountfd;
/* All paths are relative to the path data->mountfd points to */
while (*path == '/') { while (*path && fd != -1) {
path++; const char *c;
int next_fd;
char *head;
/* Only relative paths without consecutive slashes */
assert(*path != '/');
head = g_strdup(path);
c = strchrnul(path, '/');
if (*c) {
/* Intermediate path element */
head[c - path] = 0;
path = c + 1;
next_fd = openat_dir(fd, head);
} else {
/* Rightmost path element */
next_fd = openat_file(fd, head, flags, mode);
path = c;
}
g_free(head);
if (fd != data->mountfd) {
close_preserve_errno(fd);
}
fd = next_fd;
} }
return relative_openat_nofollow(data->mountfd, path, flags, mode); assert(fd != data->mountfd);
return fd;
} }
int local_opendir_nofollow(FsContext *fs_ctx, const char *path) int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
......
...@@ -14,49 +14,6 @@ ...@@ -14,49 +14,6 @@
#include "qemu/xattr.h" #include "qemu/xattr.h"
#include "9p-util.h" #include "9p-util.h"
int relative_openat_nofollow(int dirfd, const char *path, int flags,
mode_t mode)
{
int fd;
fd = dup(dirfd);
if (fd == -1) {
return -1;
}
while (*path) {
const char *c;
int next_fd;
char *head;
/* Only relative paths without consecutive slashes */
assert(path[0] != '/');
head = g_strdup(path);
c = strchr(path, '/');
if (c) {
head[c - path] = 0;
next_fd = openat_dir(fd, head);
} else {
next_fd = openat_file(fd, head, flags, mode);
}
g_free(head);
if (next_fd == -1) {
close_preserve_errno(fd);
return -1;
}
close(fd);
fd = next_fd;
if (!c) {
break;
}
path = c + 1;
}
return fd;
}
ssize_t fgetxattrat_nofollow(int dirfd, const char *filename, const char *name, ssize_t fgetxattrat_nofollow(int dirfd, const char *filename, const char *name,
void *value, size_t size) void *value, size_t size)
{ {
......
...@@ -50,8 +50,6 @@ static inline int openat_file(int dirfd, const char *name, int flags, ...@@ -50,8 +50,6 @@ static inline int openat_file(int dirfd, const char *name, int flags,
return fd; return fd;
} }
int relative_openat_nofollow(int dirfd, const char *path, int flags,
mode_t mode);
ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name, ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
void *value, size_t size); void *value, size_t size);
int fsetxattrat_nofollow(int dirfd, const char *path, const char *name, int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册