[sheepdog] [PATCH v4 09/18] collie/farm: implement sha1_file

Kai Zhang kyle at zelin.io
Fri May 17 08:27:17 CEST 2013


moved sha1_file from sheep/farm to collie/farm

Changes:
1. move function get_object_directory() to farm.h.
   It will be moved to farm.c later.
2. fix style problem

Signed-off-by: Kai Zhang <kyle at zelin.io>
---
 collie/Makefile.am                 |    8 ++++----
 collie/farm/farm.h                 |   26 ++++++++++++++++++++++++++
 {sheep => collie}/farm/sha1_file.c |   22 +++++++++++-----------
 3 files changed, 41 insertions(+), 15 deletions(-)
 rename {sheep => collie}/farm/sha1_file.c (91%)

diff --git a/collie/Makefile.am b/collie/Makefile.am
index 7922c68..3c45e81 100644
--- a/collie/Makefile.am
+++ b/collie/Makefile.am
@@ -23,7 +23,7 @@ INCLUDES		= -I$(top_builddir)/include -I$(top_srcdir)/include
 
 sbin_PROGRAMS		= collie
 
-collie_SOURCES		= farm/object_rb_tree.c \
+collie_SOURCES		= farm/object_rb_tree.c farm/sha1_file.c \
 			  collie.c common.c treeview.c vdi.c node.c cluster.c
 
 if BUILD_TRACE
@@ -31,14 +31,14 @@ collie_SOURCES          += debug.c
 override CFLAGS         := $(subst -pg -gstabs,,$(CFLAGS))
 endif
 
-collie_LDADD	  	= ../lib/libsheepdog.a -lpthread
+collie_LDADD		= ../lib/libsheepdog.a -lpthread
 collie_DEPENDENCIES	= ../lib/libsheepdog.a
 
 noinst_HEADERS		= treeview.h collie.h
 
-EXTRA_DIST		= 
+EXTRA_DIST		=
 
-all-local: 
+all-local:
 	@echo Built collie
 
 clean-local:
diff --git a/collie/farm/farm.h b/collie/farm/farm.h
index 3d08396..8dc47a0 100644
--- a/collie/farm/farm.h
+++ b/collie/farm/farm.h
@@ -20,11 +20,37 @@
 #include "strbuf.h"
 #include "sha1.h"
 
+#define TAG_LEN         6
+#define TAG_DATA        "data\0\0"
+#define TAG_TRUNK       "trunk\0"
+#define TAG_SNAP        "snap\0\0"
+
 enum obj_type {
 	OBJECT = 1,
 	VDI,
 };
 
+struct sha1_file_hdr {
+	char tag[TAG_LEN];
+	uint64_t size;
+	uint64_t priv;
+	uint64_t reserved;
+};
+
+static char farm_obj_dir[PATH_MAX];
+static char farm_dir[PATH_MAX];
+
+static inline char *get_object_directory(void)
+{
+	return farm_obj_dir;
+}
+
+/* sha1_file.c */
+int sha1_file_write(unsigned char *buf, unsigned len, unsigned char *);
+void *sha1_file_read(const unsigned char *sha1, struct sha1_file_hdr *);
+int get_sha1_hex(const char *hex, unsigned char *sha1);
+int sha1_file_try_delete(const unsigned char *sha1);
+
 /* object_rb_tree.c */
 int get_obj_nr(void);
 void obj_tree_insert(uint64_t oid, enum obj_type type, int nr_copies);
diff --git a/sheep/farm/sha1_file.c b/collie/farm/sha1_file.c
similarity index 91%
rename from sheep/farm/sha1_file.c
rename to collie/farm/sha1_file.c
index dd29e23..c775130 100644
--- a/sheep/farm/sha1_file.c
+++ b/collie/farm/sha1_file.c
@@ -28,11 +28,6 @@
 #include "farm.h"
 #include "util.h"
 
-static inline char *get_object_directory(void)
-{
-	return farm_obj_dir;
-}
-
 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
 {
 	int i;
@@ -45,9 +40,8 @@ static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
 	}
 }
 
-char *sha1_to_path(const unsigned char *sha1)
+static char *sha1_to_path(const unsigned char *sha1)
 {
-
 	static char buf[PATH_MAX];
 	const char *objdir;
 	int len;
@@ -110,15 +104,19 @@ static int put_sha1_file(char *name)
 	return 0;
 }
 
-static int sha1_buffer_write(const unsigned char *sha1, void *buf, unsigned int size)
+static int sha1_buffer_write(const unsigned char *sha1,
+			     void *buf, unsigned int size)
 {
 	char *filename = sha1_to_path(sha1);
 	int fd, ret = 0, len;
 
 	fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 	if (fd < 0) {
-		if (errno != EEXIST)
+		if (errno != EEXIST) {
+			sd_eprintf("failed to open file %s with error: %m",
+				   filename);
 			ret = -1;
+		}
 		goto err_open;
 	}
 	len = xwrite(fd, buf, size);
@@ -176,7 +174,8 @@ static void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
 	return map;
 }
 
-static void *unpack_sha1_file(void *map, unsigned long mapsize, struct sha1_file_hdr *hdr)
+static void *unpack_sha1_file(void *map, unsigned long mapsize,
+			      struct sha1_file_hdr *hdr)
 {
 	int hdr_len;
 	char *buf;
@@ -193,7 +192,8 @@ static void *unpack_sha1_file(void *map, unsigned long mapsize, struct sha1_file
 	return buf;
 }
 
-static int verify_sha1_file(const unsigned char *sha1, void *buf, unsigned long len)
+static int verify_sha1_file(const unsigned char *sha1,
+			    void *buf, unsigned long len)
 {
 	unsigned char tmp[SHA1_LEN];
 	struct sha1_ctx c;
-- 
1.7.1




More information about the sheepdog mailing list