From 384f7c422acca2f3658393abfc16ea57cd74caa6 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 21 Feb 2016 19:22:26 -0500 Subject: Implement utimens --- src/operations.cc | 23 +++++++++++++++++++++++ src/posix_extras.cc | 13 +++++++++++++ src/posix_extras.h | 7 +++++++ 3 files changed, 43 insertions(+) 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 #include #include +#include #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(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_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 #include #include #include @@ -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 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 #include #include +#include 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() {} -- cgit v1.2.3