GnuCash 2.4.99
Functions
gnc-filepath-utils.h File Reference

File path resolution utility functions. More...

Go to the source code of this file.

Functions

gchar * gnc_resolve_file_path (const gchar *filefrag)
 Create an absolute path when given a relative path; otherwise return the argument.
const gchar * gnc_dotgnucash_dir (void)
 Ensure that the user's configuration directory exists and is minimally populated.
gchar * gnc_build_dotgnucash_path (const gchar *filename)
 Make a path to filename in the user's configuration directory.
gchar * gnc_build_book_path (const gchar *filename)
 Make a path to filename in the book subdirectory of the user's configuration directory.
gchar * gnc_build_translog_path (const gchar *filename)
 Make a path to filename in the translog subdirectory of the user's configuration directory.
gchar * gnc_build_data_path (const gchar *filename)
 Make a path to filename in the data subdirectory of the user's configuration directory.
gchar * gnc_build_report_path (const gchar *filename)
 Make a path to filename in the report directory.
gchar * gnc_build_stdreports_path (const gchar *filename)
 Make a path to filename in the standard reports directory.

Detailed Description

File path resolution utility functions.

Author:
Copyright (c) 1998-2004 Linas Vepstas <linas@linas.org>
Copyright (c) 2000 Dave Peticolas

Definition in file gnc-filepath-utils.h.


Function Documentation

gchar* gnc_build_book_path ( const gchar *  filename)

Make a path to filename in the book subdirectory of the user's configuration directory.

Parameters:
filenameThe name of the file
Returns:
An absolute path.

Definition at line 343 of file gnc-filepath-utils.c.

{
    gchar* filename_dup = g_strdup(filename);
    gchar* result = NULL;

    scrub_filename(filename_dup);
    result = g_build_filename(gnc_dotgnucash_dir(), "books",
                              filename_dup, (gchar *)NULL);
    g_free(filename_dup);
    return result;
}
gchar* gnc_build_data_path ( const gchar *  filename)

Make a path to filename in the data subdirectory of the user's configuration directory.

Parameters:
filenameThe name of the file
Returns:
An absolute path.

Definition at line 385 of file gnc-filepath-utils.c.

{
    gchar* filename_dup = g_strdup(filename);
    gchar* result;

    scrub_filename(filename_dup);
    result = g_build_filename(gnc_dotgnucash_dir(), "data", filename_dup, (gchar *)NULL);
    g_free(filename_dup);
    return result;
}
gchar* gnc_build_dotgnucash_path ( const gchar *  filename)

Make a path to filename in the user's configuration directory.

Parameters:
filenameThe name of the file
Returns:
An absolute path.

Definition at line 329 of file gnc-filepath-utils.c.

{
    return g_build_filename(gnc_dotgnucash_dir(), filename, (gchar *)NULL);
}
gchar* gnc_build_report_path ( const gchar *  filename)

Make a path to filename in the report directory.

Parameters:
filenameThe name of the file
Returns:
An absolute path.

Definition at line 405 of file gnc-filepath-utils.c.

{
    gchar *result = g_build_filename(gnc_path_get_reportdir(), filename, (gchar *)NULL);
    return result;
}
gchar* gnc_build_stdreports_path ( const gchar *  filename)

Make a path to filename in the standard reports directory.

Parameters:
filenameThe name of the file
Returns:
An absolute path.

Definition at line 420 of file gnc-filepath-utils.c.

{
    gchar *result = g_build_filename(gnc_path_get_stdreportsdir(), filename, (gchar *)NULL);
    return result;
}
gchar* gnc_build_translog_path ( const gchar *  filename)

Make a path to filename in the translog subdirectory of the user's configuration directory.

Parameters:
filenameThe name of the file
Returns:
An absolute path.

Definition at line 364 of file gnc-filepath-utils.c.

{
    gchar* filename_dup = g_strdup(filename);
    gchar* result = NULL;

    scrub_filename(filename_dup);
    result = g_build_filename(gnc_dotgnucash_dir(), "translog",
                              filename_dup, (gchar *)NULL);
    g_free(filename_dup);
    return result;
}
const gchar* gnc_dotgnucash_dir ( void  )

Ensure that the user's configuration directory exists and is minimally populated.

Note that the default path is $HOME/.gnucash; This can be changed by the environment variable $GNC_DOT_DIR.

Returns:
An absolute path to the configuration directory

