From 6aa9095865017c06a61f0cb10c1f1231a89b761c Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 21 Feb 2016 19:23:11 -0500 Subject: Implement fgetattr and read --- src/operations.cc | 33 +++++++++++++++++++++++++++++++++ src/posix_extras.cc | 18 ++++++++++++++++++ src/posix_extras.h | 7 +++++++ 3 files changed, 58 insertions(+) diff --git a/src/operations.cc b/src/operations.cc index 355ffc3..897b193 100644 --- a/src/operations.cc +++ b/src/operations.cc @@ -25,8 +25,10 @@ #include #include #include +#include #include +#include #include #include #include @@ -75,6 +77,19 @@ int Getattr(const char* const path, struct stat* output) noexcept { } } +int Fgetattr(const char*, struct stat* const output, + struct fuse_file_info* const file_info) noexcept { + try { + *output = reinterpret_cast(file_info->fh)->Stat(); + return 0; + } catch (const std::system_error& e) { + return -e.code().value(); + } catch (...) { + LOG(ERROR) << "getattr: caught unexpected value"; + return -ENOTRECOVERABLE; + } +} + template int OpenResource(const char* const path, const int flags, uint64_t* const handle) noexcept { @@ -131,6 +146,22 @@ int Open(const char* const path, fuse_file_info* const file_info) noexcept { return OpenResource(path, file_info->flags, &file_info->fh); } +int Read(const char*, char* const buffer, const size_t bytes, + const off_t offset, fuse_file_info* const file_info) noexcept { + LOG(INFO) << "read with offset " << offset; + try { + auto* const file = reinterpret_cast(file_info->fh); + const std::vector read = file->Read(offset, bytes); + std::memcpy(buffer, read.data(), read.size()); + return static_cast(read.size()); + } catch (const std::system_error& e) { + return -e.code().value(); + } catch (...) { + LOG(ERROR) << "read: caught unexpected value"; + return -ENOTRECOVERABLE; + } +} + int Utimens(const char* const path, const timespec times[2]) noexcept { try { root_->UTimeNs( @@ -230,9 +261,11 @@ fuse_operations FuseOperations(File* const root) { result.destroy = &Destroy; result.getattr = &Getattr; + result.fgetattr = &Fgetattr; result.mknod = &Mknod; result.open = &Open; + result.read = &Read; result.utimens = &Utimens; result.release = &Release; result.unlink = &Unlink; diff --git a/src/posix_extras.cc b/src/posix_extras.cc index 7529251..ac6e3b2 100644 --- a/src/posix_extras.cc +++ b/src/posix_extras.cc @@ -16,9 +16,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -101,6 +103,22 @@ File File::OpenAt(const char* const path, const int flags, return result; } +std::vector File::Read(off_t offset, size_t bytes) const { + std::vector result(bytes, 0); + size_t cursor = 0; + ssize_t bytes_read; + while (0 < (bytes_read = pread(fd_, result.data() + cursor, bytes, offset))) { + cursor += static_cast(bytes_read); + offset += bytes_read; + bytes -= static_cast(bytes_read); + } + if (bytes_read == -1) { + throw SystemError(); + } + result.resize(cursor); + return result; +} + void File::UnlinkAt(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 a73f9fe..5571da6 100644 --- a/src/posix_extras.h +++ b/src/posix_extras.h @@ -15,8 +15,10 @@ #ifndef POSIX_EXTRAS_H_ #define POSIX_EXTRAS_H_ +#include #include #include +#include #include #include @@ -78,6 +80,11 @@ class File { } File OpenAt(const char* path, int flags, mode_t mode) const; + // Reads exactly the specified number of bytes from the file at the given + // offset, unless doing so would run past the end of the file, in which case + // fewer bytes are returned. + std::vector Read(off_t, size_t) 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; -- cgit v1.2.3