From 821d73d24a1f5ec5f25d0780b300d56266496cae Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 28 Sep 2020 15:19:07 +0200
Subject: [PATCH] MODULE: Differentiate different types of downloads

---
 src/module/module_repo.c | 110 ++++++++++++++++++++-------------------
 1 file changed, 56 insertions(+), 54 deletions(-)

diff --git a/src/module/module_repo.c b/src/module/module_repo.c
index afe682b2..67099a8f 100644
--- a/src/module/module_repo.c
+++ b/src/module/module_repo.c
@@ -20,15 +20,59 @@
 #include <time.h>
 #include <stdarg.h>
 
+/* Different kinds of updates */
+#define REPO_UPDATE_KARA (1 << 1)   /* Downloading or rescanning the bakabase */
+#define REPO_UPDATE_FAV  (1 << 2)   /* Downloading the favorites */
+#define REPO_UPDATE_TYPE_COUNT 2    /* Different kinds of download, for some sizes... */
+
 /***********
  * Globals *
  ***********/
 
 static volatile unsigned int __curl_init = false;
 
-/******************************************************
- * Private functions needed for the private structure *
- ******************************************************/
+/*********************
+ * Private structure *
+ *********************/
+
+struct module_repo_internal {
+    /* Just the repo */
+    char *name;
+    char *base_url;
+    char *kara_dir;
+    char *get_all_json;
+    char *get_id_json;
+    char *get_id_file;
+    const uint64_t version;
+
+    /* Worker threads */
+    struct worker_pool workers;
+    volatile int updating : REPO_UPDATE_TYPE_COUNT;
+
+    /* The database and the queue */
+    struct queue *queue;
+    volatile sqlite3 *db;
+
+    /* Craft a filename for newly downloaded karas. Arguments:
+     * - str: the destination string, the prefix, i.e. the kara_dir, is already here
+     * - len: the length of the string, not its size (which is PATH_MAX)
+     * - kara: the kara structure, which contains all the data to create the filename */
+    void (*craft_filename)(char str[PATH_MAX], size_t, struct kara *);
+};
+
+struct __memory {
+    void *mem;
+    size_t size;
+};
+
+struct __file {
+    const char *path;
+    int fd;
+};
+
+/*********************
+ * Private functions *
+ *********************/
 
 /* Recursive mkdir, where the last word of the string is a file, not a folder. */
 static inline void
@@ -74,49 +118,6 @@ __craft_filename_non_obfuscate(char str[PATH_MAX], size_t len, struct kara *kara
                   kara->mdt.song_type, kara->mdt.song_number, kara->mdt.song_name);
 }
 
-/*********************
- * Private structure *
- *********************/
-
-struct module_repo_internal {
-    /* Just the repo */
-    char *name;
-    char *base_url;
-    char *kara_dir;
-    char *get_all_json;
-    char *get_id_json;
-    char *get_id_file;
-    const uint64_t version;
-
-    /* Worker threads */
-    struct worker_pool workers;
-    volatile int updating;
-
-    /* The database and the queue */
-    struct queue *queue;
-    volatile sqlite3 *db;
-
-    /* Craft a filename for newly downloaded karas. Arguments:
-     * - str: the destination string, the prefix, i.e. the kara_dir, is already here
-     * - len: the length of the string, not its size (which is PATH_MAX)
-     * - kara: the kara structure, which contains all the data to create the filename */
-    void (*craft_filename)(char str[PATH_MAX], size_t, struct kara *);
-};
-
-struct __memory {
-    void *mem;
-    size_t size;
-};
-
-struct __file {
-    const char *path;
-    int fd;
-};
-
-/*********************
- * Private functions *
- *********************/
-
 static inline void
 __clean_file(struct __file *f)
 {
@@ -426,7 +427,7 @@ __worker_update(void *__repo)
 {
     struct module_repo_internal *repo = __repo;
     lkt_queue_send(repo->queue, lkt_event_db_updating, LKT_DB_UPDATING_PROGRESS);
-    repo->updating = 1;
+    repo->updating &= REPO_UPDATE_KARA;
 
     struct json_object *json;
     LOG_INFO("REPO", "Download kara list from %s (%s), directory is %s",
@@ -456,10 +457,10 @@ __worker_rescan(void *__repo)
         LOG_ERROR("REPO", "Failed to get kara prefix from config");
         pthread_exit(NULL);
     }
-    repo->updating = 1;
+    repo->updating &= REPO_UPDATE_KARA;
     database_update(repo->db, kara_prefix, 0); /* Don't check timestamp.
-                                                  TODO: Sometimes we want to check them */
-    repo->updating = 0;
+                                                * TODO: Sometimes we want to check them */
+    repo->updating &= (~ REPO_UPDATE_KARA);
     lkt_queue_send(repo->queue, lkt_event_db_updating, LKT_DB_UPDATING_FINISHED);
     pthread_exit(NULL);
 }
@@ -646,12 +647,12 @@ mod_update(va_list *va)
     va_copy(copy, *va);
     repo = (struct module_repo_internal **) va_arg(copy, void **);
 
-    if ((*repo)->updating) {
+    if ((*repo)->updating & REPO_UPDATE_KARA) {
         LOG_WARN("REPO", "Already updating");
         va_end(copy);
         return 0;
     }
-    (*repo)->updating = 1;
+    (*repo)->updating &= REPO_UPDATE_KARA;
     if (worker_pool_push(&(*repo)->workers, __worker_update, (void *) *repo)) {
         LOG_ERROR("REPO", "Out of memory");
         va_end(copy);
@@ -671,12 +672,12 @@ mod_rescan(va_list *va)
     va_copy(copy, *va);
     repo = (struct module_repo_internal **) va_arg(copy, void **);
 
-    if ((*repo)->updating) {
+    if ((*repo)->updating & REPO_UPDATE_KARA) {
         LOG_WARN("REPO", "Already updating");
         va_end(copy);
         return 0;
     }
-    (*repo)->updating = 1;
+    (*repo)->updating &= REPO_UPDATE_KARA;
     if (worker_pool_push(&(*repo)->workers, __worker_rescan, (void *) *repo)) {
         LOG_ERROR("REPO", "Out of memory");
         va_end(copy);
@@ -699,6 +700,7 @@ mod_is_updating(va_list *va)
     ret  = va_arg(copy, int *);
     if (ret == NULL)
         return 1;
+    /* Don't differentiate kinds of downloads here */
     *ret = (*repo)->updating;
     va_end(copy);
     return 0;
-- 
GitLab