From e8f722adefb8eab42fd2b8d3bdd5060254a79cae Mon Sep 17 00:00:00 2001 From: Stefan Sayer Date: Wed, 12 Mar 2008 00:19:38 +0000 Subject: [PATCH] storage path configurable etc git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@785 8eb893ce-cfd4-0310-b710-fb5ebe64c474 --- apps/msg_storage/Makefile | 2 +- apps/msg_storage/MsgStorage.cpp | 49 +++++++++++++++++++++------ apps/msg_storage/MsgStorage.h | 2 ++ apps/msg_storage/Readme.msg_storage | 12 +++++++ apps/msg_storage/etc/msg_storage.conf | 8 +++++ 5 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 apps/msg_storage/Readme.msg_storage create mode 100644 apps/msg_storage/etc/msg_storage.conf diff --git a/apps/msg_storage/Makefile b/apps/msg_storage/Makefile index 7971e172..dea0f31a 100644 --- a/apps/msg_storage/Makefile +++ b/apps/msg_storage/Makefile @@ -1,7 +1,7 @@ plug_in_name = msg_storage module_ldflags = -module_cflags = +module_cflags = -DMOD_NAME=\"$(plug_in_name)\" COREPATH ?=../../core include $(COREPATH)/plug-in/Makefile.app_module diff --git a/apps/msg_storage/MsgStorage.cpp b/apps/msg_storage/MsgStorage.cpp index 022ade81..c654947c 100644 --- a/apps/msg_storage/MsgStorage.cpp +++ b/apps/msg_storage/MsgStorage.cpp @@ -5,6 +5,8 @@ #include "MsgStorageAPI.h" #include "MsgStorage.h" +#include "AmConfigReader.h" + #include #include #include @@ -12,11 +14,11 @@ #include #include -#define MSG_DIR "/var/lib/voicebox/" // TODO: configurabel... +#define MSG_DIR "/var/spool/voicebox/" // default MsgStorage* MsgStorage::_instance = 0; -EXPORT_PLUGIN_CLASS_FACTORY(MsgStorage,"msg_storage"); +EXPORT_PLUGIN_CLASS_FACTORY(MsgStorage, MOD_NAME); MsgStorage::MsgStorage(const string& name) : AmDynInvokeFactory(name) { @@ -26,6 +28,27 @@ MsgStorage::MsgStorage(const string& name) MsgStorage::~MsgStorage() { } int MsgStorage::onLoad() { + + msg_dir = MSG_DIR; + + AmConfigReader cfg; + if(cfg.loadFile(AmConfig::ModConfigPath + string(MOD_NAME ".conf"))) { + DBG("no configuration could be loaded, assuming defaults.\n"); + } else { + msg_dir = cfg.getParameter("storage_dir",MSG_DIR); + DBG("storage_dir set to '%s'.\n", msg_dir.c_str()); + } + + string path = msg_dir + "/_test_dir_"; + int status = mkdir(path.c_str(), + S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (status && (errno != EEXIST)) { + ERROR("creating test path in storage '%s': %s\n", + path.c_str(),strerror(errno)); + return -1; + } + rmdir(path.c_str()); + DBG("MsgStorage loaded.\n"); return 0; } @@ -82,9 +105,9 @@ void MsgStorage::invoke(const string& method, int MsgStorage::msg_new(string domain, string user, - string msg_name, FILE* data) { + string msg_name, FILE* data) { - string path = MSG_DIR "/" + domain + "/" + user + "/"; + string path = msg_dir+ "/" + domain + "/" + user + "/"; int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (status && (errno != EEXIST)) { @@ -108,7 +131,7 @@ int MsgStorage::msg_new(string domain, string user, void MsgStorage::msg_get(string domain, string user, string msg_name, AmArg& ret) { - string fname = MSG_DIR "/" + domain + "/" + user + "/"+ msg_name; + string fname = msg_dir + "/" + domain + "/" + user + "/"+ msg_name; FILE* fp = fopen(fname.c_str(), "r"); if (!fp) ret.push(MSG_EMSGNOTFOUND); @@ -121,7 +144,7 @@ void MsgStorage::msg_get(string domain, string user, } int MsgStorage::msg_markread(string domain, string user, string msg_name) { - string path = MSG_DIR "/" + domain + "/" + user + "/" + msg_name; + string path = msg_dir + "/" + domain + "/" + user + "/" + msg_name; struct stat e_stat; if (stat(path.c_str(), &e_stat)) { @@ -144,7 +167,8 @@ int MsgStorage::msg_markread(string domain, string user, string msg_name) { } int MsgStorage::msg_delete(string domain, string user, string msg_name) { - string path = MSG_DIR "/" + domain + "/" + user + "/" + msg_name; + // TODO: check the directory lock + string path = msg_dir + "/" + domain + "/" + user + "/" + msg_name; if (unlink(path.c_str())) { ERROR("cannot unlink '%s': %s\n", path.c_str(),strerror(errno)); @@ -154,12 +178,13 @@ int MsgStorage::msg_delete(string domain, string user, string msg_name) { } void MsgStorage::userdir_open(string domain, string user, AmArg& ret) { - string path = MSG_DIR "/" + domain + "/" + user + "/"; + // TODO: block the directory from delete (increase lock) + string path = msg_dir + "/" + domain + "/" + user + "/"; DBG("trying to list '%s'\n", path.c_str()); DIR* dir = opendir(path.c_str()); if (!dir) { ret.push(MSG_EUSRNOTFOUND); - ret.push(AmArg()); // empty lis + ret.push(AmArg()); // empty list return; } @@ -199,7 +224,11 @@ void MsgStorage::userdir_open(string domain, string user, AmArg& ret) { ret.push(msglist); } -int MsgStorage::userdir_close(string domain, string user) { return 0; } +int MsgStorage::userdir_close(string domain, string user) { + // TODO: unblock the directory from delete (decrease lock) + return 0; +} + void MsgStorage::userdir_getcount(string domain, string user, AmArg& ret) { } // copies ifp to ofp, blockwise diff --git a/apps/msg_storage/MsgStorage.h b/apps/msg_storage/MsgStorage.h index 8e773d3d..67c12661 100644 --- a/apps/msg_storage/MsgStorage.h +++ b/apps/msg_storage/MsgStorage.h @@ -9,6 +9,8 @@ class MsgStorage : public AmDynInvokeFactory, static MsgStorage* _instance; + string msg_dir; + int msg_new(string domain, string user, string msg_name, FILE* data); void msg_get(string domain, string user, string msg_name, AmArg& ret); int msg_markread(string domain, string user, string msg_name); diff --git a/apps/msg_storage/Readme.msg_storage b/apps/msg_storage/Readme.msg_storage new file mode 100644 index 00000000..fc8e2887 --- /dev/null +++ b/apps/msg_storage/Readme.msg_storage @@ -0,0 +1,12 @@ + +This is an example module for file system based storage for +messages. It uses the file atime vs. mtime to determine whether a +messagee is new. + +This module lacks delete-locking of an open message +directory, which means that its possible to delete a message in +one call and on a parallel call not be able to listen to that +deleted message any more (even if it was in the list before). This +should be very rare usage though. + + diff --git a/apps/msg_storage/etc/msg_storage.conf b/apps/msg_storage/etc/msg_storage.conf new file mode 100644 index 00000000..b1ee1259 --- /dev/null +++ b/apps/msg_storage/etc/msg_storage.conf @@ -0,0 +1,8 @@ + +# +# storage_dir is where messages are saved on disk. this +# folder must be writable. +# +# default: /var/spool/voicebox/ +# +#storage_dir=/var/spool/voicebox/ \ No newline at end of file