[sheepdog] [PATCH v2 3/4] sheepfs: add support for "rmdir" and "unlink"

Robin Dong robin.k.dong at gmail.com
Fri Jan 24 04:47:02 CET 2014


From: Robin Dong <sanbai at taobao.com>

Add support for "rmdir" and "unlink" so users can remove all directories
of accounts/containers and files of objects after using it.
This "remove" operation only remove local entry in sheepfs, the objects
are still in remote object-service.

Signed-off-by: Robin Dong <sanbai at taobao.com>
---
 sheepfs/core.c        | 33 +++++++++++++++++++++++++++++++--
 sheepfs/http.c        | 15 +++++++++++++++
 sheepfs/shadow_file.c | 12 ++++++++++++
 sheepfs/sheepfs.h     |  5 ++++-
 4 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/sheepfs/core.c b/sheepfs/core.c
index f80270b..fd89fab 100644
--- a/sheepfs/core.c
+++ b/sheepfs/core.c
@@ -57,7 +57,9 @@ static struct sheepfs_file_operation {
 	int (*write)(const char *path, const char *buf, size_t size, off_t);
 	size_t (*get_size)(const char *path);
 	int (*sync)(const char *path);
-	int (*open)(const char *paht, struct fuse_file_info *);
+	int (*open)(const char *path, struct fuse_file_info *);
+	int (*unlink)(const char *path);
+	int (*rmdir)(const char *path);
 } sheepfs_file_ops[] = {
 	[OP_NULL]           = { NULL, NULL, NULL },
 	[OP_CLUSTER_INFO]   = { cluster_info_read, NULL,
@@ -79,7 +81,10 @@ static struct sheepfs_file_operation {
 	[OP_HTTP_ADDRESS]   = { http_address_read, http_address_write,
 				http_address_get_size },
 	[OP_HTTP_OBJECT]    = { NULL, http_object_write },
-	[OP_OBJECT]         = { object_read, NULL, object_get_size },
+	[OP_OBJECT]         = { object_read, NULL, object_get_size, NULL, NULL,
+				object_unlink },
+	[OP_CONTAINER]      = { NULL, NULL, NULL, NULL, NULL, NULL,
+				container_rmdir },
 };
 
 __printf(3, 4)
@@ -153,6 +158,28 @@ out:
 	return ret;
 }
 
+static int sheepfs_unlink(const char *path)
+{
+	int ret = 0;
+	unsigned op = sheepfs_get_op(path);
+
+	if (sheepfs_file_ops[op].unlink)
+		ret = sheepfs_file_ops[op].unlink(path);
+
+	return ret;
+}
+
+static int sheepfs_rmdir(const char *path)
+{
+	int ret = 0;
+	unsigned op = sheepfs_get_op(path);
+
+	if (sheepfs_file_ops[op].rmdir)
+		ret = sheepfs_file_ops[op].rmdir(path);
+
+	return ret;
+}
+
 static int sheepfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 			   off_t offset, struct fuse_file_info *fi)
 {
@@ -247,6 +274,8 @@ static int sheepfs_open(const char *path, struct fuse_file_info *fi)
 
 static struct fuse_operations sheepfs_ops =  {
 	.getattr  = sheepfs_getattr,
+	.unlink   = sheepfs_unlink,
+	.rmdir    = sheepfs_rmdir,
 	.readdir  = sheepfs_readdir,
 	.truncate = sheepfs_truncate,
 	.read     = sheepfs_read,
diff --git a/sheepfs/http.c b/sheepfs/http.c
index e3521bd..9582237 100644
--- a/sheepfs/http.c
+++ b/sheepfs/http.c
@@ -262,6 +262,16 @@ size_t object_get_size(const char *path)
 	return object_size;
 }
 
+int object_unlink(const char *path)
+{
+	return shadow_file_delete(path);
+}
+
+int container_rmdir(const char *path)
+{
+	return shadow_dir_delete(path);
+}
+
 static int object_create_entry(const char *entry, const char *url)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -307,6 +317,11 @@ static int object_create_entry(const char *entry, const char *url)
 				sheepfs_pr("Create dir %s fail", path);
 				goto out;
 			}
+			if (sheepfs_set_op(path, OP_CONTAINER) < 0) {
+				sheepfs_pr("Set_op %s fail", path);
+				shadow_dir_delete(path);
+				goto out;
+			}
 		}
 	}
 	ret = 0;
diff --git a/sheepfs/shadow_file.c b/sheepfs/shadow_file.c
index 999533e..3c44a68 100644
--- a/sheepfs/shadow_file.c
+++ b/sheepfs/shadow_file.c
@@ -92,6 +92,18 @@ int shadow_dir_create(const char *path)
 	return 0;
 }
 
+int shadow_dir_delete(const char *path)
+{
+	char p[PATH_MAX];
+
+	snprintf(p, sizeof(p), "%s%s", sheepfs_shadow, path);
+	if (rmdir(p) < 0) {
+		sheepfs_pr("%m\n");
+		return -1;
+	}
+	return 0;
+}
+
 int shadow_file_setxattr(const char *path, const char *name,
 			 const void *value, size_t size)
 {
diff --git a/sheepfs/sheepfs.h b/sheepfs/sheepfs.h
index b426f66..9748238 100644
--- a/sheepfs/sheepfs.h
+++ b/sheepfs/sheepfs.h
@@ -18,6 +18,7 @@ enum sheepfs_opcode {
 	OP_HTTP_ADDRESS,
 	OP_HTTP_OBJECT,
 	OP_OBJECT,
+	OP_CONTAINER,
 };
 
 #define COMMAND_LEN  512
@@ -45,6 +46,7 @@ extern printf_fn fs_printf;
 size_t shadow_file_write(const char *path, const char *buf, size_t size);
 int shadow_file_read(const char *, char *buf, size_t size, off_t);
 int shadow_dir_create(const char *path);
+int shadow_dir_delete(const char *path);
 int shadow_file_create(const char *path);
 int shadow_file_setxattr(const char *path, const char *name,
 				const void *value, size_t size);
@@ -111,5 +113,6 @@ int http_object_write(const char *path, const char *buf, size_t size,
 
 int object_read(const char *path, char *buf, size_t size, off_t ignore);
 size_t object_get_size(const char *path);
-int object_open(const char *path, struct fuse_file_info *);
+int object_unlink(const char *path);
+int container_rmdir(const char *path);
 #endif
-- 
1.7.12.4




More information about the sheepdog mailing list