]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
conf: ConfFile can parse bufferlists
authorYehuda Sadeh <yehuda@hq.newdream.net>
Tue, 11 Jan 2011 22:06:33 +0000 (14:06 -0800)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 12 Jan 2011 18:21:56 +0000 (10:21 -0800)
src/common/ConfUtils.cc
src/common/ConfUtils.h
src/config.cc

index 19b19d911708f4017d2c589590038cc4c7ba1e38..295651ef1744b7dbaec198419bac8b150785812e 100644 (file)
@@ -473,15 +473,31 @@ ConfLine::~ConfLine()
                free(section);
 }
 
-ConfFile::ConfFile(const char *fname)
-  : filename(NULL), auto_update(false), post_process_func(NULL)
+void ConfFile::common_init()
 {
+  filename = NULL;
+  pbl = NULL;
+  buf_pos = 0;
+  post_process_func = NULL;
+  default_global = true;
+}
+
+ConfFile::ConfFile(const char *fname) : parse_lock("ConfFile::parse_lock")
+{
+  common_init();
+
   if (fname)
     filename = strdup(fname);
   else
     filename = NULL;
 }
 
+ConfFile::ConfFile(ceph::bufferlist *_pbl) : parse_lock("ConfFile::parse_lock")
+{
+  common_init();
+  pbl =  _pbl;
+}
+
 ConfFile::~ConfFile()
 {
        SectionList::iterator sec_iter, sec_end;
@@ -599,7 +615,49 @@ ConfSection *ConfFile::_add_section(const char *section, ConfLine *cl)
        return sec;
 }
 
-bool ConfFile::_parse(char *filename, ConfSection **psection)
+int ConfFile::_open()
+{
+       if (filename)
+               return open(filename, O_RDONLY);
+
+       if (!pbl)
+               return -EINVAL;
+
+       buf_pos = 0;
+
+       return 0;
+}
+
+int ConfFile::_read(int fd, char *buf, size_t size)
+{
+       if (filename)
+               return ::read(fd, buf, size);
+
+       if (!pbl)
+               return -EINVAL;
+
+       size_t left = min(pbl->length() - buf_pos, size);
+
+       if (left) {
+               memcpy(buf, pbl->c_str() + buf_pos, left);
+               buf_pos += left;
+       }
+
+       return left;
+}
+
+int ConfFile::_close(int fd)
+{
+       if (filename)
+               return close(fd);
+
+       if (!pbl)
+               return -EINVAL;
+
+       return 0;
+}
+
+bool ConfFile::_parse(const char *filename, ConfSection **psection)
 {
        char *buf;
        int len, i, l;
@@ -609,17 +667,23 @@ bool ConfFile::_parse(char *filename, ConfSection **psection)
        int fd;
        int max_line = MAX_LINE;
        int eof = 0;
+       bool ret = true;
 
-       fd = open(filename, O_RDONLY);
+       fd = _open();
        if (fd < 0)
-               return 0;
+               return false;
 
        line = (char *)malloc(max_line);
        l = 0;
 
        buf = (char *)malloc(BUF_SIZE);
        do {
-               len = ::read(fd, buf, BUF_SIZE);
+               len = _read(fd, buf, BUF_SIZE);
+               if (len < 0) {
+                       ret = false;
+                       goto done;
+               }
+
                if (len < BUF_SIZE) {
                        eof = 1;
                        buf[len] = '\0';
@@ -641,11 +705,19 @@ bool ConfFile::_parse(char *filename, ConfSection **psection)
                                parse_line(line, cl);
                                if (cl->get_var()) {
                                        if (strcmp(cl->get_var(), "include") == 0) {
+                                               if (!filename) { // don't allow including if we're not using files
+                                                       ret = false;
+                                                       goto done;
+                                               }
                                                if (!_parse(cl->get_val(), &section)) {
                                                        printf("error parsing %s\n", cl->get_val());
                                                }
                                        } else {
                                                char *norm_var = cl->get_norm_var();
+                                               if (!section) {
+                                                       ret = false;
+                                                       goto done;
+                                               }
                                                section->conf_map[norm_var] = cl;
                                                free(norm_var);
                                                global_list.push_back(cl);
@@ -668,25 +740,31 @@ bool ConfFile::_parse(char *filename, ConfSection **psection)
                        }
                }
        } while (!eof);
-
+done:
        free(buf);
 
        *psection = section;
-       close(fd);
+       _close(fd);
        free(line);
 
-       return 1;
+       return ret;
 }
 
 bool ConfFile::parse()
 {
-       ConfSection *section;
+       ConfSection *section = NULL;
+
+       parse_lock.Lock();
 
-       section = new ConfSection("global");
-       sections["global"] = section;
-       sections_list.push_back(section);
+       if (default_global) {
+               section = new ConfSection("global");
+               sections["global"] = section;
+               sections_list.push_back(section);
+       }
 
-       return _parse(filename, &section);
+       bool res = _parse(filename, &section);
+       parse_lock.Unlock();
+       return res;
 }
 
 int ConfFile::flush()
@@ -941,9 +1019,6 @@ int ConfFile::_read(const char *section, const char *var, T *val, T def_val)
 notfound:
        _conf_copy<T>(val, def_val);
 
-       if (auto_update)
-               _write<T>(section, var, def_val);
-
        return 0;
 }
 
