]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
confutils: can flush config, preliminary config.cc integration
authorYehuda Sadeh <yehuda@hq.newdream.net>
Thu, 22 Jan 2009 21:41:56 +0000 (13:41 -0800)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Fri, 23 Jan 2009 17:31:42 +0000 (09:31 -0800)
src/Makefile.am
src/common/ConfUtils.cc
src/common/ConfUtils.h
src/config.cc
src/config.h

index 5967c318be22be27eda981ef14f66341c6f4028e..09eb5504b41c20d9c5b3248515cee848806c4909 100644 (file)
@@ -183,6 +183,7 @@ libcommon_a_SOURCES = \
        common/assert.cc \
        common/debug.cc \
        common/WorkQueue.cc \
+       common/ConfUtils.cc \
        mon/MonMap.cc \
        mon/MonClient.cc \
        osd/OSDMap.cc \
@@ -274,6 +275,7 @@ noinst_HEADERS = \
        common/BackTrace.h\
         common/Clock.h\
         common/Cond.h\
+        common/ConfUtils.h\
         common/DecayCounter.h\
         common/Finisher.h\
         common/LogType.h\
index 83c37d4e16766ddc8ea7a23ec078734f14f434c6..839e1a9d1888398027d25d49cfa52bf7d75a33e0 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <errno.h>
 
 #include <map>
 #include <list>
@@ -25,7 +26,7 @@ struct ltstr
 static const char *_def_delim=" \t\n\r";
 static const char *_eq_delim="= \t\n\r";
 static const char *_eol_delim="\n\r";
-static const char *_pr_delim="[] \t\n\r";
+/* static const char *_pr_delim="[] \t\n\r"; */
 
 
 static int is_delim(char c, const char *delim)
@@ -41,10 +42,8 @@ static int is_delim(char c, const char *delim)
 
 char *get_next_tok(char *str, const char *delim, int alloc, char **p)
 {
-       char *tok;
        int i=0;
        char *out;
-       int is_str = 0;
 
        while (*str && is_delim(*str, delim)) {
                str++;
@@ -82,7 +81,6 @@ char *get_next_tok(char *str, const char *delim, int alloc, char **p)
 
 char *get_next_delim(char *str, const char *delim, int alloc, char **p)
 {
-       char *tok;
        int i=0;
        char *out;
 
@@ -105,7 +103,6 @@ char *get_next_delim(char *str, const char *delim, int alloc, char **p)
 
 static int _parse_section(char *str, ConfLine *parsed)
 {
-       char *open, *close;
        char *name = NULL;
        char *p;
        int ret = 0;
@@ -147,7 +144,6 @@ static int _parse_section(char *str, ConfLine *parsed)
        if (*line)      
                parsed->set_section(line);
 
-out:
        return ret;
 }
 
@@ -155,8 +151,6 @@ int parse_line(char *line, ConfLine *parsed)
 {
        char *dup=strdup(line);
        char *p = NULL;
-       char *tok;
-       int i;
        char *eq;
        int ret = 0;
 
@@ -203,7 +197,7 @@ out:
 
 static int _str_cat(char *str1, int max, char *str2)
 {
-       int len;
+       int len = 0;
 
        if (max)
                len = snprintf(str1, max, "%s", str2);
@@ -299,6 +293,9 @@ ConfFile::~ConfFile()
 
                delete sec;
        }
+
+       if (fd >= 0)
+               close(fd);
 }
 
 int ConfLine::output(char *line, int max_len)
@@ -324,7 +321,7 @@ int ConfLine::output(char *line, int max_len)
 }
 
 
-void ConfFile::dump()
+void ConfFile::_dump(int fd)
 {
        SectionList::iterator sec_iter, sec_end;
         ConfLine *cl;
@@ -335,8 +332,6 @@ void ConfFile::dump()
 
        sec_end=sections_list.end();
 
-       printf("------ config starts here ------\n");
-
        for (sec_iter=sections_list.begin(); sec_iter != sec_end; ++sec_iter) {
                ConfList::iterator iter, end;
                ConfSection *sec;
@@ -352,10 +347,21 @@ void ConfFile::dump()
                        if (cl) {
                                line[0] = '\0';
                                cl->output(line, MAX_LINE);
-                               printf("%s\n", line);
+                               ::write(fd, line, strlen(line));
+                               ::write(fd, "\n", 1);
                        }
                }
        }
+}
+
+void ConfFile::dump()
+{
+       SectionList::iterator sec_iter, sec_end;
+
+       sec_end=sections_list.end();
+
+       printf("------ config starts here ------\n");
+       _dump(STDOUT_FILENO);
        printf("------  config ends here  ------\n");
 }
 
@@ -413,6 +419,34 @@ int ConfFile::parse()
        return 1;
 }
 
