Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-filepath-utils.c -- file path resolution utility *
3 : : * *
4 : : * This program is free software; you can redistribute it and/or *
5 : : * modify it under the terms of the GNU General Public License as *
6 : : * published by the Free Software Foundation; either version 2 of *
7 : : * the License, or (at your option) any later version. *
8 : : * *
9 : : * This program is distributed in the hope that it will be useful, *
10 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 : : * GNU General Public License for more details. *
13 : : * *
14 : : * You should have received a copy of the GNU General Public License*
15 : : * along with this program; if not, contact: *
16 : : * *
17 : : * Free Software Foundation Voice: +1-617-542-5942 *
18 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
20 : : \********************************************************************/
21 : :
22 : : /*
23 : : * @file gnc-filepath-utils.c
24 : : * @brief file path resolution utilities
25 : : * @author Copyright (c) 1998-2004 Linas Vepstas <linas@linas.org>
26 : : * @author Copyright (c) 2000 Dave Peticolas
27 : : */
28 : :
29 : : #include <glib.h>
30 : : #include <glib/gi18n.h>
31 : : #include <glib/gprintf.h>
32 : : #include <glib/gstdio.h>
33 : :
34 : : #include <config.h>
35 : :
36 : : #include <platform.h>
37 : : #if PLATFORM(WINDOWS)
38 : : #include <windows.h>
39 : : #include <Shlobj.h>
40 : : #endif
41 : :
42 : :
43 : : #include <stdlib.h>
44 : : #include <stdio.h>
45 : : #include <string.h>
46 : : #include <sys/types.h>
47 : : #include <sys/stat.h>
48 : : #ifdef HAVE_UNISTD_H
49 : : # include <unistd.h>
50 : : #endif
51 : : #include <errno.h>
52 : :
53 : : #include "gnc-path.h"
54 : : #include "gnc-filepath-utils.h"
55 : :
56 : : #if defined (_MSC_VER) || defined (G_OS_WIN32)
57 : : #include <glib/gwin32.h>
58 : : #ifndef PATH_MAX
59 : : #define PATH_MAX MAXPATHLEN
60 : : #endif
61 : : #endif
62 : : #ifdef MAC_INTEGRATION
63 : : #include <Foundation/Foundation.h>
64 : : #endif
65 : :
66 : : #include "gnc-locale-utils.hpp"
67 : : #include <boost/filesystem.hpp>
68 : : #include <boost/locale.hpp>
69 : : #include <regex>
70 : : #include <iostream>
71 : :
72 : : /* Below cvt and bfs_locale should be used with boost::filesystem::path (bfs)
73 : : * objects created alter in this source file. The rationale is as follows:
74 : : * - a bfs object has an internal, locale and platform dependent
75 : : * representation of a file system path
76 : : * - glib's internal representation is always utf8
77 : : * - when creating a bfs object, we should pass a cvt to convert from
78 : : * utf8 to the object's internal representation
79 : : * - if we later want to use the bfs object's internal representation
80 : : * in a glib context we should imbue the locale below so that
81 : : * bfs will use it to convert back to utf8
82 : : * - if the bfs object's internal representation will never be used
83 : : * in a glib context, imbuing is not needed (although probably more
84 : : * future proof)
85 : : * - also note creating a bfs based on another one will inherit the
86 : : * locale from the source path. So in that case there's not need
87 : : * to imbue it again.
88 : : */
89 : : #if PLATFORM(WINDOWS)
90 : : #include <codecvt>
91 : : using codecvt = std::codecvt_utf8<wchar_t, 0x10FFFF, std::little_endian>;
92 : : using string = std::wstring;
93 : : #else
94 : : /* See https://stackoverflow.com/questions/41744559/is-this-a-bug-of-gcc */
95 : : template<class I, class E, class S>
96 : : struct codecvt_r : std::codecvt<I, E, S>
97 : : {
98 : 352 : ~codecvt_r() {}
99 : : };
100 : : using codecvt = codecvt_r<wchar_t, char, std::mbstate_t>;
101 : : using string = std::string;
102 : : #endif
103 : : static codecvt cvt;
104 : : static std::locale bfs_locale(std::locale(), new codecvt);
105 : :
106 : : namespace bfs = boost::filesystem;
107 : : namespace bst = boost::system;
108 : : namespace bl = boost::locale;
109 : :
110 : : /** Check if the path exists and is a regular file.
111 : : *
112 : : * \param path -- freed if the path doesn't exist or isn't a regular file
113 : : *
114 : : * \return NULL or the path
115 : : */
116 : : static gchar *
117 : 96 : check_path_return_if_valid(gchar *path)
118 : : {
119 : 96 : if (g_file_test(path, G_FILE_TEST_IS_REGULAR))
120 : : {
121 : 0 : return path;
122 : : }
123 : 96 : g_free (path);
124 : 96 : return NULL;
125 : : }
126 : :
127 : : /** @brief Create an absolute path when given a relative path;
128 : : * otherwise return the argument.
129 : : *
130 : : * @warning filefrag should be a simple path fragment. It shouldn't
131 : : * contain xml:// or http:// or <whatever>:// other protocol specifiers.
132 : : *
133 : : * If passed a string which g_path_is_absolute declares an absolute
134 : : * path, return the argument.
135 : : *
136 : : * Otherwise, assume that filefrag is a well-formed relative path and
137 : : * try to find a file with its path relative to
138 : : * \li the current working directory,
139 : : * \li the installed system-wide data directory (e.g., /usr/local/share/gnucash),
140 : : * \li the installed system configuration directory (e.g., /usr/local/etc/gnucash),
141 : : * \li or in the user's configuration directory (e.g., $HOME/.gnucash/data)
142 : : *
143 : : * The paths are searched for in that order. If a matching file is
144 : : * found, return the absolute path to it.
145 : :
146 : : * If one isn't found, return a absolute path relative to the
147 : : * user's configuration directory and note in the trace file that it
148 : : * needs to be created.
149 : : *
150 : : * @param filefrag The file path to resolve
151 : : *
152 : : * @return An absolute file path.
153 : : */
154 : : gchar *
155 : 184 : gnc_resolve_file_path (const gchar * filefrag)
156 : : {
157 : 184 : gchar *fullpath = NULL, *tmp_path = NULL;
158 : :
159 : : /* seriously invalid */
160 : 184 : if (!filefrag)
161 : : {
162 : 0 : g_critical("filefrag is NULL");
163 : 0 : return NULL;
164 : : }
165 : :
166 : : /* ---------------------------------------------------- */
167 : : /* OK, now we try to find or build an absolute file path */
168 : :
169 : : /* check for an absolute file path */
170 : 184 : if (g_path_is_absolute(filefrag))
171 : 152 : return g_strdup (filefrag);
172 : :
173 : : /* Look in the current working directory */
174 : 32 : tmp_path = g_get_current_dir();
175 : 32 : fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
176 : 32 : g_free(tmp_path);
177 : 32 : fullpath = check_path_return_if_valid(fullpath);
178 : 32 : if (fullpath != NULL)
179 : 0 : return fullpath;
180 : :
181 : : /* Look in the data dir (e.g. $PREFIX/share/gnucash) */
182 : 32 : tmp_path = gnc_path_get_pkgdatadir();
183 : 32 : fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
184 : 32 : g_free(tmp_path);
185 : 32 : fullpath = check_path_return_if_valid(fullpath);
186 : 32 : if (fullpath != NULL)
187 : 0 : return fullpath;
188 : :
189 : : /* Look in the config dir (e.g. $PREFIX/share/gnucash/accounts) */
190 : 32 : tmp_path = gnc_path_get_accountsdir();
191 : 32 : fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
192 : 32 : g_free(tmp_path);
193 : 32 : fullpath = check_path_return_if_valid(fullpath);
194 : 32 : if (fullpath != NULL)
195 : 0 : return fullpath;
196 : :
197 : : /* Look in the users config dir (e.g. $HOME/.gnucash/data) */
198 : 32 : fullpath = g_strdup(gnc_build_data_path(filefrag));
199 : 32 : if (g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
200 : 0 : return fullpath;
201 : :
202 : : /* OK, it's not there. Note that it needs to be created and pass it
203 : : * back anyway */
204 : 32 : g_warning("create new file %s", fullpath);
205 : 32 : return fullpath;
206 : :
207 : : }
208 : :
209 : 195 : gchar *gnc_file_path_relative_part (const gchar *prefix, const gchar *path)
210 : : {
211 : 390 : std::string p{path};
212 : 195 : if (p.find(prefix) == 0)
213 : : {
214 : 195 : auto str = p.substr(strlen(prefix));
215 : 390 : return g_strdup(str.c_str());
216 : 195 : }
217 : 0 : return g_strdup(path);
218 : 195 : }
219 : :
220 : : /* Searches for a file fragment paths set via GNC_DOC_PATH environment
221 : : * variable. If this variable is not set, fall back to search in
222 : : * - a html directory in the local user's gnucash settings directory
223 : : * (typically $HOME/.gnucash/html)
224 : : * - the gnucash documentation directory
225 : : * (typically /usr/share/doc/gnucash)
226 : : * - the gnucash data directory
227 : : * (typically /usr/share/gnucash)
228 : : * It searches in this order.
229 : : *
230 : : * This is used by gnc_path_find_localized_file to search for
231 : : * localized versions of files if they exist.
232 : : */
233 : : static gchar *
234 : 0 : gnc_path_find_localized_html_file_internal (const gchar * file_name)
235 : : {
236 : 0 : gchar *full_path = NULL;
237 : : int i;
238 : 0 : const gchar *env_doc_path = g_getenv("GNC_DOC_PATH");
239 : 0 : const gchar *default_dirs[] =
240 : : {
241 : 0 : gnc_build_userdata_path ("html"),
242 : 0 : gnc_path_get_pkgdocdir (),
243 : 0 : gnc_path_get_pkgdatadir (),
244 : : NULL
245 : 0 : };
246 : : gchar **dirs;
247 : :
248 : 0 : if (!file_name || *file_name == '\0')
249 : 0 : return NULL;
250 : :
251 : : /* Allow search path override via GNC_DOC_PATH environment variable */
252 : 0 : if (env_doc_path)
253 : 0 : dirs = g_strsplit (env_doc_path, G_SEARCHPATH_SEPARATOR_S, -1);
254 : : else
255 : 0 : dirs = (gchar **)default_dirs;
256 : :
257 : 0 : for (i = 0; dirs[i]; i++)
258 : : {
259 : 0 : full_path = g_build_filename (dirs[i], file_name, (gchar *)NULL);
260 : 0 : g_debug ("Checking for existence of %s", full_path);
261 : 0 : full_path = check_path_return_if_valid (full_path);
262 : 0 : if (full_path != NULL)
263 : 0 : return full_path;
264 : : }
265 : :
266 : 0 : return NULL;
267 : : }
268 : :
269 : : /** @brief Find an absolute path to a localized version of a given
270 : : * relative path to a html or html related file.
271 : : * If no localized version exists, an absolute path to the file
272 : : * is searched for. If that file doesn't exist either, returns NULL.
273 : : *
274 : : * @warning file_name should be a simple path fragment. It shouldn't
275 : : * contain xml:// or http:// or <whatever>:// other protocol specifiers.
276 : : *
277 : : * If passed a string which g_path_is_absolute declares an absolute
278 : : * path, return the argument.
279 : : *
280 : : * Otherwise, assume that file_name is a well-formed relative path and
281 : : * try to find a file with its path relative to
282 : : * \li a localized subdirectory in the html directory
283 : : * of the user's configuration directory
284 : : * (e.g. $HOME/.gnucash/html/de_DE, $HOME/.gnucash/html/en,...)
285 : : * \li a localized subdirectory in the gnucash documentation directory
286 : : * (e.g. /usr/share/doc/gnucash/C,...)
287 : : * \li the html directory of the user's configuration directory
288 : : * (e.g. $HOME/.gnucash/html)
289 : : * \li the gnucash documentation directory
290 : : * (e.g. /usr/share/doc/gnucash/)
291 : : * \li last resort option: the gnucash data directory
292 : : * (e.g. /usr/share/gnucash/)
293 : : *
294 : : * The paths are searched for in that order. If a matching file is
295 : : * found, return the absolute path to it.
296 : :
297 : : * If one isn't found, return NULL.
298 : : *
299 : : * @param file_name The file path to resolve
300 : : *
301 : : * @return An absolute file path or NULL if no file is found.
302 : : */
303 : : gchar *
304 : 0 : gnc_path_find_localized_html_file (const gchar *file_name)
305 : : {
306 : 0 : gchar *loc_file_name = NULL;
307 : 0 : gchar *full_path = NULL;
308 : : const gchar * const *lang;
309 : :
310 : 0 : if (!file_name || *file_name == '\0')
311 : 0 : return NULL;
312 : :
313 : : /* An absolute path is returned unmodified. */
314 : 0 : if (g_path_is_absolute (file_name))
315 : 0 : return g_strdup (file_name);
316 : :
317 : : /* First try to find the file in any of the localized directories
318 : : * the user has set up on his system
319 : : */
320 : 0 : for (lang = g_get_language_names (); *lang; lang++)
321 : : {
322 : 0 : loc_file_name = g_build_filename (*lang, file_name, (gchar *)NULL);
323 : 0 : full_path = gnc_path_find_localized_html_file_internal (loc_file_name);
324 : 0 : g_free (loc_file_name);
325 : 0 : if (full_path != NULL)
326 : 0 : return full_path;
327 : : }
328 : :
329 : : /* If not found in a localized directory, try to find the file
330 : : * in any of the base directories
331 : : */
332 : 0 : return gnc_path_find_localized_html_file_internal (file_name);
333 : :
334 : : }
335 : :
336 : : /* ====================================================================== */
337 : : static auto gnc_userdata_home = bfs::path();
338 : : static auto gnc_userconfig_home = bfs::path();
339 : : static auto build_dir = bfs::path();
340 : : /*Provide static-stored strings for gnc_userdata_home and
341 : : * gnc_userconfig_home to ensure that the cstrings don't go out of
342 : : * scope when gnc_userdata_dir and gnc_userconfig_dir return them.
343 : : */
344 : : static std::string gnc_userdata_home_str;
345 : : static std::string gnc_userconfig_home_str;
346 : :
347 : 219 : static bool dir_is_descendant (const bfs::path& path, const bfs::path& base)
348 : : {
349 : 219 : auto test_path = path;
350 : 219 : if (bfs::exists (path))
351 : 196 : test_path = bfs::canonical (path);
352 : 219 : auto test_base = base;
353 : 219 : if (bfs::exists (base))
354 : 212 : test_base = bfs::canonical (base);
355 : :
356 : 219 : auto is_descendant = (test_path.string() == test_base.string());
357 : 896 : while (!test_path.empty() && !is_descendant)
358 : : {
359 : 677 : test_path = test_path.parent_path();
360 : 677 : is_descendant = (test_path.string() == test_base.string());
361 : : }
362 : 219 : return is_descendant;
363 : 219 : }
364 : :
365 : : /** @brief Check that the supplied directory path exists, is a directory, and
366 : : * that the user has adequate permissions to use it.
367 : : *
368 : : * @param dirname The path to check
369 : : */
370 : : static bool
371 : 219 : gnc_validate_directory (const bfs::path &dirname)
372 : : {
373 : 219 : if (dirname.empty())
374 : 0 : return false;
375 : :
376 : 219 : auto create_dirs = true;
377 : 219 : if (build_dir.empty() || !dir_is_descendant (dirname, build_dir))
378 : : {
379 : : /* Gnucash won't create a home directory
380 : : * if it doesn't exist yet. So if the directory to create
381 : : * is a descendant of the homedir, we can't create it either.
382 : : * This check is conditioned on do_homedir_check because
383 : : * we need to overrule it during build (when guile interferes)
384 : : * and testing.
385 : : */
386 : 204 : bfs::path home_dir(g_get_home_dir(), cvt);
387 : 204 : home_dir.imbue(bfs_locale);
388 : 204 : auto homedir_exists = bfs::exists(home_dir);
389 : 204 : auto is_descendant = dir_is_descendant (dirname, home_dir);
390 : 204 : if (!homedir_exists && is_descendant)
391 : 2 : create_dirs = false;
392 : 204 : }
393 : :
394 : : /* Create directories if they don't exist yet and we can
395 : : *
396 : : * Note this will do nothing if the directory and its
397 : : * parents already exist, but will fail if the path
398 : : * points to a file or a softlink. So it serves as a test
399 : : * for that as well.
400 : : */
401 : 219 : if (create_dirs)
402 : 217 : bfs::create_directories(dirname);
403 : : else
404 : : throw (bfs::filesystem_error (
405 : 8 : std::string (dirname.string() +
406 : 4 : " is a descendant of a non-existing home directory. As " +
407 : : PACKAGE_NAME +
408 : : " will never create a home directory this path can't be used"),
409 : 6 : dirname, bst::error_code(bst::errc::permission_denied, bst::generic_category())));
410 : :
411 : 217 : auto d = bfs::directory_entry (dirname);
412 : 217 : auto perms = d.status().permissions();
413 : :
414 : : /* On Windows only write permission will be checked.
415 : : * So strictly speaking we'd need two error messages here depending
416 : : * on the platform. For simplicity this detail is glossed over though. */
417 : : #if PLATFORM(WINDOWS)
418 : : auto check_perms = bfs::owner_read | bfs::owner_write;
419 : : #else
420 : 217 : auto check_perms = bfs::owner_all;
421 : : #endif
422 : 217 : if ((perms & check_perms) != check_perms)
423 : : throw (bfs::filesystem_error(
424 : 0 : std::string("Insufficient permissions, at least write and access permissions required: ")
425 : 0 : + dirname.string(), dirname,
426 : 0 : bst::error_code(bst::errc::permission_denied, bst::generic_category())));
427 : :
428 : 217 : return true;
429 : 217 : }
430 : :
431 : : /* Will attempt to copy all files and directories from src to dest
432 : : * Returns true if successful or false if not */
433 : : static bool
434 : 4 : copy_recursive(const bfs::path& src, const bfs::path& dest)
435 : : {
436 : 4 : if (!bfs::exists(src))
437 : 4 : return false;
438 : :
439 : : // Don't copy on self
440 : 0 : if (src.compare(dest) == 0)
441 : 0 : return false;
442 : :
443 : 0 : auto old_str = src.string();
444 : 0 : auto old_len = old_str.size();
445 : : try
446 : : {
447 : : /* Note: the for(auto elem : iterator) construct fails
448 : : * on travis (g++ 4.8.x, boost 1.54) so I'm using
449 : : * a traditional for loop here */
450 : 0 : for(auto direntry = bfs::recursive_directory_iterator(src);
451 : 0 : direntry != bfs::recursive_directory_iterator(); ++direntry)
452 : : {
453 : : #ifdef G_OS_WIN32
454 : : string cur_str = direntry->path().wstring();
455 : : #else
456 : 0 : string cur_str = direntry->path().string();
457 : : #endif
458 : 0 : auto cur_len = cur_str.size();
459 : 0 : string rel_str(cur_str, old_len, cur_len - old_len);
460 : 0 : bfs::path relpath(rel_str, cvt);
461 : 0 : auto newpath = bfs::absolute (relpath.relative_path(), dest);
462 : 0 : newpath.imbue(bfs_locale);
463 : 0 : bfs::copy(direntry->path(), newpath);
464 : 0 : }
465 : : }
466 : 0 : catch(const bfs::filesystem_error& ex)
467 : : {
468 : 0 : g_warning("An error occurred while trying to migrate the user configation from\n%s to\n%s"
469 : : "(Error: %s)",
470 : : src.string().c_str(), gnc_userdata_home_str.c_str(),
471 : : ex.what());
472 : 0 : return false;
473 : 0 : }
474 : :
475 : 0 : return true;
476 : 0 : }
477 : :
478 : : #ifdef G_OS_WIN32
479 : : /* g_get_user_data_dir defaults to CSIDL_LOCAL_APPDATA, but
480 : : * the roaming profile makes more sense.
481 : : * So this function is a copy of glib's internal get_special_folder
482 : : * and minimally adjusted to fetch CSIDL_APPDATA
483 : : */
484 : : static bfs::path
485 : : get_user_data_dir ()
486 : : {
487 : : wchar_t path[MAX_PATH+1];
488 : : HRESULT hr;
489 : : LPITEMIDLIST pidl = NULL;
490 : :
491 : : hr = SHGetSpecialFolderLocation (NULL, CSIDL_APPDATA, &pidl);
492 : : if (hr == S_OK)
493 : : {
494 : : [[maybe_unused]] auto b = SHGetPathFromIDListW (pidl, path);
495 : : CoTaskMemFree (pidl);
496 : : }
497 : : bfs::path retval(path, cvt);
498 : : retval.imbue(bfs_locale);
499 : : return retval;
500 : : }
501 : : #elif defined MAC_INTEGRATION
502 : : static bfs::path
503 : : get_user_data_dir()
504 : : {
505 : : NSFileManager*fm = [NSFileManager defaultManager];
506 : : NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
507 : : inDomains:NSUserDomainMask];
508 : : NSString *dirPath = nullptr;
509 : : if ([appSupportDir count] > 0)
510 : : {
511 : : NSURL* dirUrl = [appSupportDir objectAtIndex:0];
512 : : dirPath = [dirUrl path];
513 : : }
514 : : return [dirPath UTF8String];
515 : : }
516 : : #else
517 : : static bfs::path
518 : 33 : get_user_data_dir()
519 : : {
520 : 33 : return g_get_user_data_dir();
521 : : }
522 : : #endif
523 : :
524 : : /* Returns an absolute path to the user's data directory.
525 : : * Note the default path depends on the platform.
526 : : * - Windows: CSIDL_APPDATA
527 : : * - OS X: $HOME/Application Support
528 : : * - Linux: $XDG_DATA_HOME (or the default $HOME/.local/share)
529 : : */
530 : : static bfs::path
531 : 33 : get_userdata_home(void)
532 : : {
533 : 33 : auto try_tmp_dir = true;
534 : 33 : auto userdata_home = get_user_data_dir();
535 : :
536 : : /* g_get_user_data_dir doesn't check whether the path exists nor attempts to
537 : : * create it. So while it may return an actual path we may not be able to use it.
538 : : * Let's check that now */
539 : 33 : if (!userdata_home.empty())
540 : : {
541 : : try
542 : : {
543 : 33 : gnc_validate_directory(userdata_home); // May throw
544 : 32 : try_tmp_dir = false;
545 : : }
546 : 1 : catch (const bfs::filesystem_error& ex)
547 : : {
548 : 1 : auto path_string = userdata_home.string();
549 : 1 : g_warning("%s is not a suitable base directory for the user data. "
550 : : "Trying temporary directory instead.\n(Error: %s)",
551 : : path_string.c_str(), ex.what());
552 : 1 : }
553 : : }
554 : :
555 : : /* The path we got is not usable, so fall back to a path in TMP_DIR.
556 : : Hopefully we can always write there. */
557 : 33 : if (try_tmp_dir)
558 : : {
559 : 1 : bfs::path newpath(g_get_tmp_dir (), cvt);
560 : 2 : userdata_home = newpath / g_get_user_name ();
561 : 1 : userdata_home.imbue(bfs_locale);
562 : 1 : }
563 : 33 : g_assert(!userdata_home.empty());
564 : :
565 : 33 : return userdata_home;
566 : 0 : }
567 : :
568 : : /* Returns an absolute path to the user's config directory.
569 : : * Note the default path depends on the platform.
570 : : * - Windows: CSIDL_APPDATA
571 : : * - OS X: $HOME/Application Support
572 : : * - Linux: $XDG_CONFIG_HOME (or the default $HOME/.config)
573 : : */
574 : : static bfs::path
575 : 71 : get_userconfig_home(void)
576 : : {
577 : : /* On Windows and Macs the data directory is used, for Linux
578 : : $HOME/.config is used */
579 : : #if defined (G_OS_WIN32) || defined (MAC_INTEGRATION)
580 : : return get_user_data_dir();
581 : : #else
582 : 71 : return g_get_user_config_dir();
583 : : #endif
584 : : }
585 : :
586 : 4 : static std::string migrate_gnc_datahome()
587 : : {
588 : : // Specify location of dictionaries
589 : 4 : bfs::path old_dir(g_get_home_dir(), cvt);
590 : : old_dir /= ".gnucash";
591 : :
592 : 4 : std::stringstream migration_msg;
593 : 4 : migration_msg.imbue(gnc_get_boost_locale());
594 : :
595 : : /* Step 1: copy directory $HOME/.gnucash to $GNC_DATA_HOME */
596 : 4 : auto full_copy = copy_recursive (old_dir, gnc_userdata_home);
597 : :
598 : : /* Step 2: move user editable config files from GNC_DATA_HOME to GNC_CONFIG_HOME
599 : : These files are:
600 : : - log.conf
601 : : - the most recent of "config-2.0.user", "config-1.8.user", "config-1.6.user",
602 : : "config.user"
603 : : Note: we'll also rename config.user to config-user.scm to make it more clear
604 : : this file is meant for custom scm code to load at run time */
605 : 4 : auto failed = std::vector<std::string>{};
606 : 4 : auto succeeded = std::vector<std::string>{};
607 : :
608 : : /* Move log.conf
609 : : * Note on OS X/Quarz and Windows GNC_DATA_HOME and GNC_CONFIG_HOME are the same, so this will do nothing */
610 : 8 : auto oldlogpath = gnc_userdata_home / "log.conf";
611 : 8 : auto newlogpath = gnc_userconfig_home / "log.conf";
612 : : try
613 : : {
614 : 4 : if (bfs::exists (oldlogpath) && gnc_validate_directory (gnc_userconfig_home) &&
615 : 0 : (oldlogpath != newlogpath))
616 : : {
617 : 0 : bfs::rename (oldlogpath, newlogpath);
618 : 0 : succeeded.emplace_back ("log.conf");
619 : : }
620 : : }
621 : 0 : catch (const bfs::filesystem_error& ex)
622 : : {
623 : 0 : failed.emplace_back ("log.conf");
624 : 0 : }
625 : :
626 : : /* Move/rename the most relevant config*.user file. The relevance comes from
627 : : * the order in which these files were searched for at gnucash startup.
628 : : * Make note of other config*.user files found to inform the user they are now ignored */
629 : : auto user_config_files = std::vector<std::string>
630 : : {
631 : : "config-2.0.user", "config-1.8.user",
632 : : "config-1.6.user", "config.user"
633 : 24 : };
634 : 4 : auto conf_exist_vec = std::vector<std::string> {};
635 : 4 : auto renamed_config = std::string();
636 : 20 : for (auto conf_file : user_config_files)
637 : : {
638 : 32 : auto oldconfpath = gnc_userdata_home / conf_file;
639 : : try
640 : : {
641 : 16 : if (bfs::exists (oldconfpath) && gnc_validate_directory (gnc_userconfig_home))
642 : : {
643 : : // Only migrate the most relevant of the config*.user files
644 : 0 : if (renamed_config.empty())
645 : : {
646 : : /* Translators: this string refers to a file name that gets renamed */
647 : 0 : renamed_config = conf_file + " (" + _("Renamed to:") + " config-user.scm)";
648 : 0 : auto newconfpath = gnc_userconfig_home / "config-user.scm";
649 : 0 : bfs::rename (oldconfpath, newconfpath);
650 : 0 : }
651 : : else
652 : : {
653 : : /* We want to report the obsolete file to the user */
654 : 0 : conf_exist_vec.emplace_back (conf_file);
655 : 0 : if (full_copy)
656 : : /* As we copied from .gnucash, just delete the obsolete file as well. It's still
657 : : * present in .gnucash if the user wants to recover it */
658 : 0 : bfs::remove (oldconfpath);
659 : : }
660 : : }
661 : : }
662 : 0 : catch (const bfs::filesystem_error& ex)
663 : : {
664 : 0 : failed.emplace_back (conf_file);
665 : 0 : }
666 : 16 : }
667 : 4 : if (!renamed_config.empty())
668 : 0 : succeeded.emplace_back (renamed_config);
669 : :
670 : : /* Step 3: inform the user of additional changes */
671 : 4 : if (full_copy || !succeeded.empty() || !conf_exist_vec.empty() || !failed.empty())
672 : 0 : migration_msg << _("Notice") << std::endl << std::endl;
673 : :
674 : 4 : if (full_copy)
675 : : {
676 : : migration_msg
677 : 0 : << _("Your gnucash metadata has been migrated.") << std::endl << std::endl
678 : : /* Translators: this refers to a directory name. */
679 : 0 : << _("Old location:") << " " << old_dir.string() << std::endl
680 : : /* Translators: this refers to a directory name. */
681 : 0 : << _("New location:") << " " << gnc_userdata_home.string() << std::endl << std::endl
682 : : // Translators {1} will be replaced with the package name (typically Gnucash) at runtime
683 : 0 : << bl::format (std::string{_("If you no longer intend to run {1} 2.6.x or older on this system you can safely remove the old directory.")})
684 : 0 : % PACKAGE_NAME;
685 : : }
686 : :
687 : 4 : if (full_copy &&
688 : 0 : (!succeeded.empty() || !conf_exist_vec.empty() || !failed.empty()))
689 : 0 : migration_msg << std::endl << std::endl
690 : 0 : << _("In addition:");
691 : :
692 : 4 : if (!succeeded.empty())
693 : : {
694 : 0 : migration_msg << std::endl << std::endl;
695 : 0 : if (full_copy)
696 : 0 : migration_msg << bl::format (std::string{ngettext("The following file has been copied to {1} instead:",
697 : : "The following files have been copied to {1} instead:",
698 : 0 : succeeded.size())}) % gnc_userconfig_home.string().c_str();
699 : : else
700 : 0 : migration_msg << bl::format (std::string{_("The following file in {1} has been renamed:")})
701 : 0 : % gnc_userconfig_home.string().c_str();
702 : :
703 : 0 : migration_msg << std::endl;
704 : 0 : for (const auto& success_file : succeeded)
705 : 0 : migration_msg << "- " << success_file << std::endl;
706 : : }
707 : 4 : if (!conf_exist_vec.empty())
708 : : {
709 : : migration_msg << "\n\n"
710 : 0 : << ngettext("The following file has become obsolete and will be ignored:",
711 : : "The following files have become obsolete and will be ignored:",
712 : 0 : conf_exist_vec.size())
713 : 0 : << std::endl;
714 : 0 : for (const auto& obs_file : conf_exist_vec)
715 : 0 : migration_msg << "- " << obs_file << std::endl;
716 : : }
717 : 4 : if (!failed.empty())
718 : : {
719 : 0 : migration_msg << std::endl << std::endl
720 : 0 : << bl::format (std::string{ngettext("The following file could not be moved to {1}:",
721 : : "The following files could not be moved to {1}:",
722 : 0 : failed.size())}) % gnc_userconfig_home.string().c_str()
723 : 0 : << std::endl;
724 : 0 : for (const auto& failed_file : failed)
725 : 0 : migration_msg << "- " << failed_file << std::endl;
726 : : }
727 : :
728 : 8 : return migration_msg.str ();
729 : 16 : }
730 : :
731 : :
732 : :
733 : : #if defined G_OS_WIN32 ||defined MAC_INTEGRATION
734 : : constexpr auto path_package = PACKAGE_NAME;
735 : : #else
736 : : constexpr auto path_package = PROJECT_NAME;
737 : : #endif
738 : :
739 : : // Initialize the user's config directory for gnucash
740 : : // creating it if it doesn't exist yet.
741 : : static void
742 : 37 : gnc_file_path_init_config_home (void)
743 : : {
744 : 37 : auto have_valid_userconfig_home = false;
745 : :
746 : : /* If this code is run while building/testing, use a fake GNC_CONFIG_HOME
747 : : * in the base of the build directory. This is to deal with all kinds of
748 : : * issues when the build environment is not a complete environment (like
749 : : * it could be missing a valid home directory). */
750 : 37 : auto env_build_dir = g_getenv ("GNC_BUILDDIR");
751 : 37 : bfs::path new_dir(env_build_dir ? env_build_dir : "", cvt);
752 : 37 : new_dir.imbue(bfs_locale);
753 : 37 : build_dir = std::move(new_dir);
754 : 37 : auto running_uninstalled = (g_getenv ("GNC_UNINSTALLED") != NULL);
755 : 37 : if (running_uninstalled && !build_dir.empty())
756 : : {
757 : 6 : gnc_userconfig_home = build_dir / "gnc_config_home";
758 : : try
759 : : {
760 : 3 : gnc_validate_directory (gnc_userconfig_home); // May throw
761 : 3 : have_valid_userconfig_home = true;
762 : : }
763 : 0 : catch (const bfs::filesystem_error& ex)
764 : : {
765 : 0 : auto path_string = gnc_userconfig_home.string();
766 : 0 : g_warning("%s (due to run during at build time) is not a suitable directory for user configuration files. "
767 : : "Trying another directory instead.\n(Error: %s)",
768 : : path_string.c_str(), ex.what());
769 : 0 : }
770 : : }
771 : :
772 : 37 : if (!have_valid_userconfig_home)
773 : : {
774 : : /* If environment variable GNC_CONFIG_HOME is set, try whether
775 : : * it points at a valid directory. */
776 : 34 : auto gnc_userconfig_home_env = g_getenv ("GNC_CONFIG_HOME");
777 : 34 : if (gnc_userconfig_home_env)
778 : : {
779 : 0 : bfs::path newdir(gnc_userconfig_home_env, cvt);
780 : 0 : newdir.imbue(bfs_locale);
781 : 0 : gnc_userconfig_home = std::move(newdir);
782 : : try
783 : : {
784 : 0 : gnc_validate_directory (gnc_userconfig_home); // May throw
785 : 0 : have_valid_userconfig_home = true;
786 : : }
787 : 0 : catch (const bfs::filesystem_error& ex)
788 : : {
789 : 0 : auto path_string = gnc_userconfig_home.string();
790 : 0 : g_warning("%s (from environment variable 'GNC_CONFIG_HOME') is not a suitable directory for user configuration files. "
791 : : "Trying the default instead.\n(Error: %s)",
792 : : path_string.c_str(), ex.what());
793 : 0 : }
794 : 0 : }
795 : : }
796 : :
797 : 37 : if (!have_valid_userconfig_home)
798 : : {
799 : : /* Determine platform dependent default userconfig_home_path
800 : : * and check whether it's valid */
801 : 34 : auto userconfig_home = get_userconfig_home();
802 : 68 : gnc_userconfig_home = userconfig_home / path_package;
803 : : try
804 : : {
805 : 34 : gnc_validate_directory (gnc_userconfig_home);
806 : : }
807 : 0 : catch (const bfs::filesystem_error& ex)
808 : : {
809 : 0 : g_warning ("User configuration directory doesn't exist, yet could not be created. Proceed with caution.\n"
810 : : "(Error: %s)", ex.what());
811 : 0 : }
812 : 34 : }
813 : 37 : gnc_userconfig_home_str = gnc_userconfig_home.string();
814 : 37 : }
815 : :
816 : : // Initialize the user's config directory for gnucash
817 : : // creating it if it didn't exist yet.
818 : : // The function will return true if the directory already
819 : : // existed or false if it had to be created
820 : : static bool
821 : 37 : gnc_file_path_init_data_home (void)
822 : : {
823 : : // Initialize the user's data directory for gnucash
824 : 37 : auto gnc_userdata_home_exists = false;
825 : 37 : auto have_valid_userdata_home = false;
826 : :
827 : : /* If this code is run while building/testing, use a fake GNC_DATA_HOME
828 : : * in the base of the build directory. This is to deal with all kinds of
829 : : * issues when the build environment is not a complete environment (like
830 : : * it could be missing a valid home directory). */
831 : 37 : auto env_build_dir = g_getenv ("GNC_BUILDDIR");
832 : 37 : bfs::path new_dir(env_build_dir ? env_build_dir : "", cvt);
833 : 37 : new_dir.imbue(bfs_locale);
834 : 37 : build_dir = std::move(new_dir);
835 : 37 : auto running_uninstalled = (g_getenv ("GNC_UNINSTALLED") != NULL);
836 : 37 : if (running_uninstalled && !build_dir.empty())
837 : : {
838 : 6 : gnc_userdata_home = build_dir / "gnc_data_home";
839 : : try
840 : : {
841 : 3 : gnc_validate_directory (gnc_userdata_home); // May throw
842 : 3 : have_valid_userdata_home = true;
843 : 3 : gnc_userdata_home_exists = true; // To prevent possible migration further down
844 : : }
845 : 0 : catch (const bfs::filesystem_error& ex)
846 : : {
847 : 0 : auto path_string = gnc_userdata_home.string();
848 : 0 : g_warning("%s (due to run during at build time) is not a suitable directory for user data. "
849 : : "Trying another directory instead.\n(Error: %s)",
850 : : path_string.c_str(), ex.what());
851 : 0 : }
852 : : }
853 : :
854 : 37 : if (!have_valid_userdata_home)
855 : : {
856 : : /* If environment variable GNC_DATA_HOME is set, try whether
857 : : * it points at a valid directory. */
858 : 34 : auto gnc_userdata_home_env = g_getenv ("GNC_DATA_HOME");
859 : 34 : if (gnc_userdata_home_env)
860 : : {
861 : 2 : bfs::path newdir(gnc_userdata_home_env, cvt);
862 : 2 : newdir.imbue(bfs_locale);
863 : 2 : gnc_userdata_home = std::move(newdir);
864 : : try
865 : : {
866 : 2 : gnc_userdata_home_exists = bfs::exists (gnc_userdata_home);
867 : 2 : gnc_validate_directory (gnc_userdata_home); // May throw
868 : 1 : have_valid_userdata_home = true;
869 : : }
870 : 1 : catch (const bfs::filesystem_error& ex)
871 : : {
872 : 1 : auto path_string = gnc_userdata_home.string();
873 : 1 : g_warning("%s (from environment variable 'GNC_DATA_HOME') is not a suitable directory for user data. "
874 : : "Trying the default instead.\n(Error: %s)",
875 : : path_string.c_str(), ex.what());
876 : 1 : }
877 : 2 : }
878 : : }
879 : :
880 : 37 : if (!have_valid_userdata_home)
881 : : {
882 : : /* Determine platform dependent default userdata_home_path
883 : : * and check whether it's valid */
884 : 33 : auto userdata_home = get_userdata_home();
885 : 66 : gnc_userdata_home = userdata_home / path_package;
886 : : try
887 : : {
888 : 33 : gnc_userdata_home_exists = bfs::exists (gnc_userdata_home);
889 : 33 : gnc_validate_directory (gnc_userdata_home);
890 : : }
891 : 0 : catch (const bfs::filesystem_error& ex)
892 : : {
893 : 0 : g_warning ("User data directory doesn't exist, yet could not be created. Proceed with caution.\n"
894 : : "(Error: %s)", ex.what());
895 : 0 : }
896 : 33 : }
897 : 37 : gnc_userdata_home_str = gnc_userdata_home.string();
898 : 37 : return gnc_userdata_home_exists;
899 : 37 : }
900 : :
901 : : // Initialize the user's config and data directory for gnucash
902 : : // This function will also create these directories if they didn't
903 : : // exist yet.
904 : : // In addition it will trigger a migration if the user's data home
905 : : // didn't exist but the now obsolete GNC_DOT_DIR ($HOME/.gnucash)
906 : : // does.
907 : : // Finally it well ensure a number of default required directories
908 : : // will be created if they don't exist yet.
909 : : char *
910 : 37 : gnc_filepath_init (void)
911 : : {
912 : 74 : gnc_userconfig_home = get_userconfig_home() / path_package;
913 : 37 : gnc_userconfig_home_str = gnc_userconfig_home.string();
914 : :
915 : 37 : gnc_file_path_init_config_home ();
916 : 37 : auto gnc_userdata_home_exists = gnc_file_path_init_data_home ();
917 : :
918 : : /* Run migration code before creating the default directories
919 : : If migrating, these default directories are copied instead of created. */
920 : 37 : auto migration_notice = std::string ();
921 : 37 : if (!gnc_userdata_home_exists)
922 : 4 : migration_notice = migrate_gnc_datahome();
923 : :
924 : : /* Try to create the standard subdirectories for gnucash' user data */
925 : : try
926 : : {
927 : 74 : gnc_validate_directory (gnc_userdata_home / "books");
928 : 74 : gnc_validate_directory (gnc_userdata_home / "checks");
929 : 74 : gnc_validate_directory (gnc_userdata_home / "translog");
930 : : }
931 : 0 : catch (const bfs::filesystem_error& ex)
932 : : {
933 : 0 : g_warning ("Default user data subdirectories don't exist, yet could not be created. Proceed with caution.\n"
934 : : "(Error: %s)", ex.what());
935 : 0 : }
936 : :
937 : 74 : return migration_notice.empty() ? NULL : g_strdup (migration_notice.c_str());
938 : 37 : }
939 : :
940 : : /** @fn const gchar * gnc_userdata_dir ()
941 : : * @brief Ensure that the user's configuration directory exists and is minimally populated.
942 : : *
943 : : * Note that the default path depends on the platform.
944 : : * - Windows: CSIDL_APPDATA/Gnucash
945 : : * - OS X: $HOME/Application Support/Gnucash
946 : : * - Linux: $XDG_DATA_HOME/gnucash (or the default $HOME/.local/share/gnucash)
947 : : *
948 : : * If any of these paths don't exist and can't be created the function will
949 : : * fall back to
950 : : * - $HOME/.gnucash
951 : : * - <TMP_DIR>/gnucash
952 : : * (in that order)
953 : : *
954 : : * @return An absolute path to the configuration directory. This string is
955 : : * owned by the gnc_filepath_utils code and should not be freed by the user.
956 : : */
957 : :
958 : :
959 : : /* Note Don't create missing directories automatically
960 : : * here and in the next function except if the
961 : : * target directory is the temporary directory. This
962 : : * should be done properly at a higher level (in the gui
963 : : * code most likely) very early in application startup.
964 : : * This call is just a fallback to prevent the code from
965 : : * crashing because no directories were configured. This
966 : : * weird construct is set up because compiling our guile
967 : : * scripts also triggers this code and that's not the
968 : : * right moment to start creating the necessary directories.
969 : : * FIXME A better approach would be to have the gnc_userdata_home
970 : : * verification/creation be part of the application code instead
971 : : * of libgnucash. If libgnucash needs access to this directory
972 : : * libgnucash will need some kind of initialization routine
973 : : * that the application can call to set (among others) the proper
974 : : * gnc_uderdata_home for libgnucash. The only other aspect to
975 : : * consider here is how to handle this in the bindings (if they
976 : : * need it).
977 : : */
978 : : const gchar *
979 : 0 : gnc_userdata_dir (void)
980 : : {
981 : 0 : if (gnc_userdata_home.empty())
982 : 0 : gnc_filepath_init();
983 : 0 : return g_strdup(gnc_userdata_home_str.c_str());
984 : : }
985 : :
986 : : /** @fn const gchar * gnc_userconfig_dir ()
987 : : * @brief Return the user's config directory for gnucash
988 : : *
989 : : * @note the default path depends on the platform.
990 : : * - Windows: CSIDL_APPDATA/Gnucash
991 : : * - OS X: $HOME/Application Support/Gnucash
992 : : * - Linux: $XDG_CONFIG_HOME/gnucash (or the default $HOME/.config/gnucash)
993 : : *
994 : : * @note gnucash will not create this directory if it doesn't exist
995 : : *
996 : : * @return An absolute path to the configuration directory. This string is
997 : : * owned by the gnc_filepath_utils code and should not be freed by the user.
998 : : */
999 : : const gchar *
1000 : 0 : gnc_userconfig_dir (void)
1001 : : {
1002 : 0 : if (gnc_userdata_home.empty())
1003 : 0 : gnc_filepath_init();
1004 : :
1005 : 0 : return gnc_userconfig_home_str.c_str();
1006 : : }
1007 : :
1008 : : static const bfs::path&
1009 : 85 : gnc_userdata_dir_as_path (void)
1010 : : {
1011 : 85 : if (gnc_userdata_home.empty())
1012 : : /* Don't create missing directories automatically except
1013 : : * if the target directory is the temporary directory. This
1014 : : * should be done properly at a higher level (in the gui
1015 : : * code most likely) very early in application startup.
1016 : : * This call is just a fallback to prevent the code from
1017 : : * crashing because no directories were configured. */
1018 : 34 : gnc_filepath_init();
1019 : :
1020 : 85 : return gnc_userdata_home;
1021 : : }
1022 : :
1023 : : static const bfs::path&
1024 : 0 : gnc_userconfig_dir_as_path (void)
1025 : : {
1026 : 0 : if (gnc_userdata_home.empty())
1027 : : /* Don't create missing directories automatically except
1028 : : * if the target directory is the temporary directory. This
1029 : : * should be done properly at a higher level (in the gui
1030 : : * code most likely) very early in application startup.
1031 : : * This call is just a fallback to prevent the code from
1032 : : * crashing because no directories were configured. */
1033 : 0 : gnc_filepath_init();
1034 : :
1035 : 0 : return gnc_userconfig_home;
1036 : : }
1037 : :
1038 : 0 : gchar *gnc_file_path_absolute (const gchar *prefix, const gchar *relative)
1039 : : {
1040 : 0 : bfs::path path_relative (relative);
1041 : 0 : path_relative.imbue (bfs_locale);
1042 : 0 : bfs::path path_absolute;
1043 : 0 : bfs::path path_head;
1044 : :
1045 : 0 : if (prefix == nullptr)
1046 : : {
1047 : 0 : const gchar *doc_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
1048 : 0 : if (doc_dir == nullptr)
1049 : 0 : path_head = bfs::path (gnc_userdata_dir ()); // running as root maybe
1050 : : else
1051 : 0 : path_head = bfs::path (doc_dir);
1052 : :
1053 : 0 : path_head.imbue (bfs_locale);
1054 : 0 : path_absolute = absolute (path_relative, path_head);
1055 : : }
1056 : : else
1057 : : {
1058 : 0 : bfs::path path_head (prefix);
1059 : 0 : path_head.imbue (bfs_locale);
1060 : 0 : path_absolute = absolute (path_relative, path_head);
1061 : 0 : }
1062 : 0 : path_absolute.imbue (bfs_locale);
1063 : :
1064 : 0 : return g_strdup (path_absolute.string().c_str());
1065 : 0 : }
1066 : :
1067 : : /** @fn gchar * gnc_build_userdata_path (const gchar *filename)
1068 : : * @brief Make a path to filename in the user's gnucash data directory.
1069 : : *
1070 : : * @param filename The name of the file
1071 : : *
1072 : : * @return An absolute path. The returned string should be freed by the user
1073 : : * using g_free().
1074 : : */
1075 : :
1076 : : gchar *
1077 : 38 : gnc_build_userdata_path (const gchar *filename)
1078 : : {
1079 : 114 : return g_strdup((gnc_userdata_dir_as_path() / filename).string().c_str());
1080 : : }
1081 : :
1082 : : /** @fn gchar * gnc_build_userconfig_path (const gchar *filename)
1083 : : * @brief Make a path to filename in the user's configuration directory.
1084 : : *
1085 : : * @param filename The name of the file
1086 : : *
1087 : : * @return An absolute path. The returned string should be freed by the user
1088 : : * using g_free().
1089 : : */
1090 : :
1091 : : gchar *
1092 : 0 : gnc_build_userconfig_path (const gchar *filename)
1093 : : {
1094 : 0 : return g_strdup((gnc_userconfig_dir_as_path() / filename).string().c_str());
1095 : : }
1096 : :
1097 : : /* Test whether c is a valid character for a win32 file name.
1098 : : * If so return false, otherwise return true.
1099 : : */
1100 : : static bool
1101 : 973 : is_invalid_char (char c)
1102 : : {
1103 : 973 : return (c == '/') || ( c == ':');
1104 : : }
1105 : :
1106 : : static bfs::path
1107 : 47 : gnc_build_userdata_subdir_path (const gchar *subdir, const gchar *filename)
1108 : : {
1109 : 47 : auto fn = std::string(filename);
1110 : :
1111 : 47 : std::replace_if (fn.begin(), fn.end(), is_invalid_char, '_');
1112 : 94 : auto result = (gnc_userdata_dir_as_path() / subdir) / fn;
1113 : 94 : return result;
1114 : 47 : }
1115 : :
1116 : : /** @fn gchar * gnc_build_book_path (const gchar *filename)
1117 : : * @brief Make a path to filename in the book subdirectory of the user's configuration directory.
1118 : : *
1119 : : * @param filename The name of the file
1120 : : *
1121 : : * @return An absolute path. The returned string should be freed by the user
1122 : : * using g_free().
1123 : : */
1124 : :
1125 : : gchar *
1126 : 5 : gnc_build_book_path (const gchar *filename)
1127 : : {
1128 : 5 : auto path = gnc_build_userdata_subdir_path("books", filename).string();
1129 : 15 : return g_strdup(path.c_str());
1130 : 5 : }
1131 : :
1132 : : /** @fn gchar * gnc_build_translog_path (const gchar *filename)
1133 : : * @brief Make a path to filename in the translog subdirectory of the user's configuration directory.
1134 : : *
1135 : : * @param filename The name of the file
1136 : : *
1137 : : * @return An absolute path. The returned string should be freed by the user
1138 : : * using g_free().
1139 : : */
1140 : :
1141 : : gchar *
1142 : 5 : gnc_build_translog_path (const gchar *filename)
1143 : : {
1144 : 5 : auto path = gnc_build_userdata_subdir_path("translog", filename).string();
1145 : 15 : return g_strdup(path.c_str());
1146 : 5 : }
1147 : :
1148 : : /** @fn gchar * gnc_build_data_path (const gchar *filename)
1149 : : * @brief Make a path to filename in the data subdirectory of the user's configuration directory.
1150 : : *
1151 : : * @param filename The name of the file
1152 : : *
1153 : : * @return An absolute path. The returned string should be freed by the user
1154 : : * using g_free().
1155 : : */
1156 : :
1157 : : gchar *
1158 : 37 : gnc_build_data_path (const gchar *filename)
1159 : : {
1160 : 37 : auto path = gnc_build_userdata_subdir_path("data", filename).string();
1161 : 111 : return g_strdup(path.c_str());
1162 : 37 : }
1163 : :
1164 : : /** @fn gchar * gnc_build_scm_path (const gchar *filename)
1165 : : * @brief Make a path to filename in the scm directory.
1166 : : *
1167 : : * @param filename The name of the file
1168 : : *
1169 : : * @return An absolute path. The returned string should be freed by the user
1170 : : * using g_free().
1171 : : */
1172 : :
1173 : : gchar *
1174 : 12 : gnc_build_scm_path (const gchar *filename)
1175 : : {
1176 : 12 : gchar *scmdir = gnc_path_get_scmdir ();
1177 : 12 : gchar *result = g_build_filename (scmdir, filename, (gchar *)NULL);
1178 : 12 : g_free (scmdir);
1179 : 12 : return result;
1180 : : }
1181 : :
1182 : : /** @fn gchar * gnc_build_report_path (const gchar *filename)
1183 : : * @brief Make a path to filename in the report directory.
1184 : : *
1185 : : * @param filename The name of the file
1186 : : *
1187 : : * @return An absolute path. The returned string should be freed by the user
1188 : : * using g_free().
1189 : : */
1190 : :
1191 : : gchar *
1192 : 0 : gnc_build_report_path (const gchar *filename)
1193 : : {
1194 : 0 : gchar *rptdir = gnc_path_get_reportdir ();
1195 : 0 : gchar *result = g_build_filename (rptdir, filename, (gchar *)NULL);
1196 : 0 : g_free (rptdir);
1197 : 0 : return result;
1198 : : }
1199 : :
1200 : : /** @fn gchar * gnc_build_reports_path (const gchar *dirname)
1201 : : * @brief Make a path to dirname in the reports directory.
1202 : : *
1203 : : * @param dirname The name of the subdirectory
1204 : : *
1205 : : * @return An absolute path. The returned string should be freed by the user
1206 : : * using g_free().
1207 : : */
1208 : :
1209 : : gchar *
1210 : 0 : gnc_build_reports_path (const gchar *dirname)
1211 : : {
1212 : 0 : gchar *rptsdir = gnc_path_get_reportsdir ();
1213 : 0 : gchar *result = g_build_filename (rptsdir, dirname, (gchar *)NULL);
1214 : 0 : g_free (rptsdir);
1215 : 0 : return result;
1216 : : }
1217 : :
1218 : : /** @fn gchar * gnc_build_stdreports_path (const gchar *filename)
1219 : : * @brief Make a path to filename in the standard reports directory.
1220 : : *
1221 : : * @param filename The name of the file
1222 : : *
1223 : : * @return An absolute path. The returned string should be freed by the user
1224 : : * using g_free().
1225 : : */
1226 : :
1227 : : gchar *
1228 : 0 : gnc_build_stdreports_path (const gchar *filename)
1229 : : {
1230 : 0 : gchar *stdrptdir = gnc_path_get_stdreportsdir ();
1231 : 0 : gchar *result = g_build_filename (stdrptdir, filename, (gchar *)NULL);
1232 : 0 : g_free (stdrptdir);
1233 : 0 : return result;
1234 : : }
1235 : :
1236 : : static gchar *
1237 : 0 : gnc_filepath_locate_file (const gchar *default_path, const gchar *name)
1238 : : {
1239 : : gchar *fullname;
1240 : :
1241 : 0 : g_return_val_if_fail (name != NULL, NULL);
1242 : :
1243 : 0 : if (g_path_is_absolute (name))
1244 : 0 : fullname = g_strdup (name);
1245 : 0 : else if (default_path)
1246 : 0 : fullname = g_build_filename (default_path, name, nullptr);
1247 : : else
1248 : 0 : fullname = gnc_resolve_file_path (name);
1249 : :
1250 : 0 : if (!g_file_test (fullname, G_FILE_TEST_IS_REGULAR))
1251 : : {
1252 : 0 : g_warning ("Could not locate file %s", name);
1253 : 0 : g_free (fullname);
1254 : 0 : return NULL;
1255 : : }
1256 : :
1257 : 0 : return fullname;
1258 : : }
1259 : :
1260 : : gchar *
1261 : 0 : gnc_filepath_locate_data_file (const gchar *name)
1262 : : {
1263 : 0 : gchar *pkgdatadir = gnc_path_get_pkgdatadir ();
1264 : 0 : gchar *result = gnc_filepath_locate_file (pkgdatadir, name);
1265 : 0 : g_free (pkgdatadir);
1266 : 0 : return result;
1267 : : }
1268 : :
1269 : : gchar *
1270 : 0 : gnc_filepath_locate_pixmap (const gchar *name)
1271 : : {
1272 : : gchar *default_path;
1273 : : gchar *fullname;
1274 : 0 : gchar* pkgdatadir = gnc_path_get_pkgdatadir ();
1275 : :
1276 : 0 : default_path = g_build_filename (pkgdatadir, "pixmaps", nullptr);
1277 : 0 : g_free(pkgdatadir);
1278 : 0 : fullname = gnc_filepath_locate_file (default_path, name);
1279 : 0 : g_free(default_path);
1280 : :
1281 : 0 : return fullname;
1282 : : }
1283 : :
1284 : : gchar *
1285 : 0 : gnc_filepath_locate_ui_file (const gchar *name)
1286 : : {
1287 : : gchar *default_path;
1288 : : gchar *fullname;
1289 : 0 : gchar* pkgdatadir = gnc_path_get_pkgdatadir ();
1290 : :
1291 : 0 : default_path = g_build_filename (pkgdatadir, "ui", nullptr);
1292 : 0 : g_free(pkgdatadir);
1293 : 0 : fullname = gnc_filepath_locate_file (default_path, name);
1294 : 0 : g_free(default_path);
1295 : :
1296 : 0 : return fullname;
1297 : : }
1298 : :
1299 : : gchar *
1300 : 0 : gnc_filepath_locate_doc_file (const gchar *name)
1301 : : {
1302 : 0 : gchar *docdir = gnc_path_get_pkgdocdir ();
1303 : 0 : gchar *result = gnc_filepath_locate_file (docdir, name);
1304 : 0 : g_free (docdir);
1305 : 0 : return result;
1306 : : }
1307 : :
1308 : : std::vector<EnvPaths>
1309 : 0 : gnc_list_all_paths ()
1310 : : {
1311 : 0 : if (gnc_userdata_home.empty())
1312 : 0 : gnc_filepath_init ();
1313 : :
1314 : : return {
1315 : 0 : { "GNC_DATA_HOME", gnc_userdata_home_str.c_str(), true},
1316 : 0 : { "GNC_CONFIG_HOME", gnc_userconfig_home_str.c_str(), true },
1317 : 0 : { "GNC_BIN", g_getenv ("GNC_BIN"), false },
1318 : 0 : { "GNC_LIB", g_getenv ("GNC_LIB"), false },
1319 : 0 : { "GNC_CONF", g_getenv ("GNC_CONF"), false },
1320 : 0 : { "GNC_DATA", g_getenv ("GNC_DATA"), false },
1321 : 0 : };
1322 : : }
1323 : :
1324 : : static const std::regex
1325 : : backup_regex (".*[.](?:xac|gnucash)[.][0-9]{14}[.](?:xac|gnucash)$");
1326 : :
1327 : 0 : gboolean gnc_filename_is_backup (const char *filename)
1328 : : {
1329 : 0 : return std::regex_match (filename, backup_regex);
1330 : : }
1331 : :
1332 : : static const std::regex
1333 : : datafile_regex (".*[.](?:xac|gnucash)$");
1334 : :
1335 : 0 : gboolean gnc_filename_is_datafile (const char *filename)
1336 : : {
1337 : 0 : return !gnc_filename_is_backup (filename) &&
1338 : 0 : std::regex_match (filename, datafile_regex);
1339 : : }
1340 : :
1341 : : std::ofstream
1342 : 0 : gnc_open_filestream(const char* path)
1343 : : {
1344 : 0 : bfs::path bfs_path(path, cvt);
1345 : 0 : bfs_path.imbue(bfs_locale);
1346 : 0 : return std::ofstream(bfs_path.c_str());
1347 : 0 : }
1348 : : /* =============================== END OF FILE ========================== */
|