Commit cea177ef authored by johan's avatar johan

sqlite vfs moved from liblinphone to bctoolbox

parent 1c0922ac
......@@ -38,6 +38,7 @@ option(ENABLE_STATIC "Build static library." ON)
option(ENABLE_POLARSSL "Enable polarssl support" ON)
option(ENABLE_MBEDTLS "Enable mabedtls support" ON)
option(ENABLE_DECAF "Enable Elliptic Curve Cryptography support" ON)
option(ENABLE_SQLITE_VFS "Enable Sqlite3 Virtual File System support" ON)
option(ENABLE_STRICT "Pass strict flags to the compiler" ON)
option(ENABLE_TESTS_COMPONENT "Enable compilation of tests helper library" ON)
option(ENABLE_TESTS "Enable compilation of tests" ON)
......@@ -91,6 +92,11 @@ if(ENABLE_POLARSSL AND NOT MBEDTLS_FOUND)
endif()
endif()
if(ENABLE_SQLITE_VFS)
find_package(Sqlite3 REQUIRED)
endif()
if(HAVE_SSL_GET_DTLS_SRTP_PROTECTION_PROFILE)
message(STATUS "DTLS SRTP available")
set(HAVE_DTLS_SRTP 1)
......
############################################################################
# FindSqlite3.cmake
# Copyright (C) 2014 Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
############################################################################
#
# - Find the sqlite3 include file and library
#
# SQLITE3_FOUND - system has sqlite3
# SQLITE3_INCLUDE_DIRS - the sqlite3 include directory
# SQLITE3_LIBRARIES - The libraries needed to use sqlite3
if(APPLE AND NOT IOS)
set(SQLITE3_HINTS "/usr")
endif()
if(SQLITE3_HINTS)
set(SQLITE3_LIBRARIES_HINTS "${SQLITE3_HINTS}/lib")
endif()
find_path(SQLITE3_INCLUDE_DIRS
NAMES sqlite3.h
HINTS "${SQLITE3_HINTS}"
PATH_SUFFIXES include
)
if(SQLITE3_INCLUDE_DIRS)
set(HAVE_SQLITE3_H 1)
endif()
find_library(SQLITE3_LIBRARIES
NAMES sqlite3
HINTS "${SQLITE3_LIBRARIES_HINTS}"
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Sqlite3
DEFAULT_MSG
SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES HAVE_SQLITE3_H
)
mark_as_advanced(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES HAVE_SQLITE3_H)
......@@ -43,6 +43,11 @@ if(MBEDTLS_FOUND OR POLARSSL_FOUND)
list(APPEND HEADER_FILES crypto.h)
endif()
if (ENABLE_SQLITE_VFS)
list(APPEND HEADER_FILES sqlite3_vfs.h)
endif()
if(ENABLE_TESTS_COMPONENT)
list(APPEND HEADER_FILES tester.h)
endif()
......
/*
* Copyright (c) 2010-2019 Belledonne Communications SARL.
*
* This file is part of Liblinphone.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BCTBX_SQLITE3_VFS_H
#define BCTBX_SQLITE3_VFS_H
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <bctoolbox/vfs.h>
#include "sqlite3.h"
#define BCTBX_SQLITE3_VFS "sqlite3bctbx_vfs"
/*
** The maximum pathname length supported by this VFS.
*/
#define MAXPATHNAME 512
#ifdef __cplusplus
extern "C"{
#endif
/**
* sqlite3_bctbx_file_t VFS file structure.
*/
typedef struct sqlite3_bctbx_file_t sqlite3_bctbx_file_t;
struct sqlite3_bctbx_file_t {
sqlite3_file base; /* Base class. Must be first. */
bctbx_vfs_file_t* pbctbx_file;
char *filename;
};
/**
* Very simple VFS structure based on sqlite3_vfs.
* Only the Open function is implemented,
*/
typedef struct sqlite3_bctbx_vfs_t sqlite3_bctbx_vfs_t;
struct sqlite3_bctbx_vfs_t {
sqlite3_bctbx_vfs_t *pNext; /* Next registered VFS */
const char *vfsName; /* Virtual file system name */
int (*xOpen)(sqlite3_vfs* pVfs, const char *fName, sqlite3_file *pFile,int flags, int *pOutFlags);
};
/****************************************************
VFS API to register this VFS to sqlite3 VFS
*****************************************************/
/**
* Returns a sqlite3_vfs pointer to the VFS named sqlite3bctbx_vfs
* implemented in this file.
* Methods not implemented:
* xDelete
* xAccess
* xFullPathname
* xDlOpen
* xDlError
* xDlSym
* xDlClose
* xRandomness
* xSleep
* xCurrentTime , xCurrentTimeInt64,
* xGetLastError
* xGetSystemCall
* xSetSystemCall
* xNextSystemCall
* To make the VFS available to SQLite
* @return Pointer to bctbx_vfs.
*/
BCTBX_PUBLIC sqlite3_vfs *sqlite3_bctbx_vfs_create(void);
/**
* Registers sqlite3bctbx_vfs to SQLite VFS. If makeDefault is 1,
* the VFS will be used by default.
* Methods not implemented by sqlite3_bctbx_vfs_t are initialized to the one
* used by the unix-none VFS where all locking file operations are no-ops.
* @param makeDefault set to 1 to make the newly registered VFS be the default one, set to 0 instead.
*/
BCTBX_PUBLIC void sqlite3_bctbx_vfs_register(int makeDefault);
/**
* Unregisters sqlite3bctbx_vfs from SQLite.
*/
BCTBX_PUBLIC void sqlite3_bctbx_vfs_unregister(void);
#ifdef __cplusplus
}
#endif
#endif /* BCTBX_SQLITE3_VFS_H */
......@@ -45,6 +45,10 @@ if(APPLE)
)
endif()
if (ENABLE_SQLITE_VFS)
list(APPEND BCTOOLBOX_C_SOURCE_FILES sqlite3_vfs.c)
endif()
if(ANDROID)
list(APPEND BCTOOLBOX_CXX_SOURCE_FILES conversion/charconv_android.cc)
elseif(WIN32)
......@@ -242,6 +246,17 @@ if(DECAF_FOUND)
target_link_libraries(bctoolbox PRIVATE ${DECAF_TARGETNAME})
endif()
endif()
if(SQLITE3_FOUND)
if(ENABLE_STATIC)
target_include_directories(bctoolbox-static PRIVATE ${SQLITE3_INCLUDE_DIRS})
target_link_libraries(bctoolbox-static PUBLIC ${SQLITE3_LIBRARIES})
endif()
if(ENABLE_SHARED)
target_include_directories(bctoolbox PRIVATE ${SQLITE3_INCLUDE_DIRS})
target_link_libraries(bctoolbox PRIVATE ${SQLITE3_LIBRARIES})
endif()
endif()
if(ENABLE_TESTS_COMPONENT)
if(ENABLE_STATIC)
......
This diff is collapsed.
......@@ -40,6 +40,10 @@ if(ENABLE_TESTS AND NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
if(APPLE)
list(APPEND TESTER_SOURCES ios_utils.cc)
endif()
if(SQLITE3_FOUND)
list(APPEND TESTER_SOURCES sqlite3_vfs.c)
endif()
string(REPLACE ";" " " LINK_FLAGS_STR "${LINK_FLAGS}")
......@@ -59,6 +63,9 @@ if(ENABLE_TESTS AND NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
if(DECAF_FOUND)
target_link_libraries(bctoolbox_tester_exe PRIVATE ${DECAF_TARGETNAME})
endif()
if(SQLITE3_FOUND)
target_link_libraries(bctoolbox_tester_exe PRIVATE ${SQLITE3_LIBRARIES})
endif()
set_target_properties(bctoolbox_tester_exe PROPERTIES XCODE_ATTRIBUTE_WARNING_CFLAGS "")
add_test(NAME bctoolbox_tester COMMAND bctoolbox_tester --verbose)
endif()
......@@ -42,7 +42,7 @@ static void log_handler(int lev, const char *fmt, va_list args) {
void bctoolbox_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
bc_tester_init(log_handler,BCTBX_LOG_ERROR, 0,NULL);
bc_tester_init(log_handler,BCTBX_LOG_MESSAGE, 0,NULL);
bc_tester_add_suite(&containers_test_suite);
bc_tester_add_suite(&utils_test_suite);
#if (HAVE_MBEDTLS | HAVE_POLARSSL)
......@@ -50,8 +50,9 @@ void bctoolbox_tester_init(void(*ftester_printf)(int level, const char *fmt, va_
#endif
bc_tester_add_suite(&parser_test_suite);
#ifdef __APPLE__
bc_tester_add_suite(&ios_utils_test_suite);
bc_tester_add_suite(&ios_utils_test_suite);
#endif
bc_tester_add_suite(&sqlite3_vfs_test_suite);
}
void bctoolbox_tester_uninit(void) {
......
......@@ -38,6 +38,7 @@ extern test_suite_t utils_test_suite;
extern test_suite_t crypto_test_suite;
extern test_suite_t parser_test_suite;
extern test_suite_t ios_utils_test_suite;
extern test_suite_t sqlite3_vfs_test_suite;
#ifdef __cplusplus
};
......
/*
* Copyright (c) 2020 Belledonne Communications SARL.
*
* This file is part of bctoolbox.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "bctoolbox_tester.h"
#include "bctoolbox/sqlite3_vfs.h"
#include "sqlite3.h"
static int bctbx_sqlite3_open(const char *db_file, sqlite3 **db) {
char* errmsg = NULL;
int ret;
int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
#if TARGET_OS_IPHONE
/* the secured filesystem of the iPHone doesn't allow writing while the app is in background mode, which is problematic.
* We workaround by asking that the open is made with no protection*/
flags |= SQLITE_OPEN_FILEPROTECTION_NONE;
#endif
ret = sqlite3_open_v2(db_file, db, flags, NULL);
if (ret != SQLITE_OK) return ret;
// Some platforms do not provide a way to create temporary files which are needed
// for transactions... so we work in memory only
// see http ://www.sqlite.org/compile.html#temp_store
ret = sqlite3_exec(*db, "PRAGMA temp_store=MEMORY", NULL, NULL, &errmsg);
if (ret != SQLITE_OK) {
sqlite3_free(errmsg);
}
return ret;
}
void basic_test() {
/* register sqlite3_vfs as default */
sqlite3_bctbx_vfs_register(1);
/* create a database */
sqlite3 *db=NULL;
char *dbFile = bc_tester_file("sqlite3_vfs_basic.sqlite");;
remove(dbFile);
bctbx_sqlite3_open(dbFile, &db);
/* insert a table */
char* errmsg=NULL;
int ret=sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS table1 ("
"id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"a_blob BLOB NOT NULL DEFAULT '000000000000',"
"some_text TEXT NOT NULL DEFAULT 'unset',"
"an_int INTEGER DEFAULT 0"
");",
0,0,&errmsg);
if(ret != SQLITE_OK) {
sqlite3_free(errmsg);
BC_FAIL("Unable to create table1");
return;
}
/* close databse */
sqlite3_close(db);
/* cleaning */
remove(dbFile);
}
static test_t sqlite3_vfs_tests[] = {
TEST_NO_TAG("basic access", basic_test)
};
test_suite_t sqlite3_vfs_test_suite = {"Sqlite3_vfs", NULL, NULL, NULL, NULL,
sizeof(sqlite3_vfs_tests) / sizeof(sqlite3_vfs_tests[0]), sqlite3_vfs_tests};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment