[sheepdog] [PATCH] sheep: add a helper function to copy out data from strbuf

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Sun May 20 17:23:53 CEST 2012


This patch also fixes a problem that local_get_store_list() doesn't
set a null terminated string.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 sheep/farm/farm.c    |    4 ++--
 sheep/object_cache.c |    2 +-
 sheep/ops.c          |    2 +-
 sheep/recovery.c     |    2 +-
 sheep/strbuf.c       |    7 +++++++
 sheep/strbuf.h       |    1 +
 6 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c
index d9c63a8..9691c43 100644
--- a/sheep/farm/farm.c
+++ b/sheep/farm/farm.c
@@ -41,7 +41,7 @@ static int create_directory(char *p)
 	}
 
 	if (!strlen(farm_dir))
-		memcpy(farm_dir, buf.buf, buf.len);
+		strbuf_copyout(&buf, farm_dir);
 
 	strbuf_addstr(&buf, "/objects");
 	if (mkdir(buf.buf, 0755) < 0) {
@@ -64,7 +64,7 @@ static int create_directory(char *p)
 	}
 
 	if (!strlen(farm_obj_dir))
-		memcpy(farm_obj_dir, buf.buf, buf.len);
+		strbuf_copyout(&buf, farm_obj_dir);
 err:
 	strbuf_release(&buf);
 	return ret;
diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index cf1c302..dab5dfc 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -707,7 +707,7 @@ int object_cache_init(const char *p)
 			goto err;
 		}
 	}
-	memcpy(cache_dir, buf.buf, buf.len);
+	strbuf_copyout(&buf, cache_dir);
 err:
 	strbuf_release(&buf);
 	return ret;
diff --git a/sheep/ops.c b/sheep/ops.c
index 1b35552..b829e34 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -312,7 +312,7 @@ static int local_get_store_list(struct request *req)
 	list_for_each_entry(driver, &store_drivers, list) {
 		strbuf_addf(&buf, "%s ", driver->name);
 	}
-	memcpy(req->data, buf.buf, buf.len);
+	strbuf_copyout(&buf, req->data);
 
 	strbuf_release(&buf);
 	return SD_RES_SUCCESS;
diff --git a/sheep/recovery.c b/sheep/recovery.c
index e31f226..71d0176 100644
--- a/sheep/recovery.c
+++ b/sheep/recovery.c
@@ -666,7 +666,7 @@ static int screen_obj_list(struct recovery_work *rw,  uint64_t *list, int list_n
 			continue;
 		strbuf_add(&buf, &list[i], sizeof(uint64_t));
 	}
-	memcpy(list, buf.buf, buf.len);
+	strbuf_copyout(&buf, list);
 
 	ret = buf.len / sizeof(uint64_t);
 	dprintf("%d\n", ret);
diff --git a/sheep/strbuf.c b/sheep/strbuf.c
index 1a5f855..142e9f9 100644
--- a/sheep/strbuf.c
+++ b/sheep/strbuf.c
@@ -190,3 +190,10 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 		strbuf_setlen(sb, sb->len-1);
 	return 0;
 }
+
+int strbuf_copyout(struct strbuf *sb, void *buf)
+{
+	memcpy(buf, sb->buf, sb->len + 1);
+
+	return sb->len;
+}
diff --git a/sheep/strbuf.h b/sheep/strbuf.h
index 573784b..03efd99 100644
--- a/sheep/strbuf.h
+++ b/sheep/strbuf.h
@@ -91,5 +91,6 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 /* XXX: if read fails, any partial read is undone */
 extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
 int strbuf_getline(struct strbuf *sb, FILE *fp, int term);
+extern int strbuf_copyout(struct strbuf *sb, void *buf);
 
 #endif
-- 
1.7.2.5




More information about the sheepdog mailing list