00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <sys/param.h>
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021 #include <errno.h>
00022 #include <unistd.h>
00023
00024 #include "DurableStore.h"
00025 #include "debug/DebugUtils.h"
00026 #include "serialize/MarshalSerialize.h"
00027
00028 namespace oasys {
00029
00030 void
00031 DurableStoreImpl::prune_db_dir(const char* dir, int tidy_wait)
00032 {
00033 char cmd[256];
00034 for (int i = tidy_wait; i > 0; --i)
00035 {
00036 log_warn("PRUNING CONTENTS OF %s IN %d SECONDS", dir, i);
00037 sleep(1);
00038 }
00039 sprintf(cmd, "/bin/rm -rf %s", dir);
00040 log_notice("tidy option removing directory '%s'", cmd);
00041 system(cmd);
00042 }
00043
00044 int
00045 DurableStoreImpl::check_db_dir(const char* db_dir, bool* dir_exists)
00046 {
00047 *dir_exists = false;
00048
00049 struct stat f_stat;
00050 if (stat(db_dir, &f_stat) == -1)
00051 {
00052 if (errno == ENOENT)
00053 {
00054 *dir_exists = false;
00055 }
00056 else
00057 {
00058 log_err("error trying to stat database directory %s: %s",
00059 db_dir, strerror(errno));
00060 return DS_ERR;
00061 }
00062 }
00063 else
00064 {
00065 *dir_exists = true;
00066 }
00067
00068 return 0;
00069 }
00070
00071 int
00072 DurableStoreImpl::create_db_dir(const char* db_dir)
00073 {
00074
00075 char pwd[PATH_MAX];
00076
00077 log_notice("creating new database directory %s%s%s",
00078 db_dir[0] == '/' ? "" : getcwd(pwd, PATH_MAX),
00079 db_dir[0] == '/' ? "" : "/",
00080 db_dir);
00081
00082 if (mkdir(db_dir, 0700) != 0)
00083 {
00084 log_crit("can't create datastore directory %s: %s",
00085 db_dir, strerror(errno));
00086 return DS_ERR;
00087 }
00088 return 0;
00089 }
00090
00091 int
00092 DurableTableImpl::get(const SerializableObject& key,
00093 SerializableObject** data,
00094 TypeCollection::Allocator_t allocator)
00095 {
00096 (void)key;
00097 (void)data;
00098 (void)allocator;
00099 PANIC("Generic DurableTableImpl get method called for "
00100 "multi-type tables");
00101 }
00102
00103 size_t
00104 DurableTableImpl::flatten(const SerializableObject& key,
00105 u_char* key_buf, size_t size)
00106 {
00107 MarshalSize sizer(Serialize::CONTEXT_LOCAL);
00108 sizer.action(&key);
00109
00110 if (sizer.size() > size)
00111 {
00112 return 0;
00113 }
00114
00115 Marshal marshaller(Serialize::CONTEXT_LOCAL, key_buf, 256);
00116 marshaller.action(&key);
00117
00118 return sizer.size();
00119 }
00120
00121 }