Correctly handle "~" in database directories.

[GB.DB.SQLITE2]
* BUG: Correctly handle "~" in database directories.

[GB.DB.SQLITE3]
* BUG: Correctly handle "~" in database directories.
This commit is contained in:
gambas 2020-08-02 21:14:15 +02:00
parent 577fe110ba
commit 8af699ce91
5 changed files with 59 additions and 75 deletions

View file

@ -324,7 +324,7 @@ static int do_query(DB_DATABASE *db, const char *error, Dataset **pres,
}
/* Internal function to check whether a file is a sqlite database file */
static bool IsDatabaseFile (const char *filename)
static bool is_database_file(const char *filename)
{
/* SQLite databases start with the string:
* ** This file contains an SQLite 2.1 database **
@ -353,57 +353,54 @@ static bool IsDatabaseFile (const char *filename)
/* Internal function to locate database and return full qualified */
/* path. */
static char *FindDatabase (const char *name, const char *hostName)
static char *find_database(const char *name, const char *hostName)
{
char *dbhome = NULL;
char *fullpath = NULL;
char *path;
/* Does Name includes fullpath */
if (*name == '/')
{
if (IsDatabaseFile(name))
fullpath = GB.NewZeroString(name);
return fullpath;
if (is_database_file(name))
return (char *)name;
}
/* Hostname contains home area */
fullpath = GB.NewZeroString(hostName);
fullpath = GB.AddChar(fullpath, '/');
fullpath = GB.AddString(fullpath, name, 0);
if (IsDatabaseFile(fullpath))
return fullpath;
path = GB.FileName(fullpath, GB.StringLength(fullpath));
GB.FreeString(&fullpath);
if (is_database_file(path))
return path;
/* Check the GAMBAS_SQLITE_DBHOME setting */
dbhome = getenv("GAMBAS_SQLITE_DBHOME");
if (dbhome)
if (dbhome != NULL)
{
fullpath = GB.NewZeroString(dbhome);
fullpath = GB.AddChar(fullpath, '/');
fullpath = GB.AddString(fullpath, name, 0);
if (IsDatabaseFile(fullpath))
return fullpath;
}
path = GB.FileName(fullpath, GB.StringLength(fullpath));
GB.FreeString(&fullpath);
#if 0
/* Now check for database in current working directory */
if (getcwd(cwd, MAX_PATH) == NULL){
GB.Error("Unable to get databases: &1", "Can't find current directory");
return NULL;
if (is_database_file(path))
return path;
}
#endif
fullpath = GB.NewZeroString(GB.TempDir());
fullpath = GB.AddString(fullpath, "/sqlite/", 0);
fullpath = GB.AddString(fullpath, name, 0);
GB.FreeStringLater(fullpath);
if (IsDatabaseFile(fullpath))
if (is_database_file(fullpath))
return fullpath;
GB.FreeString(&fullpath);
return NULL;
}
@ -473,7 +470,7 @@ static int WalkDirectory(const char *dir, char ***databases)
if (S_ISREG(statbuf.st_mode))
{
if (IsDatabaseFile(entry->d_name))
if (is_database_file(entry->d_name))
*(char **)GB.Add(databases) = GB.NewZeroString(entry->d_name);
}
}
@ -554,7 +551,7 @@ static int open_database(DB_DESC *desc, DB_DATABASE *db)
{
conn->setDatabase(name);
}
else if ((db_fullpath = FindDatabase(name, conn->getHostName())) != NULL)
else if ((db_fullpath = find_database(name, conn->getHostName())) != NULL)
{
conn->setDatabase(db_fullpath);
}
@ -567,7 +564,6 @@ static int open_database(DB_DESC *desc, DB_DATABASE *db)
}
GB.FreeString(&name);
GB.FreeString(&db_fullpath);
if ( conn->connect() != DB_CONNECTION_OK)
{
@ -1969,13 +1965,10 @@ static int database_exist(DB_DATABASE *db, const char *name)
if (strcmp(name,":memory:") == 0)
return TRUE; //Database is loaded in memory only
if((fullpath = FindDatabase(name, conn->getHostName())) != NULL){
GB.FreeString(&fullpath);
if ((fullpath = find_database(name, conn->getHostName())) != NULL)
return TRUE;
}
GB.FreeString(&fullpath);
return FALSE;
else
return FALSE;
}
/*****************************************************************************
@ -2065,19 +2058,16 @@ static int database_delete(DB_DATABASE *db, const char *name)
char *fullpath = NULL;
SqliteDatabase *conn = (SqliteDatabase *)db->handle;
if((fullpath = FindDatabase(name, conn->getHostName())) == NULL){
GB.FreeString(&fullpath);
if((fullpath = find_database(name, conn->getHostName())) == NULL){
GB.Error("Cannot Find database: &1", name);
return TRUE;
}
if( remove(fullpath) != 0){
GB.Error("Unable to delete database &1", fullpath);
GB.FreeString(&fullpath);
return TRUE;
}
GB.FreeString(&fullpath);
return FALSE;
}

View file

@ -371,25 +371,25 @@ static char *find_database(const char *name, const char *hostName)
{
char *dbhome = NULL;
char *fullpath = NULL;
char *path;
/* Does Name includes fullpath */
if (*name == '/')
{
if (is_database_file(name))
fullpath = GB.NewZeroString(name);
return fullpath;
return (char *)name;
}
/* Hostname contains home area */
fullpath = GB.NewZeroString(hostName);
fullpath = GB.AddChar(fullpath, '/');
fullpath = GB.AddString(fullpath, name, 0);
if (is_database_file(fullpath))
{
return fullpath;
}
path = GB.FileName(fullpath, GB.StringLength(fullpath));
GB.FreeString(&fullpath);
if (is_database_file(path))
return path;
/* Check the GAMBAS_SQLITE_DBHOME setting */
dbhome = getenv("GAMBAS_SQLITE_DBHOME");
@ -400,20 +400,21 @@ static char *find_database(const char *name, const char *hostName)
fullpath = GB.AddChar(fullpath, '/');
fullpath = GB.AddString(fullpath, name, 0);
if (is_database_file(fullpath))
return fullpath;
path = GB.FileName(fullpath, GB.StringLength(fullpath));
GB.FreeString(&fullpath);
if (is_database_file(path))
return path;
}
fullpath = GB.NewZeroString(GB.TempDir());
fullpath = GB.AddString(fullpath, "/sqlite/", 0);
fullpath = GB.AddString(fullpath, name, 0);
GB.FreeStringLater(fullpath);
if (is_database_file(fullpath))
return fullpath;
GB.FreeString(&fullpath);
return NULL;
}
@ -582,13 +583,11 @@ static int open_database(DB_DESC *desc, DB_DATABASE *db)
if (is_sqlite2_database(path))
{
DB.TryAnother("sqlite2");
GB.FreeString(&path);
return TRUE;
}
}
conn = sqlite_open_database(path, host);
GB.FreeString(&path);
if (!conn)
{
@ -2175,7 +2174,6 @@ static int database_exist(DB_DATABASE *db, const char *name)
fullpath = find_database(name, conn->host);
exist = fullpath != NULL;
GB.FreeString(&fullpath);
return exist;
}
@ -2292,7 +2290,6 @@ static int database_delete(DB_DATABASE * db, const char *name)
err = FALSE;
}
GB.FreeString(&fullpath);
return err;
}

