From 8af699ce9198a2fc027a466e894f65f348b6e611 Mon Sep 17 00:00:00 2001 From: gambas Date: Sun, 2 Aug 2020 21:14:15 +0200 Subject: [PATCH] Correctly handle "~" in database directories. [GB.DB.SQLITE2] * BUG: Correctly handle "~" in database directories. [GB.DB.SQLITE3] * BUG: Correctly handle "~" in database directories. --- gb.db.sqlite2/src/main.cpp | 56 +++++++++++++++--------------------- gb.db.sqlite3/src/main.c | 27 ++++++++--------- main/lib/db/gb.db/.component | 2 +- main/lib/db/gb.db/.project | 2 +- main/lib/db/sqlite.c | 47 ++++++++++++++---------------- 5 files changed, 59 insertions(+), 75 deletions(-) diff --git a/gb.db.sqlite2/src/main.cpp b/gb.db.sqlite2/src/main.cpp index 0ffa7ad52..883782125 100644 --- a/gb.db.sqlite2/src/main.cpp +++ b/gb.db.sqlite2/src/main.cpp @@ -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; } diff --git a/gb.db.sqlite3/src/main.c b/gb.db.sqlite3/src/main.c index a4b4bfb6a..19de4d968 100644 --- a/gb.db.sqlite3/src/main.c +++ b/gb.db.sqlite3/src/main.c @@ -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; } diff --git a/main/lib/db/gb.db/.component b/main/lib/db/gb.db/.component index fc97eac07..449008729 100644 --- a/main/lib/db/gb.db/.component +++ b/main/lib/db/gb.db/.component @@ -1,3 +1,3 @@ [Component] Key=gb.db -Version=3.14.90 +Version=3.15.90 diff --git a/main/lib/db/gb.db/.project b/main/lib/db/gb.db/.project index e68bc285d..8255f39c7 100644 --- a/main/lib/db/gb.db/.project +++ b/main/lib/db/gb.db/.project @@ -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" diff --git a/main/lib/db/sqlite.c b/main/lib/db/sqlite.c index b218f9fb2..ffbcd9c45 100644 --- a/main/lib/db/sqlite.c +++ b/main/lib/db/sqlite.c @@ -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