Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
konect-V committed Sep 2, 2024
1 parent 201b5a1 commit 874a541
Show file tree
Hide file tree
Showing 25 changed files with 1,385 additions and 7 deletions.
136 changes: 136 additions & 0 deletions sources/core/apps/store-ui/source/apps/apps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>

#include "apps.h"

static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp){
fetch_apps_data_t* buffer_info = (fetch_apps_data_t*)userp;
size_t real_size = size * nmemb;

buffer_info->size += real_size;
buffer_info->buffer = realloc(buffer_info->buffer, buffer_info->size);

memcpy(&(buffer_info->buffer[buffer_info->size - real_size]), contents, real_size);

return real_size;
}

fetch_apps_data_t* fetch_apps_data(CURL* curl){
fetch_apps_data_t* buffer_info = malloc(sizeof(fetch_apps_data_t));
buffer_info->buffer = NULL;
buffer_info->size = 0;
curl_easy_setopt(curl, CURLOPT_URL, "https://kot-store.github.io/apps/");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer_info);
curl_easy_setopt(curl, CURLOPT_CAINFO, "/usr/etc/ssl/cert.pem");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
CURLcode res = curl_easy_perform(curl);

if(res != CURLE_OK){
fprintf(stderr, "Error : curl_%s\n", curl_easy_strerror(res));
free(buffer_info->buffer);
free(buffer_info);
return NULL;
}else{
return buffer_info;
}
}

char* find_apps_url_by_name(CURL* curl, char* name){
fetch_apps_data_t* data_info = fetch_apps_data(curl);
if(data_info != NULL){
cJSON* root = cJSON_Parse(data_info->buffer);
if(root != NULL){
cJSON* applications = cJSON_GetObjectItem(root, "applications");

for(int i = 0 ; i < cJSON_GetArraySize(applications); i++){
cJSON* application = cJSON_GetArrayItem(applications, i);
cJSON* application_name = cJSON_GetObjectItem(application, "name");
if(cJSON_IsString(application_name) && (application_name->valuestring != NULL)){
if(!strcmp(name, application_name->valuestring)){
cJSON* json_link_application_url = cJSON_GetObjectItem(application, "json_link");
if(cJSON_IsString(json_link_application_url) && (json_link_application_url->valuestring != NULL)){
char* return_value = malloc(strlen(json_link_application_url->valuestring) + 1);
strcpy(return_value, json_link_application_url->valuestring);
cJSON_Delete(root);

free(data_info->buffer);
free(data_info);

return return_value;
}
}
}
}

}

cJSON_Delete(root);

free(data_info->buffer);
free(data_info);
}

return NULL;
}

app_url_by_tag_t** find_apps_url_by_tag(CURL* curl, char* tag){
fetch_apps_data_t* data_info = fetch_apps_data(curl);
app_url_by_tag_t** return_value = NULL;

if(data_info != NULL){
cJSON* root = cJSON_Parse(data_info->buffer);
if(root != NULL){
cJSON* applications = cJSON_GetObjectItem(root, "applications");

size_t return_value_count_url = 0;
for(int i = 0 ; i < cJSON_GetArraySize(applications); i++){
cJSON* application = cJSON_GetArrayItem(applications, i);
cJSON* application_tags = cJSON_GetObjectItem(application, "tags");
cJSON* application_name = cJSON_GetObjectItem(application, "name");

if(cJSON_IsString(application_name) && (application_name->valuestring != NULL)){
for(int y = 0 ; y < cJSON_GetArraySize(application_tags); y++){
cJSON* application_tag = cJSON_GetArrayItem(application_tags, y);
if(cJSON_IsString(application_tag) && (application_tag->valuestring != NULL)){
if(!strcmp(tag, application_tag->valuestring)){
cJSON* json_link_application_url = cJSON_GetObjectItem(application, "json_link");
if(cJSON_IsString(json_link_application_url) && (json_link_application_url->valuestring != NULL)){
return_value = realloc(return_value, (return_value_count_url + 2) * sizeof(app_url_by_tag_t*));
return_value[return_value_count_url + 1] = NULL;
return_value[return_value_count_url] = malloc(sizeof(app_url_by_tag_t));
return_value[return_value_count_url]->name = malloc(strlen(application_name->valuestring) + 1);
strcpy(return_value[return_value_count_url]->name, application_name->valuestring);
return_value[return_value_count_url]->url = malloc(strlen(json_link_application_url->valuestring) + 1);
strcpy(return_value[return_value_count_url]->url, json_link_application_url->valuestring);
return_value_count_url++;
}
}
}
}
}
}
}

cJSON_Delete(root);

free(data_info->buffer);
free(data_info);
}

return return_value;
}

void free_app_url_by_tag(app_url_by_tag_t** data){
int i = 0;
while(data[i] != NULL){
free(data[i]->name);
free(data[i]->url);
free(data[i]);
i++;
}
free(data);
}
21 changes: 21 additions & 0 deletions sources/core/apps/store-ui/source/apps/apps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef APPS_H
#define APPS_H

#include <stddef.h>

typedef struct{
char* buffer;
size_t size;
}fetch_apps_data_t;

typedef struct{
char* url;
char* name;
}app_url_by_tag_t;

fetch_apps_data_t* fetch_apps_data(CURL* curl);
char* find_apps_url_by_name(CURL* curl, char* name);
app_url_by_tag_t** find_apps_url_by_tag(CURL* curl, char* tag);
void free_app_url_by_tag(app_url_by_tag_t** data);

