From f4b0c2e93a2f45b548465dba84b257bcb3b41446 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sat, 20 Feb 2016 15:22:17 -0500 Subject: Implement unlink --- src/operations.cc | 19 +++++++++++++++++++ src/posix_extras.cc | 10 ++++++++++ src/posix_extras.h | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/src/operations.cc b/src/operations.cc index 375a039..ad9f8f3 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -134,6 +134,24 @@ int Release(const char*, fuse_file_info* const file_info) noexcept { return ReleaseResource(file_info->fh); } +int Unlink(const char* c_path) noexcept { + try { + if (std::strcmp(path, "/") == 0) { + // Removing the root is probably a bad idea. + return -EPERM; + } + + // Trim the leading slash so UnlinkAt will treat it relative to root_. + root_->UnlinkAt(path + 1); + return 0; + } catch (const std::system_error& e) { + return -e.code().value(); + } catch (...) { + LOG(ERROR) << "unlink: caught unexpected value"; + return -ENOTRECOVERABLE; + } +} + int Opendir(const char* const path, fuse_file_info* const file_info) noexcept { return OpenResource(path, O_DIRECTORY, 0777, &file_info->fh); } @@ -193,6 +211,7 @@ fuse_operations FuseOperations(File* const root) { result.open = &Open; result.create = &Create; result.release = &Release; + result.unlink = &Unlink; result.opendir = &Opendir; result.readdir = &Readdir; diff --git a/src/posix_extras.cc b/src/posix_extras.cc index 4093cc0..6844785 100644 --- a/src/posix_extras.cc +++ b/src/posix_extras.cc @@ -102,6 +102,16 @@ File File::OpenAt(const char* const path, const int flags, return result; } +void File::UnlinkAt(const char* const path) const { + if (path[0] == '/') { + throw std::invalid_argument("absolute path"); + } + + if (unlinkat(fd_, path, 0) == -1) { + throw SystemError(); + } +} + int File::Duplicate() const { int result; if ((result = dup(fd_)) == -1) { diff --git a/src/posix_extras.h b/src/posix_extras.h index b6dbdfd..f4e7c69 100644 --- a/src/posix_extras.h +++ b/src/posix_extras.h @@ -71,6 +71,10 @@ class File { File OpenAt(const char* path, int flags) const; File OpenAt(const char* path, int flags, mode_t mode) const; + // Removes the file at the path relative to the file descriptor. The path + // must indeed be relative (i.e., it must not start with '/'). + void UnlinkAt(const char* path) const; + private: File() {} -- cgit v1.2.3