diff options
author | Benjamin Barenblat <bbaren@mit.edu> | 2016-02-21 20:19:12 -0500 |
---|---|---|
committer | Benjamin Barenblat <bbaren@mit.edu> | 2016-02-21 20:38:12 -0500 |
commit | 4f9c17ee5044c68a358a92e90772eea5b3966e3c (patch) | |
tree | b98cd677b7ba7b0b7d010ebe0e2797afc74f7d09 | |
parent | 90c61f66909affa269dfb396f0e5cc67f4e4de44 (diff) |
Implement rename
-rw-r--r-- | src/operations.cc | 34 | ||||
-rw-r--r-- | src/posix_extras.cc | 11 | ||||
-rw-r--r-- | src/posix_extras.h | 5 |
3 files changed, 50 insertions, 0 deletions
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<File>(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 <dirent.h> #include <fcntl.h> #include <glog/logging.h> +#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> @@ -129,6 +130,16 @@ std::vector<std::uint8_t> 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<std::uint8_t> 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; |