From 4ce99996e81c8e10af0f404ce8a258d57eb0b910 Mon Sep 17 00:00:00 2001 From: Marc Date: Fri, 7 Oct 2022 10:47:14 +0200 Subject: [PATCH] Adder the type alias error_t and refactored the code to use it. also extracted archive function from install.c to archives.c --- CMakeLists.txt | 1 + src/archives.c | 96 +++++++++++++++++++++++++++++++++++++++ src/archives.h | 30 ++++++++++++ src/file.h | 11 +++-- src/fomod.c | 19 ++++---- src/fomod.h | 7 +-- src/fomod/parser.c | 17 +++---- src/fomod/parser.h | 4 +- src/fomod/xmlUtil.h | 12 +++++ src/install.c | 108 +++++--------------------------------------- src/install.h | 4 +- src/main.h | 7 +++ src/order.c | 8 ++-- src/order.h | 3 +- src/steam.h | 1 + 15 files changed, 198 insertions(+), 130 deletions(-) create mode 100644 src/archives.c create mode 100644 src/archives.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c42017..777ab41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set(SOURCES src/getHome.c src/file.c src/order.c + src/archives.c src/fomod.c src/fomod/group.c src/fomod/xmlUtil.c diff --git a/src/archives.c b/src/archives.c new file mode 100644 index 0000000..fabd680 --- /dev/null +++ b/src/archives.c @@ -0,0 +1,96 @@ +#include "archives.h" +#include "file.h" + +#include +#include +#include +#include +#include +#include + +int unzip(char * path, char * outdir) { + char * const args[] = { + "unzip", + "-LL", // to lowercase + "-q", + path, + "-d", + outdir, + NULL + }; + + pid_t pid = fork(); + + if(pid == 0) { + execv("/usr/bin/unzip", args); + return EXIT_FAILURE; + } else { + int returnValue; + waitpid(pid, &returnValue, 0); + + if(returnValue != 0) { + fprintf(stderr, "\nFailed to decompress archive\n"); + } + return returnValue; + } +} + +int unrar(char * path, char * outdir) { + char * const args[] = { + "unrar", + "x", + "-y", //assume yes + "-cl", // to lowercase + path, + outdir, + NULL + }; + + pid_t pid = fork(); + + if(pid == 0) { + execv("/usr/bin/unrar", args); + return EXIT_FAILURE; + } else { + int returnValue; + waitpid(pid, &returnValue, 0); + + if(returnValue != 0) { + fprintf(stderr, "\nFailed to decompress archive\n"); + } + return returnValue; + } +} + + +int un7z(char * path, const char * outdir) { + gchar * outParameter = g_strjoin("", "-o", outdir, NULL); + + char * const args[] = { + "7z", + "-y", //assume yes + "x", + path, + outParameter, + NULL + }; + + pid_t pid = fork(); + + if(pid == 0) { + execv("/usr/bin/7z", args); + return EXIT_FAILURE; + } else { + g_free(outParameter); + int returnValue; + waitpid(pid, &returnValue, 0); + + if(returnValue != 0) { + fprintf(stderr, "\nFailed to decompress archive\n"); + return returnValue; + } + //make everything lowercase since 7z don't have an argument for that. + casefold(outdir); + return returnValue; + } +} diff --git a/src/archives.h b/src/archives.h new file mode 100644 index 0000000..01032a5 --- /dev/null +++ b/src/archives.h @@ -0,0 +1,30 @@ +#ifndef __ARCHIVES_H__ +#define __ARCHIVES_H__ + +//all of these function will make every file into lowercase + +/** + * @brief Execute the unzip command + * @param path path to archive + * @param outdir output director + * @return int return code + */ +int unzip(char * path, char * outdir); + +/** + * @brief Execute the unrar command + * @param path path to archive + * @param outdir output director + * @return int return code + */ +int unrar(char * path, char * outdir); + +/** + * @brief Execute the 7z command + * @param path path to archive + * @param outdir output director + * @return int return code + */ +int un7z(char * path, const char * outdir); + +#endif diff --git a/src/file.h b/src/file.h index 3c1be0c..68c65bd 100644 --- a/src/file.h +++ b/src/file.h @@ -1,9 +1,10 @@ -#include -#include - #ifndef __FILE_H__ #define __FILE_H__ +#include +#include +#include "main.h" + //valid copy flags #define cp_DEFAULT 0 @@ -16,7 +17,7 @@ * @param source path to the source files * @param dest path to the destination folder * @param flags refer to the "valid copy flags" use them like this CP_LINK | CP_RECURSIVE - * @return status code + * @return cp return value */ int copy(const char * source, const char * dest, u_int32_t flags); @@ -25,7 +26,7 @@ int copy(const char * source, const char * dest, u_int32_t flags); * @param source path to the source files * @param dest path to the destination folder * @param bool enable recursive rm -r - * @return status code + * @return rm return value */ int delete(const char * path, bool recursive); diff --git a/src/fomod.c b/src/fomod.c index 784f451..e8ffb20 100644 --- a/src/fomod.c +++ b/src/fomod.c @@ -14,6 +14,7 @@ #include "file.h" #include "fomod/fomodTypes.h" #include "libxml/globals.h" +#include "main.h" static int getInputCount(const char * input) { char buff[2]; @@ -121,7 +122,7 @@ static void sortGroup(FOModGroup_t * group) { } //TODO: handle error -int processFileOperations(GList ** pendingFileOperations, const char * modFolder, const char * destination) { +error_t processFileOperations(GList ** pendingFileOperations, const char * modFolder, const char * destination) { //priority higher a less important and should be processed first. *pendingFileOperations = g_list_sort(*pendingFileOperations, priorityCmp); GList * currentFileOperation = *pendingFileOperations; @@ -148,7 +149,7 @@ int processFileOperations(GList ** pendingFileOperations, const char * modFolder currentFileOperation = g_list_next(currentFileOperation); } - return EXIT_SUCCESS; + return ERR_SUCCESS; } GList * processCondFiles(const FOMod_t * fomod, GList * flagList, GList * pendingFileOperations) { @@ -199,7 +200,7 @@ void freeFileOperations(GList * fileOperations) { g_list_free_full(fileOperationsStart, free); } -int installFOMod(const char * modFolder, const char * destination) { +error_t installFOMod(const char * modFolder, const char * destination) { //everything should be lowercase since we use casefold() before calling any install function char * fomodFolder = g_build_path("/", modFolder, "fomod", NULL); char * fomodFile = g_build_filename(fomodFolder, "moduleconfig.xml", NULL); @@ -208,14 +209,14 @@ int installFOMod(const char * modFolder, const char * destination) { fprintf(stderr, "FOMod file not found, are you sure this is a fomod mod ?\n"); g_free(fomodFolder); g_free(fomodFile); - return EXIT_FAILURE; + return ERR_FAILURE; } FOMod_t fomod; int returnValue = parseFOMod(fomodFile, &fomod); - if(returnValue != EXIT_SUCCESS) { - return returnValue; - } + if(returnValue == ERR_FAILURE) + return ERR_FAILURE; + g_free(fomodFile); GList * flagList = NULL; @@ -283,7 +284,7 @@ int installFOMod(const char * modFolder, const char * destination) { default: //never happen; fprintf(stderr, "unexpected type please report this issue %d, %d", group.type, __LINE__); - return EXIT_FAILURE; + return ERR_FAILURE; } @@ -351,5 +352,5 @@ int installFOMod(const char * modFolder, const char * destination) { freeFileOperations(pendingFileOperations); freeFOMod(&fomod); g_free(fomodFolder); - return EXIT_SUCCESS; + return ERR_SUCCESS; } diff --git a/src/fomod.h b/src/fomod.h index a7bb6af..30d5386 100644 --- a/src/fomod.h +++ b/src/fomod.h @@ -3,8 +3,9 @@ #include #include -#include "fomod/parser.h" +#include "main.h" +#include "fomod/parser.h" #include "fomod/group.h" #include "fomod/xmlUtil.h" @@ -15,7 +16,7 @@ * @param destination folder of the new mod that contains the result of the fomod process. * @return int */ -int installFOMod(const char * modFolder, const char * destination); +error_t installFOMod(const char * modFolder, const char * destination); /** * @brief In fomod there is file operations which depends on multiple flags this function find the ones that mach our current flags and append them to a list. @@ -35,7 +36,7 @@ GList * processCondFiles(const FOMod_t * fomod, GList * flagList, GList * pendin * @param destination folder of the new mod that contains the result of the process. * @return error code */ -int processFileOperations(GList ** pendingFileOperations, const char * modFolder, const char * destination); +error_t processFileOperations(GList ** pendingFileOperations, const char * modFolder, const char * destination); /** * @brief diff --git a/src/fomod/parser.c b/src/fomod/parser.c index 34a07c4..fbd7470 100644 --- a/src/fomod/parser.c +++ b/src/fomod/parser.c @@ -239,7 +239,7 @@ static int parseConditionalInstalls(xmlNodePtr node, FOMod_t * fomod) { return EXIT_SUCCESS; } -int parseFOMod(const char * fomodFile, FOMod_t* fomod) { +error_t parseFOMod(const char * fomodFile, FOMod_t* fomod) { xmlDocPtr doc; xmlNodePtr cur; @@ -256,7 +256,7 @@ int parseFOMod(const char * fomodFile, FOMod_t* fomod) { if(doc == NULL) { fprintf(stderr, "Document is not a valid xml file\n"); - return EXIT_FAILURE; + return ERR_FAILURE; } @@ -264,12 +264,13 @@ int parseFOMod(const char * fomodFile, FOMod_t* fomod) { if(cur == NULL) { fprintf(stderr, "emptyDocument"); xmlFreeDoc(doc); - return EXIT_FAILURE; + return ERR_FAILURE; } if(xmlStrcmp(cur->name, (const xmlChar *) "config") != 0) { fprintf(stderr, "document of the wrong type, root node is '%s' instead of config\n", cur->name); - return EXIT_FAILURE; + xmlFreeDoc(doc); + return ERR_FAILURE; } cur = cur->children; @@ -286,7 +287,7 @@ int parseFOMod(const char * fomodFile, FOMod_t* fomod) { if(validateNode(&requiredInstallFile, true, "folder", "file", NULL)) { //TODO: handle error printf("%d\n", __LINE__); - exit(EXIT_FAILURE); + exit(ERR_FAILURE); } int size = countUntilNull(fomod->requiredInstallFiles, sizeof(char **)) + 2; @@ -301,7 +302,7 @@ int parseFOMod(const char * fomodFile, FOMod_t* fomod) { if(fomod->steps != NULL) { fprintf(stderr, "Multiple 'installSteps' tags"); //TODO: handle error - return EXIT_FAILURE; + return ERR_FAILURE; } xmlChar * stepOrder = xmlGetProp(cur, (xmlChar *)"order"); @@ -315,7 +316,7 @@ int parseFOMod(const char * fomodFile, FOMod_t* fomod) { fprintf(stderr, "Failed to parse the install steps\n"); //TODO: manage the error properly - return EXIT_FAILURE; + return ERR_FAILURE; } fomod->steps = steps; @@ -327,5 +328,5 @@ int parseFOMod(const char * fomodFile, FOMod_t* fomod) { } xmlFreeDoc(doc); - return EXIT_SUCCESS; + return ERR_SUCCESS; } diff --git a/src/fomod/parser.h b/src/fomod/parser.h index 6f08a5c..604230e 100644 --- a/src/fomod/parser.h +++ b/src/fomod/parser.h @@ -3,6 +3,7 @@ #include "group.h" +#include "../main.h" //combine installStep and optionalFileGroups typedef struct FOModStep { @@ -30,9 +31,8 @@ typedef struct FOMod { * * @param fomodFile path to the moduleconfig.xml * @param fomod pointer to a new FOMod_t - * @return error code. */ -int parseFOMod(const char * fomodFile, FOMod_t* fomod); +error_t parseFOMod(const char * fomodFile, FOMod_t* fomod); /** * @brief Free content of a fomod file. diff --git a/src/fomod/xmlUtil.h b/src/fomod/xmlUtil.h index e09e236..f38f7f8 100644 --- a/src/fomod/xmlUtil.h +++ b/src/fomod/xmlUtil.h @@ -7,12 +7,24 @@ typedef enum FOModOrder { ASC, DESC, ORD } FOModOrder_t; +/** + * @brief + * + * @param node a pointer to the current node pointer (xmlNode **) + * @param skipText if a text element is found skip it. + * @param names variadic of the valid names. + * @return return true if it found a valid node + */ bool validateNode(xmlNodePtr * node, bool skipText, const char * names, ...); + /** * @brief Free memory of and xmlChar and return a strdup version. just to make sure there is nothing remaining in libxml */ char * freeAndDup(xmlChar * line); + + FOModOrder_t getFOModOrder(const char * order); + /** * @brief replace / by \ * @param path diff --git a/src/install.c b/src/install.c index a3d9c3f..a381b76 100644 --- a/src/install.c +++ b/src/install.c @@ -1,100 +1,12 @@ #include -#include #include -#include #include #include + #include "install.h" #include "main.h" -#include "file.h" - -static int unzip(char * path, char * outdir) { - char * const args[] = { - "unzip", - "-LL", // to lowercase - "-q", - path, - "-d", - outdir, - NULL - }; - - pid_t pid = fork(); - - if(pid == 0) { - execv("/usr/bin/unzip", args); - return EXIT_FAILURE; - } else { - int returnValue; - waitpid(pid, &returnValue, 0); - - if(returnValue != 0) { - fprintf(stderr, "\nFailed to decompress archive\n"); - } - return returnValue; - } -} - -static int unrar(char * path, char * outdir) { - char * const args[] = { - "unrar", - "x", - "-y", //assume yes - "-cl", // to lowercase - path, - outdir, - NULL - }; - - pid_t pid = fork(); - - if(pid == 0) { - execv("/usr/bin/unrar", args); - return EXIT_FAILURE; - } else { - int returnValue; - waitpid(pid, &returnValue, 0); - - if(returnValue != 0) { - fprintf(stderr, "\nFailed to decompress archive\n"); - } - return returnValue; - } -} - - -static int un7z(char * path, const char * outdir) { - gchar * outParameter = g_strjoin("", "-o", outdir, NULL); - - char * const args[] = { - "7z", - "-y", //assume yes - "x", - path, - outParameter, - NULL - }; - - pid_t pid = fork(); - - if(pid == 0) { - execv("/usr/bin/7z", args); - return EXIT_FAILURE; - } else { - g_free(outParameter); - int returnValue; - waitpid(pid, &returnValue, 0); - - if(returnValue != 0) { - fprintf(stderr, "\nFailed to decompress archive\n"); - return returnValue; - } - //make everything lowercase since 7z don't have an argument for that. - casefold(outdir); - return returnValue; - } -} +#include "archives.h" static const char * extractLastPart(const char * filePath, const char delimeter) { const int length = strlen(filePath); @@ -118,19 +30,18 @@ static const char * extractFileName(const char * filePath) { return extractLastPart(filePath, '/'); } -int addMod(char * filePath, int appId) { - int returnValue = EXIT_SUCCESS; - +error_t addMod(char * filePath, int appId) { + error_t resultError = ERR_SUCCESS; if (access(filePath, F_OK) != 0) { fprintf(stderr, "File not found\n"); - returnValue = EXIT_FAILURE; + resultError = ERR_FAILURE; goto exit; } char * configFolder = g_build_filename(getenv("HOME"), MANAGER_FILES, NULL); if(g_mkdir_with_parents(configFolder, 0755) < 0) { fprintf(stderr, "Could not create mod folder"); - returnValue = EXIT_FAILURE; + resultError = ERR_FAILURE; goto exit2; } @@ -143,6 +54,8 @@ int addMod(char * filePath, int appId) { char * outdir = g_build_filename(configFolder, MOD_FOLDER_NAME, appIdStr, filename, NULL); g_mkdir_with_parents(outdir, 0755); + + int returnValue = EXIT_SUCCESS; printf("Adding mod, this process can be slow depending on your hardware\n"); if(strcmp(lowercaseExtension, "rar") == 0) { returnValue = unrar(filePath, outdir); @@ -155,6 +68,9 @@ int addMod(char * filePath, int appId) { returnValue = EXIT_FAILURE; } + if(returnValue == EXIT_FAILURE) + resultError = ERR_FAILURE; + printf("Done\n"); free(lowercaseExtension); @@ -162,5 +78,5 @@ int addMod(char * filePath, int appId) { exit2: free(configFolder); exit: - return returnValue; + return resultError; } diff --git a/src/install.h b/src/install.h index 5a89ca6..51c52f2 100644 --- a/src/install.h +++ b/src/install.h @@ -1,6 +1,7 @@ #ifndef __INSTALL_H__ #define __INSTALL_H__ #include +#include "main.h" #define INSTALLED_FLAG_FILE "__DO_NOT_REMOVE__" /** @@ -8,8 +9,7 @@ * * @param filePath path to the mod file * @param appId game for which the mod is destined to be used with. - * @return EXIT_SUCCESS or EXIT_FAILURE */ -int addMod(char * filePath, int appId); +error_t addMod(char * filePath, int appId); #endif diff --git a/src/main.h b/src/main.h index 147d0ab..3aa9d13 100644 --- a/src/main.h +++ b/src/main.h @@ -1,3 +1,6 @@ +#ifndef __MAIN_H__ +#define __MAIN_H__ + //no function should ever be put here, only keep important macros here #define APP_NAME "modmanager" @@ -13,3 +16,7 @@ #define GAME_WORK_DIR_NAME "WORK_DIRS" #define VERSION "0.1" + +typedef enum { ERR_SUCCESS, ERR_FAILURE } error_t; + +#endif diff --git a/src/order.c b/src/order.c index 75cc403..becfb81 100644 --- a/src/order.c +++ b/src/order.c @@ -94,7 +94,7 @@ GList * listMods(int appid) { } -int swapPlace(int appid, int modIdA, int modIdB) { +error_t swapPlace(int appid, int modIdA, int modIdB) { char appidStr[10]; sprintf(appidStr, "%d", appid); @@ -116,7 +116,7 @@ int swapPlace(int appid, int modIdA, int modIdB) { if(listA == NULL || listB == NULL) { fprintf(stderr, "Invalid modId\n"); - return EXIT_FAILURE; + return ERR_FAILURE; } char * modAFolder = g_build_filename(modFolder, listA->data, ORDER_FILE, NULL); @@ -131,7 +131,7 @@ int swapPlace(int appid, int modIdA, int modIdB) { if(fileA == NULL || fileB == NULL) { fprintf(stderr, "Error could not open order file\n"); - return EXIT_FAILURE; + return ERR_FAILURE; } fprintf(fileA, "%d", modIdB); @@ -141,5 +141,5 @@ int swapPlace(int appid, int modIdA, int modIdB) { fclose(fileB); g_list_free_full(list, free); - return EXIT_SUCCESS; + return ERR_SUCCESS; } diff --git a/src/order.h b/src/order.h index 33da0fe..15d390d 100644 --- a/src/order.h +++ b/src/order.h @@ -2,6 +2,7 @@ #define __ORDER_H__ #include +#include "main.h" #define ORDER_FILE "__ORDER__" @@ -24,6 +25,6 @@ GList * listMods(int appid); * @param modId * @param modId2 */ -int swapPlace(int appid, int modId, int modId2); +error_t swapPlace(int appid, int modId, int modId2); #endif diff --git a/src/steam.h b/src/steam.h index 838287e..880fe18 100644 --- a/src/steam.h +++ b/src/steam.h @@ -2,6 +2,7 @@ #define __STEAM_H__ #include "macro.h" +#include "main.h" #include #include