From 4f9c17ee5044c68a358a92e90772eea5b3966e3c Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 21 Feb 2016 20:19:12 -0500 Subject: Implement rename --- src/operations.cc | 34 ++++++++++++++++++++++++++++++++++ src/posix_extras.cc | 11 +++++++++++ src/posix_extras.h | 5 +++++ 3 files changed, 50 insertions(+) diff --git a/src/operations.cc b/src/operations.cc index 382974e..697f901 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -136,6 +136,39 @@ int Mknod(const char* const c_path, const mode_t mode, } } +int Chmod(const char* const c_path, const mode_t mode) noexcept { + try { + const std::string path(c_path); + root_->ChModAt(path == "/" ? "." : EncodePath(path).c_str(), mode); + return 0; + } catch (const std::system_error& e) { + return -e.code().value(); + } catch (...) { + LOG(ERROR) << "getattr: caught unexpected value"; + return -ENOTRECOVERABLE; + } +} + +int Rename(const char* const c_old_path, + const char* const c_new_path) noexcept { + try { + const std::string old_path(c_old_path); + const std::string new_path(c_new_path); + if (old_path == "/" || new_path == "/") { + return -EINVAL; + } else { + root_->RenameAt(EncodePath(old_path).c_str(), + EncodePath(new_path).c_str()); + return 0; + } + } catch (const std::system_error& e) { + return -e.code().value(); + } catch (...) { + 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); } @@ -302,6 +335,7 @@ fuse_operations FuseOperations(File* const root) { result.fgetattr = &Fgetattr; result.mknod = &Mknod; + result.rename = &Rename; result.open = &Open; result.read = &Read; result.write = &Write; diff --git a/src/posix_extras.cc b/src/posix_extras.cc index 5fc60f6..17fe275 100644 --- a/src/posix_extras.cc +++ b/src/posix_extras.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -129,6 +130,16 @@ std::vector File::Read(off_t offset, size_t bytes) const { return result; } +void File::RenameAt(const char* old_path, const char* new_path) const { + if (old_path[0] == '/' || new_path[0] == '/') { + throw std::invalid_argument("absolute path"); + } + + if (renameat(fd_, old_path, fd_, new_path) == -1) { + throw SystemError(); + } +} + void File::RmDirAt(const char* const path) const { if (path[0] == '/') { throw std::invalid_argument("absolute path"); diff --git a/src/posix_extras.h b/src/posix_extras.h index 41117f2..b9e9da6 100644 --- a/src/posix_extras.h +++ b/src/posix_extras.h @@ -89,6 +89,11 @@ class File { // fewer bytes are returned. std::vector Read(off_t, size_t) const; + // Renames a file from old_path to new_path. Both paths are interpreted + // relative to the file descriptor, and both must indeed be relative (i.e., + // they must not start with '/'). + void RenameAt(const char* old_path, const char* new_path) const; + // Removes the directory at the path relative to the file descriptor. The // path must indeed be relative (i.e., it must not start with '/'). void RmDirAt(const char* path) const; -- cgit v1.2.3