From 445e883f550befdcf608c99335369d99a56d0a23 Mon Sep 17 00:00:00 2001 From: theBreadCompany Date: Wed, 11 Jun 2025 17:34:42 +0200 Subject: [PATCH] bug fixing --- src/main/main.c | 30 ++++++++++++++++++++---------- src/main/models.c | 9 ++++++--- src/main/sql.c | 9 ++++----- src/main/util.c | 6 +++++- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/main/main.c b/src/main/main.c index bf21807..003c649 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -65,15 +65,19 @@ int main(int argc, char **argv) { fprintf(stderr, "Missing stages.\n"); break; } - list = klist_list_init(); - list->name = (unsigned char *)list_ctx->name; - list->desc = (unsigned char *)list_ctx->desc; + klist_assure_user(ctx, getuid(), getlogin()); + user = klist_user_get_by_local(ctx, getuid()); + list = klist_list_get_by_user_and_name(ctx, user->id, list_ctx->name); + if (!list) list = klist_list_init(); + list->name = (unsigned char*)strdup(list_ctx->name); + if (list->desc) + list->desc = (unsigned char*)strdup(list_ctx->desc); list->is_preset = false; klist_list_save(ctx, list); i = 0; for (; i < list_ctx->stages_len; i++) { klist_stage *stage = klist_stage_init(); - stage->name = (unsigned char *)list_ctx->stages[i]; + stage->name = (unsigned char *)strdup(list_ctx->stages[i]); stage->list_id = list->id; klist_stage_save(ctx, stage); klist_stage_deinit(stage); @@ -154,7 +158,7 @@ int main(int argc, char **argv) { i = 0; ssize_t stage_id = -1; for (; i < stages_len; i++) - if (strcmp((char *)stages[i]->name, (char *)task_ctx->stage) == 0) + if (strcmp((char *)stages[i]->name, task_ctx->stage) == 0) stage_id = stages[i]->id; if (stage_id == -1) { @@ -298,7 +302,7 @@ void setup(klist *ctx, int argc, char **argv) { break; } - optind = 3; + optind = 2; while ((opt = getopt(argc, argv, "aden:p:s:")) != -1) switch (opt) { case 'a': @@ -311,12 +315,18 @@ void setup(klist *ctx, int argc, char **argv) { list_ctx->cmd = LIST_DELETE; break; case 'p': - list_ctx->preset = malloc((strlen(optarg) + 1) * sizeof(char)); - strcpy(list_ctx->preset, optarg); + list_ctx->preset = strdup(optarg); break; case 'n': - list_ctx->name = malloc((strlen(optarg) + 1) * sizeof(char)); - strcpy(list_ctx->name, optarg); + list_ctx->name = strdup(optarg); + break; + case 's': + char *stage = NULL; + while ((stage = strsep(&optarg, ","))) { + fprintf(stderr, "found stage %s\n", stage); + list_ctx->stages = realloc(list_ctx->stages, (++list_ctx->stages_len) * sizeof(char *)); + list_ctx->stages[list_ctx->stages_len - 1] = strdup(stage); + } break; default: print_help(argv); diff --git a/src/main/models.c b/src/main/models.c index ddc63ff..fdb77ff 100644 --- a/src/main/models.c +++ b/src/main/models.c @@ -42,7 +42,7 @@ klist_user *klist_user_get_by_local(klist *ctx, u_int local_id) { sqlite3_clear_bindings(stmt); return klist_user_init_from_sql(stmt); } - fprintf(stderr, "failed to get user by local id %u\n", local_id); + fprintf(stderr, "failed to get user by local id %u: %s\n", local_id, sqlite3_errmsg(ctx->db)); return NULL; } @@ -66,7 +66,7 @@ void klist_user_save(klist *ctx, klist_user *user) { fprintf(stderr, "failed to save logins for user: %s - %d\n", sqlite3_errmsg(ctx->db), result); sqlite3_clear_bindings(stmt); - } + } else fprintf(stderr, "no local uid saved for user %s", user->name); } void klist_user_delete(klist *ctx, klist_user *user) { @@ -139,7 +139,8 @@ void klist_stage_save(klist *ctx, klist_stage *stage) { int result = 0; sqlite3_bind_text(stmt, 1, (char *)stage->name, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, (char *)stage->name, -1, SQLITE_STATIC); - if ((result = sqlite3_step(stmt)) == SQLITE_ROW) { + sqlite3_bind_int(stmt, 3, stage->list_id); + if ((result = sqlite3_step(stmt)) == SQLITE_ROW || result == SQLITE_DONE) { stage->id = sqlite3_column_int(stmt, 0); sqlite3_clear_bindings(stmt); } else @@ -219,6 +220,7 @@ klist_list *klist_list_get_by_user_and_name(klist *ctx, u_int user_id, sqlite3_clear_bindings(stmt); return klist_list_init(stmt); } + fprintf(stderr, "failed to get lists for user\n"); return NULL; } @@ -227,6 +229,7 @@ void klist_list_save(klist *ctx, klist_list *list) { int result = 0; sqlite3_bind_text(stmt, 1, (char *)list->name, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, (char *)list->desc, -1, SQLITE_STATIC); + sqlite3_bind_int(stmt, 3, list->is_preset); if ((result = sqlite3_step(stmt)) == SQLITE_ROW) { list->id = sqlite3_column_int(stmt, 0); sqlite3_clear_bindings(stmt); diff --git a/src/main/sql.c b/src/main/sql.c index 2d989ad..1eb19ba 100644 --- a/src/main/sql.c +++ b/src/main/sql.c @@ -56,8 +56,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) { return "select users.*, user_logins.local_id from users inner join " "user_logins on user_logins.user_id = users.id where users.id = ?1"; case GET_USER_BY_LOCAL: - return "select * from users inner join user_logins on user_logins.user_id " - "= users.id where user_logins.local_id = ?1"; + return "select * from users inner join user_logins on user_logins.user_id = users.id where user_logins.local_id = ?1"; case GET_USERS: return "select * from users"; case MOD_USER_NAME: @@ -65,7 +64,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) { case DEL_USER: return "delete from users where id = ?1"; case ADD_LIST: - return "insert into lists (name, desc, is_preset) values (?1, ?2, ?3)"; + return "insert into lists (name, desc, is_preset) values (?1, ?2, ?3) on conflict (id) do update set name=excluded.name, desc=excluded.desc, is_preset=excluded.is_preset returning id"; case GET_LIST: return "select * from lists where id = ?1"; case GET_LISTS: @@ -81,7 +80,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) { case DEL_LIST: return "delete from lists where id = ?1"; case ADD_STAGE: - return "insert into task_stages (name, desc, list_id) values (?1, ?2, ?3)"; + return "insert into task_stages (name, desc, list_id) values (?1, ?2, ?3) on conflict (id) do update set name=excluded.name, desc=excluded.desc returning id"; case GET_STAGE: return "select * from task_stages where id = ?1"; case GET_STAGES_FOR_LIST: @@ -90,7 +89,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) { return "delete from task_stages where id = ?1"; case ADD_TASK: return "insert into tasks (name, desc, stage, due, target_stage) values " - "(?1, ?2, ?3, ?4, ?5);"; + "(?1, ?2, ?3, ?4, ?5) on conflict (id) do update set name=excluded.name, desc=excluded.desc, stage=excluded.stage, due=excluded.due, target_stage=excluded.target_stage returning id"; case GET_TASK: return "select * from tasks where id = ?1"; case GET_TASKS: diff --git a/src/main/util.c b/src/main/util.c index affb921..2845025 100644 --- a/src/main/util.c +++ b/src/main/util.c @@ -30,6 +30,7 @@ void klist_deinit(klist *ctx) { // if (ctx->cmd_ctx) free(ctx->cmd_ctx); free(ctx->stmts); free(ctx); +ctx = NULL; } klist_user_context *klist_user_context_init() { @@ -37,7 +38,8 @@ klist_user_context *klist_user_context_init() { ctx->cmd = -1; return ctx; } -void klist_user_context_deinit(klist_user_context *ctx) { free(ctx); } +void klist_user_context_deinit(klist_user_context *ctx) { free(ctx); +ctx = NULL; } klist_list_context *klist_list_context_init() { klist_list_context *ctx = malloc(sizeof(klist_list_context)); @@ -63,6 +65,7 @@ void klist_list_context_deinit(klist_list_context *ctx) { free(ctx->stages); } free(ctx); +ctx = NULL; } klist_task_context *klist_task_context_init(klist *list) { @@ -81,6 +84,7 @@ void klist_task_context_deinit(klist_task_context *ctx) { if (ctx->stage) free(ctx->stage); free(ctx); +ctx = NULL; } /*