This patch "sheep: converting config file and set version number" converts 
config version 1 and 2 to version 3.

Signed-off-by: Jens Weber <[email protected]>
---
 sheep/store.c |  161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 159 insertions(+), 2 deletions(-)

diff --git a/sheep/store.c b/sheep/store.c
index 0a68d1e..39b1781 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -28,13 +28,52 @@
 #include "util.h"
 #include "farm/farm.h"
 
+#define CONFIGFILE_SIGNATURE   0x0000aaaa00000000
+#define CONFIGFILE_VERSION     3
+#define CONFIGFILE_SIZE        32
+/* 
+ * configfile version 3
+ * store is 'farm'
+ * size 32 bytes
+ */
 struct sheepdog_config {
+       uint64_t configversion;
        uint64_t ctime;
        uint64_t space;
        uint16_t flags;
        uint8_t copies;
        uint8_t store[STORE_LEN];
 };
+/* 
+ * configfile version 2
+ * since v0.4.0 - sheep: get the free disk space and store it in the config 
file (commit 5e39f98abf32faa340afc9b535a56391359b5234)
+ * store is 'farm'
+ * size 24 bytes or 19 bytes if gateway_only
+ *
+ * we keep this for converting config till version <= v0.5.0
+ */
+struct sheepdog_config_2 {
+       uint64_t ctime;
+       uint64_t space;
+       uint16_t flags;
+       uint8_t copies;
+       uint8_t store[STORE_LEN];
+};
+/* 
+ * configfile version 1
+ * since v0.4.0
+ * store is 'farm'
+ * size 16 bytes or 11 bytes if gateway_only 
+ *   sheep: don't call farm_init() for gateway-only node (commit 
23168926dc519982992cc6b8f7b5244f7ed15cb2)
+ * 
+ * we keep this for converting config till version <= v0.5.0
+ */
+struct sheepdog_config_1 {
+       uint64_t ctime;
+       uint16_t flags;
+       uint8_t copies;
+       uint8_t store[STORE_LEN];
+};
 
 char *obj_path;
 char *mnt_path;
