From c05900c6767c476fe40a1aac318277a9358132b3 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 19 Jul 2022 17:04:37 +0930 Subject: [PATCH] bkpr: add option --bookkeeper-dir Allow setting custom directory for bookkeeper's database and default directory for any csv file creation --- doc/lightning-listconfigs.7 | 4 +++- doc/lightning-listconfigs.7.md | 3 ++- doc/lightningd-config.5.md | 4 ++++ doc/schemas/listconfigs.schema.json | 4 ++++ plugins/bkpr/bookkeeper.c | 23 +++++++++++++++++++++-- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/doc/lightning-listconfigs.7 b/doc/lightning-listconfigs.7 index 2d5e89bbd..7b0af2ed4 100644 --- a/doc/lightning-listconfigs.7 +++ b/doc/lightning-listconfigs.7 @@ -78,6 +78,8 @@ On success, an object is returned, containing: .RE +.IP \[bu] +\fBbookkeeper-dir\fR (string, optional): \fBbookkeeper-dir\fR field from config or cmdline, or default .IP \[bu] \fBalways-use-proxy\fR (boolean, optional): \fBalways-use-proxy\fR field from config or cmdline, or default .IP \[bu] @@ -284,4 +286,4 @@ Vincenzo Palazzo \fI wrote the initial versi Main web site: \fIhttps://github.com/ElementsProject/lightning\fR -\" SHA256STAMP:7ac9ef3477f64fd3a4181cb27b8810ecf2c1a082688180716adfe79843ab09aa +\" SHA256STAMP:b1148d7469f7bac293482be6f501031e4cac02f9bc113cf372a31747eb7a6055 diff --git a/doc/lightning-listconfigs.7.md b/doc/lightning-listconfigs.7.md index f42a96a50..2405cf602 100644 --- a/doc/lightning-listconfigs.7.md +++ b/doc/lightning-listconfigs.7.md @@ -50,6 +50,7 @@ On success, an object is returned, containing: - **rpc-file** (string, optional): `rpc-file` field from config or cmdline, or default - **disable-plugin** (array of strings, optional): - `disable-plugin` field from config or cmdline +- **bookkeeper-dir** (string, optional): `bookkeeper-dir` field from config or cmdline, or default - **always-use-proxy** (boolean, optional): `always-use-proxy` field from config or cmdline, or default - **daemon** (boolean, optional): `daemon` field from config or cmdline, or default - **wallet** (string, optional): `wallet` field from config or cmdline, or default @@ -212,4 +213,4 @@ RESOURCES --------- Main web site: -[comment]: # ( SHA256STAMP:4645e3f14c53ccc23b2ed42c2886064e5d718a8eef5e6bd6dd3a932cadfd8e8f) +[comment]: # ( SHA256STAMP:4a4cacd309d9593b45cb27563c25d9d3df7df8aef2b281145abeee27eae57fa9) diff --git a/doc/lightningd-config.5.md b/doc/lightningd-config.5.md index 433c38018..eb8bae77a 100644 --- a/doc/lightningd-config.5.md +++ b/doc/lightningd-config.5.md @@ -217,6 +217,10 @@ authenticate with username `user` and password `pass`, and then use the database `db_name`. The database must exist, but the schema will be managed automatically by `lightningd`. + **bookkeeper-dir**=*DIR* +Directory to keep the accounts.sqlite3 database file in. +Defaults to lightning-dir. + **encrypted-hsm** If set, you will be prompted to enter a password used to encrypt the `hsm_secret`. Note that once you encrypt the `hsm_secret` this option will be mandatory for diff --git a/doc/schemas/listconfigs.schema.json b/doc/schemas/listconfigs.schema.json index c3215e8e9..46feb13ca 100644 --- a/doc/schemas/listconfigs.schema.json +++ b/doc/schemas/listconfigs.schema.json @@ -93,6 +93,10 @@ "description": "`disable-plugin` field from config or cmdline" } }, + "bookkeeper-dir": { + "type": "string", + "description": "`bookkeeper-dir` field from config or cmdline, or default" + }, "always-use-proxy": { "type": "boolean", "description": "`always-use-proxy` field from config or cmdline, or default" diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c index 625334505..1f2defd47 100644 --- a/plugins/bkpr/bookkeeper.c +++ b/plugins/bkpr/bookkeeper.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,8 @@ #include #include #include +#include +#include #define CHAIN_MOVE "chain_mvt" #define CHANNEL_MOVE "channel_mvt" @@ -25,8 +28,8 @@ /* The database that we store all the accounting data in */ static struct db *db ; -// FIXME: make relative to directory we're loaded into static char *db_dsn = "sqlite3://accounts.sqlite3"; +static char *datadir; static struct fee_sum *find_sum_for_txid(struct fee_sum **sums, struct bitcoin_txid *txid) @@ -1386,7 +1389,17 @@ static const struct plugin_command commands[] = { static const char *init(struct plugin *p, const char *b, const jsmntok_t *t) { - // FIXME: pass in database DSN as an option?? + /* Switch to bookkeeper-dir, if specified */ + if (datadir && chdir(datadir) != 0) { + if (mkdir(datadir, 0700) != 0 && errno != EEXIST) + plugin_err(p, + "Unable to create 'bookkeeper-dir'=%s", + datadir); + if (chdir(datadir) != 0) + plugin_err(p, + "Unable to switch to 'bookkeeper-dir'=%s", + datadir); + } db = notleak(db_setup(p, p, db_dsn)); return NULL; @@ -1396,11 +1409,17 @@ int main(int argc, char *argv[]) { setup_locale(); + /* No datadir is default */ + datadir = NULL; plugin_main(argv, init, PLUGIN_STATIC, true, NULL, commands, ARRAY_SIZE(commands), notifs, ARRAY_SIZE(notifs), NULL, 0, NULL, 0, + plugin_option("bookkeeper-dir", + "string", + "Location for bookkeeper records.", + charp_option, &datadir), NULL); return 0; }