Skip to content

Commit

Permalink
fix #89
Browse files Browse the repository at this point in the history
add a bash-skript, that install the fastboot-assitant. The bash-skript kill the fastboot-assistant and install the update.
  • Loading branch information
NachtsternBuild authored Dec 12, 2024
1 parent 47bed3d commit 09fe638
Showing 1 changed file with 172 additions and 111 deletions.
283 changes: 172 additions & 111 deletions Anwendung/updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
*-------------------------------------------*
* Projekt 122 - GUI *
*-------------------------------------------*
* Apache License, Version 2.0 *
* Apache License, Version 2.0 *
*-------------------------------------------*
* *
* Programm, um das Installieren von *
* Custom-ROMs und GSIs auf Android-Geräte *
* zu erleichtern - updater *
* Programm, um das Installieren von *
* Custom-ROMs und GSIs auf Android-Geräte *
* zu erleichtern - updater *
* *
*-------------------------------------------*
* (C) Copyright 2024 Elias Mörz *
* (C) Copyright 2024 Elias Mörz *
*-------------------------------------------*
*
*/
Expand All @@ -23,57 +23,137 @@
#include "program_functions.h"

char output_file[2048];
char deb_on_wsl[2048];
char install_command[2048];
char output_path[2048];
char unzip_command[2048];
char cd_command[2048];
char output_path[2048];
char wsl_dir[2048];
char package_url[2048];
char bash_script_path[2048];
GtkWidget *update_window_install;
GtkWidget *info_button;