Definition at line 282 of file gnc-filepath-utils.c.

{
    static gchar *dotgnucash = NULL;
    gchar *tmp_dir;

    if (dotgnucash)
        return dotgnucash;

    dotgnucash = g_strdup(g_getenv("GNC_DOT_DIR"));

    if (!dotgnucash)
    {
        const gchar *home = g_get_home_dir();
        if (!home)
        {
            g_warning("Cannot find home directory. Using tmp directory instead.");
            home = g_get_tmp_dir();
        }
        g_assert(home);

        dotgnucash = g_build_filename(home, ".gnucash", (gchar *)NULL);
    }
    gnc_validate_directory(dotgnucash);

    /* Since we're in code that is only executed once.... */
    tmp_dir = g_build_filename(dotgnucash, "books", (gchar *)NULL);
    gnc_validate_directory(tmp_dir);
    g_free(tmp_dir);
    tmp_dir = g_build_filename(dotgnucash, "checks", (gchar *)NULL);
    gnc_validate_directory(tmp_dir);
    g_free(tmp_dir);
    tmp_dir = g_build_filename(dotgnucash, "translog", (gchar *)NULL);
    gnc_validate_directory(tmp_dir);
    g_free(tmp_dir);

    return dotgnucash;
}
gchar* gnc_resolve_file_path ( const gchar *  filefrag)

Create an absolute path when given a relative path; otherwise return the argument.

The gnc_resolve_file_path() routine is a utility that will accept a fragmentary filename as input, and resolve it into a fully qualified path in the file system, i.e. a path that begins with a leading slash. First, the current working directory is searched for the file. Next, the directory $HOME/.gnucash/data, and finally, a list of other (configurable) paths. If the file is not found, then the path $HOME/.gnucash/data is used. If $HOME is not defined, then the current working directory is used.

Warning:
filefrag should be a simple path fragment. It shouldn't contain xml:// or http:// or <whatever>:// other protocol specifiers.

If passed a string which g_path_is_absolute declares an absolute path, return the argument.

Otherwise, assume that filefrag is a well-formed relative path and try to find a file with its path relative to

  • the current working directory,
  • the installed system-wide data directory (e.g., /usr/local/share/gnucash),
  • the installed system configuration directory (e.g., /usr/local/etc/gnucash),
  • or in the user's configuration directory (e.g., $HOME/.gnucash/data)

The paths are searched for in that order. If a matching file is found, return the absolute path to it.

If one isn't found, return a absolute path relative to the user's configuration directory and note in the trace file that it needs to be created.

Parameters:
filefragThe file path to resolve
Returns:
An absolute file path.

Definition at line 120 of file gnc-filepath-utils.c.

{
    int namelen;
    gchar *fullpath = NULL, *tmp_path = NULL;

    /* seriously invalid */
    if (!filefrag)
    {
        g_critical("filefrag is NULL");
        return NULL;
    }

    /* ---------------------------------------------------- */
    /* OK, now we try to find or build an absolute file path */

    /* check for an absolute file path */
    if (g_path_is_absolute(filefrag))
        return g_strdup (filefrag);

    /* get conservative on the length so that sprintf(getpid()) works ... */
    /* strlen ("/.LCK") + sprintf (%x%d) */
    namelen = strlen (filefrag) + 25;

    /* Look in the current working directory */
    tmp_path = g_get_current_dir();
    fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
    g_free(tmp_path);
    fullpath = check_path_return_if_valid(fullpath);
    if (fullpath != NULL)
        return fullpath;

    /* Look in the data dir (e.g. $PREFIX/share/gnucash) */
    tmp_path = gnc_path_get_pkgdatadir();
    fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
    g_free(tmp_path);
    fullpath = check_path_return_if_valid(fullpath);
    if (fullpath != NULL)
        return fullpath;

    /* Look in the config dir (e.g. $PREFIX/share/gnucash/accounts) */
    tmp_path = gnc_path_get_accountsdir();
    fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
    g_free(tmp_path);
    fullpath = check_path_return_if_valid(fullpath);
    if (fullpath != NULL)
        return fullpath;

    /* Look in the users config dir (e.g. $HOME/.gnucash/data) */
    fullpath = gnc_build_data_path(filefrag);
    if (g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
        return fullpath;

    /* OK, it's not there. Note that it needs to be created and pass it
     * back anyway */
    g_warning("create new file %s", fullpath);
    return fullpath;

}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines