diff options
author | Benjamin Barenblat <bbaren@mit.edu> | 2016-02-21 19:22:26 -0500 |
---|---|---|
committer | Benjamin Barenblat <bbaren@mit.edu> | 2016-02-21 20:38:02 -0500 |
commit | 384f7c422acca2f3658393abfc16ea57cd74caa6 (patch) | |
tree | 97b225ee9a78141e4c1bedb63ec0e44f9f56f34c | |
parent | 05cafc5002360015a79484e6ee40c159b1c80958 (diff) |
Implement utimens
-rw-r--r-- | src/operations.cc | 23 | ||||
-rw-r--r-- | src/posix_extras.cc | 13 | ||||
-rw-r--r-- | src/posix_extras.h | 7 |
3 files changed, 43 insertions, 0 deletions
diff --git a/src/operations.cc b/src/operations.cc index df12738..355ffc3 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -30,6 +30,7 @@ #include <glog/logging.h> #include <sys/stat.h> #include <sys/types.h> +#include <time.h> #include "fuse.h" #include "posix_extras.h" @@ -130,6 +131,27 @@ int Open(const char* const path, fuse_file_info* const file_info) noexcept { return OpenResource<File>(path, file_info->flags, &file_info->fh); } +int Utimens(const char* const path, const timespec times[2]) noexcept { + try { + root_->UTimeNs( + std::strcmp(path, "/") == 0 + ? + // Update the times on the mount point. + "." + : + // Trim the leading slash so UTimeNs will treat it relative to + // root_. + path + 1, + times[0], times[1]); + return 0; + } catch (const std::system_error& e) { + return -e.code().value(); + } catch (...) { + LOG(ERROR) << "utimens: caught unexpected value"; + return -ENOTRECOVERABLE; + } +} + int Release(const char*, fuse_file_info* const file_info) noexcept { return ReleaseResource<File>(file_info->fh); } @@ -211,6 +233,7 @@ fuse_operations FuseOperations(File* const root) { result.mknod = &Mknod; result.open = &Open; + result.utimens = &Utimens; result.release = &Release; result.unlink = &Unlink; diff --git a/src/posix_extras.cc b/src/posix_extras.cc index f8274fb..7529251 100644 --- a/src/posix_extras.cc +++ b/src/posix_extras.cc @@ -14,6 +14,7 @@ #include "posix_extras.h" +#include <array> #include <cerrno> #include <experimental/optional> #include <stdexcept> @@ -110,6 +111,18 @@ void File::UnlinkAt(const char* const path) const { } } +void File::UTimeNs(const char* const path, const timespec& access, + const timespec& modification) const { + if (path[0] == '/') { + throw std::invalid_argument("absolute path"); + } + + std::array<const timespec, 2> times{{access, modification}}; + if (utimensat(fd_, path, times.data(), AT_SYMLINK_NOFOLLOW) == -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 60b2284..a73f9fe 100644 --- a/src/posix_extras.h +++ b/src/posix_extras.h @@ -21,6 +21,7 @@ #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> +#include <time.h> namespace scoville { @@ -81,6 +82,12 @@ class File { // must indeed be relative (i.e., it must not start with '/'). void UnlinkAt(const char* path) const; + // Sets the access and modification times of the file at the path relative to + // the file descriptor. The path must indeed be relative (i.e., it must not + // start with '/'). Does not follow symbolic links. + void UTimeNs(const char* path, const timespec& access, + const timespec& modification) const; + private: File() {} |