From 5bb9f80ea7d2c849e4dbd5738ba764fd1dad002c Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 13 Dec 2021 15:32:37 +0100
Subject: [PATCH] REPO: Force download if the kara if unavlailable

If a kara is present on disk but is marked as unavailable it would not
be downloaded. This commit fix it, if a kara is marked as unavailable it
will be downloaded anyway (may be filtered by user URI...)
---
 inc/lektor/database.h    |  1 +
 src/database/cache.c     | 21 +++++++++++++++++++++
 src/module/module_repo.c | 19 ++++++++++++++-----
 3 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index 3cadd858..5826602e 100644
--- a/inc/lektor/database.h
+++ b/inc/lektor/database.h
@@ -73,6 +73,7 @@ bool database_get_kara_mtime_path   (lkt_db *db, char filepath[PATH_MAX], uint64
 bool database_get_kara_duration_path(lkt_db *db, char filepath[PATH_MAX], uint64_t *duration);
 bool database_is_kara_mkv           (lkt_db *db, char filepath[PATH_MAX], bool *valid_mkv);
 bool database_is_kara_like          (lkt_db *db, int id, const char *str);
+bool database_is_kara_available     (lkt_db *db, int id, bool *avail);
 void database_cache_kara            (lkt_db *db, int id);
 void database_cache_kara_magic      (lkt_db *db, int id);
 
diff --git a/src/database/cache.c b/src/database/cache.c
index 383c0a5a..18deada8 100644
--- a/src/database/cache.c
+++ b/src/database/cache.c
@@ -103,6 +103,27 @@ error:
     return ret_code;
 }
 
+bool
+database_is_kara_available(lkt_db *db, int id, bool *avail)
+{
+    sqlite3_stmt *stmt     = NULL;
+    static const char *SQL = "SELECT available FROM kara where id = ?;";
+
+    SQLITE_PREPARE(db, stmt, SQL, error);
+    SQLITE_BIND_INT(db, stmt, 1, id, error);
+    SQLITE_STEP_ROW(db, stmt, error);
+
+    if (avail != NULL)
+        *avail = sqlite3_column_int(stmt, 0);
+
+    sqlite3_finalize(stmt);
+    return true;
+error:
+    if (stmt)
+        sqlite3_finalize(stmt);
+    return false;
+}
+
 bool
 database_is_kara_like(lkt_db *db, int id, const char *str)
 {
diff --git a/src/module/module_repo.c b/src/module/module_repo.c
index a793aee7..b44c82f2 100644
--- a/src/module/module_repo.c
+++ b/src/module/module_repo.c
@@ -341,9 +341,10 @@ err:
 }
 
 PRIVATE_FUNCTION void
-___handle_got_json_dl(struct kara *kara, int current_id)
+___handle_got_json_dl(struct kara *kara)
 {
     /* Download the kara */
+    int current_id;
     database_queue_current_kara(kara->db, NULL, &current_id);
     if (current_id == (int)kara->id) {
         LOG_WARN("REPO", "Update currently playing kara %d, skip it", current_id);
@@ -470,9 +471,9 @@ ___handle_got_json_internal_callback(const char *key, const char *val, int comp,
     /* The `void *user` is complete */
     else if (comp) {
         uint64_t filestamp = 0;
-        int current_id     = 0;
         bool magic_valid   = 0;
         unsigned int umask = 0;
+        bool avail         = false;
         DBCONF_GET_OCTAL(kara->db, "database", "dir_permission", &umask);
 
         /* Timestamp and presence verification */
@@ -482,6 +483,14 @@ ___handle_got_json_internal_callback(const char *key, const char *val, int comp,
             goto do_it;
         }
 
+        if (database_is_kara_available(kara->db, (int)kara->id, &avail) && (!avail)) {
+            LOG_INFO("REPO",
+                     "Force download of unavailable kara %ld "
+                     "(may still be filtered out)",
+                     kara->id);
+            goto do_it;
+        }
+
         database_get_kara_mtime_path(kara->db, kara->filename, &filestamp);
         database_is_kara_mkv(kara->db, kara->filename, &magic_valid);
         if (filestamp == 0 || kara->unix_timestamp != (long)(uint64_t)kara->unix_timestamp ||
@@ -503,13 +512,13 @@ ___handle_got_json_internal_callback(const char *key, const char *val, int comp,
 
     do_it:
         /* Even if we must download the kara, the user could have filtered it out! */
-        if (lkt_uri_match_kara_metadata(filter, &kara->mdt, kara->db, current_id)) {
-            LOG_DEBUG("REPO", "The kara %d is ignored because it was filtered out", current_id);
+        if (lkt_uri_match_kara_metadata(filter, &kara->mdt, kara->db, (int)kara->id)) {
+            LOG_DEBUG("REPO", "The kara %ld is ignored because it was filtered out", kara->id);
             ++(kara->ignored_count);
             return 0;
         }
 
-        ___handle_got_json_dl(kara, current_id);
+        ___handle_got_json_dl(kara);
         lkt_queue_send(kara->repo->queue, LKT_EVENT_DB_UPDATE_TICK, NULL);
     }
 
-- 
GitLab