From 05cafc5002360015a79484e6ee40c159b1c80958 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 21 Feb 2016 19:22:01 -0500 Subject: Replace creat with mknod+open Performance is not an immediate concern, so replace creat with mknod to compact code. --- src/operations.cc | 28 ++++++++++++++-------------- src/posix_extras.cc | 11 +++++++++++ src/posix_extras.h | 4 ++++ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/operations.cc b/src/operations.cc index 2bded60..df12738 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -75,7 +75,7 @@ int Getattr(const char* const path, struct stat* output) noexcept { } template -int OpenResource(const char* const path, const int flags, const mode_t mode, +int OpenResource(const char* const path, const int flags, uint64_t* const handle) noexcept { try { std::unique_ptr t(new T( @@ -85,7 +85,7 @@ int OpenResource(const char* const path, const int flags, const mode_t mode, *root_ : // Trim the leading slash so OpenAt will treat it relative to root_. - root_->OpenAt(path + 1, flags, mode))); + root_->OpenAt(path + 1, flags))); static_assert(sizeof(*handle) == sizeof(std::uintptr_t), "FUSE file handles are a different size than pointers"); @@ -107,29 +107,29 @@ int ReleaseResource(const uint64_t handle) noexcept { return 0; } -int Open(const char* const path, fuse_file_info* const file_info) noexcept { - return OpenResource(path, file_info->flags, 0777, &file_info->fh); -} - -int Create(const char* const path, const mode_t mode, - fuse_file_info* const file_info) noexcept { +int Mknod(const char* const c_path, const mode_t mode, + const dev_t dev) noexcept { try { if (std::strcmp(path, "/") == 0) { // They're asking to create the mount point. Huh? return -EEXIST; } - return OpenResource(path, - file_info->flags | O_CREAT | O_WRONLY | O_EXCL, - mode, &file_info->fh); + // Trim the leading slash so OpenAt will treat it relative to root_. + root_->MkNod(path + 1, mode, dev); + return 0; } catch (const std::system_error& e) { return -e.code().value(); } catch (...) { - LOG(ERROR) << "create: caught unexpected value"; + LOG(ERROR) << "mknod: caught unexpected value"; return -ENOTRECOVERABLE; } } +int Open(const char* const path, fuse_file_info* const file_info) noexcept { + return OpenResource(path, file_info->flags, &file_info->fh); +} + int Release(const char*, fuse_file_info* const file_info) noexcept { return ReleaseResource(file_info->fh); } @@ -153,7 +153,7 @@ int Unlink(const char* c_path) noexcept { } int Opendir(const char* const path, fuse_file_info* const file_info) noexcept { - return OpenResource(path, O_DIRECTORY, 0777, &file_info->fh); + return OpenResource(path, O_DIRECTORY, &file_info->fh); } int Readdir(const char*, void* const buffer, fuse_fill_dir_t filler, @@ -209,8 +209,8 @@ fuse_operations FuseOperations(File* const root) { result.getattr = &Getattr; + result.mknod = &Mknod; result.open = &Open; - result.create = &Create; result.release = &Release; result.unlink = &Unlink; diff --git a/src/posix_extras.cc b/src/posix_extras.cc index 359240f..f8274fb 100644 --- a/src/posix_extras.cc +++ b/src/posix_extras.cc @@ -75,6 +75,17 @@ struct stat File::LinkStatAt(const char* const path) const { return result; } +void File::MkNod(const char* const path, const mode_t mode, + const dev_t dev) const { + if (path[0] == '/') { + throw std::invalid_argument("absolute path"); + } + + if (mknodat(fd_, path, mode, dev) == -1) { + throw SystemError(); + } +} + File File::OpenAt(const char* const path, const int flags, const mode_t mode) const { if (path[0] == '/') { diff --git a/src/posix_extras.h b/src/posix_extras.h index 9bcbe51..60b2284 100644 --- a/src/posix_extras.h +++ b/src/posix_extras.h @@ -66,6 +66,10 @@ class File { // indeed be relative (i.e., it must not start with '/'). struct stat LinkStatAt(const char* path) const; + // Creates a file or directory at the path relative to the file descriptor. + // The path must indeed be relative (i.e., it must not start with '/'). + void MkNod(const char* path, mode_t mode, dev_t dev) const; + // Calls openat(2) on the path relative to the file descriptor. The path must // indeed be relative (i.e., it must not start with '/'). File OpenAt(const char* const path, const int flags) const { -- cgit v1.2.3