Skip to content

Commit ca7d97a

Browse files
grammmikejengelh
authored andcommitted
exmdb: create internal user subdirs as needed
Create /config, /exmdb automatically.
1 parent 31835a1 commit ca7d97a

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

exch/exmdb/db_engine.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cstdlib>
1414
#include <cstring>
1515
#include <ctime>
16+
#include <fcntl.h>
1617
#include <future>
1718
#include <list>
1819
#include <mutex>
@@ -33,6 +34,7 @@
3334
#include <gromox/dbop.h>
3435
#include <gromox/double_list.hpp>
3536
#include <gromox/eid_array.hpp>
37+
#include <gromox/fileio.h>
3638
#include <gromox/exmdb_common_util.hpp>
3739
#include <gromox/exmdb_rpc.hpp>
3840
#include <gromox/exmdb_server.hpp>
@@ -570,10 +572,15 @@ db_handle db_base::get_db(const char* dir, DB_TYPE type)
570572
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_NOMUTEX;
571573
flags |= type == DB_MAIN? 0 : SQLITE_OPEN_CREATE;
572574
sqlite3 *db = nullptr;
575+
int ret = gx_mkbasedir(path.c_str(), FMODE_PRIVATE | S_IXUSR | S_IXGRP);
576+
if (ret < 0) {
577+
mlog(LV_ERR, "E-2710: mkbasedir %s: %s", path.c_str(), strerror(-ret));
578+
return nullptr;
579+
}
573580
if (access(path.c_str(), W_OK) != 0 && errno != ENOENT)
574581
mlog(LV_ERR, "E-1734: %s is not writable (%s), there may be more errors later",
575582
path.c_str(), strerror(errno));
576-
int ret = sqlite3_open_v2(path.c_str(), &db, flags, nullptr);
583+
ret = sqlite3_open_v2(path.c_str(), &db, flags, nullptr);
577584
db_handle hdb(db); /* automatically close connection if something goes wrong */
578585
if (ret != SQLITE_OK) {
579586
mlog(LV_ERR, "E-1350: sqlite_open_v2(%s): %s (%d)",

exch/exmdb/store2.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,11 @@ static BOOL autoreply_setprop1(const char *dir, const TAGGED_PROPVAL &pv)
733733
auto path = autoreply_fspath(dir, pv.proptag);
734734

735735
/* Ensure file exists for the sake of config_file_init() */
736+
auto ret = gx_mkbasedir(path.c_str(), FMODE_PRIVATE | S_IXUSR | S_IXGRP);
737+
if (ret < 0) {
738+
mlog(LV_ERR, "E-1490: mkbasedir for %s: %s", path.c_str(), strerror(-ret));
739+
return false;
740+
}
736741
auto fdtest = open(path.c_str(), O_CREAT | O_WRONLY, FMODE_PUBLIC);
737742
if (fdtest < 0)
738743
return false;

exch/exmdb/table.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,14 +3034,19 @@ BOOL exmdb_server::store_table_state(const char *dir, uint32_t table_id,
30343034
if (ptnode->type != table_type::content)
30353035
return TRUE;
30363036
const auto &state_path = exmdb_server::get_dir() + "/tmp/state.sqlite3"s;
3037+
auto ret = gx_mkbasedir(state_path.c_str(), FMODE_PRIVATE | S_IXUSR | S_IXGRP);
3038+
if (ret < 0) {
3039+
mlog(LV_ERR, "E-2711: mkbasedir %s: %s", state_path.c_str(), strerror(-ret));
3040+
return false;
3041+
}
30373042
/*
30383043
* sqlite3_open does not expose O_EXCL, so let's create the file under
30393044
* EXCL semantics ahead of time.
30403045
*/
30413046
auto tfd = open(state_path.c_str(), O_RDWR | O_CREAT | O_EXCL, FMODE_PRIVATE);
30423047
if (tfd >= 0) {
30433048
close(tfd);
3044-
auto ret = sqlite3_open_v2(state_path.c_str(), &psqlite, SQLITE_OPEN_READWRITE, nullptr);
3049+
ret = sqlite3_open_v2(state_path.c_str(), &psqlite, SQLITE_OPEN_READWRITE, nullptr);
30453050
if (ret != SQLITE_OK) {
30463051
mlog(LV_ERR, "E-1435: sqlite3_open %s: %s", state_path.c_str(), sqlite3_errstr(ret));
30473052
return FALSE;
@@ -3072,7 +3077,7 @@ BOOL exmdb_server::store_table_state(const char *dir, uint32_t table_id,
30723077
return FALSE;
30733078
}
30743079
} else if (errno == EEXIST) {
3075-
auto ret = sqlite3_open_v2(state_path.c_str(), &psqlite, SQLITE_OPEN_READWRITE, nullptr);
3080+
ret = sqlite3_open_v2(state_path.c_str(), &psqlite, SQLITE_OPEN_READWRITE, nullptr);
30763081
if (ret != SQLITE_OK) {
30773082
mlog(LV_ERR, "E-1436: sqlite3_open %s: %s", state_path.c_str(), sqlite3_errstr(ret));
30783083
return FALSE;

0 commit comments

Comments
 (0)