+int ConfFile::flush()
+{
+       int rc;
+
+       if (fd < 0) {
+               fd = open(filename, O_RDWR | O_CREAT);
+
+               if (fd < 0) {
+                       printf("error opening file %s errno=%d\n", filename, errno);
+                       return 0;
+               }
+       } else {
+               rc = lseek(fd, 0, SEEK_SET);
+               if (rc < 0) {
+                       printf("error seeking file %s errno = %d\n", filename, errno);
+                       return 0;
+               }
+       }
+
+       _dump(fd);
+       rc = fsync(fd);
+
+       if (rc < 0)
+               return 0;
+
+       return 1;
+}
+
 ConfLine *ConfFile::_find_var(const char *section, const char* var)
 {
        SectionMap::iterator iter = sections.find(section);
@@ -453,7 +487,6 @@ ConfLine *ConfFile::_add_var(const char *section, const char* var)
                memset(cl, 0, sizeof(ConfLine));
                snprintf(buf, sizeof(buf), "[%s]", section);
                cl->set_prefix(buf);
-               cl->set_mid(" = ");
                sec->conf_list.push_back(cl);
        } else {
                sec = iter->second;
@@ -477,6 +510,7 @@ static void _conf_copy(T *dst_val, T def_val)
        *dst_val = def_val;
 }
 
