diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2015-06-03 23:38:36 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2015-06-03 23:39:29 +0200 |
| commit | 052a162715449252bb6126c41dd1700b1440c394 (patch) | |
| tree | d4277bae097d8b2e476d4fab2f6e065995b651e2 /src | |
| parent | fec5364398c0cdb59db056c9530730a15c9fba9e (diff) | |
Improve db_path
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile.am | 5 | ||||
| -rw-r--r-- | src/event_main.cc | 29 | ||||
| -rw-r--r-- | src/fsutils.cc | 34 | ||||
| -rw-r--r-- | src/fsutils.hh | 14 |
4 files changed, 75 insertions, 7 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 45bca7d..7439c4b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,11 +1,12 @@ MAINTAINERCLEANFILES = Makefile.in -AM_CPPFLAGS = @DEFINES@ +AM_CPPFLAGS = @DEFINES@ -DLOCALSTATEDIR='"@localstatedir@/stuff"' bin_PROGRAMS = event noinst_LTLIBRARIES = libdb.la libcgi.la -event_SOURCES = event.cc event.hh event_main.cc common.hh cgi.hh db.hh +event_SOURCES = event.cc event.hh event_main.cc common.hh cgi.hh db.hh \ + fsutils.cc fsutils.hh event_LDADD = libdb.la libcgi.la libcgi_la_SOURCES = cgi.hh common.hh cgi.cc \ diff --git a/src/event_main.cc b/src/event_main.cc index d7303da..ce3800c 100644 --- a/src/event_main.cc +++ b/src/event_main.cc @@ -2,6 +2,7 @@ #include <algorithm> #include <functional> +#include <iostream> #include <memory> #include <set> #include <sstream> @@ -10,13 +11,12 @@ #include "args.hh" #include "cgi.hh" -#include "event.hh" #include "db.hh" +#include "event.hh" +#include "fsutils.hh" #include "http.hh" #include "sqlite3_db.hh" -#define DB_PATH "/var/stuff/" - /* token=gIkuvaNzQIHg97ATvDxqgjtO team_id=T0001 @@ -33,6 +33,8 @@ using namespace stuff; namespace { +std::string g_db_path; + std::shared_ptr<DB> open(const std::string& channel) { std::string tmp = channel; for (auto it = tmp.begin(); it != tmp.end(); ++it) { @@ -43,7 +45,11 @@ std::shared_ptr<DB> open(const std::string& channel) { *it = '.'; } } - auto db = SQLite3::open(DB_PATH "/" + tmp + ".db"); + if (!mkdir_p(g_db_path)) { + Http::response(200, "Unable to create database directory"); + return nullptr; + } + auto db = SQLite3::open(g_db_path + tmp + ".db"); if (!db || db->bad()) { Http::response(200, "Unable to open database"); db.reset(); @@ -524,6 +530,19 @@ bool handle_request(CGI* cgi) { } // namespace -int main() { +int main(int argc, char** argv) { + if (argc < 2) { + g_db_path = LOCALSTATEDIR "/"; + } else if (argc == 2) { + g_db_path = argv[1]; + if (g_db_path.empty()) { + g_db_path = "."; + } else if (g_db_path.back() != '/') { + g_db_path.push_back('/'); + } + } else { + std::cerr << "Too many arguments" << std::endl; + return EXIT_FAILURE; + } return CGI::run(handle_request); } diff --git a/src/fsutils.cc b/src/fsutils.cc new file mode 100644 index 0000000..6058a18 --- /dev/null +++ b/src/fsutils.cc @@ -0,0 +1,34 @@ +#include "common.hh" + +#include <sys/stat.h> +#include <sys/types.h> +#include <cerrno> +#include <cstring> +#include <libgen.h> + +#include "fsutils.hh" + +namespace stuff { + +bool isdir(const std::string& path) { + struct stat buf; + if (stat(path.c_str(), &buf)) { + return false; + } + return S_ISDIR(buf.st_mode); +} + +bool mkdir_p(const std::string& path) { + if (mkdir(path.c_str(), 0777)) { + if (errno == EEXIST) return isdir(path); + char* dir = dirname(const_cast<char*>(path.c_str())); + if (strcmp(dir, ".") == 0 || strcmp(dir, "/") == 0) return false; + if (!mkdir_p(dir)) return false; + if (mkdir(path.c_str(), 0777)) { + return errno == EEXIST && isdir(path); + } + } + return true; +} + +} // namespace diff --git a/src/fsutils.hh b/src/fsutils.hh new file mode 100644 index 0000000..29bc022 --- /dev/null +++ b/src/fsutils.hh @@ -0,0 +1,14 @@ +#ifndef FSUTILS_HH +#define FSUTILS_HH + +#include <string> + +namespace stuff { + +bool isdir(const std::string& path); + +bool mkdir_p(const std::string& path); + +} // namespace stuff + +#endif /* FSUTILS_HH */ |
