Skip to content

Commit

Permalink
Update store and bug fix for fat32
Browse files Browse the repository at this point in the history
  • Loading branch information
konect-V committed Jun 22, 2024
1 parent ff951a6 commit e360240
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 120 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@
"apps.h": "c",
"download.h": "c",
"stdlib.h": "c",
"update.h": "c"
"update.h": "c",
"launch.h": "c",
"untar.h": "c"
},
"files.exclude": {
"bundled": true,
Expand Down
14 changes: 11 additions & 3 deletions sources/core/apps/store/source/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
#include <curl/curl.h>

#include "../apps/apps.h"
#include "../launch/launch.h"
#include "../update/update.h"
#include "../install/install.h"
#include "../uninstall/uninstall.h"
#include "../remove/remove.h"

void print_help(){
printf("For help: ./store --help\n");
printf("To install an application: ./store --install [app name]\n");
printf("To remove an application: ./store --remove [app name]\n");
printf("To update an application: ./store --update [app name]\n");
printf("To launch an application: ./store --launch [app name] [arguments]\n");
}

int main(int argc, char *argv[]){
Expand Down Expand Up @@ -116,9 +120,13 @@ int main(int argc, char *argv[]){
}else if(!strcmp(argv[1], "--update") && argc == 3){
char* name = argv[2];
update_app(curl, name);
}else if(!strcmp(argv[1], "--uninstall") && argc == 3){
}else if(!strcmp(argv[1], "--remove") && argc == 3){
char* name = argv[2];
uninstall_app(name);
remove_app(name, true);
}else if(!strcmp(argv[1], "--launch") && argc >= 3){
char* name = argv[2];
// Keep the app name
launch_app(name, argc - 2, &argv[2]);
}else{
print_help();
}
Expand Down
14 changes: 6 additions & 8 deletions sources/core/apps/store/source/install/install.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
Expand Down Expand Up @@ -26,16 +27,13 @@ int install_app(CURL* curl, char* url, char* name, bool reinstall){
char* path_store = getenv("PATHSTORE");
create_dir_if_not_exist(path_store);

char* path_store_app = malloc(strlen(path_store) + sizeof((char)'/') + strlen(name) + sizeof((char)'/') + 1);
strcpy(path_store_app, path_store);
strcat(path_store_app, "/");
strcat(path_store_app, name);
strcat(path_store_app, "/");
char* path_store_app;
assert(asprintf(&path_store_app, "%s/%s/", path_store, name) >= 0);

create_dir_if_not_exist(path_store_app);

char* path_store_app_info_json = malloc(strlen(path_store_app) + strlen("app-info.json") + 1);
strcpy(path_store_app_info_json, path_store_app);
strcat(path_store_app_info_json, "app-info.json");
char* path_store_app_info_json;
assert(asprintf(&path_store_app_info_json, "%sapp-info.json", path_store_app) >= 0);

if(download_file(curl, url, path_store_app_info_json, reinstall)){
printf("Error: Aborting installation of %s!\n", name);
Expand Down
96 changes: 96 additions & 0 deletions sources/core/apps/store/source/launch/launch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <stdio.h>
#include <assert.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <cjson/cJSON.h>

#include "launch.h"

static bool is_dir_exist(char* path){
struct stat sb;
return (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
}

static bool is_file_exists(char* path){
struct stat sb;
return (stat(path, &sb) == 0);
}

static int get_executable_path(char* app_info_path, char** relative_path){
FILE* fp = fopen(app_info_path, "r");

if(fp != NULL){
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
fseek(fp, 0, SEEK_SET);

void* buffer = malloc(size);
int len = fread(buffer, 1, size, fp);
fclose(fp);

cJSON* root = cJSON_Parse(buffer);
if(root != NULL){
cJSON* executable_path = cJSON_GetObjectItem(root, "executable_path");
if(executable_path != NULL){
if(cJSON_IsString(executable_path) && (executable_path->valuestring != NULL)){
*relative_path = malloc(strlen(executable_path->valuestring) + 1);
strcpy(*relative_path, executable_path->valuestring);

cJSON_Delete(root);
free(buffer);
return 0;
}
}
cJSON_Delete(root);
}

free(buffer);
return -1;
}else{
return -1;
}
}

int launch_app(char* name, int argc, char *argv[]){
char* path_store = getenv("PATHSTORE");
if(is_dir_exist(path_store)){
char* path_store_app;
assert(asprintf(&path_store_app, "%s/%s/", path_store, name) >= 0);

if(is_dir_exist(path_store_app)){
char* path_store_app_info_json;
assert(asprintf(&path_store_app_info_json, "%sapp-info.json", path_store_app) >= 0);

char* relative_path = NULL;
int result = get_executable_path(path_store_app_info_json, &relative_path);
free(path_store_app_info_json);

if(!result){
char* absolute_path = NULL;
assert(asprintf(&absolute_path, "%s%s", path_store_app, relative_path) >= 0);

printf("Launching : %s\n", absolute_path);
int r = execve(absolute_path, argv, NULL);
printf("Error when launching : %s, error code : %d\n", absolute_path, r);

free(relative_path);
free(absolute_path);
}

printf("Error when launching : %d\n", name);
free(path_store_app);

return -1;
}

free(path_store_app);
}

printf("Can't find the app named : %d\n", name);

return -1;
}
6 changes: 6 additions & 0 deletions sources/core/apps/store/source/launch/launch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef LAUNCH_H
#define LAUNCH_H

int launch_app(char* name, int argc, char *argv[]);

#endif // LAUNCH_H
98 changes: 98 additions & 0 deletions sources/core/apps/store/source/remove/remove.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <stdio.h>
#include <assert.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/stat.h>

#include "remove.h"

#define MAX_REMOVE_DIR 256

static bool is_dir_exist(char* path){
struct stat sb;
return (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
}

static bool check_user_allow(char* name){
char allow_remove[3];
printf("%s > Would you like to remove it? (Y/N)\n", name);
fgets(allow_remove, sizeof(allow_remove), stdin);
allow_remove[strcspn(allow_remove, "\n")] = 0;
return (!strcmp("Y", allow_remove));
}

static int remove_directory(char* path){
DIR* d = opendir(path);
if(!d){
return -1;
}

struct dirent* p;
int count = 0;
char* path_list[MAX_REMOVE_DIR];

while((p = readdir(d)) != NULL){
int r;
char* buffer;
size_t path_len = strlen(path);


if(asprintf(&path_list[count], "%s%s", path, p->d_name) >= 0){
count++;
if(count >= MAX_REMOVE_DIR){
remove_directory(path);
break;
}
}
}

closedir(d);

for(int i = 0; i < count; i++){
printf("Removing : %s\n", path_list[i]);
struct stat statbuf;
if(lstat(path_list[i], &statbuf) >= 0){
if(S_ISDIR(statbuf.st_mode)) {
remove_directory(path_list[i]);
}else{
remove(path_list[i]);
}
}
free(path_list[i]);
}

printf("Removing : %s\n", path);
return rmdir(path);
}

int remove_app(char* name, bool check_user){
char* path_store = getenv("PATHSTORE");
if(is_dir_exist(path_store)){
char* path_store_app;
assert(asprintf(&path_store_app, "%s/%s/", path_store, name) >= 0);

if(is_dir_exist(path_store_app)){
bool do_remove = true;

if(check_user){
do_remove = check_user_allow(name);
}

if(do_remove){
printf("Removing %s\n", name);
remove_directory(path_store_app);
printf("Done remove %s\n", name);
return 0;
}else{
printf("Cancel the removal of %s\n", name);
return -2;
}
}
}

printf("Can't find : ' %s\n", name);
return -1;
}
6 changes: 6 additions & 0 deletions sources/core/apps/store/source/remove/remove.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef REMOVE_H
#define REMOVE_H

int remove_app(char* name, bool check_user);

#endif // REMOVE_H
45 changes: 0 additions & 45 deletions sources/core/apps/store/source/uninstall/uninstall.c

This file was deleted.

6 changes: 0 additions & 6 deletions sources/core/apps/store/source/uninstall/uninstall.h

This file was deleted.

21 changes: 11 additions & 10 deletions sources/core/apps/store/source/update/update.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
Expand All @@ -8,7 +9,7 @@

#include "update.h"
#include "../install/install.h"
#include "../uninstall/uninstall.h"
#include "../remove/remove.h"

static bool is_dir_exist(char* path){
struct stat sb;
Expand Down Expand Up @@ -161,27 +162,25 @@ static int check_update(CURL* curl, char* app_info_path, char** url){
int update_app(CURL* curl, char* name){
char* path_store = getenv("PATHSTORE");
if(is_dir_exist(path_store)){
char* path_store_app = malloc(strlen(path_store) + sizeof((char)'/') + strlen(name) + sizeof((char)'/') + 1);
strcpy(path_store_app, path_store);
strcat(path_store_app, "/");
strcat(path_store_app, name);
strcat(path_store_app, "/");
char* path_store_app;
assert(asprintf(&path_store_app, "%s/%s/", path_store, name) >= 0);

if(is_dir_exist(path_store_app)){
char* path_store_app_info_json = malloc(strlen(path_store_app) + strlen("app-info.json") + 1);
strcpy(path_store_app_info_json, path_store_app);
strcat(path_store_app_info_json, "app-info.json");
char* path_store_app_info_json;
assert(asprintf(&path_store_app_info_json, "%sapp-info.json", path_store_app) >= 0);

char* url = NULL;
int result = check_update(curl, path_store_app_info_json, &url);

free(path_store_app_info_json);

if(result == 0){
printf("%s : is up-to-date\n", name);
}else if(result == 1 && url != NULL){
printf("%s : is not up-to-date\n", name);
printf("Updating...\n");
printf("Removing old files...\n");
uninstall_app(name);
remove_app(name, false);

printf("Installing new files...\n");
int r = install_app(curl, url, name, true);
Expand All @@ -196,6 +195,8 @@ int update_app(CURL* curl, char* name){
}else{
printf("%s : is not installed\n", name);
}

free(path_store_app);
}else{
printf("%s : is not installed\n", name);
}
Expand Down
Loading

0 comments on commit e360240

Please sign in to comment.