View file

@ -1,3 +1,3 @@
[Component]
Key=gb.db
Version=3.14.90
Version=3.15.90

View file

@ -1,7 +1,7 @@
# Gambas Project File 3.0
Title=gb.db
Startup=Main
Version=3.14.90
Version=3.15.90
VersionFile=1
Component=gb.db
Environment="GB_PCODE_VERSION=3.8"

View file

@ -38,7 +38,7 @@
/* Internal function to check whether a file is a sqlite database file */
static bool is_sqlite2_database(char *filename)
static bool is_sqlite2_database(const char *filename)
{
FILE* fp;
bool res;
@ -62,7 +62,7 @@ static bool is_sqlite2_database(char *filename)
return TRUE;
}
static bool is_sqlite3_database(char *filename)
static bool is_sqlite3_database(const char *filename)
{
FILE *fp;
bool res;
@ -86,34 +86,34 @@ static bool is_sqlite3_database(char *filename)
return TRUE;
}
static bool IsDatabaseFile(char *filename)
static bool is_database_file(const char *filename)
{
return is_sqlite3_database(filename) || is_sqlite2_database(filename);
}
static char *FindDatabase(char *name, char *hostName)
static char *find_database(const char *name, const char *hostName)
{
char *dbhome = NULL;
char *fullpath = NULL;
char *path;
/* Does Name includes fullpath */
if (strcmp(basename(name), name))
if (*name == '/')
{
if (IsDatabaseFile(name))
fullpath = GB.NewZeroString(name);
return fullpath;
if (is_database_file(name))
return (char *)name;
}
/* Hostname contains home area */
fullpath = GB.NewZeroString(hostName);
fullpath = GB.AddChar(fullpath, '/');
fullpath = GB.AddString(fullpath, name, 0);
if (IsDatabaseFile(fullpath))
{
return fullpath;
}
path = GB.FileName(fullpath, GB.StringLength(fullpath));
GB.FreeString(&fullpath);
if (is_database_file(path))
return path;
/* Check the GAMBAS_SQLITE_DBHOME setting */
dbhome = getenv("GAMBAS_SQLITE_DBHOME");
@ -124,22 +124,21 @@ static char *FindDatabase(char *name, char *hostName)
fullpath = GB.AddChar(fullpath, '/');
fullpath = GB.AddString(fullpath, name, 0);
if (IsDatabaseFile(fullpath))
{
return fullpath;
}
path = GB.FileName(fullpath, GB.StringLength(fullpath));
GB.FreeString(&fullpath);
if (is_database_file(path))
return path;
}
fullpath = GB.NewZeroString(GB.TempDir());
fullpath = GB.AddString(fullpath, "/sqlite/", 0);
fullpath = GB.AddString(fullpath, name, 0);
GB.FreeStringLater(fullpath);
if (IsDatabaseFile(fullpath))
{
if (is_database_file(fullpath))
return fullpath;
}
GB.FreeString(&fullpath);
return NULL;
}
@ -148,7 +147,7 @@ static char *FindDatabase(char *name, char *hostName)
static int open_database(DB_DESC *desc, DB_DATABASE * db)
{
char *host;
char *db_fullpath = NULL;
const char *db_fullpath = NULL;
bool ver2 = FALSE;
if (!desc->name) // memory database
@ -158,7 +157,7 @@ static int open_database(DB_DESC *desc, DB_DATABASE * db)
if (!host)
host = "";
db_fullpath = FindDatabase(desc->name, host);
db_fullpath = find_database(desc->name, host);
if (!db_fullpath)
{
GB.Error("Unable to locate database `&1` in `&2`", desc->name, host);
@ -167,8 +166,6 @@ static int open_database(DB_DESC *desc, DB_DATABASE * db)
ver2 = is_sqlite2_database(db_fullpath);
GB.FreeString(&db_fullpath);
if (ver2)
goto __SQLITE2;
else