Skip to content

Commit 703fc2c

Browse files
committed
core: move index/data path detection into zdb bootstrap
1 parent d7afdd3 commit 703fc2c

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

libzdb/bootstrap.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ zdb_settings_t *zdb_initialize() {
3131
return NULL;
3232

3333
// apply default settings
34-
s->datapath = strdup(ZDB_DEFAULT_DATAPATH);
35-
s->indexpath = strdup(ZDB_DEFAULT_INDEXPATH);
34+
s->datapath = ZDB_DEFAULT_DATAPATH;
35+
s->indexpath = ZDB_DEFAULT_INDEXPATH;
3636
s->datasize = ZDB_DEFAULT_DATA_MAXSIZE;
3737

3838
// running 0-db in mixed mode by default
@@ -108,6 +108,19 @@ zdb_settings_t *zdb_open(zdb_settings_t *zdb_settings) {
108108
zdb_dir_create(zdb_settings->indexpath);
109109
}
110110

111+
// ensure that data and index does not points to the
112+
// same directory
113+
char *datapath = realpath(zdb_settings->datapath, NULL);
114+
char *indexpath = realpath(zdb_settings->indexpath, NULL);
115+
116+
if(strcmp(datapath, indexpath) == 0) {
117+
zdb_danger("[-] system: cannot use the same directory for index and data path");
118+
return NULL;
119+
}
120+
121+
free(datapath);
122+
free(indexpath);
123+
111124
// check/install a lock on the index and data directory to avoid
112125
// multiple instance of zdb running on theses directories
113126
snprintf(lockpath, sizeof(lockpath), "%s/.lockfile", zdb_settings->indexpath);
@@ -139,8 +152,6 @@ void zdb_close(zdb_settings_t *zdb_settings) {
139152

140153
zdb_debug("[+] bootstrap: cleaning library\n");
141154
free(zdb_settings->zdbid);
142-
free(zdb_settings->indexpath);
143-
free(zdb_settings->datapath);
144155
zdb_settings->zdbid = NULL;
145156
zdb_settings->initialized = 0;
146157

zdbd/zdbd.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,11 @@ int main(int argc, char *argv[]) {
325325
return 0;
326326

327327
case 'd':
328-
// free default path allocated
329-
free(zdb_settings->datapath);
330-
331-
// resolve data path passed
332-
zdb_settings->datapath = realpath(optarg, NULL);
328+
zdb_settings->datapath = optarg;
333329
break;
334330

335331
case 'i':
336-
// free default path allocated
337-
free(zdb_settings->indexpath);
338-
339-
// resolve index path passed
340-
zdb_settings->indexpath = realpath(optarg, NULL);
332+
zdb_settings->indexpath = optarg;
341333
break;
342334

343335
case 'l':
@@ -479,11 +471,6 @@ int main(int argc, char *argv[]) {
479471
exit(EXIT_FAILURE);
480472
}
481473

482-
if(strcmp(zdb_settings->datapath, zdb_settings->indexpath) == 0) {
483-
zdbd_danger("[-] system: cannot use the same directory for index and data path");
484-
exit(EXIT_FAILURE);
485-
}
486-
487474
//
488475
// print information relative to database instance
489476
//

0 commit comments

Comments
 (0)