From d53109eaa890ab807b66961a89291cea3cd3c003 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 18 Aug 2020 23:58:16 +0200 Subject: first part of a proper lutris implementation and cleanup - remove all old build platform related variables - change behavior of clean - change tabs into spaces - make XDG fetching method static - replace strcpy and cat with strn alternative with proper bounds checking - add cache dir - reenable dxvk and download from ram - completely rework lutris fetching and convert it into an interal struct - only show http errors in debug - add sanity checks to methods with possible NULL return - change extracting methods to extract tar from ram --- src/common.h | 10 +- src/config.c | 19 ++- src/config.h | 5 +- src/dxvk.c | 23 ++- src/lutris.c | 490 +++++++++++++++++++++++++++++++++++++++++------------------ src/lutris.h | 143 +++++++++++------ src/main.c | 15 +- src/net.c | 18 ++- src/net.h | 1 + src/tar.c | 10 +- src/tar.h | 2 +- src/wine.c | 33 ++-- 12 files changed, 539 insertions(+), 230 deletions(-) (limited to 'src') diff --git a/src/common.h b/src/common.h index 3b071cc..fba45cd 100644 --- a/src/common.h +++ b/src/common.h @@ -15,14 +15,14 @@ #define USAGE_STR "Usage: " NAME struct MemoryStruct { - uint8_t* memory; - size_t size; + uint8_t* memory; + size_t size; }; struct Command { - char* name; - int (*func)(int, char**); - char* description; + char* name; + int (*func)(int, char**); + char* description; }; void print_help(const struct Command*, size_t); diff --git a/src/config.c b/src/config.c index f691648..b728e10 100644 --- a/src/config.c +++ b/src/config.c @@ -7,7 +7,7 @@ #include "config.h" #include "common.h" -void getXDGDir(const char* envvar, const char* homeext, char* config) +static void getXDGDir(const char* envvar, const char* homeext, char* config, const size_t size) { char* xdg_var = getenv(envvar); @@ -18,19 +18,24 @@ void getXDGDir(const char* envvar, const char* homeext, char* config) else { char* home = getenv("HOME"); - strcpy(config, home); - strcat(config, homeext); + strncpy(config, home, size); + strncat(config, homeext, size - strlen(config)); } } -void getConfigDir(char* config) +void getConfigDir(char* config, const size_t size) { - getXDGDir("XDG_CONFIG_HOME", "/.config/" NAME, config); + getXDGDir("XDG_CONFIG_HOME", "/.config/" NAME, config, size); } -void getDataDir(char* config) +void getDataDir(char* config, const size_t size) { - getXDGDir("XDG_DATA_HOME", "/.local/share/" NAME, config); + getXDGDir("XDG_DATA_HOME", "/.local/share/" NAME, config, size); +} + +void getCacheDir(char* config, const size_t size) +{ + getXDGDir("XDG_CACHE_HOME", "/.cache/" NAME, config, size); } void makeDir(const char* path) diff --git a/src/config.h b/src/config.h index cba95d8..07f0dc6 100644 --- a/src/config.h +++ b/src/config.h @@ -3,8 +3,9 @@ #include -void getConfigDir(char* config); -void getDataDir(char* config); +void getConfigDir(char*, const size_t); +void getDataDir(char*, const size_t); +void getCacheDir(char*, const size_t); void makeDir(const char* path); diff --git a/src/dxvk.c b/src/dxvk.c index c5ac1d0..3ef429c 100644 --- a/src/dxvk.c +++ b/src/dxvk.c @@ -2,10 +2,13 @@ #include #include #include +#include #include "dxvk.h" #include "net.h" +#include "tar.h" #include "common.h" +#include "config.h" const static struct Command dxvk_commands[] = { { .name = "install", .func = dxvk_install, .description = "download and install a dxvk version" }, @@ -52,11 +55,27 @@ int dxvk_install(int argc, char** argv) json_object_object_get_ex(version, "browser_download_url", &assets); char* name = basename((char*)json_object_get_string(assets)); + struct MemoryStruct* tar; + + char datadir[PATH_MAX]; + getDataDir(datadir, sizeof(datadir)); + makeDir(datadir); printf("Downloading %s", json_object_get_string(assets)); - downloadFile(json_object_get_string(assets), name); - printf("\nDone\n"); + //downloadFile(json_object_get_string(assets), name); + tar = downloadToRam(json_object_get_string(assets)); + + printf("Extracting %s\n", name); + + extract(tar, datadir); + + printf("Done\n"); + + free(tar->memory); + free(tar); } + + json_object_put(runner); } } else diff --git a/src/lutris.c b/src/lutris.c index 4adaa81..beb8384 100644 --- a/src/lutris.c +++ b/src/lutris.c @@ -28,191 +28,393 @@ int lutris(int argc, char** argv) int lutris_install(int argc, char** argv) { - + return 0; } int lutris_info(int argc, char** argv) { if (argc == 2) + { + struct script installer = lutris_getInstaller(argv[1]); + if (installer.error != NO_SLUG) + { + + printf("Name: %s\n" + "Version: %s\n" + "Runner: %i\n" + "Wine: %s\n" + "Error %i\n" + "Files: %zu\n" + "Directives: %zu\n", + installer.name, + installer.version, + installer.runner, + installer.wine, + installer.error, + installer.filecount, + installer.directivecount); + + if (installer.filecount) + { + puts("\nFiles:"); + for (int i = 0; i < installer.filecount; ++i) + { + printf("\t%s\t->\t%s\n", installer.files[i]->filename, installer.files[i]->url); + } + } + + if (installer.directivecount) + { + + puts("\nDirectives:"); + for (int i = 0; i < installer.directivecount; ++i) + { + printf("\t%s", keywordstr[installer.directives[i]->command]); + + if (installer.directives[i]->task != NO_TASK) printf(" %s", taskKeywordstr[installer.directives[i]->task]); + + for (int j = 0; j < installer.directives[i]->size; ++j) + { + printf(" %s", installer.directives[i]->arguments[j]); + } + + puts(""); + } + } + } + else + { + + } + + lutris_freeInstaller(&installer); + } + return 0; +} + +int lutris_help(int argc, char** argv) +{ + puts(USAGE_STR " lutris \n\nList of commands:"); + + print_help(lutris_commands, ARRAY_LEN(lutris_commands)); + + return 0; +} + +void lutris_getInstallerURL(char* buffer, char* name, size_t size) +{ + strncpy(buffer, INSTALLER_API, size); + strncat(buffer, name, size - strlen(buffer)); +} + +struct script lutris_getInstaller(char* installername) +{ + struct script installer; + installer.name = NULL; + installer.version = NULL; + installer.runner = UNKNOWN_RUNNER; + installer.description = NULL; + installer.notes = NULL; + installer.wine = NULL; + installer.directives = NULL; + installer.directivecount = 0; + installer.files = NULL; + installer.filecount = 0; + installer.error = NONE; + + if (installername) { char installerurl[PATH_MAX]; - lutris_getInstallerURL(installerurl, argv[1]); + lutris_getInstallerURL(installerurl, installername, sizeof(installerurl)); - struct json_object* installer = fetchJSON(installerurl); + struct json_object* installerjson = fetchJSON(installerurl); - if (installer) + if (installerjson) { struct json_object* count, *results, *slug; - json_object_object_get_ex(installer, "count", &count); - json_object_object_get_ex(installer, "results", &results); + json_object_object_get_ex(installerjson, "count", &count); + json_object_object_get_ex(installerjson, "results", &results); slug = json_object_array_get_idx(results, 0); - if (json_object_get_int(count) != 0) + if (json_object_get_int(count) == 1) { - struct json_object* name, *version, *runner, *description, *notes, *script, *scriptinstall, *wine, *winever; - json_object_object_get_ex(slug, "name", &name); - json_object_object_get_ex(slug, "version", &version); - json_object_object_get_ex(slug, "runner", &runner); - json_object_object_get_ex(slug, "description", &description); - json_object_object_get_ex(slug, "notes", ¬es); - json_object_object_get_ex(slug, "script", &script); - json_object_object_get_ex(script, "installer", &scriptinstall); + struct json_object* script, *scriptinstall, *files; + + if(json_object_object_get_ex(slug, "script", &script)) + { + { + struct json_object* name, *version, *runner, *description, *notes, *wine, *winever; + const char* namestr, *versionstr, *runnerstr, *descriptionstr, *notesstr, *winestr; + + json_object_object_get_ex(slug, "name", &name); + namestr = json_object_get_string(name); + installer.name = malloc( strlen(namestr) * sizeof(char) +1 ); + strcpy(installer.name, namestr); + + json_object_object_get_ex(slug, "version", &version); + versionstr = json_object_get_string(version); + installer.version = malloc( strlen(versionstr) * sizeof(char) +1 ); + strcpy(installer.version, versionstr); + + json_object_object_get_ex(slug, "runner", &runner); + runnerstr = json_object_get_string(runner); + for (int i = 0; i < RUNNERMAX; ++i) + { + if(!strcmp(runnerstr, runnerStr[i])) + { + installer.runner = i; + break; + } + } - json_object_object_get_ex(script, "wine", &wine); - json_object_object_get_ex(wine, "version", &winever); + json_object_object_get_ex(slug, "description", &description); + if (description) + { + descriptionstr = json_object_get_string(description); + installer.description = malloc( strlen(descriptionstr) * sizeof(char) +1 ); + strcpy(installer.description, descriptionstr); + } - printf("[%s]", json_object_get_string(runner)); - if (winever) printf("[%s]", json_object_get_string(winever)); + json_object_object_get_ex(slug, "notes", ¬es); + if (notes) + { + notesstr = json_object_get_string(notes); + installer.notes = malloc( strlen(notesstr) * sizeof(char) +1 ); + strcpy(installer.notes, notesstr); + } - printf(" %s - %s\n", json_object_get_string(name), json_object_get_string(version)); + json_object_object_get_ex(script, "wine", &wine); + json_object_object_get_ex(wine, "version", &winever); + if (winever) + { + winestr = json_object_get_string(winever); + installer.wine = malloc( strlen(winestr) * sizeof(char) +1 ); + strcpy(installer.name, winestr); + } - if (description) printf("\n%s\n", json_object_get_string(description)); - if (notes) printf("\n%s\n", json_object_get_string(notes)); + } - if (scriptinstall) - { - puts("\ninstall script:"); - for (int i = 0; i < json_object_array_length(scriptinstall); ++i) + if (json_object_object_get_ex(script, "files", &files)) + { + installer.filecount = json_object_array_length(files); + + installer.files = malloc(installer.filecount * sizeof(void*)); + for (int i = 0; i < installer.filecount; ++i) + { + struct json_object* file = json_object_array_get_idx(files, i); + struct lh_entry* entry = json_object_get_object(file)->head; + + installer.files[i] = malloc(sizeof(struct file_t)); + + { + size_t namelen = strlen((char*)entry->k); + installer.files[i]->filename = malloc(namelen * sizeof(char) +1); + strcpy(installer.files[i]->filename, (char*)entry->k); + } + + const char* urlstr; + + if(json_object_get_type((struct json_object*)entry->v) == json_type_object) + + { + struct json_object* url; + json_object_object_get_ex((struct json_object*)entry->v, "url", &url); + urlstr = json_object_get_string(url); + } + else + { + urlstr = json_object_get_string((struct json_object*)entry->v); + } + + { + size_t urllen = strlen(urlstr); + installer.files[i]->url = malloc(urllen * sizeof(char) +1); + strcpy(installer.files[i]->url, urlstr); + } + } + } + + if (json_object_object_get_ex(script, "installer", &scriptinstall)) { - struct json_object* step = json_object_array_get_idx(scriptinstall, i); - struct json_object* directive; + installer.directivecount = json_object_array_length(scriptinstall); - for (int l = 0; l < KEYWORDMAX; ++l) + installer.directives = malloc(installer.directivecount * sizeof(void*)); + for (int i = 0; i < installer.directivecount; ++i) { - json_object_object_get_ex(step, keywordstr[l], &directive); - if (directive) + struct json_object* step = json_object_array_get_idx(scriptinstall, i); + struct json_object* directive; + + installer.directives[i] = malloc(sizeof(struct directive_t)); + installer.directives[i]->size = 0; + installer.directives[i]->command = UNKNOWN_DIRECTIVE; + installer.directives[i]->task = NO_TASK; + + for (int l = 0; l < KEYWORDMAX; ++l) { - struct json_object* options[5]; - printf(" - "); - switch (l) + if (json_object_object_get_ex(step, keywordstr[l], &directive)) { - case MOVE: - case COPY: - case MERGE: - json_object_object_get_ex(directive, "src", &options[0]); - json_object_object_get_ex(directive, "dst", &options[1]); - printf("%s %s %s\n", keywordstr[l], json_object_get_string(options[0]), json_object_get_string(options[1])); - break; - - case EXTRACT: - json_object_object_get_ex(directive, "file", &options[0]); - printf("%s %s\n", keywordstr[l], json_object_get_string(options[0])); - break; - - case CHMODX: - printf("%s %s\n", keywordstr[l], json_object_get_string(directive)); - break; - - case EXECUTE: - json_object_object_get_ex(directive, "file", &options[0]); - printf("%s %s\n", keywordstr[l], json_object_get_string(options[0])); - break; - - case WRITE_FILE: - json_object_object_get_ex(directive, "file", &options[0]); - printf("%s %s\n", keywordstr[l], json_object_get_string(options[0])); - break; - - case WRITE_CONFIG: - json_object_object_get_ex(directive, "file", &options[0]); - json_object_object_get_ex(directive, "section", &options[1]); - json_object_object_get_ex(directive, "key", &options[2]); - json_object_object_get_ex(directive, "value", &options[3]); - - printf("%s %s [%s] %s = %s\n", keywordstr[l], json_object_get_string(options[0]), json_object_get_string(options[1]), json_object_get_string(options[2]), json_object_get_string(options[3])); - break; - - case WRITE_JSON: - printf("%s\n", keywordstr[l]); - break; - - case INPUT_MENU: - json_object_object_get_ex(directive, "description", &options[0]); - printf("%s `%s'\n", keywordstr[l], json_object_get_string(options[0])); - break; - - case INSERT_DISC: - json_object_object_get_ex(directive, "requires", &options[0]); - printf("%s %s\n", keywordstr[l], json_object_get_string(options[0])); - break; - - case TASK: - json_object_object_get_ex(directive, "name", &options[0]); - const char* name = json_object_get_string(options[0]); - for (int k = 0; k <= TASKKEYWORDMAX; ++k) - { - if (!strcmp(name, taskKeywordstr[k])) + struct json_object* options[5]; + switch (l) + { + case MOVE: + case COPY: + case MERGE: + json_object_object_get_ex(directive, "src", &options[0]); + json_object_object_get_ex(directive, "dst", &options[1]); + installer.directives[i]->size = 2; + break; + + case EXTRACT: + json_object_object_get_ex(directive, "file", &options[0]); + installer.directives[i]->size = 1; + break; + + case CHMODX: + installer.directives[i]->size = 1; + break; + + case EXECUTE: + if(!json_object_object_get_ex(directive, "command", &options[0])) json_object_object_get_ex(directive, "file", &options[0]); + installer.directives[i]->size = 1; + break; + + case WRITE_FILE: + json_object_object_get_ex(directive, "file", &options[0]); + json_object_object_get_ex(directive, "content", &options[1]); + installer.directives[i]->size = 2; + break; + + case WRITE_CONFIG: + json_object_object_get_ex(directive, "file", &options[0]); + json_object_object_get_ex(directive, "section", &options[1]); + json_object_object_get_ex(directive, "key", &options[2]); + json_object_object_get_ex(directive, "value", &options[3]); + installer.directives[i]->size = 4; + break; + + case WRITE_JSON: + break; + + case INPUT_MENU: + json_object_object_get_ex(directive, "description", &options[0]); + installer.directives[i]->size = 1; + break; + + case INSERT_DISC: + json_object_object_get_ex(directive, "requires", &options[0]); + installer.directives[i]->size = 1; + break; + + case TASK: + json_object_object_get_ex(directive, "name", &options[0]); + const char* name = json_object_get_string(options[0]); + for (int k = 0; k <= TASKKEYWORDMAX; ++k) { - switch(k) + if (!strcmp(name, taskKeywordstr[k])) { - case WINEEXEC: - json_object_object_get_ex(directive, "executable", &options[1]); - printf("%s %s\n", name, json_object_get_string(options[1])); - break; - - case WINETRICKS: - json_object_object_get_ex(directive, "app", &options[1]); - printf("%s %s\n", name, json_object_get_string(options[1])); - break; - - case CREATE_PREFIX: - case WINEKILL: - json_object_object_get_ex(directive, "prefix", &options[1]); - printf("%s %s\n", name, json_object_get_string(options[1])); - break; - - case SET_REGEDIT: - json_object_object_get_ex(directive, "path", &options[1]); - json_object_object_get_ex(directive, "key", &options[2]); - json_object_object_get_ex(directive, "value", &options[3]); - - printf("%s %s\\%s = %s\n", name, json_object_get_string(options[1]), json_object_get_string(options[2]), json_object_get_string(options[3])); - break; - - default: - puts(name); - break; + switch(k) + { + case WINEEXEC: + json_object_object_get_ex(directive, "executable", &options[1]); + installer.directives[i]->size = 1; + break; + + case WINETRICKS: + json_object_object_get_ex(directive, "app", &options[1]); + json_object_object_get_ex(directive, "prefix", &options[2]); + installer.directives[i]->size = 2; + break; + + case CREATE_PREFIX: + case WINEKILL: + json_object_object_get_ex(directive, "prefix", &options[1]); + installer.directives[i]->size = 1; + break; + + case SET_REGEDIT: + json_object_object_get_ex(directive, "path", &options[1]); + json_object_object_get_ex(directive, "key", &options[2]); + json_object_object_get_ex(directive, "value", &options[3]); + installer.directives[i]->size = 3; + break; + } + installer.directives[i]->task = k; + break; } - break; } - if (k == TASKKEYWORDMAX) printf("FIXME: unknown task %s\n", name); - } - break; - - default: - printf("FIXME: unknown %s\n", keywordstr[l]); + break; + } + installer.directives[i]->command = l; + + const char* str; + uint8_t offset = 0; + if (installer.directives[i]->task != NO_TASK) + { + offset = 1; + } + + + installer.directives[i]->arguments = malloc(installer.directives[i]->size * sizeof(char*)); + for (int j = 0; j < installer.directives[i]->size; ++j) + { + str = json_object_get_string(options[j+offset]); + installer.directives[i]->arguments[j] = malloc(strlen(str) * sizeof(char) +1); + strcpy(installer.directives[i]->arguments[j], str); + } + break; } - break; } } } - } - else puts("no install script found"); - } - else - { - printf("`%s' does not exist on lutris\n", argv[1]); + else installer.error = NO_INSTALLER; + } + else installer.error = NO_SCRIPT; } + else installer.error = NO_SLUG; - json_object_put(installer); + json_object_put(installerjson); } + else installer.error = NO_JSON; } - else puts(USAGE_STR " lutris info \nInstaller IDs are obtained from the lutris website"); - return 0; + return installer; } -void lutris_getInstallerURL(char* buffer, char* name) +void lutris_freeInstaller(struct script* installer) { - strcpy(buffer, INSTALLER_API); - strcat(buffer, name); -} -int lutris_help(int argc, char** argv) -{ - puts(USAGE_STR " lutris \n\nList of commands:"); + if (installer) + { + free(installer->name); + free(installer->version); + free(installer->description); + free(installer->notes); + free(installer->wine); - print_help(lutris_commands, ARRAY_LEN(lutris_commands)); + if (installer->directives) + { + for (int i = 0; i < installer->directivecount; ++i) + { + for (int j = 0; j < installer->directives[i]->size; ++j) + { + free(installer->directives[i]->arguments[j]); + } - return 0; + free(installer->directives[i]->arguments); + free(installer->directives[i]); + } + free(installer->directives); + } + + if (installer->files) + { + for (int i = 0; i < installer->filecount; ++i) + { + free(installer->files[i]->filename); + free(installer->files[i]->url); + free(installer->files[i]); + } + free(installer->files); + } + } } \ No newline at end of file diff --git a/src/lutris.h b/src/lutris.h index 6f5fa40..0991e14 100644 --- a/src/lutris.h +++ b/src/lutris.h @@ -1,61 +1,118 @@ #ifndef LUTRIS_H #define LUTRIS_H -enum keywords { - MOVE = 0, - MERGE, - EXTRACT, - COPY, - CHMODX, - EXECUTE, - WRITE_FILE, - WRITE_CONFIG, - WRITE_JSON, - INPUT_MENU, - INSERT_DISC, - TASK, - - KEYWORDMAX +#include + +enum keyword { + MOVE = 0, + MERGE, + EXTRACT, + COPY, + CHMODX, + EXECUTE, + WRITE_FILE, + WRITE_CONFIG, + WRITE_JSON, + INPUT_MENU, + INSERT_DISC, + TASK, + + KEYWORDMAX, + UNKNOWN_DIRECTIVE }; static const char keywordstr[KEYWORDMAX][0xF] = { - "move", - "merge", - "extract", - "copy", - "chmodx", - "execute", - "write_file", - "write_config", - "write_json", - "input_menu", - "insert-disc", - "task", -}; - -enum taskKeywords { - WINEEXEC = 0, - WINETRICKS, - CREATE_PREFIX, - SET_REGEDIT, - WINEKILL, - - TASKKEYWORDMAX + "move", + "merge", + "extract", + "copy", + "chmodx", + "execute", + "write_file", + "write_config", + "write_json", + "input_menu", + "insert-disc", + "task", +}; + +enum task { + WINEEXEC = 0, + WINETRICKS, + CREATE_PREFIX, + SET_REGEDIT, + WINEKILL, + + TASKKEYWORDMAX, + NO_TASK, + UNKNOWN_TASK }; static const char taskKeywordstr[TASKKEYWORDMAX][0xF] = { - "wineexec", - "winetricks", - "create_prefix", - "set_regedit", - "winekill" + "wineexec", + "winetricks", + "create_prefix", + "set_regedit", + "winekill", +}; + +enum runner_t { + WINE, + LINUX, + + RUNNERMAX, + UNKNOWN_RUNNER +}; + +static const char runnerStr[RUNNERMAX][0xF] = +{ + "wine", + "linux", +}; + + +enum errors { + NONE, + NO_JSON, + NO_SLUG, + NO_SCRIPT, + NO_INSTALLER, +}; + +struct directive_t { + enum keyword command; + enum task task; + char** arguments; + size_t size; +}; + +struct file_t { + char* filename; + char* url; +}; + +struct script { + char* name; + char* version; + enum runner_t runner; + char* description; + char* notes; + char* wine; + struct directive_t** directives; + size_t directivecount; + struct file_t** files; + size_t filecount; + enum errors error; }; int lutris(int, char**); int lutris_install(int, char**); int lutris_info(int, char**); int lutris_help(int, char**); -void lutris_getInstallerURL(char*, char*); + +void lutris_getInstallerURL(char*, char*, size_t); +struct script lutris_getInstaller(char*); +void lutris_freeInstaller(struct script*); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index aa4f356..a10e00c 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,7 @@ const static struct Command main_commands[] = { { .name = "wine", .func = wine, .description = "manage wine versions" }, - //{ .name = "dxvk", .func = dxvk, .description = "manage dxvk versions (TODO)" }, + { .name = "dxvk", .func = dxvk, .description = "manage dxvk versions" }, { .name = "lutris", .func = lutris, .description = "run lutris instraller"}, { .name = "info", .func = main_info, .description = "show some information about polecat" }, }; @@ -32,19 +32,20 @@ int main(int argc, char** argv) int main_info(int argc, char** argv) { - char cfgdir[PATH_MAX]; - char datadir[PATH_MAX]; + char cfgdir[PATH_MAX], datadir[PATH_MAX], cachedir[PATH_MAX]; - getConfigDir(cfgdir); - getDataDir(datadir); + getConfigDir(cfgdir, sizeof(cfgdir)); + getDataDir(datadir, sizeof(datadir)); + getCacheDir(cachedir, sizeof(cachedir)); printf("version:\t\t%s\n" "user-Agent:\t\t%s/%s\n" "config dir\t\t%s\n" - "data dir\t\t%s\n", + "data dir\t\t%s\n" + "cache dir\t\t%s\n", VERSION, NAME, VERSION, - cfgdir, datadir); + cfgdir, datadir, cachedir); return 0; } diff --git a/src/net.c b/src/net.c index a23f772..cf0488f 100644 --- a/src/net.c +++ b/src/net.c @@ -56,12 +56,14 @@ struct MemoryStruct* downloadToRam(const char* URL) curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &http_code); if(res != CURLE_OK) { - printf("libcurl error: %s\n", curl_easy_strerror(res)); + puts(curl_easy_strerror(res)); return NULL; } else if (http_code != 200) { +#ifdef DEBUG printf("HTTP Error %li\n", http_code); +#endif return NULL; } @@ -79,8 +81,11 @@ void downloadFile(const char* URL, const char* path) if (chunk) { FILE* file = fopen(path, "wb"); - fwrite(chunk->memory, chunk->size, 1, file); - fclose(file); + if (file) + { + fwrite(chunk->memory, chunk->size, 1, file); + fclose(file); + } free(chunk->memory); free(chunk); @@ -91,15 +96,16 @@ struct json_object* fetchJSON(const char* URL) { struct MemoryStruct* chunk = downloadToRam(URL); + struct json_object* json = NULL; + if (chunk) { - struct json_object* json = json_tokener_parse((char*)chunk->memory); + json = json_tokener_parse((char*)chunk->memory); free(chunk->memory); free(chunk); - return json; } - return NULL; + return json; } \ No newline at end of file diff --git a/src/net.h b/src/net.h index 1ad4f90..91f6828 100644 --- a/src/net.h +++ b/src/net.h @@ -4,6 +4,7 @@ #include size_t WriteMemoryCallback(void*, size_t, size_t, void*); +struct MemoryStruct* downloadToRam(const char* URL); void downloadFile(const char*, const char*); struct json_object* fetchJSON(const char*); diff --git a/src/tar.c b/src/tar.c index 0fd4fc3..f7282e2 100644 --- a/src/tar.c +++ b/src/tar.c @@ -3,6 +3,10 @@ #include #include #include +#include + +#include "common.h" +#include "tar.h" static int copy_data(struct archive* ar, struct archive* aw) { @@ -25,9 +29,9 @@ static int copy_data(struct archive* ar, struct archive* aw) } } -void extract(const char* filename, const char* outputdir) +void extract(const struct MemoryStruct* tar, const char* outputdir) { - char cwd[256]; + char cwd[PATH_MAX]; void* err = getcwd(cwd, sizeof(cwd)); if (chdir(outputdir) < 0) @@ -55,7 +59,7 @@ void extract(const char* filename, const char* outputdir) archive_write_disk_set_options(ext, flags); archive_write_disk_set_standard_lookup(ext); - if ((r = archive_read_open_filename(a, filename, 0x4000))) return; + if ((r = archive_read_open_memory(a, tar->memory, tar->size))) return; for (;;) { diff --git a/src/tar.h b/src/tar.h index 4235583..ab68566 100644 --- a/src/tar.h +++ b/src/tar.h @@ -1,6 +1,6 @@ #ifndef TAR_H #define TAR_H -void extract(const char* filename, const char* outputdir); +void extract(const struct MemoryStruct* tar, const char* outputdir); #endif \ No newline at end of file diff --git a/src/wine.c b/src/wine.c index c6f445e..0f9796e 100644 --- a/src/wine.c +++ b/src/wine.c @@ -64,19 +64,32 @@ int wine_download(int argc, char** argv) char datadir[PATH_MAX]; char downloadpath[PATH_MAX]; + struct MemoryStruct* tar; - getDataDir(datadir); + getDataDir(datadir, sizeof(datadir)); makeDir(datadir); - strcpy(downloadpath, datadir); - strcat(downloadpath, "/"); - strcat(downloadpath, name); + strncpy(downloadpath, datadir, sizeof(downloadpath)); + strncat(downloadpath, "/", sizeof(downloadpath) - strlen(downloadpath)); + strncat(downloadpath, name, sizeof(downloadpath) - strlen(downloadpath)); printf("Downloading %s\n", name); - downloadFile(json_object_get_string(url), downloadpath); - printf("Extracting %s\n", name); - extract(downloadpath, datadir); - printf("Done\n"); + + tar = downloadToRam(json_object_get_string(url)); + if (tar) + { + printf("Extracting %s\n", name); + extract(tar, datadir); + puts("Done"); + } + else + { + puts("Something went wrong. The tar is not valid"); + } + + + free(tar->memory); + free(tar); } json_object_put(runner); @@ -118,7 +131,7 @@ int wine_run(int argc, char** argv) if (argc > 1) { char winepath[PATH_MAX]; - getDataDir(winepath); + getDataDir(winepath, sizeof(winepath)); char* winever = argv[1]; strcat(winepath, "/"); @@ -142,7 +155,7 @@ int wine_run(int argc, char** argv) int wine_installed(int argc, char** argv) { char datadir[PATH_MAX]; - getDataDir(datadir); + getDataDir(datadir, sizeof(datadir)); DIR *dir; struct dirent *ent; -- cgit v1.2.3