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;
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;
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';
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);
}
}
} 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()
notfound:
_conf_copy<T>(val, def_val);
- if (auto_update)
- _write<T>(section, var, def_val);
-
return 0;
}
- cf.set_auto_update(true);
-
cf.read("core", "repositoryformatversion", &val, 12);
cf.read("foo", "lala1", &val, 10);
cf.read("foo", "lala2", &val, 11);
#include <string>
#include <list>
+#include "include/buffer.h"
+#include "common/Mutex.h"
+
class ConfLine {
char *prefix;
char *var;
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 *);
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; }
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