+#include <curl/system.h>
#include <stddef.h>
#include <curl/curl.h>
#include <stdio.h>
#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
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
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);
#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);
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);
}
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) {
}
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);
//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);
else
update(ctx);
+ free_ctx(ctx);
return 0;
}