mirror of
https://github.com/cottongin/cardputerzero-sync.git
synced 2026-11-20 14:44:47 -06:00
fix(device-ui): fail closed on corrupt sqlite fallback and fix linux build portability
This commit is contained in:
@@ -10,6 +10,7 @@ option(CP0_UI_BUILD_APP "Build cp0-sync-ui LVGL executable" ON)
|
|||||||
option(CP0_UI_TARGET_DEVICE "Target CardputerZero Linux fbdev/evdev instead of SDL2 simulator" OFF)
|
option(CP0_UI_TARGET_DEVICE "Target CardputerZero Linux fbdev/evdev instead of SDL2 simulator" OFF)
|
||||||
|
|
||||||
find_package(SQLite3 REQUIRED)
|
find_package(SQLite3 REQUIRED)
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
add_library(cp0_ui_core STATIC
|
add_library(cp0_ui_core STATIC
|
||||||
src/theme.cpp
|
src/theme.cpp
|
||||||
@@ -34,7 +35,10 @@ if(CP0_UI_BUILD_TESTS)
|
|||||||
add_executable(test_ipc_and_theme
|
add_executable(test_ipc_and_theme
|
||||||
tests/test_ipc_and_theme.cpp
|
tests/test_ipc_and_theme.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(test_ipc_and_theme PRIVATE cp0_ui_core)
|
target_link_libraries(test_ipc_and_theme PRIVATE
|
||||||
|
cp0_ui_core
|
||||||
|
Threads::Threads
|
||||||
|
)
|
||||||
target_compile_options(test_ipc_and_theme PRIVATE -Wall -Wextra -Wpedantic)
|
target_compile_options(test_ipc_and_theme PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
add_test(NAME test_ipc_and_theme COMMAND test_ipc_and_theme)
|
add_test(NAME test_ipc_and_theme COMMAND test_ipc_and_theme)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -970,17 +971,31 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
|
|||||||
std::string db_path = resolved_db_path();
|
std::string db_path = resolved_db_path();
|
||||||
SqliteDbGuard db_guard;
|
SqliteDbGuard db_guard;
|
||||||
if (sqlite3_open_v2(db_path.c_str(), &db_guard.db, SQLITE_OPEN_READONLY, nullptr) != SQLITE_OK) {
|
if (sqlite3_open_v2(db_path.c_str(), &db_guard.db, SQLITE_OPEN_READONLY, nullptr) != SQLITE_OK) {
|
||||||
|
std::string err = db_guard.db != nullptr ? sqlite3_errmsg(db_guard.db) : "Failed to open SQLite DB";
|
||||||
status.ok = false;
|
status.ok = false;
|
||||||
status.error = db_guard.db != nullptr ? sqlite3_errmsg(db_guard.db) : "Failed to open SQLite DB";
|
status.error = err;
|
||||||
|
status.last_error = err;
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
sqlite3_busy_timeout(db_guard.db, 1000);
|
sqlite3_busy_timeout(db_guard.db, 1000);
|
||||||
|
|
||||||
|
auto fail_sqlite = [&](const char* fallback_msg) -> DaemonStatus {
|
||||||
|
const char* raw_err = db_guard.db != nullptr ? sqlite3_errmsg(db_guard.db) : nullptr;
|
||||||
|
std::string err = (raw_err != nullptr && raw_err[0] != '\0') ? raw_err : fallback_msg;
|
||||||
|
status.ok = false;
|
||||||
|
status.error = err;
|
||||||
|
status.last_error = err;
|
||||||
|
return status;
|
||||||
|
};
|
||||||
|
|
||||||
// 1. Read meta table
|
// 1. Read meta table
|
||||||
{
|
{
|
||||||
SqliteStmtGuard stmt;
|
SqliteStmtGuard stmt;
|
||||||
if (sqlite3_prepare_v2(db_guard.db, "SELECT key, value FROM meta", -1, &stmt.stmt, nullptr) == SQLITE_OK) {
|
if (sqlite3_prepare_v2(db_guard.db, "SELECT key, value FROM meta", -1, &stmt.stmt, nullptr) != SQLITE_OK) {
|
||||||
while (sqlite3_step(stmt.stmt) == SQLITE_ROW) {
|
return fail_sqlite("Failed to prepare SQLite meta query");
|
||||||
|
}
|
||||||
|
int rc = SQLITE_OK;
|
||||||
|
while ((rc = sqlite3_step(stmt.stmt)) == SQLITE_ROW) {
|
||||||
std::string k = col_text(stmt.stmt, 0);
|
std::string k = col_text(stmt.stmt, 0);
|
||||||
std::string v = col_text(stmt.stmt, 1);
|
std::string v = col_text(stmt.stmt, 1);
|
||||||
if (k == "mac_online") {
|
if (k == "mac_online") {
|
||||||
@@ -995,6 +1010,8 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
|
|||||||
status.last_error = v;
|
status.last_error = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (rc != SQLITE_DONE) {
|
||||||
|
return fail_sqlite("Failed while reading SQLite meta table");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1014,8 +1031,11 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
|
|||||||
const char* sql =
|
const char* sql =
|
||||||
"SELECT folder_id, status, COUNT(*), COALESCE(SUM(size_bytes), 0) "
|
"SELECT folder_id, status, COUNT(*), COALESCE(SUM(size_bytes), 0) "
|
||||||
"FROM files GROUP BY folder_id, status";
|
"FROM files GROUP BY folder_id, status";
|
||||||
if (sqlite3_prepare_v2(db_guard.db, sql, -1, &stmt.stmt, nullptr) == SQLITE_OK) {
|
if (sqlite3_prepare_v2(db_guard.db, sql, -1, &stmt.stmt, nullptr) != SQLITE_OK) {
|
||||||
while (sqlite3_step(stmt.stmt) == SQLITE_ROW) {
|
return fail_sqlite("Failed to prepare SQLite files query");
|
||||||
|
}
|
||||||
|
int rc = SQLITE_OK;
|
||||||
|
while ((rc = sqlite3_step(stmt.stmt)) == SQLITE_ROW) {
|
||||||
std::string fid = col_text(stmt.stmt, 0);
|
std::string fid = col_text(stmt.stmt, 0);
|
||||||
std::string st = col_text(stmt.stmt, 1);
|
std::string st = col_text(stmt.stmt, 1);
|
||||||
int cnt = sqlite3_column_int(stmt.stmt, 2);
|
int cnt = sqlite3_column_int(stmt.stmt, 2);
|
||||||
@@ -1044,6 +1064,8 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
|
|||||||
status.failed_count += cnt;
|
status.failed_count += cnt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (rc != SQLITE_DONE) {
|
||||||
|
return fail_sqlite("Failed while reading SQLite files table");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1081,8 +1103,11 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
|
|||||||
{
|
{
|
||||||
SqliteStmtGuard stmt;
|
SqliteStmtGuard stmt;
|
||||||
const char* sql = "SELECT id, ts, level, message FROM logs ORDER BY id DESC LIMIT 50";
|
const char* sql = "SELECT id, ts, level, message FROM logs ORDER BY id DESC LIMIT 50";
|
||||||
if (sqlite3_prepare_v2(db_guard.db, sql, -1, &stmt.stmt, nullptr) == SQLITE_OK) {
|
if (sqlite3_prepare_v2(db_guard.db, sql, -1, &stmt.stmt, nullptr) != SQLITE_OK) {
|
||||||
while (sqlite3_step(stmt.stmt) == SQLITE_ROW) {
|
return fail_sqlite("Failed to prepare SQLite logs query");
|
||||||
|
}
|
||||||
|
int rc = SQLITE_OK;
|
||||||
|
while ((rc = sqlite3_step(stmt.stmt)) == SQLITE_ROW) {
|
||||||
LogEntry entry;
|
LogEntry entry;
|
||||||
entry.id = sqlite3_column_int64(stmt.stmt, 0);
|
entry.id = sqlite3_column_int64(stmt.stmt, 0);
|
||||||
entry.ts = sqlite3_column_int64(stmt.stmt, 1);
|
entry.ts = sqlite3_column_int64(stmt.stmt, 1);
|
||||||
@@ -1090,6 +1115,8 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
|
|||||||
entry.message = col_text(stmt.stmt, 3);
|
entry.message = col_text(stmt.stmt, 3);
|
||||||
status.logs.push_back(std::move(entry));
|
status.logs.push_back(std::move(entry));
|
||||||
}
|
}
|
||||||
|
if (rc != SQLITE_DONE) {
|
||||||
|
return fail_sqlite("Failed while reading SQLite logs table");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -370,6 +370,29 @@ void test_sqlite_fallback_and_config() {
|
|||||||
cp0::IpcClient missing_all((tmp.path / "nonexistent.sock").string(), (tmp.path / "no.db").string(), (tmp.path / "no-cfg.json").string());
|
cp0::IpcClient missing_all((tmp.path / "nonexistent.sock").string(), (tmp.path / "no.db").string(), (tmp.path / "no-cfg.json").string());
|
||||||
assert(missing_all.read_sqlite_fallback().ok == false);
|
assert(missing_all.read_sqlite_fallback().ok == false);
|
||||||
assert(missing_all.fetch_status().ok == false);
|
assert(missing_all.fetch_status().ok == false);
|
||||||
|
|
||||||
|
// Corrupt / non-SQLite DB file returns ok=false with SQLite error
|
||||||
|
fs::path corrupt_db = tmp.path / "corrupt.db";
|
||||||
|
{
|
||||||
|
std::ofstream bad_out(corrupt_db, std::ios::binary);
|
||||||
|
bad_out << "this is definitely not a sqlite database header";
|
||||||
|
}
|
||||||
|
cp0::IpcClient corrupt_client((tmp.path / "nonexistent.sock").string(), corrupt_db.string(), cfg_path.string());
|
||||||
|
auto corrupt_st = corrupt_client.read_sqlite_fallback();
|
||||||
|
assert(corrupt_st.ok == false);
|
||||||
|
assert(!corrupt_st.error.empty());
|
||||||
|
assert(!corrupt_st.last_error.empty());
|
||||||
|
|
||||||
|
// Empty / schema-less 0-byte DB file returns ok=false
|
||||||
|
fs::path empty_db = tmp.path / "empty.db";
|
||||||
|
{
|
||||||
|
std::ofstream empty_out(empty_db, std::ios::binary);
|
||||||
|
}
|
||||||
|
cp0::IpcClient empty_client((tmp.path / "nonexistent.sock").string(), empty_db.string(), cfg_path.string());
|
||||||
|
auto empty_st = empty_client.read_sqlite_fallback();
|
||||||
|
assert(empty_st.ok == false);
|
||||||
|
assert(!empty_st.error.empty());
|
||||||
|
assert(!empty_st.last_error.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_status_parsing_and_env_paths() {
|
void test_status_parsing_and_env_paths() {
|
||||||
@@ -432,14 +455,18 @@ void test_list_tailscale_peers_subprocess() {
|
|||||||
}
|
}
|
||||||
fs::permissions(fake_bin, fs::perms::owner_exec | fs::perms::owner_read | fs::perms::owner_write);
|
fs::permissions(fake_bin, fs::perms::owner_exec | fs::perms::owner_read | fs::perms::owner_write);
|
||||||
|
|
||||||
const char* old_path = std::getenv("PATH");
|
const char* old_path_env = std::getenv("PATH");
|
||||||
std::string new_path = tmp.path.string() + (old_path ? (std::string(":") + old_path) : "");
|
const bool had_path = (old_path_env != nullptr);
|
||||||
|
const std::string saved_path = had_path ? old_path_env : "";
|
||||||
|
std::string new_path = tmp.path.string() + (had_path ? (std::string(":") + saved_path) : "");
|
||||||
setenv("PATH", new_path.c_str(), 1);
|
setenv("PATH", new_path.c_str(), 1);
|
||||||
|
|
||||||
cp0::IpcClient client;
|
cp0::IpcClient client;
|
||||||
auto peers = client.list_tailscale_peers();
|
auto peers = client.list_tailscale_peers();
|
||||||
if (old_path) {
|
if (had_path) {
|
||||||
setenv("PATH", old_path, 1);
|
setenv("PATH", saved_path.c_str(), 1);
|
||||||
|
} else {
|
||||||
|
unsetenv("PATH");
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(peers.size() == 1);
|
assert(peers.size() == 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user