]> git.ayabusa.dev Git - thoryum.git/commitdiff
Some stuff working, needs a serious series of fixes
authorAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 16:44:43 +0000 (18:44 +0200)
committerAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 16:44:43 +0000 (18:44 +0200)
Makefile
src/downloader.c
src/downloader.h
src/main.c
src/unzip.c
src/unzip.h

index 9336d74c16e52c338074fcc23026e688e1258938..d1dd355fe71027a8943253f1ab1fb528d621e635 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,6 @@
-CFLAGS = -Wall -Wextra -g -fsanitize=address
-LIBS = -lcurl -lzip
+CC = musl-gcc
+CFLAGS = -Wall -Wextra -g -static -O3
+LIBS =  /usr/local/lib/libcurl.a /usr/local/lib/libzip.a -lzip -lssl -lcrypto -lz -lbz2 -llzma -lzstd -lnghttp2 
 SRC = src/main.c src/cpu.c src/downloader.c src/cli.c src/unzip.c
 OBJ = build/main.o build/cpu.o build/downloader.o build/cli.o build/unzip.o
 RES = build/Thoryum
index 01e27624f0e885b1398cd79dd08298d3c0d6fce4..45dc6b821c8f365dfa79ec8a12013e2ff6f9890c 100644 (file)
@@ -1,3 +1,4 @@
+#include <curl/system.h>
 #include <stddef.h>
 #include <curl/curl.h>
 #include <stdio.h>
@@ -6,14 +7,14 @@
 #include <err.h>
 #include "downloader.h"
 
-#define THORIUM_RELEASE_API_URL "https://api.github.com/repos/Alex313031/thorium/releases/latest"
-#define THORIUM_RELEASE_BASE_URL "https://github.com/Alex313031/thorium/releases/download"
+#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;
+    size_t written = fwrite(ptr, size, nmemb, (FILE *)*stream);
+    return written;
 }
 
 size_t ver_str_size = 1; // Malloced size
@@ -24,50 +25,53 @@ static size_t write_char(char *ptr, size_t size, size_t nmemb, void **stream) {
     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)
+        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);
     }
     memcpy(*stream + old_len, ptr, size*nmemb);
-       return nmemb;
+    return nmemb;
 }
 
 // Ref : https://curl.se/libcurl/c/simple.html
 //       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;
+    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;
-       }
+    CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
+    if(result != CURLE_OK) {
+        err(EXIT_FAILURE, "Failed to initialize CURL, Abort\n");
+        return 1;
+    }
+
+    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));
 
-       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));
+        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(EXIT_FAILURE, "Failed to perform download : '%s', Abort\n", curl_easy_strerror(result));
-                       return 1;
-               }
+        result = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &content_len);
 
-               curl_easy_cleanup(curl);
-       }
-       curl_global_cleanup();
-       return 0;
+        curl_easy_cleanup(curl);
+    }
+    curl_global_cleanup();
+    return 0;
 }
 
 // Get the latest version url according to it's suffix : AVX.zip
