From: Yehuda Sadeh Date: Tue, 11 Jan 2011 22:06:33 +0000 (-0800) Subject: conf: ConfFile can parse bufferlists X-Git-Tag: v0.25~342 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=70f3c2c21067e57ed8c45bbcb15d0e76de4c1a63;p=ceph.git conf: ConfFile can parse bufferlists --- diff --git a/src/common/ConfUtils.cc b/src/common/ConfUtils.cc index 19b19d911708..295651ef1744 100644 --- a/src/common/ConfUtils.cc +++ b/src/common/ConfUtils.cc @@ -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(), §ion)) { 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, §ion); + bool res = _parse(filename, §ion); + 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(val, def_val); - if (auto_update) - _write(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); diff --git a/src/common/ConfUtils.h b/src/common/ConfUtils.h index 14c2be7d5009..c7a08682e01b 100644 --- a/src/common/ConfUtils.h +++ b/src/common/ConfUtils.h @@ -7,6 +7,9 @@ #include #include +#include "include/buffer.h" +#include "common/Mutex.h" + class ConfLine { char *prefix; char *var; @@ -65,7 +68,11 @@ typedef std::list 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 diff --git a/src/config.cc b/src/config.cc index 791935a990ca..d7149c79b16e 100644 --- a/src/config.cc +++ b/src/config.cc @@ -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;