@@ -401,11 +440,130 @@ static int init_jrnl_path(const char *base_path)
 
 static int init_config_path(const char *base_path)
 {
+       /*
+        * read and convert config
+        *  new config has a version in the first 8 bytes
+        *  4 bytes version number
+        *  4 bytes fix version signature
+        */
+       int fd, ret;
+       uint64_t version;
+       uint64_t ctime;
+       uint64_t space;
+       uint16_t flags;
+       uint8_t copies;
+       uint8_t filesize;
+       struct stat sb;
+
+       /*
+        * we keep this for converting config till version <= v0.5.0
+        */
+       const char name[] = "farm";
+
        config_path = zalloc(strlen(base_path) + strlen(CONFIG_PATH) + 1);
        sprintf(config_path, "%s" CONFIG_PATH, base_path);
 
-       mknod(config_path, def_fmode, S_IFREG);
+       fd = open(config_path, O_RDONLY);
+       if (fd < 0)
+       {
+               /* new config file */
+               if (mknod(config_path, def_fmode, S_IFREG)) return 1;
+               fd = open(config_path, O_DSYNC | O_WRONLY);
+               if (fd < 0) return 1;
+
+               version = CONFIGFILE_SIGNATURE | CONFIGFILE_VERSION;
+               ret = xpwrite(fd, &version, sizeof(version), offsetof(struct 
sheepdog_config, configversion));
+
+               close(fd);
+               if (ret != sizeof(version)) return 1;
+
+               return 0;
+       }
+
+       if (fstat(fd,&sb) < 0) return 1;
+       filesize = sb.st_size;
+       ret = xpread(fd, &version, sizeof(version),
+                    offsetof(struct sheepdog_config, configversion));
+       close(fd);
+
+       if (ret != sizeof(version)) return 1;
+       if ((version & CONFIGFILE_SIGNATURE) == CONFIGFILE_SIGNATURE)
+       {
+               version = version & 0x00000000ffffffff;
+               if ((version == CONFIGFILE_VERSION) && (filesize == 
CONFIGFILE_SIZE)) return 0;
+               /* convert config file with version number */
+               /* unknown version */
+               return 1;
+       }
+       /* 
+        * convert config files without version number
+        *      
+        * configfile version 2 - since v0.4.0 commit 
5e39f98abf32faa340afc9b535a56391359b5234
+        * size 24 bytes or 19 bytes if gateway_only
+        *
+        * configfile version 1 - since v0.4.0
+        * size 16 bytes of 11 bytes if gateway_only since commit 
23168926dc519982992cc6b8f7b5244f7ed15cb2
+        *
+        * we keep this for converting config till version <= v0.5.0
+        *
+        */
+       if ((filesize == 19) || (filesize == 24))
+       {
+               fd = open(config_path, O_RDONLY);
+               if (fd < 0) return 1;
+
+               ret = xpread(fd, &ctime, sizeof(ctime),
+                            offsetof(struct sheepdog_config_2, ctime));
+               if (ret != sizeof(ctime)) return 1;
+               ret = xpread(fd, &space, sizeof(space),
+                            offsetof(struct sheepdog_config_2, space));
+               if (ret != sizeof(space)) return 1;
+               ret = xpread(fd, &flags, sizeof(flags),
+                            offsetof(struct sheepdog_config_2, flags));
+               if (ret != sizeof(flags)) return 1;
+               ret = xpread(fd, &copies, sizeof(copies),
+                            offsetof(struct sheepdog_config_2, copies));
+               if (ret != sizeof(copies)) return 1;
+
+               close(fd);
+       }
+       else if ((filesize == 11) || (filesize == 16))
+       {
+               fd = open(config_path, O_RDONLY);
+               if (fd < 0) return 1;
+
+               ret = xpread(fd, &ctime, sizeof(ctime),
+                            offsetof(struct sheepdog_config_1, ctime));
+               if (ret != sizeof(ctime)) return 1;
+               ret = xpread(fd, &flags, sizeof(flags),
+                            offsetof(struct sheepdog_config_1, flags));
+               if (ret != sizeof(flags)) return 1;
+               ret = xpread(fd, &copies, sizeof(copies),
+                            offsetof(struct sheepdog_config_1, copies));
+               if (ret != sizeof(copies)) return 1;
+
+               close(fd);
+       }
+       else return 1;
 
+       fd = open(config_path, O_DSYNC | O_WRONLY);
+       if (fd < 0) return 1;
+
+       version = CONFIGFILE_SIGNATURE | CONFIGFILE_VERSION;
+       ret = xpwrite(fd, &version, sizeof(version), offsetof(struct 
sheepdog_config, configversion));
+       if (ret != sizeof(version)) return 1;
+       ret = xpwrite(fd, &ctime, sizeof(ctime), offsetof(struct 
sheepdog_config, ctime));
+       if (ret != sizeof(ctime)) return 1;
+       ret = xpwrite(fd, &space, sizeof(space), offsetof(struct 
sheepdog_config, space));
+       if (ret != sizeof(space)) return 1;
+       ret = xpwrite(fd, &flags, sizeof(flags), offsetof(struct 
sheepdog_config, flags));
+       if (ret != sizeof(flags)) return 1;
+       ret = xpwrite(fd, &copies, sizeof(copies), offsetof(struct 
sheepdog_config, copies));
+       if (ret != sizeof(copies)) return 1;
+       ret = xpwrite(fd, name, strlen(name) + 1, offsetof(struct 
sheepdog_config, store));
+       if (ret != strlen(name) + 1) return 1;
+
+       close(fd);
        return 0;
 }
 
@@ -717,7 +875,6 @@ int set_cluster_store(const char *name, uint8_t 
gateway_only)
        len = strlen(name) + 1;
        if (len > STORE_LEN)
                goto err;
-
        /* We don't need this for gateway-only node */
        if (!gateway_only) {
                jd = jrnl_begin(name, len,
-- 
1.7.10.4
-- 
sheepdog mailing list
[email protected]
http://lists.wpkg.org/mailman/listinfo/sheepdog

Reply via email to