[sheepdog] [PATCH v1 1/3] sheepfs: add framework for http interface

Robin Dong robin.k.dong at gmail.com
Thu Jan 23 06:57:51 CET 2014


From: Robin Dong <sanbai at taobao.com>

Adding http.c in sheepfs dir as the framework and implement "address" file
to store http address.

Signed-off-by: Robin Dong <sanbai at taobao.com>
---
 sheepfs/Makefile.am   |  2 +-
 sheepfs/core.c        |  6 ++++-
 sheepfs/http.c        | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
 sheepfs/shadow_file.c | 16 +++++++++++-
 sheepfs/sheepfs.h     | 13 +++++++++-
 5 files changed, 104 insertions(+), 4 deletions(-)
 create mode 100644 sheepfs/http.c

diff --git a/sheepfs/Makefile.am b/sheepfs/Makefile.am
index 30dedce..64f84f2 100644
--- a/sheepfs/Makefile.am
+++ b/sheepfs/Makefile.am
@@ -24,7 +24,7 @@ AM_CPPFLAGS		= -I$(top_builddir)/include -I$(top_srcdir)/include
 sbin_PROGRAMS		= sheepfs
 
 sheepfs_SOURCES		= core.c cluster.c vdi.c shadow_file.c volume.c node.c \
-			  config.c
+			  config.c http.c
 
 sheepfs_LDADD	  	= ../lib/libsheepdog.a $(fuse_LIBS) $(LIBS) -lpthread
 sheepfs_DEPENDENCIES	= ../lib/libsheepdog.a
diff --git a/sheepfs/core.c b/sheepfs/core.c
index c71d6ed..2f900bc 100644
--- a/sheepfs/core.c
+++ b/sheepfs/core.c
@@ -76,6 +76,9 @@ static struct sheepfs_file_operation {
 				config_sheep_info_get_size },
 	[OP_VOLUME]         = { volume_read, volume_write, volume_get_size,
 				volume_sync, volume_open },
+	[OP_HTTP_ADDRESS]   = { http_address_read, http_address_write,
+				http_address_get_size },
+	[OP_HTTP_OBJECT]    = { NULL, http_object_write },
 };
 
 __printf(3, 4)
@@ -288,7 +291,8 @@ static int create_sheepfs_layout(void)
 		return -1;
 	if (create_config_layout() < 0)
 		return -1;
-
+	if (create_http_layout() < 0)
+		return -1;
 	return 0;
 }
 
diff --git a/sheepfs/http.c b/sheepfs/http.c
new file mode 100644
index 0000000..e220e9c
--- /dev/null
+++ b/sheepfs/http.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2014 Taobao Inc.
+ *
+ * Robin Dong <sanbai at taobao.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+
+#include "strbuf.h"
+#include "sheepfs.h"
+#include "net.h"
+
+#define PATH_HTTP		"/http"
+#define PATH_HTTP_ADDRESS	"/http/address"
+#define PATH_HTTP_OBJECT	"/http/object"
+
+int create_http_layout(void)
+{
+	if (shadow_dir_create(PATH_HTTP) < 0)
+		return -1;
+
+	if (shadow_file_create(PATH_HTTP_ADDRESS) < 0)
+		return -1;
+	if (sheepfs_set_op(PATH_HTTP_ADDRESS, OP_HTTP_ADDRESS) < 0)
+		return -1;
+
+	if (shadow_file_create(PATH_HTTP_OBJECT) < 0)
+		return -1;
+	if (sheepfs_set_op(PATH_HTTP_OBJECT, OP_HTTP_OBJECT) < 0)
+		return -1;
+
+	return 0;
+}
+
+int http_address_read(const char *path, char *buf, size_t size, off_t ignore)
+{
+	return shadow_file_read(path, buf, size, 0);
+}
+
+int http_address_write(const char *path, const char *buf, size_t size,
+		       off_t ignore)
+{
+	return shadow_file_write(path, buf, size);
+}
+
+size_t http_address_get_size(const char *path)
+{
+	struct stat st;
+	if (shadow_file_stat(path, &st))
+		return st.st_size;
+	return 0;
+}
+
+int http_object_write(const char *path, const char *buf, size_t size,
+		      off_t ignore)
+{
+	return size;
+}
diff --git a/sheepfs/shadow_file.c b/sheepfs/shadow_file.c
index 9579c78..999533e 100644
--- a/sheepfs/shadow_file.c
+++ b/sheepfs/shadow_file.c
@@ -43,7 +43,7 @@ int shadow_file_read(const char *path, char *buf, size_t size, off_t offset)
 	return len;
 }
 
-size_t shadow_file_write(const char *path, char *buf, size_t size)
+size_t shadow_file_write(const char *path, const char *buf, size_t size)
 {
 	char p[PATH_MAX];
 	int fd;
@@ -145,3 +145,17 @@ bool shadow_file_exsit(const char *path)
 
 	return true;
 }
+
+bool shadow_file_stat(const char *path, struct stat *st)
+{
+	char p[PATH_MAX];
+
+	snprintf(p, sizeof(p), "%s%s", sheepfs_shadow, path);
+	if (stat(p, st) < 0) {
+		if (errno != ENOENT)
+			sheepfs_pr("%m\n");
+		return false;
+	}
+
+	return true;
+}
diff --git a/sheepfs/sheepfs.h b/sheepfs/sheepfs.h
index cfd2cad..64e560a 100644
--- a/sheepfs/sheepfs.h
+++ b/sheepfs/sheepfs.h
@@ -15,6 +15,8 @@ enum sheepfs_opcode {
 	OP_CONFIG_OCACHE,
 	OP_CONFIG_SHEEP,
 	OP_VOLUME,
+	OP_HTTP_ADDRESS,
+	OP_HTTP_OBJECT,
 };
 
 #define COMMAND_LEN  512
@@ -39,7 +41,7 @@ extern printf_fn fs_printf;
 })
 
 /* shadow_file.c */
-size_t shadow_file_write(const char *path, char *buf, size_t size);
+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_file_create(const char *path);
@@ -49,6 +51,7 @@ int shadow_file_getxattr(const char *path, const char *name,
 				void *value, size_t size);
 int shadow_file_delete(const char *path);
 bool shadow_file_exsit(const char *path);
+bool shadow_file_stat(const char *path, struct stat *st);
 
 /* volume.c */
 int create_volume_layout(void);
@@ -96,4 +99,12 @@ int config_sheep_info_read(const char *path, char *, size_t size, off_t);
 int config_sheep_info_write(const char *, const char *, size_t, off_t);
 size_t config_sheep_info_get_size(const char *path);
 
+/* http.c */
+int create_http_layout(void);
+int http_address_read(const char *path, char *buf, size_t size, off_t ignore);
+int http_address_write(const char *path, const char *buf, size_t size,
+		       off_t ignore);
+size_t http_address_get_size(const char *path);
+int http_object_write(const char *path, const char *buf, size_t size,
+		      off_t ignore);
 #endif
-- 
1.7.12.4




More information about the sheepdog mailing list