[sheepdog] [PATCH] Add more macros for sha1.h

Liu Yuan namei.unix at gmail.com
Sun Jun 9 06:58:11 CEST 2013


- use SHA1_DIGEST_SIZE instead of home-grew SHA1_LEN
- move snap_log into farm.h

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 collie/common.c          |    3 ++-
 collie/farm/farm.c       |   14 +++++++-------
 collie/farm/farm.h       |   12 +++++++++---
 collie/farm/sha1_file.c  |   12 ++++++------
 collie/farm/snap.c       |    4 ++--
 collie/vdi.c             |    3 ++-
 include/sha1.h           |    7 +++++--
 include/sheepdog_proto.h |    9 ---------
 sheep/plain_store.c      |    5 +++--
 sheep/recovery.c         |    7 ++++---
 10 files changed, 40 insertions(+), 36 deletions(-)

diff --git a/collie/common.c b/collie/common.c
index bb8dd2e..51da1f4 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -10,6 +10,7 @@
  */
 
 #include "collie.h"
+#include "sha1.h"
 
 char *size_to_str(uint64_t _size, char *str, int str_size)
 {
@@ -55,7 +56,7 @@ int sd_read_object_sha1(uint64_t oid, uint32_t epoch, int nr_copies,
 		addr_to_str(host, sizeof(host), vnode->nid.addr, 0);
 		port = vnode->nid.port;
 		if (collie_exec_req(host, port, &req, NULL) == 0) {
-			memcpy(sha1, rsp->hash.digest, SHA1_LEN);
+			memcpy(sha1, rsp->hash.digest, SHA1_DIGEST_SIZE);
 			ret = 0;
 			goto out;
 		}
diff --git a/collie/farm/farm.c b/collie/farm/farm.c
index c08a758..385397f 100644
--- a/collie/farm/farm.c
+++ b/collie/farm/farm.c
@@ -165,7 +165,7 @@ static int get_trunk_sha1(uint32_t idx, const char *tag, unsigned char *outsha1)
 		snap_buf = snap_file_read(log_buf->sha1);
 		if (!snap_buf)
 			goto out;
-		memcpy(outsha1, snap_buf->trunk_sha1, SHA1_LEN);
+		memcpy(outsha1, snap_buf->trunk_sha1, SHA1_DIGEST_SIZE);
 		ret = 0;
 		goto out;
 	}
@@ -213,7 +213,7 @@ out:
 
 bool farm_contain_snapshot(uint32_t idx, const char *tag)
 {
-	unsigned char trunk_sha1[SHA1_LEN];
+	unsigned char trunk_sha1[SHA1_DIGEST_SIZE];
 	return (get_trunk_sha1(idx, tag, trunk_sha1) == 0);
 }
 
@@ -222,7 +222,7 @@ static void do_save_object(struct work *work)
 	void *buf;
 	size_t size;
 	struct snapshot_work *sw;
-	unsigned char object_sha1[SHA1_LEN];
+	unsigned char object_sha1[SHA1_DIGEST_SIZE];
 
 	if (uatomic_is_true(&work_error))
 		return;
@@ -233,7 +233,7 @@ static void do_save_object(struct work *work)
 	if (sd_read_object_sha1(sw->entry.oid, sd_epoch, sw->entry.nr_copies,
 				object_sha1) == 0 &&
 	    sha1_file_exist(object_sha1)) {
-		memcpy(sw->entry.sha1, object_sha1, SHA1_LEN);
+		memcpy(sw->entry.sha1, object_sha1, SHA1_DIGEST_SIZE);
 		return;
 	}
 
@@ -285,8 +285,8 @@ static int queue_save_snapshot_work(uint64_t oid, int nr_copies, void *data)
 
 int farm_save_snapshot(const char *tag)
 {
-	unsigned char snap_sha1[SHA1_LEN];
-	unsigned char trunk_sha1[SHA1_LEN];
+	unsigned char snap_sha1[SHA1_DIGEST_SIZE];
+	unsigned char trunk_sha1[SHA1_DIGEST_SIZE];
 	struct strbuf trunk_buf;
 	void *snap_log = NULL;
 	int log_nr, idx, ret = -1;
@@ -387,7 +387,7 @@ static int queue_load_snapshot_work(struct trunk_entry *entry, void *data)
 int farm_load_snapshot(uint32_t idx, const char *tag)
 {
 	int ret = -1;
-	unsigned char trunk_sha1[SHA1_LEN];
+	unsigned char trunk_sha1[SHA1_DIGEST_SIZE];
 
 	if (get_trunk_sha1(idx, tag, trunk_sha1) < 0)
 		goto out;
diff --git a/collie/farm/farm.h b/collie/farm/farm.h
index ecd76e4..5aca4d8 100644
--- a/collie/farm/farm.h
+++ b/collie/farm/farm.h
@@ -14,7 +14,6 @@
 #include <linux/limits.h>
 
 #include "collie.h"
-#include "sheepdog_proto.h"
 #include "sheep.h"
 #include "logger.h"
 #include "strbuf.h"
@@ -23,7 +22,7 @@
 struct trunk_entry {
 	uint64_t oid;
 	int nr_copies;
-	unsigned char sha1[SHA1_LEN];
+	unsigned char sha1[SHA1_DIGEST_SIZE];
 };
 
 struct trunk_file {
@@ -33,7 +32,14 @@ struct trunk_file {
 
 struct snap_file {
 	int idx;
-	unsigned char trunk_sha1[SHA1_LEN];
+	unsigned char trunk_sha1[SHA1_DIGEST_SIZE];
+};
+
+struct snap_log {
+	uint32_t idx;
+	char tag[SD_MAX_SNAPSHOT_TAG_LEN];
+	uint64_t time;
+	unsigned char sha1[SHA1_DIGEST_SIZE];
 };
 
 /* farm.c */
diff --git a/collie/farm/sha1_file.c b/collie/farm/sha1_file.c
index 5acb1d8..452ee96 100644
--- a/collie/farm/sha1_file.c
+++ b/collie/farm/sha1_file.c
@@ -49,7 +49,7 @@ static void get_sha1(unsigned char *buf, unsigned len, unsigned char *sha1)
 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
 {
 	int i;
-	for (i = 0; i < SHA1_LEN; i++) {
+	for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
 		static const char hex[] = "0123456789abcdef";
 		unsigned int val = sha1[i];
 		char *pos = pathbuf + i*2 + (i > 0);
@@ -157,23 +157,23 @@ bool sha1_file_exist(const unsigned char *sha1)
 
 int sha1_file_write(void *buf, size_t len, unsigned char *outsha1)
 {
-	unsigned char sha1[SHA1_LEN];
+	unsigned char sha1[SHA1_DIGEST_SIZE];
 
 	get_sha1(buf, len, sha1);
 	if (sha1_buffer_write(sha1, buf, len) < 0)
 		return -1;
 	if (outsha1)
-		memcpy(outsha1, sha1, SHA1_LEN);
+		memcpy(outsha1, sha1, SHA1_DIGEST_SIZE);
 	return 0;
 }
 
 static int verify_sha1_file(const unsigned char *sha1,
 			    void *buf, unsigned long len)
 {
-	unsigned char tmp[SHA1_LEN];
+	unsigned char tmp[SHA1_DIGEST_SIZE];
 
 	get_sha1(buf, len, tmp);
-	if (memcmp((char *)tmp, (char *)sha1, SHA1_LEN) != 0) {
+	if (memcmp((char *)tmp, (char *)sha1, SHA1_DIGEST_SIZE) != 0) {
 		fprintf(stderr, "failed, %s != %s\n", sha1_to_hex(sha1),
 			sha1_to_hex(tmp));
 		return -1;
@@ -240,7 +240,7 @@ static unsigned hexval(char c)
 int get_sha1_hex(const char *hex, unsigned char *sha1)
 {
 	int i;
-	for (i = 0; i < SHA1_LEN; i++) {
+	for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
 		unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
 		if (val & ~0xff)
 			return -1;
diff --git a/collie/farm/snap.c b/collie/farm/snap.c
index 14b8aac..071c12d 100644
--- a/collie/farm/snap.c
+++ b/collie/farm/snap.c
@@ -56,7 +56,7 @@ int snap_log_write(uint32_t idx, const char *tag, unsigned char *sha1)
 	struct snap_log log = { .idx = idx,
 				.time = time(NULL) };
 	pstrcpy(log.tag, SD_MAX_SNAPSHOT_TAG_LEN, tag);
-	memcpy(log.sha1, sha1, SHA1_LEN);
+	memcpy(log.sha1, sha1, SHA1_DIGEST_SIZE);
 
 	fd = open(snap_log_path, O_WRONLY | O_APPEND);
 	if (fd < 0) {
@@ -120,7 +120,7 @@ int snap_file_write(uint32_t idx, unsigned char *trunk_sha1,
 {
 	struct snap_file snap;
 	snap.idx = idx;
-	memcpy(snap.trunk_sha1, trunk_sha1, SHA1_LEN);
+	memcpy(snap.trunk_sha1, trunk_sha1, SHA1_DIGEST_SIZE);
 
 	return sha1_file_write(&snap, sizeof(struct snap_file),
 			       outsha1);
diff --git a/collie/vdi.c b/collie/vdi.c
index 4bbae67..bc4ffb5 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -19,6 +19,7 @@
 
 #include "collie.h"
 #include "treeview.h"
+#include "sha1.h"
 
 static struct sd_option vdi_options[] = {
 	{'P', "prealloc", false, "preallocate all the data objects"},
@@ -1435,7 +1436,7 @@ static void write_object_to(const struct sd_vnode *vnode, uint64_t oid,
 struct vdi_check_work {
 	struct vdi_check_info *info;
 	const struct sd_vnode *vnode;
-	uint8_t hash[SHA1_LEN];
+	uint8_t hash[SHA1_DIGEST_SIZE];
 	bool object_found;
 	struct work work;
 };
diff --git a/include/sha1.h b/include/sha1.h
index 332f5c1..dd1b4f4 100644
--- a/include/sha1.h
+++ b/include/sha1.h
@@ -14,10 +14,13 @@
 #include <string.h>
 #include <inttypes.h>
 
+#define SHA1_DIGEST_SIZE        20
+#define SHA1_BLOCK_SIZE         64
+
 struct sha1_ctx {
 	uint64_t count;
-	uint32_t state[5];
-	uint8_t buffer[64];
+	uint32_t state[SHA1_DIGEST_SIZE / 4];
+	uint8_t buffer[SHA1_BLOCK_SIZE];
 };
 
 void sha1_init(void *ctx);
diff --git a/include/sheepdog_proto.h b/include/sheepdog_proto.h
index af4ec32..beef0a6 100644
--- a/include/sheepdog_proto.h
+++ b/include/sheepdog_proto.h
@@ -221,15 +221,6 @@ struct sheepdog_vdi_attr {
 	char value[SD_MAX_VDI_ATTR_VALUE_LEN];
 };
 
-#define SHA1_LEN        20
-
-struct snap_log {
-	uint32_t idx;
-	char tag[SD_MAX_SNAPSHOT_TAG_LEN];
-	uint64_t time;
-	unsigned char sha1[SHA1_LEN];
-};
-
 /* 64 bit FNV-1a non-zero initial basis */
 #define FNV1A_64_INIT ((uint64_t) 0xcbf29ce484222325ULL)
 
diff --git a/sheep/plain_store.c b/sheep/plain_store.c
index 2545219..f5059a3 100644
--- a/sheep/plain_store.c
+++ b/sheep/plain_store.c
@@ -484,7 +484,8 @@ int default_remove_object(uint64_t oid)
 
 static int get_object_sha1(char *path, uint8_t *sha1)
 {
-	if (getxattr(path, SHA1NAME, sha1, SHA1_LEN) != SHA1_LEN) {
+	if (getxattr(path, SHA1NAME, sha1, SHA1_DIGEST_SIZE)
+	    != SHA1_DIGEST_SIZE) {
 		sd_eprintf("fail to get sha1, %s", path);
 		return -1;
 	}
@@ -496,7 +497,7 @@ static int set_object_sha1(char *path, const uint8_t *sha1)
 {
 	int ret;
 
-	ret = setxattr(path, SHA1NAME, sha1, SHA1_LEN, 0);
+	ret = setxattr(path, SHA1NAME, sha1, SHA1_DIGEST_SIZE, 0);
 	if (ret < 0)
 		sd_eprintf("fail to set sha1, %s", path);
 
diff --git a/sheep/recovery.c b/sheep/recovery.c
index 58fad49..63877b1 100644
--- a/sheep/recovery.c
+++ b/sheep/recovery.c
@@ -15,7 +15,7 @@
 #include <sys/types.h>
 
 #include "sheep_priv.h"
-
+#include "sha1.h"
 
 enum rw_state {
 	RW_PREPARE_LIST, /* the recovery thread is preparing object list */
@@ -51,7 +51,7 @@ struct recovery_obj_work {
 
 	/* local replica in the stale directory */
 	uint32_t local_epoch;
-	uint8_t local_sha1[SHA1_LEN];
+	uint8_t local_sha1[SHA1_DIGEST_SIZE];
 };
 
 /*
@@ -140,7 +140,8 @@ static int recover_object_from(struct recovery_obj_work *row,
 		if (ret != SD_RES_SUCCESS)
 			return ret;
 
-		if (memcmp(rsp->hash.digest, sha1, sizeof(SHA1_LEN)) == 0) {
+		if (memcmp(rsp->hash.digest, sha1,
+			   sizeof(SHA1_DIGEST_SIZE)) == 0) {
 			sd_dprintf("use local replica at epoch %d",
 				   local_epoch);
 			ret = sd_store->link(oid, local_epoch);
-- 
1.7.9.5




More information about the sheepdog mailing list