@@ -77,6 +81,8 @@ char *DL_get_latest_version_url(char *suffix) {
     ver_str_size = 1;
     ver_str_len = 0;
     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
     char *browser_url_start = strstr(name, THORIUM_RELEASE_BASE_URL);
index d7a7e572318d52475c04ef2a119b10a0b830733f..43e547db0a53b56e9ad7f8e18778be2cb53a7fa3 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef DOWNLOADER_H
 #define DOWNLOADER_H
 
+#include <curl/system.h>
 #include <stdio.h>
 
 enum DL_stream_type {
index dcaf5fd17dcb4d8c50a1a9cfd3498f499262110f..7ee0be1857779d037b1f313a923bb54415db4b12 100644 (file)
@@ -5,6 +5,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/wait.h>
+#include <sys/stat.h>
+#include <pwd.h>
 #include "cpu.h"
 #include "downloader.h"
 #include "cli.h"
 
 #define SED_PATCH "s/WMClass=thorium/WMClass=thorium-browser/g"
 #define DESKTOP_FILE "thorium-portable.desktop"
+#define TMP_LOC "/tmp/thorium.zip"
+#define INSTALL_LOC ".local/thorium"
+#define LINK_PATH ".local/share/applications/thorium-browser.desktop"
+
+const char *get_home_dir() { // feels like a hack ngl
+    return getpwuid(getuid())->pw_dir;
+}
 
 // For the icon theming to work properly we need to patch the desktop file
-void patch_desktop_file(char *install_loc) {
+char *patch_desktop_file(char *install_loc) {
     char *file_path = strdup(install_loc);
     size_t len = strlen(file_path);
     file_path = realloc(file_path, len + sizeof(DESKTOP_FILE) + 2);
@@ -32,7 +41,16 @@ void patch_desktop_file(char *install_loc) {
         execlp("sed", "sed", "-i", SED_PATCH, file_path, NULL); // I assume sed is installed, TODO verify before install
         exit(0);
     }
-    free(file_path);
+    wait(NULL);
+    return file_path;
+}
+
+void register_desktop(char *file_path) {
+    pid_t pid = fork();
+    if (!pid) { // child
+        execlp("xdg-desktop-menu", "xdg-desktop-menu", "install", file_path, NULL); // I assume xdg is installed, TODO verify before install
+        exit(0);
+    }
     wait(NULL);
 }
 
@@ -41,8 +59,38 @@ void install(struct ctx *ctx) {
     strcpy(suffix, ctx->opti_name);
     strcat(suffix, ".zip");
     char *url = DL_get_latest_version_url(suffix);
-    printf("Downloading the latest Thorium release from :\n%s\n", url);
     free(suffix);
+
+    printf("Downloading the latest Thorium release from :\n%s\n", url);
+    FILE *file = fopen(TMP_LOC, "w");
+    struct DL_stream stream = {.type = FILE_STREAM, .stream = file};
+    DL_get_file(url, &stream);
+    fclose(file);
+
+    char *install_loc = strdup(get_home_dir());
+    int install_loc_len = strlen(install_loc);
+    install_loc = realloc(install_loc, install_loc_len + sizeof(INSTALL_LOC) + 2);
+    if (install_loc[install_loc_len - 1] != '/') {
+        install_loc[install_loc_len] = '/';
+        install_loc[install_loc_len + 1] = '\0';
+    }
+    strcat(install_loc, INSTALL_LOC);
+    printf("Extracting to %s\n", install_loc);
+    safe_create_dir(install_loc);
+    zip_extract_to(TMP_LOC, install_loc);
+
+    printf("Removing temporary archive\n");
+    remove(TMP_LOC);
+
+    printf("Patching .desktop file\n");
+    char *desktop_path = patch_desktop_file(install_loc);
+
+    printf("Register that shit up\n");
+    register_desktop(desktop_path);
+
+    free(install_loc);
+    free(desktop_path);
+    printf("Cya gang ;D\n");
 }
 
 void update(struct ctx *ctx) {
@@ -50,10 +98,6 @@ void update(struct ctx *ctx) {
 }
 
 int main(int argc, char **argv) {
-
-    zip_extract_to("thorium.zip", "test");
-    return 0;
-
     /*
         FILE *file = fopen("page.out", "w");
         DL_get_file("https://ayabusa.dev", file);
@@ -63,11 +107,10 @@ int main(int argc, char **argv) {
     //patch_desktop_file(".");
     struct ctx *ctx = init_ctx(argc, argv);
     print_ctx(ctx);
-    ctx = free_ctx(ctx);
 
     // We get da CPU shit
     enum CPU_opti opti = CPU_fetch_opti();
-    char opti_name[5] = {0};
+    char opti_name[6] = {0};
     CPU_opti_name(opti_name, opti);
     printf("Detected CPU opti : '%s'\n", opti_name);
     ctx->opti_name = strdup(opti_name);
@@ -88,5 +131,6 @@ int main(int argc, char **argv) {
     else
         update(ctx);
 
+    free_ctx(ctx);
     return 0;
 }
index c0a30f7076ede5f22f8ba8002ad0eb8666880055..7b678b8cd9045ff1264ba85d3e051c35593d71a5 100644 (file)
@@ -11,7 +11,7 @@
 
 // Thank gosh I found this ref : https://gist.github.com/mobius/1759816
 
-static void safe_create_dir(const char *dir)
+void safe_create_dir(const char *dir)
 {
     if (mkdir(dir, 0755) < 0) {
         if (errno != EEXIST) {
index b040df5a6d7c53973aa3294cfc3a96caedbd6fb3..cf5a6afa23fce7154cbb77c849cd06989ae0788b 100644 (file)
@@ -2,5 +2,6 @@
 #define UNZIP_H
 
 int zip_extract_to(char *zip_path, char *extract_loc);
+void safe_create_dir(const char *dir);
 
 #endif