mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Check GNC_DATA_HOME environment to override default userdata location.
This replaces GNC_DOT_DIR as it was called in older gnucash releases.
This commit is contained in:
parent
a153412e5a
commit
045ee429bf
@ -489,8 +489,33 @@ get_userdata_home(bool create)
|
||||
gboolean
|
||||
gnc_filepath_init(gboolean create)
|
||||
{
|
||||
auto userdata_home = get_userdata_home(create);
|
||||
auto gnc_userdata_home_exists = false;
|
||||
auto gnc_userdata_home_from_env = false;
|
||||
auto gnc_userdata_home_env = g_getenv("GNC_DATA_HOME");
|
||||
if (gnc_userdata_home_env)
|
||||
{
|
||||
gnc_userdata_home = bfs::path(gnc_userdata_home_env);
|
||||
try
|
||||
{
|
||||
gnc_userdata_home_exists = bfs::exists(gnc_userdata_home);
|
||||
if (!gnc_validate_directory(gnc_userdata_home, create))
|
||||
throw (bfs::filesystem_error(
|
||||
std::string(_("Directory doesn't exist: "))
|
||||
+ userdata_home.c_str(), userdata_home,
|
||||
bst::error_code(bst::errc::permission_denied, bst::generic_category())));
|
||||
gnc_userdata_home_from_env = true;
|
||||
}
|
||||
catch (const bfs::filesystem_error& ex)
|
||||
{
|
||||
g_warning("%s (from environment variable 'GNC_DATA_HOME') is not a suitable directory for the user data."
|
||||
"Trying the default instead.\nThe failure is\n%s",
|
||||
gnc_userdata_home.c_str(), ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
if (!gnc_userdata_home_from_env)
|
||||
{
|
||||
auto userdata_home = get_userdata_home(create);
|
||||
if (userdata_is_home)
|
||||
{
|
||||
/* If we get here that means the platform
|
||||
@ -509,14 +534,13 @@ gnc_filepath_init(gboolean create)
|
||||
|
||||
if (!userdata_is_home)
|
||||
gnc_userdata_home = userdata_home / PACKAGE_NAME;
|
||||
|
||||
auto try_migrate = false;
|
||||
if (!userdata_is_home && !bfs::exists(gnc_userdata_home) && create)
|
||||
try_migrate = true;
|
||||
gnc_userdata_home_exists = bfs::exists(gnc_userdata_home);
|
||||
/* This may throw and end the program! */
|
||||
gnc_validate_directory(gnc_userdata_home, create);
|
||||
}
|
||||
|
||||
auto migrated = FALSE;
|
||||
if (try_migrate)
|
||||
if (!userdata_is_home && !gnc_userdata_home_exists && create)
|
||||
migrated = copy_recursive(bfs::path (g_get_home_dir()) / ".gnucash",
|
||||
gnc_userdata_home);
|
||||
|
||||
|
@ -41,20 +41,16 @@ typedef struct usr_confpath_strings_struct usr_confpath_strings;
|
||||
usr_confpath_strings strs2[] =
|
||||
{
|
||||
{
|
||||
0, "gnc_build_userdata_path",
|
||||
PACKAGE_NAME
|
||||
0, "gnc_build_userdata_path", ""
|
||||
},
|
||||
{
|
||||
1, "gnc_build_book_path",
|
||||
PACKAGE_NAME G_DIR_SEPARATOR_S "books"
|
||||
1, "gnc_build_book_path", "books"
|
||||
},
|
||||
{
|
||||
2, "gnc_build_translog_path",
|
||||
PACKAGE_NAME G_DIR_SEPARATOR_S "translog"
|
||||
2, "gnc_build_translog_path", "translog"
|
||||
},
|
||||
{
|
||||
3, "gnc_build_data_path",
|
||||
PACKAGE_NAME G_DIR_SEPARATOR_S "data"
|
||||
3, "gnc_build_data_path", "data"
|
||||
},
|
||||
{ 0, NULL, NULL },
|
||||
};
|
||||
@ -66,6 +62,7 @@ main(int argc, char **argv)
|
||||
char *home_dir = NULL;
|
||||
const char *userdata_dir = NULL;
|
||||
const char *tmp_dir = g_get_tmp_dir();
|
||||
char *gnc_data_home_dir = NULL;
|
||||
|
||||
if (argc > 1)
|
||||
/* One can pass a homedir on the command line. This
|
||||
@ -89,25 +86,25 @@ main(int argc, char **argv)
|
||||
|
||||
if (strs2[i].func_num == 0)
|
||||
{
|
||||
wantout = g_build_filename(tmp_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(tmp_dir, PACKAGE_NAME, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_userdata_path("foo");
|
||||
}
|
||||
else if (strs2[i].func_num == 1)
|
||||
{
|
||||
wantout = g_build_filename(tmp_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(tmp_dir, PACKAGE_NAME, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_book_path("foo");
|
||||
}
|
||||
else if (strs2[i].func_num == 2)
|
||||
{
|
||||
wantout = g_build_filename(tmp_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(tmp_dir, PACKAGE_NAME, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_translog_path("foo");
|
||||
}
|
||||
else // if (strs2[i].prefix_home == 3)
|
||||
{
|
||||
wantout = g_build_filename(tmp_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(tmp_dir, PACKAGE_NAME, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_data_path("foo");
|
||||
}
|
||||
@ -130,25 +127,67 @@ main(int argc, char **argv)
|
||||
|
||||
if (strs2[i].func_num == 0)
|
||||
{
|
||||
wantout = g_build_filename(userdata_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(userdata_dir, PACKAGE_NAME, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_userdata_path("foo");
|
||||
}
|
||||
else if (strs2[i].func_num == 1)
|
||||
{
|
||||
wantout = g_build_filename(userdata_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(userdata_dir, PACKAGE_NAME, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_book_path("foo");
|
||||
}
|
||||
else if (strs2[i].func_num == 2)
|
||||
{
|
||||
wantout = g_build_filename(userdata_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(userdata_dir, PACKAGE_NAME, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_translog_path("foo");
|
||||
}
|
||||
else // if (strs2[i].prefix_home == 3)
|
||||
{
|
||||
wantout = g_build_filename(userdata_dir, strs2[i].output, "foo",
|
||||
wantout = g_build_filename(userdata_dir, PACKAGE_NAME, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_data_path("foo");
|
||||
}
|
||||
|
||||
do_test_args(g_strcmp0(daout, wantout) == 0,
|
||||
"gnc_build_x_path",
|
||||
__FILE__, __LINE__,
|
||||
"%s (%s) vs %s", daout, strs2[i].funcname, wantout);
|
||||
g_free(wantout);
|
||||
g_free(daout);
|
||||
}
|
||||
|
||||
/* Third run, after setting GNC_DATA_HOME gnc_filepath_init */
|
||||
gnc_data_home_dir = g_build_filename(home_dir, "Test", NULL);
|
||||
g_setenv("GNC_DATA_HOME", gnc_data_home_dir, TRUE);
|
||||
gnc_filepath_init(TRUE);
|
||||
for (i = 0; strs2[i].funcname != NULL; i++)
|
||||
{
|
||||
char *daout;
|
||||
char *wantout;
|
||||
|
||||
if (strs2[i].func_num == 0)
|
||||
{
|
||||
wantout = g_build_filename(gnc_data_home_dir, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_userdata_path("foo");
|
||||
}
|
||||
else if (strs2[i].func_num == 1)
|
||||
{
|
||||
wantout = g_build_filename(gnc_data_home_dir, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_book_path("foo");
|
||||
}
|
||||
else if (strs2[i].func_num == 2)
|
||||
{
|
||||
wantout = g_build_filename(gnc_data_home_dir, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_translog_path("foo");
|
||||
}
|
||||
else // if (strs2[i].prefix_home == 3)
|
||||
{
|
||||
wantout = g_build_filename(gnc_data_home_dir, strs2[i].output, "foo",
|
||||
(gchar *)NULL);
|
||||
daout = gnc_build_data_path("foo");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user