From 5ac951134600c9ac3f20f3d8b7e7ef5f2c862d5c Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 22 Sep 2020 19:25:07 +0200
Subject: [PATCH] MISC & DB: Various fixes

- clear the tmp database before and after making changes on it
- fix clear queue. The problem was due of attached databases
- update max id of queue table when reordering it
- fix generate config on first run, there was a missing strcpy...
---
 inc/lektor/common.h     |  1 +
 src/base/common.c       |  2 +-
 src/base/config.c       |  3 +++
 src/database/memory.sql |  2 +-
 src/database/queue.c    | 16 ++++++++++++++--
 src/main/server.c       |  3 ++-
 6 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/inc/lektor/common.h b/inc/lektor/common.h
index d12ca94e..fc797c50 100644
--- a/inc/lektor/common.h
+++ b/inc/lektor/common.h
@@ -56,6 +56,7 @@ enum log_level {
     WARN    = 2,
     INFO    = 3,
     DEBUG   = 4,
+    __LAST_UNUSED_LOG_LEVEL,    /* Usefull to enable all by default */
 };
 extern enum log_level __log_level;
 void __lkt_log(enum log_level, const char *section, const char *func, const char *format, ...);
diff --git a/src/base/common.c b/src/base/common.c
index d1de4234..647b06aa 100644
--- a/src/base/common.c
+++ b/src/base/common.c
@@ -36,7 +36,7 @@ __set_assert(void)
 
 /* Log functions */
 
-enum log_level __log_level = 0; /* None by default */
+enum log_level __log_level = __LAST_UNUSED_LOG_LEVEL; /* All by default */
 
 void
 __lkt_log(enum log_level level, const char *section, const char *func, const char *format, ...)
diff --git a/src/base/config.c b/src/base/config.c
index e5efa353..c417c7d0 100644
--- a/src/base/config.c
+++ b/src/base/config.c
@@ -172,12 +172,15 @@ config_default_file(char *dest, size_t len)
     memset(dest, 0, len * sizeof(char));
     char *home = getenv("XDG_CONFIG_HOME");
     if (NULL == home || strlen(home) >= len) {
+        LOG_DEBUG("CONFIG", "No env variable XDG_CONFIG_HOME");
         home = getenv("HOME");
         if (NULL == home) {
             LOG_ERROR("FATAL", "Failed to get home folder for user, will now exit");
             exit(EXIT_FAILURE);
         }
+        safe_strncpy(dest, home, len);
         strncat(dest, "/.config/lektor/lektor.ini", len - 1);
+        LOG_DEBUG("CONFIG", "Use '%s' insted of XDG_CONFIG_HOME", home);
     } else {
         strncat(dest, "/lektor/lektor.ini", len - 1);
     }
diff --git a/src/database/memory.sql b/src/database/memory.sql
index 83d8bfe7..cdfb70a0 100644
--- a/src/database/memory.sql
+++ b/src/database/memory.sql
@@ -29,7 +29,7 @@ INSERT INTO queue_state (id) VALUES (42);
 -- Temporary queue table used when reordering the queue (for inserts)
 CREATE TABLE IF NOT EXISTS queue_tmp
    ( position INTEGER PRIMARY KEY AUTOINCREMENT CHECK(position > 0)
-   , kara_id INTEGER
+   , kara_id  INTEGER
    , priority INTEGER NOT NULL DEFAULT 1 CHECK(priority > 0 AND priority < 6)
    );
 
diff --git a/src/database/queue.c b/src/database/queue.c
index 959d416e..1b8a8720 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -26,7 +26,7 @@ sqlite_just_exec(database_queue_stop,           "UPDATE queue_state SET current
 sqlite_just_exec(database_queue_clear,          "DELETE FROM queue;"
                                                 "DELETE FROM queue_tmp;"
                                                 "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
-                                                "DELETE FROM sqlite_sequence WHERE name = 'queue';"
+                                                "DELETE FROM " LKT_PROTECTED_DATABASE ".sqlite_sequence WHERE name = 'queue';"
                                                 "UPDATE queue_state SET current = NULL;")
 sqlite_just_exec(database_config_queue_default, "UPDATE queue_state SET volume = 100, paused = 1,"
                                                 " random = 0, repeat = 0, single = 0, consume = 0,"
@@ -142,16 +142,21 @@ __queue_reorder(volatile sqlite3 *db)
     "(SELECT CASE WHEN (SELECT current FROM queue_state) IS NULL THEN 0"    \
     " ELSE (SELECT current FROM queue_state) END AS val LIMIT 1)"
     static const char *SQL_REORDER =
+        /* Clear the TMP */
+        "DELETE FROM queue_tmp;"
+        "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
         /* Separate karas that are after the current one */
         "INSERT INTO queue_tmp (kara_id, priority)"
         " SELECT kara_id, priority FROM queue WHERE position > " CURRENT_POS_OR_0
         " ORDER BY priority DESC, position ASC;"
         "DELETE FROM queue WHERE position > " CURRENT_POS_OR_0 ";"
+        /* Update the sqlite_sequence table */
+        "UPDATE sqlite_sequence SET seq = " CURRENT_POS_OR_0 " WHERE name = 'queue';"
         /* Insert back */
         "INSERT INTO queue (position, kara_id, priority)"
         " SELECT position + " CURRENT_POS_OR_0 ", kara_id, priority"
         " FROM queue_tmp;"
-        /* Drop temporary tables */
+        /* Clear the TMP */
         "DELETE FROM queue_tmp;"
         "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';";
 #undef CURRENT_POS_OR_0
@@ -294,6 +299,7 @@ database_queue_del_id(volatile sqlite3 *db, int id)
 {
     static const char *SQL_TEMPLATE =
         "BEGIN TRANSACTION;"
+        /* Clear the TMP */
         "DELETE FROM queue_tmp;"
         "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
         /* Move the current 'pointer' */
@@ -303,6 +309,9 @@ database_queue_del_id(volatile sqlite3 *db, int id)
         "DELETE FROM queue;"
         "INSERT INTO queue(priority, position, kara_id) SELECT priority, ROW_NUMBER() OVER(ORDER BY position ASC), kara_id FROM queue_tmp;"
         "UPDATE sqlite_sequence SET seq = (SELECT COUNT(*) FROM queue) WHERE name = 'queue';"   /* Update the sqlite_sequence            */
+        /* Clear the TMP */
+        "DELETE FROM queue_tmp;"
+        "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
         "COMMIT;";
     char SQL[LKT_MAX_SQLITE_STATEMENT];
     safe_snprintf(SQL, LKT_MAX_SQLITE_STATEMENT, SQL_TEMPLATE, id, id);
@@ -599,6 +608,9 @@ database_queue_shuffle(volatile sqlite3 *db)
         "  SET current = CASE"
         "    WHEN current NOT NULL THEN 1"
         "    ELSE NULL END;"
+        /* Clear the TMP */
+        "DELETE FROM queue_tmp;"
+        "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
         "COMMIT;";
     SQLITE_EXEC(db, SQL, error);
     return true;
diff --git a/src/main/server.c b/src/main/server.c
index 12515efe..cb2e7c20 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -111,7 +111,8 @@ retry_config:
             __mkdir(conf_file); /* Create the folder for the file. */
             FILE *file_desc = fopen(conf_file, "w+");
             if (NULL == file_desc) {
-                LOG_ERROR("FATAL", "Failed to open default config fiel for initializing it");
+                LOG_ERROR("FATAL", "Failed to open default config file '%s' to initialize it",
+                          conf_file);
                 exit(EXIT_FAILURE);
             }
             config_default(file_desc);
-- 
GitLab