fix(device-ui): fail closed on corrupt sqlite fallback and fix linux build portability

This commit is contained in:
2026-11-18 08:19:09 -05:00
parent b6fcf5ae36
commit 8ea46a26ae
3 changed files with 116 additions and 58 deletions

View File

@@ -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)
find_package(SQLite3 REQUIRED)
find_package(Threads REQUIRED)
add_library(cp0_ui_core STATIC
src/theme.cpp
@@ -34,7 +35,10 @@ if(CP0_UI_BUILD_TESTS)
add_executable(test_ipc_and_theme
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)
add_test(NAME test_ipc_and_theme COMMAND test_ipc_and_theme)
endif()

View File

@@ -14,6 +14,7 @@
#include <cctype>
#include <cerrno>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
@@ -970,32 +971,48 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
std::string db_path = resolved_db_path();
SqliteDbGuard db_guard;
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.error = db_guard.db != nullptr ? sqlite3_errmsg(db_guard.db) : "Failed to open SQLite DB";
status.error = err;
status.last_error = err;
return status;
}
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
{
SqliteStmtGuard stmt;
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) {
std::string k = col_text(stmt.stmt, 0);
std::string v = col_text(stmt.stmt, 1);
if (k == "mac_online") {
status.mac_online = (v == "true");
} else if (k == "mac_dest_root") {
status.mac_dest_root = v;
} else if (k == "last_sync_ts") {
status.last_sync_ts = parse_int64_safe(v, 0);
} else if (k == "last_synced_file") {
status.last_synced_file = v;
} else if (k == "last_error") {
status.last_error = v;
}
if (sqlite3_prepare_v2(db_guard.db, "SELECT key, value FROM meta", -1, &stmt.stmt, nullptr) != SQLITE_OK) {
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 v = col_text(stmt.stmt, 1);
if (k == "mac_online") {
status.mac_online = (v == "true");
} else if (k == "mac_dest_root") {
status.mac_dest_root = v;
} else if (k == "last_sync_ts") {
status.last_sync_ts = parse_int64_safe(v, 0);
} else if (k == "last_synced_file") {
status.last_synced_file = v;
} else if (k == "last_error") {
status.last_error = v;
}
}
if (rc != SQLITE_DONE) {
return fail_sqlite("Failed while reading SQLite meta table");
}
}
// 2. Aggregate files table per folder_id and status
@@ -1014,36 +1031,41 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
const char* sql =
"SELECT folder_id, status, COUNT(*), COALESCE(SUM(size_bytes), 0) "
"FROM files GROUP BY folder_id, status";
if (sqlite3_prepare_v2(db_guard.db, sql, -1, &stmt.stmt, nullptr) == SQLITE_OK) {
while (sqlite3_step(stmt.stmt) == SQLITE_ROW) {
std::string fid = col_text(stmt.stmt, 0);
std::string st = col_text(stmt.stmt, 1);
int cnt = sqlite3_column_int(stmt.stmt, 2);
int64_t bytes = sqlite3_column_int64(stmt.stmt, 3);
if (sqlite3_prepare_v2(db_guard.db, sql, -1, &stmt.stmt, nullptr) != SQLITE_OK) {
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 st = col_text(stmt.stmt, 1);
int cnt = sqlite3_column_int(stmt.stmt, 2);
int64_t bytes = sqlite3_column_int64(stmt.stmt, 3);
if (by_folder.find(fid) == by_folder.end()) {
seen_folder_order.push_back(fid);
}
auto& bucket = by_folder[fid];
if (st == "PENDING") {
bucket.pending += cnt;
bucket.pending_bytes += bytes;
status.pending_count += cnt;
status.pending_bytes += bytes;
} else if (st == "UPLOADING") {
bucket.pending += cnt;
bucket.uploading += cnt;
bucket.pending_bytes += bytes;
status.pending_count += cnt;
status.pending_bytes += bytes;
} else if (st == "SYNCED") {
bucket.synced += cnt;
status.synced_count += cnt;
} else if (st == "FAILED") {
bucket.failed += cnt;
status.failed_count += cnt;
}
if (by_folder.find(fid) == by_folder.end()) {
seen_folder_order.push_back(fid);
}
auto& bucket = by_folder[fid];
if (st == "PENDING") {
bucket.pending += cnt;
bucket.pending_bytes += bytes;
status.pending_count += cnt;
status.pending_bytes += bytes;
} else if (st == "UPLOADING") {
bucket.pending += cnt;
bucket.uploading += cnt;
bucket.pending_bytes += bytes;
status.pending_count += cnt;
status.pending_bytes += bytes;
} else if (st == "SYNCED") {
bucket.synced += cnt;
status.synced_count += cnt;
} else if (st == "FAILED") {
bucket.failed += cnt;
status.failed_count += cnt;
}
}
if (rc != SQLITE_DONE) {
return fail_sqlite("Failed while reading SQLite files table");
}
}
@@ -1081,15 +1103,20 @@ DaemonStatus IpcClient::read_sqlite_fallback() const {
{
SqliteStmtGuard stmt;
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) {
while (sqlite3_step(stmt.stmt) == SQLITE_ROW) {
LogEntry entry;
entry.id = sqlite3_column_int64(stmt.stmt, 0);
entry.ts = sqlite3_column_int64(stmt.stmt, 1);
entry.level = col_text(stmt.stmt, 2);
entry.message = col_text(stmt.stmt, 3);
status.logs.push_back(std::move(entry));
}
if (sqlite3_prepare_v2(db_guard.db, sql, -1, &stmt.stmt, nullptr) != SQLITE_OK) {
return fail_sqlite("Failed to prepare SQLite logs query");
}
int rc = SQLITE_OK;
while ((rc = sqlite3_step(stmt.stmt)) == SQLITE_ROW) {
LogEntry entry;
entry.id = sqlite3_column_int64(stmt.stmt, 0);
entry.ts = sqlite3_column_int64(stmt.stmt, 1);
entry.level = col_text(stmt.stmt, 2);
entry.message = col_text(stmt.stmt, 3);
status.logs.push_back(std::move(entry));
}
if (rc != SQLITE_DONE) {
return fail_sqlite("Failed while reading SQLite logs table");
}
}

View File

@@ -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());
assert(missing_all.read_sqlite_fallback().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() {
@@ -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);
const char* old_path = std::getenv("PATH");
std::string new_path = tmp.path.string() + (old_path ? (std::string(":") + old_path) : "");
const char* old_path_env = std::getenv("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);
cp0::IpcClient client;
auto peers = client.list_tailscale_peers();
if (old_path) {
setenv("PATH", old_path, 1);
if (had_path) {
setenv("PATH", saved_path.c_str(), 1);
} else {
unsetenv("PATH");
}
assert(peers.size() == 1);