[sheepdog] [PATCH v3 08/12] collie/farm: impelement trunk object

Kai Zhang kyle at zelin.io
Tue May 14 09:51:53 CEST 2013


moved trunk.c from sheep/farm to collie/farm and made minor modification.

Signed-off-by: Kai Zhang <kyle at zelin.io>
---
 collie/Makefile.am  |    2 +-
 collie/farm/farm.h  |   12 ++++++++
 collie/farm/trunk.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 1 deletions(-)
 create mode 100644 collie/farm/trunk.c

diff --git a/collie/Makefile.am b/collie/Makefile.am
index 49d760c..f6403b1 100644
--- a/collie/Makefile.am
+++ b/collie/Makefile.am
@@ -24,7 +24,7 @@ INCLUDES		= -I$(top_builddir)/include -I$(top_srcdir)/include
 sbin_PROGRAMS		= collie
 
 collie_SOURCES		= farm/object_rb_tree.c farm/sha1_file.c farm/snap.c \
-			  farm/farm.c \
+			  farm/trunk.c farm/farm.c \
 			  collie.c common.c treeview.c vdi.c node.c cluster.c
 
 if BUILD_TRACE
diff --git a/collie/farm/farm.h b/collie/farm/farm.h
index 704a1ba..1d4c92a 100644
--- a/collie/farm/farm.h
+++ b/collie/farm/farm.h
@@ -30,6 +30,13 @@ enum obj_type {
 	VDI,
 };
 
+struct trunk_entry {
+	uint64_t oid;
+	enum obj_type type;
+	int nr_copies;
+	unsigned char sha1[SHA1_LEN];
+};
+
 struct sha1_file_hdr {
 	char tag[TAG_LEN];
 	uint64_t size;
@@ -41,6 +48,11 @@ struct sha1_file_hdr {
 int farm_init(const char *path);
 char *get_object_directory(void);
 
+/* trunk.c */
+int trunk_init(void);
+int trunk_file_write(unsigned char *trunk_sha1, struct strbuf *trunk_entries);
+void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *);
+
 /* snap.c */
 int snap_init(const char *path);
 void *snap_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr);
diff --git a/collie/farm/trunk.c b/collie/farm/trunk.c
new file mode 100644
index 0000000..049ec83
--- /dev/null
+++ b/collie/farm/trunk.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2011 Taobao Inc.
+ * Copyright (C) 2013 Zelin Inc.
+ *
+ * Liu Yuan <namei.unix at gmail.com>
+ * Kai Zhang <kyle at zelin.io>
+ *
+ * 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/>.
+ */
+
+/*
+ * Trunk object is meta data that describes the structure of the data objects
+ * at the time of snapshot being taken. It ties data objects together into a
+ * flat directory structure.
+ */
+#include <pthread.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "farm.h"
+#include "strbuf.h"
+#include "list.h"
+#include "util.h"
+#include "sheepdog_proto.h"
+
+int trunk_file_write(unsigned char *trunk_sha1, struct strbuf *trunk_entries)
+{
+	struct strbuf buf;
+	struct sha1_file_hdr hdr = {};
+	uint64_t data_size, object_nr = 0;
+	int ret = -1;
+
+	/* Init trunk hdr */
+	object_nr = get_obj_nr();
+	data_size = sizeof(struct trunk_entry) * object_nr;
+	hdr.size = data_size;
+	hdr.priv = object_nr;
+	memcpy(hdr.tag, TAG_TRUNK, TAG_LEN);
+	strbuf_init(&buf, sizeof(hdr) + data_size);
+	strbuf_add(&buf, &hdr, sizeof(hdr));
+
+	/* trunk entries */
+	strbuf_addbuf(&buf, trunk_entries);
+
+	/* write to sha1 file */
+	if (sha1_file_write((void *)buf.buf, buf.len, trunk_sha1) < 0)
+		goto out;
+
+	sd_dprintf("wrote trunk sha1: %s", sha1_to_hex(trunk_sha1));
+	ret = 0;
+out:
+	strbuf_release(&buf);
+	return ret;
+}
+
+void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr)
+{
+	void *buffer;
+
+	sd_dprintf("reading trunk sha1: %s", sha1_to_hex(sha1));
+	buffer = sha1_file_read(sha1, outhdr);
+	if (!buffer)
+		return NULL;
+	if (strcmp(outhdr->tag, TAG_TRUNK) != 0) {
+		free(buffer);
+		return NULL;
+	}
+
+	return buffer;
+}
-- 
1.7.1





More information about the sheepdog mailing list