Open
fix(sqlite): fix unopened batch leaks, deleteDatabase hang/residual files and concurrency UAF #10
阿宝哥Add on 7 days ago
fix(sqlite): fix unopened batch leaks, deleteDatabase hang/residual files and concurrency UAF #10
Open
Total: 7 files changed+1021-501
| @@ -38,7 +38,6 @@ | |||
| 38 | <CMakeLists target="src/main/cpp/CMakeLists.txt" modules-name="cordova"> | 38 | <CMakeLists target="src/main/cpp/CMakeLists.txt" modules-name="cordova"> |
| 39 | <param target="add_library" value="sqlc/sqlite3.c"/> | 39 | <param target="add_library" value="sqlc/sqlite3.c"/> |
| 40 | <param target="add_library" value="sqlc/SQLiteConnectorDatabase.cpp"/> | 40 | <param target="add_library" value="sqlc/SQLiteConnectorDatabase.cpp"/> |
| 41 | - <param target="add_library" value="sqlc/SQLiteHarmonyDatabase.cpp"/> | ||
| 42 | <param target="add_library" value="sqlc/SQLitePlugin.cpp"/> | 41 | <param target="add_library" value="sqlc/SQLitePlugin.cpp"/> |
| 43 | </CMakeLists> | 42 | </CMakeLists> |
| 44 | 43 | ||
| @@ -46,8 +45,6 @@ | |||
| 46 | <source-file type="cpp" src="src/main/cpp/sqlc/sqlite3.c" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> | 45 | <source-file type="cpp" src="src/main/cpp/sqlc/sqlite3.c" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> |
| 47 | <source-file type="h" src="src/main/cpp/sqlc/SQLiteConnectorDatabase.h" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> | 46 | <source-file type="h" src="src/main/cpp/sqlc/SQLiteConnectorDatabase.h" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> |
| 48 | <source-file type="cpp" src="src/main/cpp/sqlc/SQLiteConnectorDatabase.cpp" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> | 47 | <source-file type="cpp" src="src/main/cpp/sqlc/SQLiteConnectorDatabase.cpp" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> |
| 49 | - <source-file type="h" src="src/main/cpp/sqlc/SQLiteHarmonyDatabase.h" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> | ||
| 50 | - <source-file type="cpp" src="src/main/cpp/sqlc/SQLiteHarmonyDatabase.cpp" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> | ||
| 51 | <source-file type="h" src="src/main/cpp/sqlc/SQLitePlugin.h" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> | 48 | <source-file type="h" src="src/main/cpp/sqlc/SQLitePlugin.h" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> |
| 52 | <source-file type="cpp" src="src/main/cpp/sqlc/SQLitePlugin.cpp" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> | 49 | <source-file type="cpp" src="src/main/cpp/sqlc/SQLitePlugin.cpp" target-dir="src/main/cpp/sqlc" modules-name="cordova"/> |
| 53 | 50 | ||
| @@ -15,57 +15,250 @@ | |||
| 15 | */ | 15 | */ |
| 16 | 16 | ||
| 17 | #include "SQLiteConnectorDatabase.h" | 17 | #include "SQLiteConnectorDatabase.h" |
| 18 | -#include <bundle/native_interface_bundle.h> | 18 | + |
| 19 | #include "Application.h" | 19 | #include "Application.h" |
| 20 | -#include "hilog/log.h" | 20 | +#include "PluginResult.h" |
| 21 | #include "cJSON.h" | 21 | #include "cJSON.h" |
| 22 | +#include "hilog/log.h" | ||
| 23 | +#include <array> | ||
| 24 | +#include <cerrno> | ||
| 25 | +#include <cmath> | ||
| 26 | +#include <cstdlib> | ||
| 27 | +#include <limits> | ||
| 28 | +#include <sys/stat.h> | ||
| 29 | +#include <unistd.h> | ||
| 22 | 30 | ||
| 23 | -const unsigned int SQliteConnectorDatabase::LOG_PRINT_DOMAIN = 0xFF00; | 31 | +namespace { |
| 32 | +constexpr unsigned int LOG_PRINT_DOMAIN = 0xFF00U; | ||
| 33 | +constexpr const char* DEFAULT_OHOS_DB_DIR = "/data/storage/el2/database"; | ||
| 34 | +constexpr const char* MEMORY_DB_NAME = ":memory:"; | ||
| 35 | +constexpr size_t MAX_DB_NAME_LEN = 255U; | ||
| 36 | +constexpr unsigned char MIN_PRINTABLE_CHAR = 0x20U; | ||
| 37 | +constexpr double MAX_SAFE_JS_INT = 9007199254740991.0; | ||
| 38 | +constexpr std::array<const char*, 3> DB_SIDECAR_SUFFIXES = {"-wal", "-shm", "-journal"}; | ||
| 24 | 39 | ||
| 25 | -bool SQliteConnectorDatabase::open(std::string fileName) | 40 | +bool HasJsonType(const cJSON* node, unsigned int expectedType) |
| 26 | { | 41 | { |
| 27 | - m_fileName = fileName; | 42 | + if (node == nullptr) { |
| 28 | - OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo(); | 43 | + return false; |
| 29 | - // 创建OH_Rdb_Config对象 | 44 | + } |
| 30 | - OH_Rdb_Config config; | 45 | + const unsigned int masked = static_cast<unsigned int>(node->type) & 0xFFU; |
| 31 | - // 该路径为应用沙箱路径,直接指定 | 46 | + return masked == expectedType; |
| 32 | - config.dataBaseDir = "/data/storage/el2/database"; // Application::g_databaseDir.c_str(); | 47 | +} |
| 33 | - // 数据库文件名 | ||
| 34 | - config.storeName = fileName.c_str(); | ||
| 35 | - // 应用包名 | ||
| 36 | - config.bundleName = application.bundleName; | ||
| 37 | - // 应用模块名 | ||
| 38 | - config.moduleName = application.bundleName; | ||
| 39 | - // 数据库文件安全等级 | ||
| 40 | - config.securityLevel = OH_Rdb_SecurityLevel::S1; | ||
| 41 | - // 数据库是否加密 | ||
| 42 | - config.isEncrypt = false; | ||
| 43 | - // config所占内存大小 | ||
| 44 | - config.selfSize = sizeof(OH_Rdb_Config); | ||
| 45 | - // 数据库文件存放的安全区域 | ||
| 46 | - config.area = RDB_SECURITY_AREA_EL1; | ||
| 47 | 48 | ||
| 48 | - int errCode = 0; | 49 | +const char* SafeSqliteErrorMsg(sqlite3* db) |
| 49 | - // 获取获取OH_Rdb_Store实例 | 50 | +{ |
| 50 | - std::string path = config.dataBaseDir; | 51 | + const char* rawMsg = (db != nullptr) ? sqlite3_errmsg(db) : nullptr; |
| 51 | - path += "/" + fileName; | 52 | + return (rawMsg != nullptr) ? rawMsg : "unknown sqlite error"; |
| 52 | - int rc = sqlite3_open(path.c_str(), &m_db); | 53 | +} |
| 53 | - if (rc != SQLITE_OK) { | 54 | + |
| 54 | - OH_LOG_Print( | 55 | +void onSqliteRowUpdate(void* userData, // NOLINT |
| 55 | - LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "SQliteConnectorDatabase sqlite3_open return %d", rc); | 56 | + int operation, |
| 57 | + const char* dbName, // NOLINT(bugprone-easily-swappable-parameters) | ||
| 58 | + const char* tableName, | ||
| 59 | + sqlite3_int64 rowId) | ||
| 60 | +{ | ||
| 61 | + (void)dbName; | ||
| 62 | + (void)tableName; | ||
| 63 | + if (userData == nullptr || operation != SQLITE_INSERT) { | ||
| 64 | + return; | ||
| 65 | + } | ||
| 66 | + auto* tracker = static_cast<SQliteConnectorDatabase::InsertRowTracker*>(userData); | ||
| 67 | + tracker->inserted = true; | ||
| 68 | + tracker->lastRowId = rowId; | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +// The bound cJSON string remains alive and immutable until sqlite3_finalize completes. | ||
| 72 | +int BindBorrowedUtf8(sqlite3_stmt* stmt, int paramIndex, const char* text) | ||
| 73 | +{ | ||
| 74 | + if (text == nullptr) { | ||
| 75 | + return sqlite3_bind_null(stmt, paramIndex); | ||
| 76 | + } | ||
| 77 | + const size_t rawLen = std::char_traits<char>::length(text); | ||
| 78 | + if (rawLen > static_cast<size_t>(std::numeric_limits<int>::max())) { | ||
| 79 | + return SQLITE_TOOBIG; | ||
| 80 | + } | ||
| 81 | + return sqlite3_bind_text(stmt, paramIndex, text, static_cast<int>(rawLen), SQLITE_STATIC); | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +int BindNumberParam(sqlite3_stmt* stmt, int paramIndex, double value) | ||
| 85 | +{ | ||
| 86 | + if (std::isfinite(value) && std::trunc(value) == value && | ||
| 87 | + !(value == 0.0 && std::signbit(value)) && | ||
| 88 | + value >= -MAX_SAFE_JS_INT && value <= MAX_SAFE_JS_INT) { | ||
| 89 | + return sqlite3_bind_int64(stmt, paramIndex, static_cast<sqlite3_int64>(value)); | ||
| 90 | + } | ||
| 91 | + return sqlite3_bind_double(stmt, paramIndex, value); | ||
| 92 | +} | ||
| 93 | +} // namespace | ||
| 94 | + | ||
| 95 | +SQliteConnectorDatabase::~SQliteConnectorDatabase() | ||
| 96 | +{ | ||
| 97 | + closeDatabaseNow(); | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +bool SQliteConnectorDatabase::isValidDatabaseName(const std::string& fileName) // NOLINT | ||
| @@ -19,37 +19,78 @@ | |||
| 19 | 19 | ||
| 20 | #include "CallbackContext.h" | 20 | #include "CallbackContext.h" |
| 21 | #include "cJSON.h" | 21 | #include "cJSON.h" |
| 22 | -#include <database/rdb/relational_store.h> | ||
| 23 | -#include "SQLiteHarmonyDatabase.h" | ||
| 24 | #include "sqlite3.h" | 22 | #include "sqlite3.h" |
| 23 | +#include <cstdint> | ||
| 24 | +#include <list> | ||
| 25 | +#include <string> | ||
| 26 | +#include <vector> | ||
| 25 | 27 | ||
| 26 | -class SQliteConnectorDatabase : public SQLiteHarmonyDatabase { | 28 | +class SQliteConnectorDatabase { |
| 27 | - static const unsigned int LOG_PRINT_DOMAIN; | ||
| 28 | - std::string m_fileName; | ||
| 29 | - OH_Rdb_Config m_config; | ||
| 30 | - sqlite3* m_db; | ||
| 31 | - | ||
| 32 | public: | 29 | public: |
| 33 | - SQliteConnectorDatabase() | 30 | + struct InsertRowTracker { |
| 34 | - { | 31 | + bool inserted{false}; |
| 35 | - m_db = nullptr; | 32 | + sqlite3_int64 lastRowId{0}; |
| 36 | - } | 33 | + }; |
| 37 | 34 | ||
| 38 | - ~SQliteConnectorDatabase(){}; | 35 | + SQliteConnectorDatabase() = default; |
| 36 | + ~SQliteConnectorDatabase(); | ||
| 39 | 37 | ||
| 40 | - sqlite3* getSqlite() | 38 | + SQliteConnectorDatabase(const SQliteConnectorDatabase&) = delete; |
| 39 | + SQliteConnectorDatabase& operator=(const SQliteConnectorDatabase&) = delete; | ||
| 40 | + SQliteConnectorDatabase(SQliteConnectorDatabase&&) = delete; | ||
| 41 | + SQliteConnectorDatabase& operator=(SQliteConnectorDatabase&&) = delete; | ||
| 42 | + | ||
| 43 | + sqlite3* getSqlite() const // NOLINT | ||
| 41 | { | 44 | { |
| 42 | return m_db; | 45 | return m_db; |
| 43 | } | 46 | } |
| 44 | 47 | ||
| 45 | - bool open(std::string fileName); | 48 | + static bool isValidDatabaseName(const std::string& fileName); // NOLINT |
| 46 | - void closeDatabaseNow(); | 49 | + static std::string resolveDatabaseDir(); // NOLINT |
| 47 | - void deleteDatbaseNow(); | 50 | + static std::string resolveDatabasePath(const std::string& fileName); // NOLINT |
| 48 | - void executeSqlBatch(const std::vector<std::string>& queryarr, | 51 | + static bool deleteDatabaseFiles(const std::string& fileName); // NOLINT |
| 49 | - const std::vector<cJSON*>& jsonParams, | 52 | + |
| 50 | - CallbackContext cbc); | 53 | + bool open(const std::string& fileName); // NOLINT |
| 51 | - cJSON* executeSQLiteStatement( | 54 | + void closeDatabaseNow(); // NOLINT |
| 52 | - const std::string& query, cJSON* paramsAsJson, CallbackContext cbc, int& rc, std::string& errMsg); | 55 | + void executeSqlBatch( // NOLINT |
| 53 | - static void freeBindMemory(void* pVoid); | 56 | + const std::vector<std::string>& queryarr, |
| 57 | + const std::vector<cJSON*>& jsonParams, | ||
| 58 | + CallbackContext cbc); | ||
| 59 | + cJSON* executeSQLiteStatement( // NOLINT | ||
| 60 | + const std::string& query, | ||
| 61 | + cJSON* paramsAsJson, | ||
| 62 | + const CallbackContext& cbc, | ||
| 63 | + int& rc, | ||
| 64 | + std::string& errMsg); | ||
| 65 | + | ||
| 66 | +private: | ||
| 67 | + std::string m_fileName; | ||
| 68 | + sqlite3* m_db{nullptr}; | ||
| 69 | + | ||
| 70 | + bool bindStatementParams( // NOLINT | ||
| 71 | + sqlite3_stmt* stmt, | ||
| 72 | + cJSON* paramsAsJson, | ||
| 73 | + std::list<std::string>& serializedBuffers, | ||
| 74 | + int& rc, | ||
| 75 | + std::string& errMsg); | ||
| 76 | + bool bindSingleParam( // NOLINT | ||
| 77 | + sqlite3_stmt* stmt, | ||
| 78 | + int paramIndex, | ||
| 79 | + cJSON* item, | ||
| 80 | + std::list<std::string>& serializedBuffers, | ||
| 81 | + int& rc, | ||
| 82 | + std::string& errMsg); | ||
| 83 | + bool collectStatementRows( // NOLINT | ||
| 84 | + sqlite3_stmt* stmt, int& rc, std::string& errMsg, cJSON*& outRowsArray); | ||
| 85 | + static cJSON* buildRowObject( // NOLINT | ||
| 86 | + sqlite3_stmt* stmt, int colCount); | ||
| 87 | + void appendSuccessItem( // NOLINT | ||
| 88 | + cJSON* batchResults, | ||
| 89 | + cJSON* queryResult, | ||
| 90 | + int64_t lastTotal, | ||
| 91 | + const InsertRowTracker& insertTracker); | ||
| 92 | + static void appendErrorItem( // NOLINT | ||
| 93 | + cJSON* batchResults, const std::string& errorMessage, int code); | ||
| 54 | }; | 94 | }; |
| 55 | -#endif | 95 | + |
| 96 | +#endif | ||
| @@ -1,58 +0,0 @@ | |||
| 1 | -/* | ||
| 2 | - * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>. | ||
| 3 | - * | ||
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | - * you may not use this file except in compliance with the License. | ||
| 6 | - * You may obtain a copy of the License at | ||
| 7 | - * | ||
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | - * | ||
| 10 | - * Unless required by applicable law or agreed to in writing, software | ||
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | - * See the License for the specific language governing permissions and | ||
| 14 | - * limitations under the License. | ||
| 15 | - */ | ||
| 16 | - | ||
| 17 | -#include "SQLiteHarmonyDatabase.h" | ||
| 18 | -#include "Application.h" | ||
| 19 | -#include "bundle/native_interface_bundle.h" | ||
| 20 | - | ||
| 21 | -void SQLiteHarmonyDatabase::open(std::string fileName) | ||
| 22 | -{ | ||
| 23 | - m_fileName = fileName; | ||
| 24 | - OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo(); | ||
| 25 | - // 创建OH_Rdb_Config对象 | ||
| 26 | - OH_Rdb_Config config; | ||
| 27 | - // 该路径为应用沙箱路径 | ||
| 28 | - config.dataBaseDir = Application::g_databaseDir.c_str(); | ||
| 29 | - // 数据库文件名 | ||
| 30 | - config.storeName = fileName.c_str(); | ||
| 31 | - // 应用包名 | ||
| 32 | - config.bundleName = application.bundleName; | ||
| 33 | - // 应用模块名 | ||
| 34 | - config.moduleName = application.bundleName; | ||
| 35 | - // 数据库文件安全等级 | ||
| 36 | - config.securityLevel = OH_Rdb_SecurityLevel::S1; | ||
| 37 | - // 数据库是否加密 | ||
| 38 | - config.isEncrypt = false; | ||
| 39 | - // config所占内存大小 | ||
| 40 | - config.selfSize = sizeof(OH_Rdb_Config); | ||
| 41 | - // 数据库文件存放的安全区域 | ||
| 42 | - config.area = RDB_SECURITY_AREA_EL1; | ||
| 43 | - | ||
| 44 | - int errCode = 0; | ||
| 45 | - // 获取获取OH_Rdb_Store实例 | ||
| 46 | - m_store = OH_Rdb_GetOrOpen(&config, &errCode); | ||
| 47 | -} | ||
| 48 | - | ||
| 49 | -void SQLiteHarmonyDatabase::closeDatabaseNow() | ||
| 50 | -{ | ||
| 51 | - OH_Rdb_CloseStore(m_store); | ||
| 52 | -} | ||
| 53 | - | ||
| 54 | -void SQLiteHarmonyDatabase::bugWorkaround() | ||
| 55 | -{ | ||
| 56 | - closeDatabaseNow(); | ||
| 57 | - open(m_fileName); | ||
| 58 | -} | ||
| @@ -1,37 +0,0 @@ | |||
| 1 | -/* | ||
| 2 | - * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>. | ||
| 3 | - * | ||
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | - * you may not use this file except in compliance with the License. | ||
| 6 | - * You may obtain a copy of the License at | ||
| 7 | - * | ||
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | - * | ||
| 10 | - * Unless required by applicable law or agreed to in writing, software | ||
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | - * See the License for the specific language governing permissions and | ||
| 14 | - * limitations under the License. | ||
| 15 | - */ | ||
| 16 | - | ||
| 17 | -#ifndef SQLITE_HARMONY_DATABASE_H | ||
| 18 | -#define SQLITE_HARMONY_DATABASE_H | ||
| 19 | - | ||
| 20 | -#include "CallbackContext.h" | ||
| 21 | -#include "cJSON.h" | ||
| 22 | -#include <database/rdb/relational_store.h> | ||
| 23 | - | ||
| 24 | -#include <regex> | ||
| 25 | -#include <string> | ||
| 26 | - | ||
| 27 | -class SQLiteHarmonyDatabase { | ||
| 28 | - std::string m_fileName; | ||
| 29 | - OH_Rdb_Store* m_store; | ||
| 30 | - OH_Rdb_Config m_config; | ||
| 31 | - | ||
| 32 | -public: | ||
| 33 | - void open(std::string fileName); | ||
| 34 | - void closeDatabaseNow(); | ||
| 35 | - void bugWorkaround(); | ||
| 36 | -}; | ||
| 37 | -#endif | ||
| @@ -15,149 +15,477 @@ | |||
| 15 | */ | 15 | */ |
| 16 | 16 | ||
| 17 | #include "SQLitePlugin.h" | 17 | #include "SQLitePlugin.h" |
| 18 | -#include "cJSON.h" | ||
| 19 | 18 | ||
| 19 | +#include "cJSON.h" | ||
| 20 | +#include "hilog/log.h" | ||
| 21 | +#include <system_error> | ||
| 22 | +#include <utility> | ||
| 23 | + | ||
| 24 | +namespace { | ||
| 25 | +constexpr unsigned int PLUGIN_LOG_DOMAIN = 0xFF00U; | ||
| 26 | +constexpr unsigned int JSON_TYPE_MASK = 0xFFU; | ||
| 27 | +constexpr int CJSON_DEEP_COPY = 1; | ||
| 28 | +constexpr const char* DATABASE_BUSY_MESSAGE = "Database is closing or being deleted"; | ||
| 29 | +} // namespace | ||
| 20 | 30 | ||
| 21 | REGISTER_PLUGIN_CLASS(SQLitePlugin) | 31 | REGISTER_PLUGIN_CLASS(SQLitePlugin) |
| 22 | 32 | ||
| 23 | -bool SQLitePlugin::execute(const std::string& actionAsString, cJSON* args, CallbackContext cbc) | 33 | +SQLitePlugin::DBRunner::DBRunner(std::string dbname) : m_dbName(std::move(dbname)) {} |
| 34 | + | ||
| 35 | +SQLitePlugin::DBRunner::~DBRunner() | ||
| 24 | { | 36 | { |
| 25 | - Action action; | 37 | + try { |
| 26 | - if (actionAsString == "echoStringValue") { | 38 | + shutdown(true); |
| 27 | - action = Action::echoStringValue; | 39 | + } catch (...) { |
| 40 | + OH_LOG_Print(LOG_APP, LOG_ERROR, PLUGIN_LOG_DOMAIN, "SQLitePlugin", | ||
| 41 | + "Unexpected exception in DBRunner destructor"); | ||
| 42 | + } | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +bool SQLitePlugin::DBRunner::start() // NOLINT | ||
| 46 | +{ | ||
| 47 | + std::lock_guard<std::mutex> lock(m_mutex); | ||
| 48 | + if (m_running) { | ||
| 49 | + return true; | ||
| 50 | + } | ||
| 51 | + m_stopping = false; | ||
| 52 | + try { | ||
| 53 | + m_worker = std::thread(&DBRunner::workerLoop, this); | ||
| 54 | + } catch (const std::system_error&) { | ||
| 55 | + OH_LOG_Print(LOG_APP, LOG_ERROR, PLUGIN_LOG_DOMAIN, "SQLitePlugin", | ||
| 56 | + "Failed to spawn database worker thread"); | ||
| 57 | + return false; | ||
| 58 | + } | ||
| 59 | + m_running = true; | ||
| 60 | + return true; | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +bool SQLitePlugin::DBRunner::addTask(std::unique_ptr<DBQuery>& query) // NOLINT | ||
| 64 | +{ | ||
| 65 | + if (!query) { | ||
| 66 | + return false; | ||
| 67 | + } | ||
| 68 | + { | ||
| 69 | + std::lock_guard<std::mutex> lock(m_mutex); | ||
| 70 | + if (!m_running || m_stopping) { | ||
| 71 | + return false; | ||
| 72 | + } | ||
| 73 | + m_queue.push(std::move(query)); | ||
| 74 | + } | ||
| 75 | + m_cv.notify_one(); | ||
| 76 | + return true; | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +void SQLitePlugin::DBRunner::processQuery(DBQuery& dbq) // NOLINT | ||
| 80 | +{ | ||
| 81 | + if (dbq.openOnly) { | ||
| 82 | + if (m_mydb.getSqlite() != nullptr || m_mydb.open(m_dbName)) { | ||
| 83 | + dbq.cbc.success(); | ||
| 84 | + } else { | ||
| 85 | + dbq.cbc.error("open database failed"); | ||
| 86 | + } | ||
| 87 | + return; | ||
| 28 | } | 88 | } |
| 29 | 89 | ||
| 90 | + if (m_mydb.getSqlite() == nullptr && !m_mydb.open(m_dbName)) { | ||
| 91 | + dbq.cbc.error("open database failed"); | ||
| 92 | + return; | ||
| 93 | + } | ||
| 94 | + m_mydb.executeSqlBatch(dbq.queries, dbq.jsonParams, dbq.cbc); | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +void SQLitePlugin::DBRunner::workerLoop() // NOLINT | ||
| 98 | +{ | ||
| 99 | + bool keepRunning = true; | ||
| 100 | + while (keepRunning) { | ||
| 101 | + std::unique_ptr<DBQuery> query; | ||
| 102 | + { | ||
| 103 | + std::unique_lock<std::mutex> lock(m_mutex); | ||
| 104 | + m_cv.wait(lock, [this]() { return !m_queue.empty() || m_stopping; }); | ||
| 105 | + if (m_queue.empty() && m_stopping) { | ||
| 106 | + keepRunning = false; | ||
| 107 | + } else { | ||
| 108 | + query = std::move(m_queue.front()); | ||
| 109 | + m_queue.pop(); | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + if (query) { | ||
| 113 | + processQuery(*query); | ||
| @@ -17,147 +17,138 @@ | |||
| 17 | #ifndef SQLITE_PLUGIN_H | 17 | #ifndef SQLITE_PLUGIN_H |
| 18 | #define SQLITE_PLUGIN_H | 18 | #define SQLITE_PLUGIN_H |
| 19 | 19 | ||
| 20 | -#include "Thread.h" | ||
| 21 | #include "CordovaPlugin.h" | 20 | #include "CordovaPlugin.h" |
| 22 | -#include "cJSON.h" | ||
| 23 | #include "SQLiteConnectorDatabase.h" | 21 | #include "SQLiteConnectorDatabase.h" |
| 22 | +#include "cJSON.h" | ||
| 23 | +#include <condition_variable> | ||
| 24 | #include <cstddef> | 24 | #include <cstddef> |
| 25 | #include <map> | 25 | #include <map> |
| 26 | +#include <memory> | ||
| 27 | +#include <mutex> | ||
| 28 | +#include <queue> | ||
| 29 | +#include <set> | ||
| 30 | +#include <string> | ||
| 31 | +#include <thread> | ||
| 32 | +#include <utility> | ||
| 33 | +#include <vector> | ||
| 26 | 34 | ||
| 27 | class SQLitePlugin : public CordovaPlugin { | 35 | class SQLitePlugin : public CordovaPlugin { |
| 28 | - enum Action { | 36 | +public: |
| 29 | - echoStringValue, | 37 | + enum class Action { |
| 30 | - open, | 38 | + UNKNOWN = 0, |
| 31 | - close, | 39 | + ECHO_STRING_VALUE, |
| 32 | - del, | 40 | + OPEN, |
| 33 | - executeSqlBatch, | 41 | + CLOSE, |
| 34 | - backgroundExecuteSqlBatch, | 42 | + DEL, |
| 43 | + EXECUTE_SQL_BATCH, | ||
| 44 | + BACKGROUND_EXECUTE_SQL_BATCH, | ||
| 35 | }; | 45 | }; |
| 36 | 46 | ||
| 47 | + SQLitePlugin() = default; | ||
| 48 | + ~SQLitePlugin(); | ||
| 49 | + | ||
| 50 | + SQLitePlugin(const SQLitePlugin&) = delete; | ||
| 51 | + SQLitePlugin& operator=(const SQLitePlugin&) = delete; | ||
| 52 | + SQLitePlugin(SQLitePlugin&&) = delete; | ||
| 53 | + SQLitePlugin& operator=(SQLitePlugin&&) = delete; | ||
| 54 | + | ||
| 55 | + bool execute( // NOLINT | ||
| 56 | + const std::string& actionAsString, cJSON* args, CallbackContext cbc) override; | ||
| 57 | + void onDestroy(const std::string& strWebTag) override; // NOLINT | ||
| 58 | + bool executeAndPossiblyThrow( // NOLINT | ||
| 59 | + Action action, cJSON* args, CallbackContext cbc); | ||
| 60 | + void startDatabase( // NOLINT | ||
| 61 | + const std::string& dbname, cJSON* options, CallbackContext cbc); | ||
| 62 | + void closeDatabase( // NOLINT | ||
| 63 | + const std::string& dbname, CallbackContext cbc); | ||
| 64 | + void deleteDatabase( // NOLINT | ||
| 65 | + const std::string& dbname, CallbackContext cbc); | ||
| 66 | + | ||
| 67 | +private: | ||
| 37 | struct DBQuery { | 68 | struct DBQuery { |
| 38 | - bool m_stop; | 69 | + bool openOnly{false}; |
| 39 | - bool m_close; | 70 | + std::vector<std::string> queries; |
| 40 | - bool m_del; | 71 | + std::vector<cJSON*> jsonParams; |
| 41 | - std::vector<std::string> m_queries; | 72 | + CallbackContext cbc; |
| 42 | - std::vector<cJSON*> m_jsonparams; | ||
| 43 | - CallbackContext m_cbc; | ||
| 44 | 73 | ||
| 45 | - DBQuery(std::vector<std::string> queries, std::vector<cJSON*> jsonparams, CallbackContext c) | 74 | + DBQuery(std::vector<std::string> queryList, |
| 75 | + std::vector<cJSON*> paramList, | ||
| 76 | + const CallbackContext& callbackCtx) | ||
| 77 | + : queries(std::move(queryList)), | ||
| 78 | + jsonParams(std::move(paramList)), | ||
| 79 | + cbc(callbackCtx) | ||
| 46 | { | 80 | { |
| 47 | - m_stop = false; | ||
| 48 | - m_close = false; | ||
| 49 | - m_del = false; | ||
| 50 | - m_queries = queries; | ||
| 51 | - m_jsonparams = jsonparams; | ||
| 52 | - m_cbc = c; | ||
| 53 | } | 81 | } |
| 54 | 82 | ||
| 55 | - DBQuery(bool del, CallbackContext cbc) | 83 | + explicit DBQuery(const CallbackContext& callbackCtx) |
| 84 | + : openOnly(true), cbc(callbackCtx) | ||
| 56 | { | 85 | { |
| 57 | - m_stop = false; | ||
| 58 | - m_close = false; | ||
| 59 | - m_del = del; | ||
| 60 | - m_cbc = cbc; | ||
| 61 | } | 86 | } |
| 62 | 87 | ||
| 63 | - DBQuery() | 88 | + ~DBQuery() noexcept |
| 64 | { | 89 | { |
| 65 | - m_stop = false; | 90 | + for (cJSON* param : jsonParams) { |
| 66 | - m_close = false; | 91 | + if (param != nullptr) { |
| 67 | - m_del = false; | 92 | + cJSON_Delete(param); |
| 68 | - } | 93 | + } |
| 69 | - | ||
| 70 | - ~DBQuery() | ||
| 71 | - { | ||
| 72 | - for (int i = 0; i < m_jsonparams.size(); i++) { | ||
| 73 | - cJSON_Delete(m_jsonparams[i]); | ||
| 74 | } | 94 | } |
| 75 | } | 95 | } |
| 76 | 96 | ||
| 77 | - DBQuery(const DBQuery& arg) | 97 | + DBQuery(const DBQuery&) = delete; |
| 78 | - { | 98 | + DBQuery& operator=(const DBQuery&) = delete; |
| 79 | - m_stop = arg.m_stop; | 99 | + DBQuery(DBQuery&&) = delete; |
| 80 | - m_close = arg.m_close; | 100 | + DBQuery& operator=(DBQuery&&) = delete; |
| 81 | - m_del = arg.m_del; | ||
| 82 | - m_queries = arg.m_queries; | ||
| 83 | - m_jsonparams = arg.m_jsonparams; | ||
| 84 | - m_cbc = arg.m_cbc; | ||
| 85 | - } | ||
| 86 | - | ||
| 87 | - DBQuery& operator=(const DBQuery& arg) | ||
| 88 | - { | ||
| 89 | - if (this == &arg) | ||
| 90 | - return *this; | ||
| 91 | - m_stop = arg.m_stop; | ||
| 92 | - m_close = arg.m_close; | ||
| 93 | - m_del = arg.m_del; | ||
| 94 | - m_queries = arg.m_queries; | ||
| 95 | - m_jsonparams = arg.m_jsonparams; | ||
| 96 | - m_cbc = arg.m_cbc; | ||
| 97 | - return *this; | ||
| 98 | - } | ||
| 99 | }; | 101 | }; |
| 100 | 102 | ||
| 101 | - class DBRunner : public CThreadPool { | 103 | + class DBRunner { |
| 102 | - std::string m_dbName; | ||
| 103 | - | ||
| 104 | public: | 104 | public: |
| 105 | + explicit DBRunner(std::string dbname); | ||
| 106 | + ~DBRunner(); | ||
| 107 | + | ||
| 108 | + DBRunner(const DBRunner&) = delete; | ||
| 109 | + DBRunner& operator=(const DBRunner&) = delete; | ||
| 110 | + DBRunner(DBRunner&&) = delete; | ||
| 111 | + DBRunner& operator=(DBRunner&&) = delete; | ||
| 112 | + | ||
| 113 | + bool start(); // NOLINT | ||
| 114 | + bool addTask(std::unique_ptr<DBQuery>& query); // NOLINT | ||
| 115 | + void shutdown(bool cancelPending); // NOLINT | ||
| 116 | + | ||
| 117 | + private: | ||
| 118 | + std::string m_dbName; | ||
| 119 | + std::mutex m_mutex; | ||
| 120 | + std::condition_variable m_cv; | ||
| 121 | + std::queue<std::unique_ptr<DBQuery>> m_queue; | ||
| 122 | + std::thread m_worker; | ||
| 123 | + bool m_running{false}; | ||
| 124 | + bool m_stopping{false}; | ||
| 105 | SQliteConnectorDatabase m_mydb; | 125 | SQliteConnectorDatabase m_mydb; |
| 106 | 126 | ||
| 107 | - explicit DBRunner(std::string dbname) | 127 | + void workerLoop(); // NOLINT |
| 108 | - { | 128 | + void processQuery(DBQuery& dbq); // NOLINT |
| 109 | - m_dbName = dbname; | ||
| 110 | - } | ||
| 111 | - | ||
| 112 | - void execute(const void* arg, const int nCountOfTask, void* pTmp = nullptr) override | ||
| 113 | - { | ||
| 114 | - DBQuery* dbq = (DBQuery*)arg; | ||
| 115 | - if (m_mydb.getSqlite() == nullptr) { | ||
| 116 | - if (!m_mydb.open(m_dbName)) { | ||
| 117 | - dbq->m_cbc.error("open database failed"); | ||
| 118 | - } | ||
| 119 | - } | ||
| 120 | - | ||
| 121 | - if (!dbq->m_stop) { | ||
| 122 | - m_mydb.executeSqlBatch(dbq->m_queries, dbq->m_jsonparams, dbq->m_cbc); | ||
| 123 | - } | ||
| 124 | - if (dbq->m_close) { | ||
| 125 | - m_mydb.closeDatabaseNow(); | ||
| 126 | - if (!dbq->m_del) { | ||
| 127 | - dbq->m_cbc.success(); | ||
| 128 | - } else { | ||
| 129 | - m_mydb.deleteDatbaseNow(); | ||
| 130 | - dbq->m_cbc.success(); | ||
| 131 | - } | ||
| 132 | - } | ||
| 133 | - delete dbq; | ||
| 134 | - } | ||
| 135 | - | ||
| 136 | - void* initThreadData() override | ||
| 137 | - { | ||
| 138 | - return nullptr; | ||
| 139 | - } | ||
| 140 | - | ||
| 141 | - void deInitThreadData(void* pArg) override {} | ||
| 142 | }; | 129 | }; |
| 143 | 130 | ||
| 144 | - std::map<std::string, DBRunner*>* m_dbrmap; | 131 | + std::mutex m_runnerMutex; |
| 132 | + std::map<std::string, std::shared_ptr<DBRunner>> m_dbrmap; | ||
| 133 | + std::set<std::string> m_finishingDatabases; | ||
| 134 | + bool m_closingAll{false}; | ||
| 145 | 135 | ||
| 146 | -public: | 136 | + static Action parseAction(const std::string& actionAsString); // NOLINT |
| 147 | - SQLitePlugin() | 137 | + static bool hasJsonType( // NOLINT |
| 148 | - { | 138 | + const cJSON* node, unsigned int expectedType); |
| 149 | - m_dbrmap = new std::map<std::string, DBRunner*>(); | 139 | + static cJSON* getFirstObjectArg(cJSON* args); // NOLINT |
| 150 | - } | 140 | + static bool getRequiredString( // NOLINT |
| 151 | - | 141 | + cJSON* obj, const char* key, std::string& outValue); |
| 152 | - ~SQLitePlugin() | 142 | + static void freeJsonVector(std::vector<cJSON*>& items); // NOLINT |
| 153 | - { | 143 | + static bool extractBatchQueries( // NOLINT |
| 154 | - delete m_dbrmap; | 144 | + cJSON* txargs, |
| 155 | - } | 145 | + std::vector<std::string>& queries, |
| 156 | - | 146 | + std::vector<cJSON*>& jsonParams, |
| 157 | - bool execute(const std::string& actionAsString, cJSON* args, CallbackContext cbc) override; | 147 | + std::string& errMsg); |
| 158 | - bool executeAndPossiblyThrow(Action action, cJSON* args, CallbackContext cbc); | 148 | + void handleBatchAction(cJSON* args, CallbackContext cbc); // NOLINT |
| 159 | - void startDatabase(std::string dbname, cJSON* options, CallbackContext cbc); | 149 | + void finishDatabase( // NOLINT |
| 160 | - void closeDatabase(std::string dbname, CallbackContext cbc); | 150 | + const std::string& dbname, bool removeFiles, CallbackContext cbc); |
| 161 | - void deleteDatabase(std::string dbname, CallbackContext cbc); | 151 | + void closeAllDatabases(); // NOLINT |
| 162 | }; | 152 | }; |
| 163 | -#endif | 153 | + |
| 154 | +#endif | ||