[sheepdog] [PATCH v6 09/16] collie/farm: implement sha1_file
Kai Zhang
kyle at zelin.io
Tue May 21 12:11:11 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
3. replace sd_xprintf(...) with fprintf(...)
Signed-off-by: Kai Zhang <kyle at zelin.io>
---
collie/Makefile.am | 8 +++---
collie/farm/farm.h | 26 ++++++++++++++++++++++++
{sheep => collie}/farm/sha1_file.c | 38 ++++++++++++++++++------------------
3 files changed, 49 insertions(+), 23 deletions(-)
rename {sheep => collie}/farm/sha1_file.c (87%)
diff --git a/collie/Makefile.am b/collie/Makefile.am
index 3fdb147..5a705b2 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_tree.c \
+collie_SOURCES = farm/object_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 6c47985..cd1fa26 100644
--- a/collie/farm/farm.h
+++ b/collie/farm/farm.h
@@ -20,9 +20,35 @@
#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"
+
+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;
+}
+
typedef int (*object_handler_func_t)(uint64_t oid, int nr_copies, void *buf,
size_t size, void *data);
+/* 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_tree.c */
int object_tree_size(void);
void object_tree_insert(uint64_t oid, int nr_copies);
diff --git a/sheep/farm/sha1_file.c b/collie/farm/sha1_file.c
similarity index 87%
rename from sheep/farm/sha1_file.c
rename to collie/farm/sha1_file.c
index dd29e23..2d94673 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;
@@ -90,7 +84,7 @@ static int put_sha1_file(char *name)
if (getxattr(name, CNAME, &count, CSIZE) < 0) {
if (errno == ENOENT) {
- sd_dprintf("sha1 file doesn't exist");
+ fprintf(stderr, "sha1 file doesn't exist.\n");
return -1;
} else
panic("%m");
@@ -99,10 +93,9 @@ static int put_sha1_file(char *name)
count--;
if (count == 0) {
if (unlink(name) < 0) {
- sd_dprintf("%m");
+ fprintf(stderr, "%m\n");
return -1;
}
- sd_dprintf("%s deleted", name);
} else {
if (setxattr(name, CNAME, &count, CSIZE, 0) < 0)
panic("%m");
@@ -110,20 +103,25 @@ 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) {
+ fprintf(stderr,
+ "failed to open file %s with error: %m\n",
+ filename);
ret = -1;
+ }
goto err_open;
}
len = xwrite(fd, buf, size);
if (len != size) {
- sd_dprintf("%m");
+ fprintf(stderr, "%m\n");
close(fd);
return -1;
}
@@ -162,21 +160,22 @@ static void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
return NULL;
}
if (fstat(fd, &st) < 0) {
- sd_dprintf("%m");
+ fprintf(stderr, "%m\n");
close(fd);
return NULL;
}
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (map == MAP_FAILED) {
- sd_dprintf("%m");
+ fprintf(stderr, "%m\n");
return NULL;
}
*size = st.st_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;
@@ -185,7 +184,7 @@ static void *unpack_sha1_file(void *map, unsigned long mapsize, struct sha1_file
hdr_len = sizeof(*hdr);
buf = valloc(hdr->size);
if (!buf) {
- sd_dprintf("%m");
+ fprintf(stderr, "%m\n");
return NULL;
}
@@ -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;
@@ -203,7 +203,7 @@ static int verify_sha1_file(const unsigned char *sha1, void *buf, unsigned long
sha1_final(&c, tmp);
if (memcmp((char *)tmp, (char *)sha1, SHA1_LEN) != 0) {
- sd_dprintf("failed, %s != %s", sha1_to_hex(sha1),
+ fprintf(stderr, "failed, %s != %s\n", sha1_to_hex(sha1),
sha1_to_hex(tmp));
return -1;
}
--
1.7.1
More information about the sheepdog
mailing list