From d70897f38a81fa2d0004bf9fe3d6eb0ffb50d519 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Mon, 28 Dec 1998 06:28:04 +0000 Subject: [PATCH] move path resolution to its own utility function git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@1493 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/Session.c | 216 +++++++++++++++++++++++-------------------- src/engine/Session.h | 10 ++ 2 files changed, 124 insertions(+), 102 deletions(-) diff --git a/src/engine/Session.c b/src/engine/Session.c index 33a2e7e7f5..7a03094074 100644 --- a/src/engine/Session.c +++ b/src/engine/Session.c @@ -130,14 +130,6 @@ xaccSessionGetFilePath (Session *sess) /* ============================================================== */ -/* hack alert -- we should be yanking this out of - * some config file - */ -static char * searchpaths[] = { - "/usr/share/gnucash/data", - NULL, -}; - AccountGroup * xaccSessionBegin (Session *sess, char * sid) { @@ -178,8 +170,7 @@ xaccSessionBeginFile (Session *sess, char * filefrag) struct stat statbuf; char pathbuf[PATH_MAX]; char *path = NULL; - int namelen, len; - int i, rc; + int rc; if (!sess) return NULL; @@ -201,105 +192,21 @@ xaccSessionBeginFile (Session *sess, char * filefrag) /* ---------------------------------------------------- */ /* OK, now we try to find or build an absolute file path */ - /* check for an absolute file path */ - if ('/' == *filefrag) { - sess->fullpath = strdup (filefrag); - } else { - - /* get conservative on the length so that sprintf(getpid()) works ... */ - /* strlen ("/.LCK") + sprintf (%x%d) */ - namelen = strlen (filefrag) + 25; - - for (i=-2; 1 ; i++) - { - switch (i) { - case -2: { - /* try to find a file by this name in the cwd ... */ - path = getcwd (pathbuf, PATH_MAX); - if (!path) continue; - len = strlen (path) + namelen; - if (PATH_MAX <= len) continue; - strcat (path, "/"); - break; - } - case -1: { - /* look for something in $HOME/.gnucash/data */ - path = getenv ("HOME"); - if (!path) continue; - len = strlen (path) + namelen + 20; - if (PATH_MAX <= len) continue; - strcpy (pathbuf, path); - strcat (pathbuf, "/.gnucash/data/"); - path = pathbuf; - break; - } - default: { - /* OK, check the user-configured paths */ - path = searchpaths[i]; - len = strlen (path) + namelen; - if (PATH_MAX <= len) continue; - strcpy (pathbuf, path); - path = pathbuf; - } - } - - if (!path) break; - - /* lets see if we found the file here ... */ - strcat (path, filefrag); - rc = stat (path, &statbuf); - if (!rc) { - sess->fullpath = strdup (path); - break; - } - } - - /* OK, we didn't find the file */ - /* Lets try creating a new file in $HOME/.gnucash/data */ - if (!(sess->fullpath)) - { - path = getenv ("HOME"); - if (path) { - len = strlen (path) + namelen + 50; - if (PATH_MAX > len) { - strcpy (pathbuf, path); - strcat (pathbuf, "/.gnucash/data/"); - strcat (pathbuf, filefrag); - sess->fullpath = strdup (pathbuf); - } - } - } - - /* OK, we still didn't find the file */ - /* Lets try creating a new file in the cwd */ - if (!(sess->fullpath)) - { - /* create a new file in the cwd */ - path = getcwd (pathbuf, PATH_MAX); - if (!path) { - sess->errtype = ERANGE; - return NULL; /* ouch */ - } - len = strlen (path) + namelen; - if (PATH_MAX <= len) { - sess->errtype = ERANGE; - return NULL; /* ouch */ - } - strcat (path, "/"); - strcat (path, filefrag); - sess->fullpath = strdup (path); - } + sess->fullpath = xaccResolveFilePath (filefrag); + if (! (sess->fullpath)) { + sess->errtype = ERANGE; + return NULL; /* ouch */ } - assert (sess->fullpath); /* no one fucked with the code, yeah? */ - + /* Store the sessionid URL also ... */ strcpy (pathbuf, "file:"); strcat (pathbuf, filefrag); sess->sessionid = strdup (pathbuf); /* ---------------------------------------------------- */ - /* Yow! OK, after all of that, we've finnaly got a fully - * resolved path name. Lets see if we can get a lock on it */ + /* We should now have a fully resolved path name. + * Lets see if we can get a lock on it. + */ sess->lockfile = malloc (strlen (sess->fullpath) + 5); strcpy (sess->lockfile, sess->fullpath); @@ -424,4 +331,109 @@ xaccSessionDestroy (Session *sess) free (sess); } +/* ============================================================== */ + +/* hack alert -- we should be yanking this out of + * some config file + */ +static char * searchpaths[] = { + "/usr/share/gnucash/data", + NULL, +}; + +char * +xaccResolveFilePath (const char * filefrag) +{ + struct stat statbuf; + char pathbuf[PATH_MAX]; + char *path = NULL; + int namelen, len; + int i, rc; + + /* seriously invalid */ + if (!filefrag) return NULL; + + /* ---------------------------------------------------- */ + /* OK, now we try to find or build an absolute file path */ + + /* check for an absolute file path */ + if ('/' == *filefrag) { + return (strdup (filefrag)); + } + + /* get conservative on the length so that sprintf(getpid()) works ... */ + /* strlen ("/.LCK") + sprintf (%x%d) */ + namelen = strlen (filefrag) + 25; + + for (i=-2; 1 ; i++) + { + switch (i) { + case -2: { + /* try to find a file by this name in the cwd ... */ + path = getcwd (pathbuf, PATH_MAX); + if (!path) continue; + len = strlen (path) + namelen; + if (PATH_MAX <= len) continue; + strcat (path, "/"); + break; + } + case -1: { + /* look for something in $HOME/.gnucash/data */ + path = getenv ("HOME"); + if (!path) continue; + len = strlen (path) + namelen + 20; + if (PATH_MAX <= len) continue; + strcpy (pathbuf, path); + strcat (pathbuf, "/.gnucash/data/"); + path = pathbuf; + break; + } + default: { + /* OK, check the user-configured paths */ + path = searchpaths[i]; + len = strlen (path) + namelen; + if (PATH_MAX <= len) continue; + strcpy (pathbuf, path); + path = pathbuf; + } + } + + if (!path) break; + + /* lets see if we found the file here ... */ + strcat (path, filefrag); + rc = stat (path, &statbuf); + if (!rc) { + return (strdup (path)); + } + } + + /* OK, we didn't find the file */ + /* Lets try creating a new file in $HOME/.gnucash/data */ + path = getenv ("HOME"); + if (path) { + len = strlen (path) + namelen + 50; + if (PATH_MAX > len) { + strcpy (pathbuf, path); + strcat (pathbuf, "/.gnucash/data/"); + strcat (pathbuf, filefrag); + return (strdup (pathbuf)); + } + } + + /* OK, we still didn't find the file */ + /* Lets try creating a new file in the cwd */ + path = getcwd (pathbuf, PATH_MAX); + if (path) { + len = strlen (path) + namelen; + if (PATH_MAX > len) { + strcat (path, "/"); + strcat (path, filefrag); + return (strdup (path)); + } + } + + return NULL; +} + /* ==================== END OF FILE ================== */ diff --git a/src/engine/Session.h b/src/engine/Session.h index c336a20754..2f7107b5fc 100644 --- a/src/engine/Session.h +++ b/src/engine/Session.h @@ -106,6 +106,15 @@ void xaccSessionDestroy (Session *); * save the account group to a file. Thus, this method acts as an "abort" or * "rollback" primitive. * + * The xaccResolveFilePath() 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. + * * EXAMPLE USAGE: * To read, modify and save an existing file: * ------------------------------------------ @@ -150,6 +159,7 @@ char * xaccSessionGetFilePath (Session *); void xaccSessionSave (Session *); void xaccSessionEnd (Session *); +char * xaccResolveFilePath (const char * filefrag); #endif /* __XACC_SESSION_H__ */ /* ==================== END OF FILE ================== */