moved trunk.c from sheep/farm to collie/farm Changes: 1. trunk don't need to read file from plain_store. 2. farm.c will responsible for read objects and fill trunk entry. 3. add for_each_object_in_tree(). Signed-off-by: Kai Zhang <kyle at zelin.io> --- collie/Makefile.am | 1 + collie/farm/farm.h | 13 ++++ {sheep => collie}/farm/trunk.c | 123 +++++++++++++++------------------------- 3 files changed, 59 insertions(+), 78 deletions(-) rename {sheep => collie}/farm/trunk.c (44%) diff --git a/collie/Makefile.am b/collie/Makefile.am index 8cab2a3..cc8a283 100644 --- a/collie/Makefile.am +++ b/collie/Makefile.am @@ -24,6 +24,7 @@ INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include sbin_PROGRAMS = collie collie_SOURCES = farm/object_tree.c farm/sha1_file.c farm/snap.c \ + farm/trunk.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 451ebea..d2ee35c 100644 --- a/collie/farm/farm.h +++ b/collie/farm/farm.h @@ -25,6 +25,12 @@ #define TAG_TRUNK "trunk\0" #define TAG_SNAP "snap\0\0" +struct trunk_entry { + uint64_t oid; + int nr_copies; + unsigned char sha1[SHA1_LEN]; +}; + struct sha1_file_hdr { char tag[TAG_LEN]; uint64_t size; @@ -43,6 +49,13 @@ static inline char *get_object_directory(void) typedef int (*object_handler_func_t)(uint64_t oid, int nr_copies, void *buf, size_t size, void *data); +/* 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 *); +int for_each_object_in_trunk(unsigned char *trunk_sha1, + object_handler_func_t func, void *data); + /* snap.c */ int snap_init(const char *path); void *snap_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr); diff --git a/sheep/farm/trunk.c b/collie/farm/trunk.c similarity index 44% rename from sheep/farm/trunk.c rename to collie/farm/trunk.c index c26f960..c0b416f 100644 --- a/sheep/farm/trunk.c +++ b/collie/farm/trunk.c @@ -1,7 +1,9 @@ /* * Copyright (C) 2011 Taobao Inc. + * Copyright (C) 2013 Zelin.io * * 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 @@ -25,80 +27,16 @@ #include "list.h" #include "util.h" #include "sheepdog_proto.h" -#include "sheep_priv.h" -static int fill_entry_new_sha1(struct trunk_entry *entry) -{ - struct strbuf buf = STRBUF_INIT; - int fd, ret = 0; - struct sha1_file_hdr hdr = { .priv = 0 }; - - memcpy(hdr.tag, TAG_DATA, TAG_LEN); - strbuf_addstr(&buf, md_get_object_path(entry->oid)); - strbuf_addf(&buf, "/%016" PRIx64, entry->oid); - fd = open(buf.buf, O_RDONLY); - if (fd < 0) { - sd_dprintf("%m, %s", buf.buf); - ret = -1; - goto out; - } - strbuf_reset(&buf); - if (!strbuf_read(&buf, fd, SD_DATA_OBJ_SIZE) == SD_DATA_OBJ_SIZE) { - sd_dprintf("strbuf_read fail to read full"); - ret = -1; - goto out_close; - } - hdr.size = buf.len; - strbuf_insert(&buf, 0, &hdr, sizeof(hdr)); - - if (sha1_file_write((void *)buf.buf, buf.len, entry->sha1) < 0) { - ret = -1; - goto out_close; - } - sd_dprintf("data sha1:%s, %"PRIx64, sha1_to_hex(entry->sha1), - entry->oid); -out_close: - close(fd); -out: - strbuf_release(&buf); - return ret; -} - -static int inc_object_nr(uint64_t oid, char *wd, void *arg) -{ - uint64_t *object_nr = arg; - - (*object_nr)++; - - return SD_RES_SUCCESS; -} - -static int init_trunk_entry(uint64_t oid, char *path, void *arg) -{ - struct trunk_entry entry = {}; - struct strbuf *buf = arg; - - entry.oid = oid; - if (fill_entry_new_sha1(&entry) < 0) - return SD_RES_UNKNOWN; - - strbuf_add(buf, &entry, sizeof(struct trunk_entry)); - return SD_RES_SUCCESS; -} - -int trunk_file_write(unsigned char *outsha1) +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 = 0; + int ret = -1; - /* Add the hdr first */ - for_each_object_in_wd(inc_object_nr, false, &object_nr); - if (ret != SD_RES_SUCCESS) { - ret = -1; - goto out; - } + /* Init trunk hdr */ + object_nr = object_tree_size(); data_size = sizeof(struct trunk_entry) * object_nr; hdr.size = data_size; hdr.priv = object_nr; @@ -106,17 +44,14 @@ int trunk_file_write(unsigned char *outsha1) strbuf_init(&buf, sizeof(hdr) + data_size); strbuf_add(&buf, &hdr, sizeof(hdr)); - ret = for_each_object_in_wd(init_trunk_entry, false, &buf); - if (ret != SD_RES_SUCCESS) { - ret = -1; - goto out; - } + /* trunk entries */ + strbuf_addbuf(&buf, trunk_entries); - if (sha1_file_write((void *)buf.buf, buf.len, outsha1) < 0) { - ret = -1; + /* write to sha1 file */ + if (sha1_file_write((void *)buf.buf, buf.len, trunk_sha1) < 0) goto out; - } - sd_dprintf("trunk sha1: %s", sha1_to_hex(outsha1)); + + ret = 0; out: strbuf_release(&buf); return ret; @@ -126,7 +61,6 @@ void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr) { void *buffer; - sd_dprintf("%s", sha1_to_hex(sha1)); buffer = sha1_file_read(sha1, outhdr); if (!buffer) return NULL; @@ -137,3 +71,36 @@ void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr) return buffer; } + +int for_each_object_in_trunk(unsigned char *trunk_sha1, + object_handler_func_t func, void *data) +{ + struct trunk_entry *trunk_entry, *trunk_free = NULL; + struct sha1_file_hdr trunk_hdr; + uint64_t nr_trunks; + int ret = -1; + + trunk_free = trunk_entry = trunk_file_read(trunk_sha1, &trunk_hdr); + + if (!trunk_entry) + goto out; + + nr_trunks = trunk_hdr.priv; + for (uint64_t i = 0; i < nr_trunks; i++, trunk_entry++) { + struct sha1_file_hdr hdr; + void *buffer = NULL; + + buffer = sha1_file_read(trunk_entry->sha1, &hdr); + if (!buffer) + goto out; + + if (func(trunk_entry->oid, trunk_entry->nr_copies, + buffer, hdr.size, data) < 0) + goto out; + } + + ret = 0; +out: + free(trunk_free); + return ret; +} -- 1.7.1 |