@@ -1084,8 +1159,6 @@ int main(int argc, char *argv[])
 
 
 
-       cf.set_auto_update(true);
-
        cf.read("core", "repositoryformatversion", &val, 12);
        cf.read("foo", "lala1", &val, 10);
        cf.read("foo", "lala2", &val, 11);
index 14c2be7d5009e6034b8971e02b2160da57ccbeed..c7a08682e01b4032765e17efc84686bd1b58e16b 100644 (file)
@@ -7,6 +7,9 @@
 #include <string>
 #include <list>
 
+#include "include/buffer.h"
+#include "common/Mutex.h"
+
 class ConfLine {
        char *prefix;
        char *var;
@@ -65,7 +68,11 @@ typedef std::list<ConfSection *> SectionList;
 
 class ConfFile {
        char *filename;
-       bool auto_update;
+
+       ceph::bufferlist *pbl;
+       size_t buf_pos;
+       Mutex parse_lock;
+       bool default_global;
 
        char *(*post_process_func)(const char *);
 
@@ -84,9 +91,15 @@ class ConfFile {
 
        ConfSection *_add_section(const char *section, ConfLine *cl);
        void _dump(int fd);
-       bool _parse(char *filename, ConfSection **psection);
+       bool _parse(const char *filename, ConfSection **psection);
+       void common_init();
+
+       int _read(int fd, char *buf, size_t size);
+       int _open();
+       int _close(int fd);
 public:
         ConfFile(const char *fname);
+        ConfFile(ceph::bufferlist *bl);
        ~ConfFile();
 
        const SectionList& get_section_list() { return sections_list; }
@@ -113,8 +126,8 @@ public:
 
        void dump();
        int flush();
-       void set_auto_update(bool update) { auto_update = update; }
        void set_post_process_func(char *(*func)(const char *)) {post_process_func = func; };
+       void set_global(bool global) { default_global = global; }
 };
 
 #endif
index 791935a990ca03857f3552fe4dcc220bd8d3c9f4..d7149c79b16e5a5ed4e035e44e01be979c2fc780 100644 (file)
@@ -1114,7 +1114,6 @@ bool parse_config_file(ConfFile *cf, bool auto_update)
 {
   int opt_len = sizeof(config_optionsp)/sizeof(config_option);
 
-  cf->set_auto_update(false);
   cf->set_post_process_func(conf_post_process_val);
   if (!cf->parse())
        return false;