]> git.ayabusa.dev Git - thoryum.git/commitdiff
cleaned up the downloader file
authorAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 18:04:45 +0000 (20:04 +0200)
committerAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 18:04:45 +0000 (20:04 +0200)
src/downloader.c

index 45dc6b821c8f365dfa79ec8a12013e2ff6f9890c..70e8573161316d2c8b64bdb51cd6061e2dc4c769 100644 (file)
 #define THORIUM_RELEASE_API_URL "https://api.github.com/repos/gz83/thorium/releases"
 #define THORIUM_RELEASE_BASE_URL "https://github.com/gz83/thorium/releases/download"
 
-// Well its probably usefull
 static size_t write_file(char *ptr, size_t size, size_t nmemb, void **stream)
 {
     size_t written = fwrite(ptr, size, nmemb, (FILE *)*stream);
     return written;
 }
 
+// Yep its ugly, but well
 size_t ver_str_size = 1; // Malloced size
 size_t ver_str_len = 0;  // Actual length
 static size_t write_char(char *ptr, size_t size, size_t nmemb, void **stream) {
-    printf("s = %zu, l = %zu\n", ver_str_size, ver_str_len);
     size_t old_len = ver_str_len;
     ver_str_len += size * nmemb;
-    printf("l++ = %zu\n", ver_str_len);
     if (ver_str_size < ver_str_len) {
         while (ver_str_size < ver_str_len + 2)
             ver_str_size = ver_str_size * 2 + 1;
-        printf("s++ = %zu\n", ver_str_size);
         *stream = realloc(*stream, ver_str_size);
+        if (!*stream)
+            err(1, "Failed to reallocate the (char *) stream while downloading something from the web, abort.\n");
     }
     memcpy(*stream + old_len, ptr, size*nmemb);
     return nmemb;
@@ -38,38 +37,36 @@ static size_t write_char(char *ptr, size_t size, size_t nmemb, void **stream) {
 //       https://curl.se/libcurl/c/url2file.html
 // returns 0 if good or 1 if that failed
 int DL_get_file(char *url, struct DL_stream *stream) {
-    CURL *curl;
+    CURL *curl = NULL;
     curl_off_t content_len = 0;
 
     CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
-    if(result != CURLE_OK) {
-        err(EXIT_FAILURE, "Failed to initialize CURL, Abort\n");
-        return 1;
-    }
+    if(result != CURLE_OK)
+        err(1, "Failed to initialize CURL, abort.\n");
 
     curl = curl_easy_init();
-    if(curl) {
-        curl_easy_setopt(curl, CURLOPT_URL, url);
-        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // To debug stuff
-        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); // We want to see the progress
-        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // We tell libcurl to follow redirection
-        curl_easy_setopt(curl, CURLOPT_USERAGENT, "thoryum/1.0");
-        if (stream->type == FILE_STREAM)
-                       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); // func to write some shit
-        else
-            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_char); // other func to write some shit
-        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &(stream->stream));
+    if (!curl)
+        err(1, "Failed to perform curl_easy_init, abort.\n");
+    curl_easy_setopt(curl, CURLOPT_URL, url);
+    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // To debug stuff
+    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); // We want to see the progress
+    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // We tell libcurl to follow redirection
+    curl_easy_setopt(curl, CURLOPT_USERAGENT, "thoryum/1.0");
+    if (stream->type == FILE_STREAM)
+               curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); // func to write some shit
+    else
+        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_char); // other func to write some shit
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &(stream->stream));
 
-        result = curl_easy_perform(curl);
-        if(result != CURLE_OK) {
-            err(EXIT_FAILURE, "Failed to perform download : '%s', Abort\n", curl_easy_strerror(result));
-            return 1;
-        }
+    result = curl_easy_perform(curl);
+    if (result != CURLE_OK)
+        err(1, "Failed to perform download : '%s', abort.\n", curl_easy_strerror(result));
 
-        result = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &content_len);
+    result = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &content_len);
+    if (result != CURLE_OK)
+        err(1, "Failed to gather file size, thats weird, abort.\n");
 
-        curl_easy_cleanup(curl);
-    }
+    curl_easy_cleanup(curl);
     curl_global_cleanup();
     return 0;
 }
@@ -83,13 +80,20 @@ char *DL_get_latest_version_url(char *suffix) {
     DL_get_file(THORIUM_RELEASE_API_URL, &buf);
     ((char *)(buf.stream))[ver_str_len-1] = '\0'; // to avoid some shitty overflow
 
-    printf("Received :\n%s\n", (char *)buf.stream);
     char *name = strstr(buf.stream, suffix); // We find the name in da json
+    if (!name)
+        err(1, "Failed to locate the .zip file in the latest release D: Aborting.\n");
     char *browser_url_start = strstr(name, THORIUM_RELEASE_BASE_URL);
+    if (!browser_url_start)
+        err(1, "Failed to locate the download url in the latest release D: Aborting.\n");
+
     size_t n = sizeof(THORIUM_RELEASE_BASE_URL);
-    while (browser_url_start[n] != '"')
+    while (browser_url_start[n] != '"' && browser_url_start[n] != '\0')
         n++;
-    char * browser_url = strndup(browser_url_start, n);
+
+    char *browser_url = strndup(browser_url_start, n);
+    if (!browser_url)
+        err(1, "Failed to allocate memory for the browser URL, abort.\n");
     free(buf.stream);
     return browser_url;
 }