diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2018-06-13 10:52:04 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2018-06-13 10:52:04 +0200 |
commit | 3a0917dc2e05e8c0a48b12adf19fa2ee9a4a91f8 (patch) | |
tree | 1d1da7f089691a85c04b7e04851bedd3777bae53 | |
parent | 0ab3ea2faee73f8ba3924e11c9c62d27b5bd75b8 (diff) |
Set BPKG_OPEN_CONFIG environment variable while configuration database is open
-rw-r--r-- | bpkg/bpkg.cli | 8 | ||||
-rw-r--r-- | bpkg/database.cxx | 42 |
2 files changed, 44 insertions, 6 deletions
diff --git a/bpkg/bpkg.cli b/bpkg/bpkg.cli index 22898c1..33a8018 100644 --- a/bpkg/bpkg.cli +++ b/bpkg/bpkg.cli @@ -308,6 +308,14 @@ namespace bpkg bool --version; }; + "\h|ENVIRONMENT| + + Commands executed by \cb{bpkg} while the build configuration database is + open will have the \cb{BPKG_OPEN_CONFIG} environment variable set to the + absolute and normalized configuration directory path. This can be used by + build system hooks and/or programs that they execute. + " + "\h|EXIT STATUS| Non-zero exit status is returned in case of an error. diff --git a/bpkg/database.cxx b/bpkg/database.cxx index 74a1962..f5102f0 100644 --- a/bpkg/database.cxx +++ b/bpkg/database.cxx @@ -4,6 +4,8 @@ #include <bpkg/database.hxx> +#include <stdlib.h> // getenv() setenv()/_putenv() + #include <odb/schema-catalog.hxx> #include <odb/sqlite/exceptions.hxx> @@ -19,6 +21,39 @@ namespace bpkg using namespace odb::sqlite; using odb::schema_catalog; + // Use a custom connection factory to automatically set and clear the + // BPKG_OPEN_CONFIG environment variable. A bit heavy-weight but seems like + // the best option. + // + static const char open_name[] = "BPKG_OPEN_CONFIG"; + + class conn_factory: public single_connection_factory // No need for pool. + { + public: + conn_factory (const dir_path& d) + { + dir_path v (d); + v.complete (); + v.normalize (); + +#ifndef _WIN32 + setenv (open_name, v.string ().c_str (), 1 /* overwrite */); +#else + _putenv ((string (open_name) + '=' + v.string ()).c_str ()); +#endif + } + + virtual + ~conn_factory () + { +#ifndef _WIN32 + unsetenv (open_name); +#else + _putenv ((string (open_name) + '=').c_str ()); +#endif + } + }; + database open (const dir_path& d, tracer& tr, bool create) { @@ -31,16 +66,11 @@ namespace bpkg try { - - // We don't need the thread pool. - // - unique_ptr<connection_factory> cf (new single_connection_factory); - database db (f.string (), SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0), true, // Enable FKs. "", // Default VFS. - move (cf)); + unique_ptr<connection_factory> (new conn_factory (d))); db.tracer (trace); |