+template<char *>
 static void _conf_copy(char **dst_val, char *def_val)
 {
        *dst_val = strdup(def_val);
@@ -507,7 +541,7 @@ static void _conf_decode(char **dst_val, char *str_val)
 
        if (*str_val == '"') {
                str_val++;
-               for (len-1; len > 0; len--) {
+               for (len = len-1; len > 0; len--) {
                        if (str_val[len] == '"')
                                break;
                }
@@ -529,7 +563,7 @@ static void _conf_encode(char *dst_str, int len, int val)
 
 static void _conf_encode(char *dst_str, int len, float val)
 {
-       snprintf(dst_str, len, "%f", val);
+       snprintf(dst_str, len, "%g", val);
 }
 
 static void _conf_encode(char *dst_str, int len, bool val)
@@ -537,11 +571,11 @@ static void _conf_encode(char *dst_str, int len, bool val)
        snprintf(dst_str, len, "%s", (val ? "true" : "false"));
 }
 
-static void _conf_encode(char *dst_str, int len, char *val)
+static void _conf_encode(char *dst_str, int max_len, char *val)
 {
        int have_delim = 0;
        int i;
-       len = strlen(val);
+       int len = strlen(val);
 
        for (i=0; i<len; i++) {
                if (is_delim(val[i], _def_delim)) {
@@ -551,9 +585,9 @@ static void _conf_encode(char *dst_str, int len, char *val)
        }
 
        if (have_delim)
-               snprintf(dst_str, len, """%s""", val);
+               snprintf(dst_str, max_len, "\"%s\"", val);
        else
-               snprintf(dst_str, len, "%s", val);
+               snprintf(dst_str, max_len, "%s", val);
 
        return;
 }
@@ -572,6 +606,10 @@ int ConfFile::_read(const char *section, const char *var, T *val, T def_val)
        return 1;
 notfound:
        _conf_copy<T>(val, def_val);
+
+       if (auto_update)
+               _write<T>(section, var, def_val);
+
        return 0;
 }
 
@@ -601,9 +639,9 @@ int ConfFile::read(const char *section, const char *var, bool *val, bool def_val
        return _read<bool>(section, var, val, def_val);
 }
 
-int ConfFile::read(const char *section, const char *var, char **val, char *def_val)
+int ConfFile::read(const char *section, const char *var, char **val, const char *def_val)
 {
-       return _read<char *>(section, var, val, def_val);
+       return _read<char *>(section, var, val, (char *)def_val);
 }
 
 int ConfFile::read(const char *section, const char *var, float *val, float def_val)
@@ -631,7 +669,7 @@ int ConfFile::write(const char *section, const char *var, char *val)
 {
        return _write<char *>(section, var, val);
 }
-
+#if 0
 void parse_test(char *line)
 {
        ConfLine cl;
@@ -645,21 +683,39 @@ void parse_test(char *line)
        printf("val: '%s'\n", cl.get_val());
        printf("suf: '%s'\n", cl.get_suffix());
        printf("section: '%s'\n", cl.get_section());
-
 }
 
 int main(int argc, char *argv[])
 {
        ConfFile cf(argv[1]);
        int val;
+       char *str_val;
+       float fval;
+       bool bval;
        cf.parse();
        cf.dump();
+
+       cf.set_auto_update(true);
+
        cf.read("core", "repositoryformatversion", &val, 12);
-       cf.write("core", "lola", 15);
-       cf.write("zore", "lola", 15);
+       cf.read("foo", "lala1", &val, 10);
+       cf.read("foo", "lala2", &val, 11);
+       cf.read("foo", "lala3", &val, 12);
+       cf.read("foo2", "lala4", &val, 13);
+       cf.read("foo", "lala5", &val, 14);
+       cf.read("foo", "lala6", &fval, 14.2);
+       cf.read("foo", "lala7", &bval, false);
+
+       cf.read("foo", "str", &str_val, "hello world2");
+
+       printf("read str=%s\n", str_val);
+       printf("read bool=%d\n", bval);
+       printf("read float=%f\n", fval);
        cf.dump();
+       cf.flush();
 
        printf("read val=%d\n", val);
 
        return 0;
 }
+#endif
index b60562291c4989754ae277049c9ee2b1255268b2..5fcfe40c2021790e73e822a0075a1e3d4f229de5 100644 (file)
@@ -61,6 +61,7 @@ typedef std::list<ConfSection *> SectionList;
 class ConfFile {
        int fd;
        char *filename;
+       bool auto_update;
 
        SectionMap sections;
        SectionList sections_list;
@@ -70,19 +71,20 @@ class ConfFile {
        ConfLine *_add_var(const char *section, const char* var);
 
        template<typename T>
-       int _read(const char *section, const char *var, T *val, T def_val);
+       int _read(const char *section, const char *var, T *val, const T def_val);
 
        template<typename T>
-       int _write(const char *section, const char *var, T val);
+       int _write(const char *section, const char *var, const T val);
+
+       void _dump(int fd);
 public:
-       ConfFile(char *fname) : filename(strdup(fname)) {}
+       ConfFile(const char *fname) : fd(-1), filename(strdup(fname)), auto_update(false) {}
        ~ConfFile();
 
        int parse();
        int read(const char *section, const char *var, int *val, int def_val);
        int read(const char *section, const char *var, bool *val, bool def_val);
-/*     int read(const char *section, const char *var, char *val, int size, char *def_val); */
-       int read(const char *section, const char *var, char **val, char *def_val); /* allocates new val */
+       int read(const char *section, const char *var, char **val, const char *def_val);
        int read(const char *section, const char *var, float *val, float def_val);
 
        int write(const char *section, const char *var, int val);
@@ -91,6 +93,8 @@ public:
        int write(const char *section, const char *var, char *val);
 
        void dump();
+       int flush();
+       void set_auto_update(bool update) { auto_update = update; }
 };
 
 #endif
index ae173710fb717661b1bb33eeddfdf36eee51b4af..7bdab443403ead77cf92e1000900835b3936c5bc 100644 (file)
@@ -203,6 +203,8 @@ md_config_t g_conf = {
 
   dout_dir: "out",    // if daemonize == true
   dout_sym_dir: "out",    // if daemonize == true
+
+  conf_file: "ceph.conf",
   
   fake_clock: false,
   fakemessenger_serialize: true,
@@ -599,31 +601,35 @@ void sighup_handler(int signum)
 }
 
 
-void parse_config_file(char *fname)
+#define CF_READ(section, var, inout) \
+  cf.read(section, var, &g_conf.inout, g_conf.inout)
+
+#define CF_READ_STR(section, var, inout) \
+  cf.read(section, var, (char **)&g_conf.inout, (char *)g_conf.inout)
+
+void parse_config_file(const char *fname)
 {
   ConfFile cf(fname);
 
+  cf.set_auto_update(true);
+
   cf.parse();
 
-#define CF_READ(section, type, field, inout) \
-  cf.read_##type((char *)section, (char *)#field, &inout, inout)
-
-#define CF_READ_STR(section, type, field, inout) \
-  cf.read_##type((char *)section, (char *)#field, (char **)&inout, (char *)inout)
-
-  CF_READ("global", int, num_mon, g_conf.num_mon);
-  CF_READ("global", int, num_mon, g_conf.num_mds);
-  CF_READ("global", int, num_mon, g_conf.num_osd);
-  CF_READ("global", bool, mkfs, g_conf.mkfs);
-  CF_READ("global", bool, daemonize, g_conf.daemonize);
-  CF_READ("global", bool, file_logs, g_conf.file_logs);
-  CF_READ("global", bool, log, g_conf.log);
-  CF_READ("global", int, log_interval, g_conf.log_interval);
-  CF_READ_STR("global", str_alloc, log_name, g_conf.log_name);
-  CF_READ("global", bool, log_messages, g_conf.log_messages);
-  CF_READ("global", bool, log_pins, g_conf.log_pins);
-  CF_READ_STR("global", str_alloc, dout_dir, g_conf.dout_dir);
-  CF_READ_STR("global", str_alloc, dout_sym_dir, g_conf.dout_sym_dir);
+  CF_READ("global", "num_mon", num_mon);
+  CF_READ("global", "num_mds", num_mds);
+  CF_READ("global", "num_osd", num_osd);
+  CF_READ("global", "mkfs", mkfs);
+  CF_READ("global", "daemonize", daemonize);
+  CF_READ_STR("global", "file_logs", file_logs);
+  CF_READ("global", "log", log);
+  CF_READ("global", "log_interval", log_interval);
+  CF_READ_STR("global", "str_alloc", log_name);
+  CF_READ("global", "log_messages", log_messages);
+  CF_READ("global", "log_pins", log_pins);
+  CF_READ_STR("global", "dout_dir", dout_dir);
+  CF_READ_STR("global", "dout_sym_dir", dout_sym_dir);
+
+  cf.flush();
 }
 
 void parse_config_options(std::vector<const char*>& args, bool open)
@@ -705,6 +711,8 @@ void parse_config_options(std::vector<const char*>& args, bool open)
     else if (//strcmp(args[i], "-o") == 0 ||
             strcmp(args[i], "--dout_sym_dir") == 0) 
       g_conf.dout_sym_dir = args[++i];
+    else if (strcmp(args[i], "--conf_file") == 0) 
+      g_conf.conf_file = args[++i];
 
     else if (strcmp(args[i], "--lockdep") == 0)
       g_lockdep = atoi(args[++i]);
@@ -1187,6 +1195,8 @@ void parse_config_options(std::vector<const char*>& args, bool open)
     }
   }
 
+  parse_config_file(g_conf.conf_file);
+
   // open log file?
   if (open)
     _dout_open_log();
index 84e46cd85400edd90c6ad92abcc600788d3271c3..a2fccafee5daca5f47c229816be1541ac19399f9 100644 (file)
@@ -64,6 +64,8 @@ struct md_config_t {
   const char *dout_dir;
   const char *dout_sym_dir;
 
+  const char *conf_file;
+
   bool fake_clock;
   bool fakemessenger_serialize;