From e20d0571d7d2ae274752d43010c4772d78683e72 Mon Sep 17 00:00:00 2001 From: Ayabusa Date: Tue, 9 Jun 2026 19:23:09 +0200 Subject: [PATCH] Added a unzip using libzip --- .gitignore | 2 ++ Makefile | 11 +++++--- README.txt | 9 ++++--- src/cli.c | 2 ++ src/cli.h | 3 ++- src/main.c | 69 ++++++++++++++++++++++++++++++++++++----------- src/unzip.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/unzip.h | 6 +++++ 8 files changed, 155 insertions(+), 24 deletions(-) create mode 100644 src/unzip.c create mode 100644 src/unzip.h diff --git a/.gitignore b/.gitignore index e76cd0c..a3e0286 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ *.desktop *.DESKTOP *.Desktop +/test* +*.zip diff --git a/Makefile b/Makefile index 9faa0d0..9336d74 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ -CFLAGS = -Wall -Wextra -g -LIBS = -lcurl -SRC = src/main.c src/cpu.c src/downloader.c src/cli.c -OBJ = build/main.o build/cpu.o build/downloader.o build/cli.o +CFLAGS = -Wall -Wextra -g -fsanitize=address +LIBS = -lcurl -lzip +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 all: setup $(OBJ) build @@ -19,6 +19,9 @@ build/downloader.o: src/downloader.c build/cli.o: src/cli.c $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) +build/unzip.o: src/unzip.c + $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) + setup: mkdir -p build diff --git a/README.txt b/README.txt index c5ec1ba..1565b8a 100644 --- a/README.txt +++ b/README.txt @@ -3,13 +3,13 @@ ▄▄▄█████▓ ██░ ██ ▒█████ ██▀███ ▓██ ██▓ █ ██ ███▄ ▄███▓ ▓ ██▒ ▓▒▓██░ ██▒▒██▒ ██▒▓██ ▒ ██▒▒██ ██▒ ██ ▓██▒▓██▒▀█▀ ██▒ ▒ ▓██░ ▒░▒██▀▀██░▒██░ ██▒▓██ ░▄█ ▒ ▒██ ██░▓██ ▒██░▓██ ▓██░ -░ ▓██▓ ░ ░▓█ ░██ ▒██ ██░▒██▀▀█▄ ░ ▐██▓░▓▓█ ░██░▒██ ▒██ +░ ▓██▓ ░ ░▓█ ░██ ▒██ ██░▒██▀▀█▄ ░ ▐██▓░▓▓█ ░██░▒██ ▒██ ▒██▒ ░ ░▓█▒░██▓░ ████▓▒░░██▓ ▒██▒ ░ ██▒▓░▒▒█████▓ ▒██▒ ░██▒ ▒ ░░ ▒ ░░▒░▒░ ▒░▒░▒░ ░ ▒▓ ░▒▓░ ██▒▒▒ ░▒▓▒ ▒ ▒ ░ ▒░ ░ ░ ░ ▒ ░▒░ ░ ░ ▒ ▒░ ░▒ ░ ▒░▓██ ░▒░ ░░▒░ ░ ░ ░ ░ ░ - ░ ░ ░░ ░░ ░ ░ ▒ ░░ ░ ▒ ▒ ░░ ░░░ ░ ░ ░ ░ - ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ - ░ ░ + ░ ░ ░░ ░░ ░ ░ ▒ ░░ ░ ▒ ▒ ░░ ░░░ ░ ░ ░ ░ + ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ + ░ ░ - By Ayabusa, 2026 ===== Making Thorium more at home on Linux ===== @@ -38,6 +38,7 @@ out of the box experience and an update wrapper. First you'll need to install the following libs: - Standart lib, should work with glibc or musl - Libcurl, to download from da web +- Libzip, to extract some shit TODO : write some shit about using it diff --git a/src/cli.c b/src/cli.c index 5643c76..0e4803b 100644 --- a/src/cli.c +++ b/src/cli.c @@ -46,6 +46,8 @@ struct ctx *init_ctx(int argc, char **argv) { void *free_ctx(struct ctx *ctx) { free(ctx->install_loc); + if (ctx->opti_name) + free(ctx->opti_name); free(ctx); return NULL; } diff --git a/src/cli.h b/src/cli.h index 8a36ff2..858b090 100644 --- a/src/cli.h +++ b/src/cli.h @@ -8,8 +8,9 @@ enum mode { struct ctx { enum mode mode; - char *install_loc; int silent; + char *install_loc; + char *opti_name; }; struct ctx *init_ctx(int argc, char **argv); diff --git a/src/main.c b/src/main.c index 42bf73c..dcaf5fd 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,6 +8,7 @@ #include "cpu.h" #include "downloader.h" #include "cli.h" +#include "unzip.h" #define SED_PATCH "s/WMClass=thorium/WMClass=thorium-browser/g" #define DESKTOP_FILE "thorium-portable.desktop" @@ -34,20 +36,57 @@ void patch_desktop_file(char *install_loc) { wait(NULL); } +void install(struct ctx *ctx) { + char *suffix = calloc(10, 1); + 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); +} + +void update(struct ctx *ctx) { + //TODO +} + int main(int argc, char **argv) { - printf("Hello this is Thoryum ;)\n"); - enum CPU_opti opti = CPU_fetch_opti(); - char opti_name[5] = {0}; - CPU_opti_name(opti_name, opti); - printf("You are using the CPU : '%s'\n", opti_name); - /* - FILE *file = fopen("page.out", "w"); - DL_get_file("https://ayabusa.dev", file); - fclose(file); - */ - //printf("Latest version url : '%s,'\n",DL_get_latest_version_url("AVX.zip")); - //patch_desktop_file("."); - struct ctx *ctx = init_ctx(argc, argv); - print_ctx(ctx); - ctx = free_ctx(ctx); + + zip_extract_to("thorium.zip", "test"); + return 0; + + /* + FILE *file = fopen("page.out", "w"); + DL_get_file("https://ayabusa.dev", file); + fclose(file); + */ + //printf("Latest version url : '%s,'\n",DL_get_latest_version_url("AVX.zip")); + //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}; + CPU_opti_name(opti_name, opti); + printf("Detected CPU opti : '%s'\n", opti_name); + ctx->opti_name = strdup(opti_name); + + if (!ctx->silent) { + printf("Are you sure you want to procede with the installation of Thorium ? [y/n] "); + char response; + scanf("%c", &response); + if (toupper(response) != 'Y') { + printf("Aborting.\n"); + exit(EXIT_FAILURE); + } + printf("Okkkkk lets go.\n"); + } + + if (ctx->mode == NORMAL) + install(ctx); + else + update(ctx); + + return 0; } diff --git a/src/unzip.c b/src/unzip.c new file mode 100644 index 0000000..c0a30f7 --- /dev/null +++ b/src/unzip.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "unzip.h" + +// Thank gosh I found this ref : https://gist.github.com/mobius/1759816 + +static void safe_create_dir(const char *dir) +{ + if (mkdir(dir, 0755) < 0) { + if (errno != EEXIST) { + perror(dir); + exit(1); + } + } +} + +int zip_extract_to(char *zip_path, char *extract_loc) { + struct zip *za; + int err; + char buf[100]; + + extract_loc = strdup(extract_loc); + int extract_loc_len = strlen(extract_loc); + if (extract_loc[extract_loc_len - 1] != '/') { + extract_loc = realloc(extract_loc, extract_loc_len + 2); + extract_loc[extract_loc_len] = '/'; + extract_loc[extract_loc_len + 1] = '\0'; + } + + if ((za = zip_open(zip_path, 0, &err)) == NULL) { + zip_error_to_str(buf, sizeof(buf), err, errno); + fprintf(stderr, "Failed to open zip archive `%s': %s\n", zip_path, buf); + return 1; + } + + for (int i = 0; i < zip_get_num_entries(za, 0); i++) { + zip_stat_t sb; + if (!zip_stat_index(za, i, 0, &sb)) { + int len = strlen(sb.name); + + char *name = calloc(1, extract_loc_len + len + 2); + strcpy(name, extract_loc); + strcat(name, sb.name); + + len = strlen(name); + + if (name[len - 1] == '/') + safe_create_dir(name); + else { + struct zip_file *zf = zip_fopen_index(za, i, 0); + + int fd = open(name, O_RDWR | O_TRUNC | O_CREAT, 0644); + + long long sum = 0; + while (sum != sb.size) { + len = zip_fread(zf, buf, 100); + write(fd, buf, len); + sum += len; + } + close(fd); + zip_fclose(zf); + } + free(name); + } else { + printf("Oopsie, Iv got some error while extracting the file %s\n", __FILE__); + } + } + free(extract_loc); + return zip_close(za) != 0; +} diff --git a/src/unzip.h b/src/unzip.h new file mode 100644 index 0000000..b040df5 --- /dev/null +++ b/src/unzip.h @@ -0,0 +1,6 @@ +#ifndef UNZIP_H +#define UNZIP_H + +int zip_extract_to(char *zip_path, char *extract_loc); + +#endif -- 2.43.0