aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@mit.edu>2016-02-20 15:22:17 -0500
committerGravatar Benjamin Barenblat <bbaren@mit.edu>2016-02-21 20:38:01 -0500
commitf4b0c2e93a2f45b548465dba84b257bcb3b41446 (patch)
treeb74a6959532ca4ad21c75bd85c348293cfe46e50
parent60cbb719e7e9b14d4966efcead89bca4fc022c8e (diff)
Implement unlink
-rw-r--r--src/operations.cc19
-rw-r--r--src/posix_extras.cc10
-rw-r--r--src/posix_extras.h4
3 files changed, 33 insertions, 0 deletions
diff --git a/src/operations.cc b/src/operations.cc
index 375a039..ad9f8f3 100644
--- a/src/operations.cc
+++ b/src/operations.cc
@@ -134,6 +134,24 @@ int Release(const char*, fuse_file_info* const file_info) noexcept {
return ReleaseResource<File>(file_info->fh);
}
+int Unlink(const char* c_path) noexcept {
+ try {
+ if (std::strcmp(path, "/") == 0) {
+ // Removing the root is probably a bad idea.
+ return -EPERM;
+ }
+
+ // Trim the leading slash so UnlinkAt will treat it relative to root_.
+ root_->UnlinkAt(path + 1);
+ return 0;
+ } catch (const std::system_error& e) {
+ return -e.code().value();
+ } catch (...) {
+ LOG(ERROR) << "unlink: caught unexpected value";
+ return -ENOTRECOVERABLE;
+ }
+}
+
int Opendir(const char* const path, fuse_file_info* const file_info) noexcept {
return OpenResource<Directory>(path, O_DIRECTORY, 0777, &file_info->fh);
}
@@ -193,6 +211,7 @@ fuse_operations FuseOperations(File* const root) {
result.open = &Open;
result.create = &Create;
result.release = &Release;
+ result.unlink = &Unlink;
result.opendir = &Opendir;
result.readdir = &Readdir;
diff --git a/src/posix_extras.cc b/src/posix_extras.cc
index 4093cc0..6844785 100644
--- a/src/posix_extras.cc
+++ b/src/posix_extras.cc
@@ -102,6 +102,16 @@ File File::OpenAt(const char* const path, const int flags,
return result;
}
+void File::UnlinkAt(const char* const path) const {
+ if (path[0] == '/') {
+ throw std::invalid_argument("absolute path");
+ }
+
+ if (unlinkat(fd_, path, 0) == -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 b6dbdfd..f4e7c69 100644
--- a/src/posix_extras.h
+++ b/src/posix_extras.h
@@ -71,6 +71,10 @@ class File {
File OpenAt(const char* path, int flags) const;
File OpenAt(const char* path, int flags, mode_t mode) 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;
+
private:
File() {}