summaryrefslogtreecommitdiff
path: root/src/app.c
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2026-02-21 23:47:28 +0100
committerJesper Jensen <jesper@jnsn.dev>2026-02-21 23:47:28 +0100
commit0c817cc0ae9a6e1b66d6e6ff45fa80bacabc58a9 (patch)
tree31b95a732c6523c4e0819cdcd129e0f4aed5eda8 /src/app.c
parent3c59ccdbf77ca8f6f90dd0a1b6d1f0a833e6f2fb (diff)
Parse the status closer to the database
Diffstat (limited to 'src/app.c')
-rw-r--r--src/app.c52
1 files changed, 39 insertions, 13 deletions
diff --git a/src/app.c b/src/app.c
index 28debe9..7d0df96 100644
--- a/src/app.c
+++ b/src/app.c
@@ -13,6 +13,28 @@
#include <sqlite3.h>
#include <fcntl.h>
+static const char *backup_status_db_strings[] = {
+ [STATUS_RUNNING] = "RUNNING",
+ [STATUS_FINISHED] = "FINISHED",
+ [STATUS_INTERRUPTED] = "INTERRUPTED",
+};
+
+const char *backup_status_to_db_string(enum BackupStatus status) {
+ assert(status >= 0 && status <= STATUS_INTERRUPTED);
+ return backup_status_db_strings[status];
+}
+
+enum BackupStatus backup_status_from_db_string(const char *str) {
+ assert(str != NULL);
+ for (int i = 0; i <= STATUS_INTERRUPTED; i++) {
+ if (strcmp(str, backup_status_db_strings[i]) == 0) {
+ return (enum BackupStatus)i;
+ }
+ }
+
+ abort();
+}
+
static void sqliteError(void *pArg, int iErrCode, const char *zMsg) {
// @CLEANUP: We should stuff these somewhere, but for now just print it
fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
@@ -305,7 +327,7 @@ int submit_report(struct App *app, char *client, char *secret, int is_finish) {
sqlite3_stmt *stmt;
// Mark any existing RUNNING backup as INTERRUPTED
- if(sqlite3_prepare_v2(conn, "UPDATE backups SET status = 'INTERRUPTED', completed = datetime(?1, 'unixepoch', 'subsec') WHERE client = ?2 AND status = 'RUNNING'", -1, &stmt, NULL) != SQLITE_OK) {
+ if(sqlite3_prepare_v2(conn, "UPDATE backups SET status = ?3, completed = datetime(?1, 'unixepoch', 'subsec') WHERE client = ?2 AND status = ?4", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_double(stmt, 1, TIME_AS_FLOAT(now)) != SQLITE_OK) {
@@ -314,6 +336,12 @@ int submit_report(struct App *app, char *client, char *secret, int is_finish) {
if(sqlite3_bind_int64(stmt, 2, clientId) != SQLITE_OK) {
abort();
}
+ if(sqlite3_bind_text(stmt, 3, backup_status_to_db_string(STATUS_INTERRUPTED), -1, SQLITE_STATIC) != SQLITE_OK) {
+ abort();
+ }
+ if(sqlite3_bind_text(stmt, 4, backup_status_to_db_string(STATUS_RUNNING), -1, SQLITE_STATIC) != SQLITE_OK) {
+ abort();
+ }
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
@@ -331,7 +359,7 @@ int submit_report(struct App *app, char *client, char *secret, int is_finish) {
if(sqlite3_bind_int64(stmt, 1, clientId) != SQLITE_OK) {
abort();
}
- if(sqlite3_bind_text(stmt, 2, "RUNNING", -1, SQLITE_STATIC) != SQLITE_OK) {
+ if(sqlite3_bind_text(stmt, 2, backup_status_to_db_string(STATUS_RUNNING), -1, SQLITE_STATIC) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_double(stmt, 3, TIME_AS_FLOAT(now)) != SQLITE_OK) {
@@ -350,12 +378,15 @@ int submit_report(struct App *app, char *client, char *secret, int is_finish) {
uint64_t backupId;
sqlite3_stmt *stmt;
- if(sqlite3_prepare_v2(conn, "SELECT id FROM backups WHERE client = ?1 AND status = 'RUNNING'", -1, &stmt, NULL) != SQLITE_OK) {
+ if(sqlite3_prepare_v2(conn, "SELECT id FROM backups WHERE client = ?1 AND status = ?2", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, clientId) != SQLITE_OK) {
abort();
}
+ if(sqlite3_bind_text(stmt, 2, backup_status_to_db_string(STATUS_RUNNING), -1, SQLITE_STATIC) != SQLITE_OK) {
+ abort();
+ }
if(sqlite3_step(stmt) != SQLITE_ROW) {
abort();
@@ -378,7 +409,7 @@ int submit_report(struct App *app, char *client, char *secret, int is_finish) {
if(sqlite3_prepare_v2(conn, "UPDATE backups SET status = ?1, completed = datetime(?2, 'unixepoch', 'subsec') WHERE id = ?3", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
- if(sqlite3_bind_text(stmt, 1, "FINISHED", -1, SQLITE_STATIC) != SQLITE_OK) {
+ if(sqlite3_bind_text(stmt, 1, backup_status_to_db_string(STATUS_FINISHED), -1, SQLITE_STATIC) != SQLITE_OK) {
abort();
}
@@ -517,16 +548,11 @@ int list_clients(struct App *app, struct ListClientResult *result) {
result->clients[index].last_backup = time_from_double(last_backup);
if (backup_status == NULL) {
- strncpy(result->clients[index].status, "NEVER", BACKUP_STATUS_MAX);
- result->clients[index].is_stale = 0;
- } else if (strcmp((char*)backup_status, "RUNNING") == 0) {
- strncpy(result->clients[index].status, "RUNNING", BACKUP_STATUS_MAX);
- result->clients[index].is_stale = 0;
- } else if (strcmp((char*)backup_status, "INTERRUPTED") == 0) {
- strncpy(result->clients[index].status, "INTERRUPTED", BACKUP_STATUS_MAX);
+ result->clients[index].last_backup.tv_sec = 0;
+ result->clients[index].status = 0;
result->clients[index].is_stale = 0;
} else {
- strncpy(result->clients[index].status, "OK", BACKUP_STATUS_MAX);
+ result->clients[index].status = backup_status_from_db_string((char*)backup_status);
result->clients[index].is_stale = 0;
}
@@ -619,7 +645,7 @@ int get_client_backups(struct App *app, int64_t client_id, struct GetBackupsResu
const double started = sqlite3_column_double(stmt, 2);
const double completed = sqlite3_column_double(stmt, 3);
- strncpy(result->backups[index].status, status ? (char*)status : "", BACKUP_STATUS_MAX);
+ result->backups[index].status = backup_status_from_db_string(status ? (char*)status : NULL);
result->backups[index].started = time_from_double(started);
result->backups[index].completed = time_from_double(completed);