#endif // APPS_H
128 changes: 128 additions & 0 deletions sources/core/apps/store-ui/source/core/core.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <curl/curl.h>

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

int main(int argc, char *argv[]){
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();

if(curl == NULL){
printf("Can't load curl");
return -1;
}else{
if(!strcmp(argv[1], "--install") && argc == 2){
char search_mode[3];
printf("Search with > T(tag)/N(name)\n");
fgets(search_mode, sizeof(search_mode), stdin);
search_mode[strcspn(search_mode, "\n")] = 0;
if(!strcmp("T", search_mode)){
char tag[512];
printf("What is the tag of the application you want to install?\n");
fgets(tag, sizeof(tag), stdin);
tag[strcspn(tag, "\n")] = 0;

app_url_by_tag_t** apps_available = find_apps_url_by_tag(curl, tag);

if(apps_available != NULL){
int i = 0;
while(apps_available[i] != NULL){
printf("%d) %s\n", i, apps_available[i]->name);
i++;
}

char link_index[10];
printf("Select the index you want to install :\n");
fgets(link_index, sizeof(link_index), stdin);
link_index[strcspn(link_index, "\n")] = 0;

int index_to_install = atoi(link_index);

if(index_to_install >= 0 && index_to_install < i){
char allow_install[3];
printf("%s > Would you like to install it? (Y/N)\n", apps_available[index_to_install]->name);
fgets(allow_install, sizeof(allow_install), stdin);
allow_install[strcspn(allow_install, "\n")] = 0;
if(!strcmp("Y", allow_install)){
install_app(curl, apps_available[index_to_install]->url, apps_available[index_to_install]->name, false);
}else{
printf("Cancel the installation\n");
}
}else{
printf("Unknow index !\n");
}

free_app_url_by_tag(apps_available);
}else{
printf("No application found with tag: %s. Please check the spelling or try a different tag.\n", tag);
}
}else if(!strcmp("N", search_mode)){
char name[512];
printf("What is the name of the application you want to install?\n");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;

char* url = find_apps_url_by_name(curl, name);

if(url != NULL){
char allow_install[3];
printf("%s found in the store. Would you like to install it? (Y/N)\n", name);
fgets(allow_install, sizeof(allow_install), stdin);
allow_install[strcspn(allow_install, "\n")] = 0;
if(!strcmp("Y", allow_install)){
install_app(curl, url, name, false);
}else{
printf("Cancel the installation\n");
}
free(url);
}else{
printf("Can't find %s in the store. Did you spell it correctly?\n", name);
}
}else{
printf("Unknow search method !\n");
}
}else if(!strcmp(argv[1], "--install") && argc == 3){
char* name = argv[2];

char* url = find_apps_url_by_name(curl, name);

if(url != NULL){
char allow_install[3];
printf("%s found in the store. Would you like to install it? (Y/N)\n", name);
fgets(allow_install, sizeof(allow_install), stdin);
allow_install[strcspn(allow_install, "\n")] = 0;
if(!strcmp("Y", allow_install)){
install_app(curl, url, name, false);
}else{
printf("Cancel the installation\n");
}
free(url);
}else{
printf("Can't find %s in the store. Did you spell it correctly?\n", name);
}
}else if(!strcmp(argv[1], "--update") && argc == 3){
char* name = argv[2];
update_app(curl, name);
}else if(!strcmp(argv[1], "--remove") && argc == 3){
char* name = argv[2];
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]);
}
}

curl_easy_cleanup(curl);

curl_global_cleanup();

return 0;
}
73 changes: 73 additions & 0 deletions sources/core/apps/store-ui/source/deps/deps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <cjson/cJSON.h>

#include "deps.h"

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

int check_dependencies(char* app_info_json_path, char** installation_file_url, char** executable_relative_path){
FILE* fp = fopen(app_info_json_path, "r");

if(fp == NULL){
printf("Error: Unable to open the file : %s\n", app_info_json_path);
return -2;
}

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);

int r = 0;

if(root != NULL){
cJSON* installation_file = cJSON_GetObjectItem(root, "installation_file");
if(cJSON_IsString(installation_file) && (installation_file->valuestring != NULL)){
*installation_file_url = malloc(strlen(installation_file->valuestring) + 1);
strcpy(*installation_file_url, installation_file->valuestring);
}
cJSON* executable_path = cJSON_GetObjectItem(root, "executable_path");
if(cJSON_IsString(executable_path) && (executable_path->valuestring != NULL)){
*executable_relative_path = malloc(strlen(executable_path->valuestring) + 1);
strcpy(*executable_relative_path, executable_path->valuestring);
return 0;
}

cJSON* dependencies = cJSON_GetObjectItem(root, "dependencies_file");
if(dependencies != NULL){
for(int i = 0 ; i < cJSON_GetArraySize(dependencies); i++){
cJSON* dependency = cJSON_GetArrayItem(dependencies, i);
if(cJSON_IsString(dependency) && (dependency->valuestring != NULL)){
if(!is_file_exists(dependency->valuestring)){
printf("Depency %s not found !\n", dependency->valuestring);
r = -1;
break;
}
}
}
}
}else{
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL){
printf("Error: %s\n", error_ptr);
}
r = -1;
}


cJSON_Delete(root);
free(buffer);
return r;
}
6 changes: 6 additions & 0 deletions sources/core/apps/store-ui/source/deps/deps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DEPS_H
#define DEPS_H

int check_dependencies(char* app_info_json_path, char** installation_file_url, char** executable_relative_path);

#endif // DEPS_H
Loading

0 comments on commit 874a541

Please sign in to comment.