static void install_wsl(GtkButton *button)
{
g_print("Log: install_wsl\n");
get_wsl_directory(wsl_dir, sizeof(wsl_dir));
g_print("Log: Directory: %s\n", wsl_dir);

snprintf(output_file, sizeof(output_file), "%s/fastboot-assistant.zip", wsl_dir);
snprintf(output_path, sizeof(output_path), "%s/ROM-Install", wsl_dir);
snprintf(deb_on_wsl, sizeof(deb_on_wsl), "%s/fastboot-assistant.deb", output_path);
// Function to quit the application - updater
gboolean quit_application_updater(gpointer data)
{
exit(1);
return FALSE; // Return FALSE to ensure the timeout callback is called only once
}

// Function to create and execute the Bash script
void create_and_run_bash_script(const char *package_url, const char *package_type)
{
const char *home_dir = getenv("HOME");
if (!home_dir)
{
fprintf(stderr, "Log: HOME directory not found.\n");
exit(EXIT_FAILURE);
}

g_print("Log: Package downloaded: %s\n", output_file);
// get the user
const char *user = getenv("USER");
if (user == NULL)
{
g_print("Log: Error: Could not determine the user name.\n");
exit(1); // close the program if there are errors
}

snprintf(unzip_command, sizeof(unzip_command), "unzip %s -d %s", output_file, output_path);
g_print("Log: Run: %s\n", unzip_command);
system(unzip_command);
char wsl_setup_base[2048];
char updater_path[2048];
char mkdir_command[2048];
snprintf(wsl_setup_base, sizeof(wsl_setup_base), "/mnt/c/Users/%s", user);

// Create directory for updater
// for linux
snprintf(updater_path, sizeof(updater_path), "%s/Downloads/ROM-Install/Updater", home_dir);

// for wsl
//snprintf(updater_path, sizeof(updater_path), "%s/Downloads/ROM-Install/Updater", wsl_setup_base);

snprintf(cd_command, sizeof(cd_command), "cd %s", output_path);
g_print("Log: Run: %s\n", cd_command);
system(cd_command);
snprintf(install_command, sizeof(install_command), "dpkg -i %s && rm -f %s", deb_on_wsl, deb_on_wsl);
install_with_pkexec(install_command);
gtk_window_destroy(GTK_WINDOW(update_window_install));
g_print("Log: end install_wsl\n");
}
// create ~/ROM-Install/Updater
snprintf(mkdir_command, sizeof(mkdir_command), "mkdir -p %s", updater_path);
if (system(mkdir_command) != 0)
{
fprintf(stderr, "Log: Failed to create directory.\n");
exit(EXIT_FAILURE);
}

static void install_rpm(GtkButton *button)
{
g_print("Log: install_rpm\n");
snprintf(install_command, sizeof(install_command), "rpm -U %s && rm -f %s", output_file, output_file);
install_with_pkexec(install_command);
gtk_window_destroy(GTK_WINDOW(update_window_install));
g_print("Log: end install_rpm\n");
}
// Path to the Bash script
snprintf(bash_script_path, sizeof(bash_script_path), "%s/install_update.sh", updater_path);

static void install_deb(GtkButton *button)
{
g_print("Log: install_deb\n");
snprintf(install_command, sizeof(install_command), "dpkg -i %s && rm -f %s", output_file, output_file);
install_with_pkexec(install_command);
gtk_window_destroy(GTK_WINDOW(update_window_install));
g_print("Log: end install_deb\n");
// Write Bash script content
FILE *script_file = fopen(bash_script_path, "w");
if (!script_file)
{
fprintf(stderr, "Log: Failed to create the Bash script.\n");
exit(EXIT_FAILURE);
}
const char *package = ".deb";
//const char *package = ".zip";
g_print("Log: %s\n", package);
g_print("Log: %s\n", package_url);
g_print("Log: %s\n", package_type);

fprintf(script_file,
"#!/bin/bash\n"
"UPDATER_PID=%d\n"
"PACKAGE_URL=\"%s\"\n"
"PACKAGE_TYPE=\"%s\"\n"
"kill -9 $UPDATER_PID\n"
"echo \"Starting update process...\"\n"
"curl -L \"$PACKAGE_URL\" -o ~/Downloads/ROM-Install/Updater/fastboot-assistant$PACKAGE_TYPE\n"
"if [[ \"$PACKAGE_TYPE\" == \".deb\" ]]; then\n"
" sudo dpkg -i ~/Downloads/ROM-Install/Updater/fastboot-assistant$PACKAGE_TYPE\n"
"else\n"
" echo \"Unsupported package type: $PACKAGE_TYPE\"\n"
" exit 1\n"
"fi\n"
"echo \"Update completed.\"\n",
getpid(), package_type, package);

// the skript for WSL
/*
fprintf(script_file,
"#!/bin/bash\n\n"
"UPDATER_PID=%d\n"
"PACKAGE_URL=\"%s\"\n"
"PACKAGE_TYPE=\"%s\"\n"
"USER_NAME=$(whoami)\n"
"WSL_DIR=\"/mnt/c/Users/$USER_NAME\"\n"
"OUTPUT_PATH=\"$WSL_DIR/ROM-Install/Updater\"\n"
"OUTPUT_FILE=\"$OUTPUT_PATH/fastboot-assistant.zip\"\n"
"DEB_ON_WSL=\"$OUTPUT_PATH/fastboot-assistant.deb\"\n\n"
"UNZIP_COMMAND=\"unzip \\\"$OUTPUT_FILE\\\" -d \\\"$OUTPUT_PATH\\\"\"\n"
"kill -9 $UPDATER_PID\n"
"echo \"Starting update process...\"\n"
"curl -L \"$PACKAGE_URL\" -o \"$OUTPUT_FILE\"\n"
"if ! eval $UNZIP_COMMAND; then\n"
" echo \"Error: Failed to unzip $OUTPUT_FILE\"\n"
" exit 1\n"
"fi\n\n"
"cd \"$OUTPUT_PATH\" || { echo \"Error: Failed to change directory to $OUTPUT_PATH\"; exit 1; }\n\n"
"INSTALL_COMMAND=\"sudo dpkg -i \\\"$DEB_ON_WSL\\\" \"\n"
"if ! eval $INSTALL_COMMAND; then\n"
" echo \"Error: Failed to install $DEB_ON_WSL\"\n"
" exit 1\n"
"fi\n\n"
"echo \"Update completed.\"\n",
getpid(), package_type, package);
*/
// close file
fclose(script_file);

// Set executable permissions
char chmod_command[2048];
snprintf(chmod_command, sizeof(chmod_command), "chmod a+x %s", bash_script_path);
if (system(chmod_command) != 0)
{
fprintf(stderr, "Log: Failed to set executable permissions.\n");
exit(EXIT_FAILURE);
}

char cat_command[2048];
snprintf(cat_command, sizeof(cat_command), "cat %s", bash_script_path);
g_print("Log: Run: %s\n", cat_command);
system(cat_command);

// Run the script in a new terminal
open_terminal_by_desktop(bash_script_path);
}

// function to extract the version from the url
Expand Down Expand Up @@ -143,36 +223,30 @@ int verify_package_type(const char *filepath, const char *expected_extension)
}

