-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
342 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.