From: Ayabusa Date: Thu, 11 Jun 2026 16:44:43 +0000 (+0200) Subject: Some stuff working, needs a serious series of fixes X-Git-Url: https://git.ayabusa.dev/?a=commitdiff_plain;h=719b7e6f23995f1745de99db1609940e79736237;p=thoryum.git Some stuff working, needs a serious series of fixes --- diff --git a/Makefile b/Makefile index 9336d74..d1dd355 100644 --- 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 diff --git a/src/downloader.c b/src/downloader.c index 01e2762..45dc6b8 100644 --- a/src/downloader.c +++ b/src/downloader.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,14 +7,14 @@ #include #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); diff --git a/src/downloader.h b/src/downloader.h index d7a7e57..43e547d 100644 --- a/src/downloader.h +++ b/src/downloader.h @@ -1,6 +1,7 @@ #ifndef DOWNLOADER_H #define DOWNLOADER_H +#include #include enum DL_stream_type { diff --git a/src/main.c b/src/main.c index dcaf5fd..7ee0be1 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "cpu.h" #include "downloader.h" #include "cli.h" @@ -12,9 +14,16 @@ #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; } diff --git a/src/unzip.c b/src/unzip.c index c0a30f7..7b678b8 100644 --- a/src/unzip.c +++ b/src/unzip.c @@ -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) { diff --git a/src/unzip.h b/src/unzip.h index b040df5..cf5a6af 100644 --- a/src/unzip.h +++ b/src/unzip.h @@ -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