From 11aa2411125aa38f018dfd2ee7f6a81cb3695511 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sat, 20 Feb 2016 14:56:43 -0500 Subject: Deduplicate resource creation functions --- src/operations.cc | 55 +++++++++++++++---------------------------------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/src/operations.cc b/src/operations.cc index 2154371..c6b5d79 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -87,33 +87,38 @@ int Getattr(const char* const path, struct stat* output) noexcept { } } -int Open(const char* const path, fuse_file_info* const file_info) noexcept { - LOG(INFO) << "open(" << path << ")"; - +template +int OpenResource(const char* const path, const int flags, const mode_t mode, + uint64_t* const handle) noexcept { try { - std::unique_ptr file(new File( + std::unique_ptr t(new T( std::strcmp(path, "/") == 0 ? // They're asking to open the mount point. *root_ : // Trim the leading slash so OpenAt will treat it relative to root_. - root_->OpenAt(path + 1, file_info->flags))); + root_->OpenAt(path + 1, flags, mode))); - static_assert(sizeof(file_info->fh) == sizeof(std::uintptr_t), + static_assert(sizeof(*handle) == sizeof(std::uintptr_t), "FUSE file handles are a different size than pointers"); - file_info->fh = reinterpret_cast(file.release()); + *handle = reinterpret_cast(t.release()); return 0; } catch (const std::bad_alloc&) { return -ENOMEM; } catch (const std::system_error& e) { return -e.code().value(); } catch (...) { - LOG(ERROR) << "open caught unexpected value"; + LOG(ERROR) << "caught unexpected value"; return -ENOTRECOVERABLE; } } +int Open(const char* const path, fuse_file_info* const file_info) noexcept { + LOG(INFO) << "open(" << path << ")"; + 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 { LOG(INFO) << "create(" << path << ")"; @@ -124,16 +129,7 @@ int Create(const char* const path, const mode_t mode, return -EEXIST; } - // Trim the leading slash so OpenAt will treat it relative to root_. - std::unique_ptr file( - new File(root_->OpenAt(path + 1, file_info->flags, mode))); - - static_assert(sizeof(file_info->fh) == sizeof(std::uintptr_t), - "FUSE file handles are a different size than pointers"); - file_info->fh = reinterpret_cast(file.release()); - return 0; - } catch (const std::bad_alloc&) { - return -ENOMEM; + return OpenResource(path, file_info->flags, mode, &file_info->fh); } catch (const std::system_error& e) { return -e.code().value(); } catch (...) { @@ -151,28 +147,7 @@ int Release(const char*, fuse_file_info* const file_info) noexcept { int Opendir(const char* const path, fuse_file_info* const file_info) noexcept { LOG(INFO) << "opendir(" << path << ")"; - - try { - std::unique_ptr directory(new Directory( - std::strcmp(path, "/") == 0 ? - // They're asking to open the mount point. - *root_ - : - // Trim the leading slash so OpenAt will - // treat it relative to root_. - root_->OpenAt(path + 1, O_DIRECTORY))); - static_assert(sizeof(file_info->fh) == sizeof(std::uintptr_t), - "FUSE file handles are a different size than pointers"); - file_info->fh = reinterpret_cast(directory.release()); - return 0; - } catch (const std::bad_alloc&) { - return -ENOMEM; - } catch (const std::system_error& e) { - return -e.code().value(); - } catch (...) { - LOG(ERROR) << "opendir caught unexpected value"; - return -ENOTRECOVERABLE; - } + return OpenResource(path, O_DIRECTORY, 0777, &file_info->fh); } int Readdir(const char*, void* const buffer, fuse_fill_dir_t filler, -- cgit v1.2.3