/* main function of the updater */
void updater(void)
void updater(void)
{
g_print("Log: updater\n");

gtk_init();
main_loop = g_main_loop_new(NULL, FALSE);
apply_theme();
apply_language();


/* please change the package type for the different packages → .deb or .zip*/
const char *repo = "NachtsternBuild/fastboot-assistant";
char download_url[2048];
/* please change the package type for the different packages → .rpm or .deb or .zip*/
const char *package_type = ".deb";
// const char *package_type = ".rpm";
// const char *package_type =".zip";
const char *package_type = ".deb";
//const char *package_type = ".zip";

get_latest_release_url(repo, package_type, download_url, sizeof(download_url));
get_latest_release_url(repo, package_type, package_url, sizeof(package_url));

if (strlen(download_url) > 0)
if (strlen(package_url) > 0)
{
g_print("Log: Latest version URL: %s\n", download_url);
g_print("Log: Latest version URL: %s\n", package_url);

const char* version = extract_version_from_url(download_url);
const char* version = extract_version_from_url(package_url);
if (version)
{
g_print("Log: Extracted version: %s\n", version);

char version_message[256];
snprintf(version_message, sizeof(version_message), g_strcmp0(language, "de") == 0 ? "Gefundene Version: %s" : "Found version: %s", version);
show_message(version_message);
}

else
Expand All @@ -199,65 +273,53 @@ void updater(void)
snprintf(wsl_setup_base, sizeof(wsl_setup_base), "/mnt/c/Users/%s", user);

// for linux
snprintf(output_file, sizeof(output_file), "%s/Downloads/fastboot-assistant%s", output_directory, package_type);
snprintf(output_file, sizeof(output_file), "%s/Downloads/ROM-Install/Updater/fastboot-assistant%s", output_directory, package_type);

// for wsl
// snprintf(output_file, sizeof(output_file), "%s/Downloads/fastboot-assistant%s", wsl_setup_base, package_type);
// snprintf(output_file, sizeof(output_file), "%s/Downloads/ROM-Install/Updater/fastboot-assistant%s", wsl_setup_base, package_type);

snprintf(output_file, sizeof(output_file), "%s/Downloads/fastboot-assistant%s", output_directory, package_type);

if (download_file(download_url, output_file) == 0)
snprintf(output_file, sizeof(output_file), "%s/Downloads/ROM-Install/Updater/fastboot-assistant%s", output_directory, package_type);

g_print("Log: Package downloaded: %s\n", output_file);
if (!verify_package_type(output_file, package_type))
{
g_print("Log: Package downloaded: %s\n", output_file);
if (!verify_package_type(output_file, package_type))
{
fprintf(stderr, "Log: Error: The downloaded package is not a %s package.\n", package_type);
exit(EXIT_FAILURE);
}
fprintf(stderr, "Log: Error: The downloaded package is not a %s package.\n", package_type);
exit(EXIT_FAILURE);
}

const char *confirmation = strcmp(language, "de") == 0 ? "Bestätigung" : "Confirmation";
GtkWidget *confirmation_window = gtk_window_new();
gtk_window_set_title(GTK_WINDOW(confirmation_window), confirmation);
const char *confirmation = strcmp(language, "de") == 0 ? "Bestätigung" : "Confirmation";
GtkWidget *confirmation_window = gtk_window_new();
gtk_window_set_title(GTK_WINDOW(confirmation_window), confirmation);

gtk_window_set_default_size(GTK_WINDOW(confirmation_window), 400, 350);
g_signal_connect(confirmation_window, "destroy", G_CALLBACK(on_window_destroy), NULL);
gtk_window_set_default_size(GTK_WINDOW(confirmation_window), 500, 500);
g_signal_connect(confirmation_window, "destroy", G_CALLBACK(on_window_destroy), NULL);

GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_window_set_child(GTK_WINDOW(confirmation_window), vbox);
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_window_set_child(GTK_WINDOW(confirmation_window), vbox);

GtkWidget *message_label = gtk_label_new(g_strcmp0(language, "de") == 0 ? "Das Paket wurde heruntergeladen. \nMöchten Sie es jetzt installieren?" : "The package has been downloaded. \nWould you like to install it now?");
gtk_box_append(GTK_BOX(vbox), message_label);
GtkWidget *message_label = gtk_label_new(g_strcmp0(language, "de") == 0 ? "\n\nMöchten sie das Update installieren?\n\n" : "\n\nWould you like to install the update?\n\n");
gtk_box_append(GTK_BOX(vbox), message_label);

// Version message einfügen
char version_message[256];
snprintf(version_message, sizeof(version_message), g_strcmp0(language, "de") == 0 ? "Gefundene Version: %s\n\n" : "Found version: %s\n\n", version);

GtkWidget *confirm_button = gtk_button_new_with_label(g_strcmp0(language, "de") == 0 ? "Installieren" : "Install");
gtk_box_append(GTK_BOX(vbox), confirm_button);
GtkWidget *version_label = gtk_label_new(version_message);
gtk_box_append(GTK_BOX(vbox), version_label);

if (strcmp(package_type, ".deb") == 0)
{
g_signal_connect(confirm_button, "clicked", G_CALLBACK(install_deb), NULL);
}

else if (strcmp(package_type, ".rpm") == 0)
{
g_signal_connect(confirm_button, "clicked", G_CALLBACK(install_rpm), NULL);
}

else if (strcmp(package_type, ".zip") == 0)
{
g_signal_connect(confirm_button, "clicked", G_CALLBACK(install_wsl), NULL);
}
GtkWidget *confirm_button = gtk_button_new_with_label(g_strcmp0(language, "de") == 0 ? "Installieren" : "Install");
gtk_box_append(GTK_BOX(vbox), confirm_button);

g_signal_connect(confirm_button, "clicked", G_CALLBACK(create_and_run_bash_script), package_url);

GtkWidget *cancel_button = gtk_button_new_with_label(g_strcmp0(language, "de") == 0 ? "Später Installieren" : "Install later");
gtk_box_append(GTK_BOX(vbox), cancel_button);
g_signal_connect(cancel_button, "clicked", G_CALLBACK(close_window_mainloop), confirmation_window);
GtkWidget *cancel_button = gtk_button_new_with_label(g_strcmp0(language, "de") == 0 ? "Später Installieren" : "Install later");
gtk_box_append(GTK_BOX(vbox), cancel_button);
g_signal_connect(cancel_button, "clicked", G_CALLBACK(close_window_mainloop), confirmation_window);

// show all widgets
gtk_window_present(GTK_WINDOW(confirmation_window)); // gtk_window_present instead of gtk_widget_show
}
else
{
fprintf(stderr, "Log: Error downloading the file.\n");
}
}
// show all widgets
gtk_window_present(GTK_WINDOW(confirmation_window)); // gtk_window_present instead of gtk_widget_show
}

else
{
fprintf(stderr, "Log: No release URL found.\n");
Expand All @@ -281,4 +343,3 @@ void updater(void)

g_print("Log: end updater\n");
}

0 comments on commit 09fe638

Please sign in to comment.