Removed small memory leaks and improved error code path

at first when i create the proof of concept i didn't care at all about memory leak sine the kernel
was cleaning that for me.
This commit is contained in:
Marc
2022-09-11 14:14:42 +02:00
parent ef8776b6b1
commit 2d63fd8d92
4 changed files with 169 additions and 111 deletions
+78 -66
View File
@@ -9,97 +9,109 @@
//TODO: replace ALL system call by execv / execl since it make my code into swiss cheese
void unzip(const char * path, const char * outdir) {
char * unzipCommand = g_strconcat("/bin/sh -c 'yes | unzip \"", path, "\" -d \"" , outdir, "\" > /dev/null'", NULL);
if(system(unzipCommand) != 0) {
printf("\nFailed to unzip archive\n");
exit(EXIT_FAILURE);
}
free(unzipCommand);
int unzip(const char * path, const char * outdir) {
int returnValue = EXIT_SUCCESS;
char * unzipCommand = g_strconcat("/bin/sh -c 'yes | unzip \"", path, "\" -d \"" , outdir, "\" > /dev/null'", NULL);
if(system(unzipCommand) != 0) {
printf("\nFailed to unzip archive\n");
returnValue = EXIT_FAILURE;
}
free(unzipCommand);
return returnValue;
}
void unrar(const char * path, const char * outdir) {
char * unrarCommand = g_strconcat("/bin/sh -c 'unrar -y x \"", path, "\" \"", outdir, "\" > /dev/null'", NULL);
if(system(unrarCommand) != 0) {
printf("Failed to unzip archive\n");
exit(EXIT_FAILURE);
}
free(unrarCommand);
int unrar(const char * path, const char * outdir) {
int returnValue = EXIT_SUCCESS;
char * unrarCommand = g_strconcat("/bin/sh -c 'unrar -y x \"", path, "\" \"", outdir, "\" > /dev/null'", NULL);
if(system(unrarCommand) != 0) {
printf("Failed to unzip archive\n");
returnValue = EXIT_FAILURE;
}
free(unrarCommand);
return returnValue;
}
void un7z(const char * path, const char * outdir) {
char * un7zCommand = g_strconcat("/bin/sh -c 'yes | 7z x \"", path, "\" \"-o", outdir, "\" > /dev/null'", NULL);
if(system(un7zCommand) != 0) {
printf("Failed to unzip archive\n");
exit(EXIT_FAILURE);
}
free(un7zCommand);
int un7z(const char * path, const char * outdir) {
int returnValue = EXIT_SUCCESS;
char * un7zCommand = g_strconcat("/bin/sh -c 'yes | 7z x \"", path, "\" \"-o", outdir, "\" > /dev/null'", NULL);
if(system(un7zCommand) != 0) {
printf("Failed to unzip archive\n");
returnValue = EXIT_FAILURE;
}
free(un7zCommand);
return returnValue;
}
char * extractLastPart(const char * filePath, const char delimeter) {
int length = strlen(filePath);
int index = -1;
for(int i= length -1; i >= 0; i--) {
if(filePath[i] == delimeter) {
index = i + 1;
break;
}
}
int length = strlen(filePath);
int index = -1;
for(int i= length -1; i >= 0; i--) {
if(filePath[i] == delimeter) {
index = i + 1;
break;
}
}
if(index <= 0) return NULL;
char * extension = malloc((length - index) * sizeof(char));
memcpy(extension, &filePath[index], (length - index));
return extension;
if(index <= 0 || index == length) return NULL;
char * extension = malloc((length - index) * sizeof(char));
memcpy(extension, &filePath[index], (length - index));
return extension;
}
char * extractExtension(const char * filePath) {
return extractLastPart(filePath, '.');
return extractLastPart(filePath, '.');
}
char * extractFileName(const char * filePath) {
return extractLastPart(filePath, '/');
return extractLastPart(filePath, '/');
}
void addMod(const char * filePath, int appId) {
int addMod(const char * filePath, int appId) {
int returnValue = EXIT_SUCCESS;
if (access(filePath, F_OK) != 0) {
printf("File not found\n");
exit(EXIT_FAILURE);
printf("File not found\n");
returnValue = EXIT_FAILURE;
goto exit;
}
char * configFolder = g_build_filename(getenv("HOME"), MANAGER_FILES, NULL);
char * configFolder = g_build_filename(getenv("HOME"), MANAGER_FILES, NULL);
if(g_mkdir_with_parents(configFolder, 0755) < 0) {
printf("Could not create mod folder");
exit(EXIT_FAILURE);
}
printf("Could not create mod folder");
returnValue = EXIT_FAILURE;
goto exit2;
}
char * filename = extractFileName(filePath);
char * extension = extractExtension(filename);
char * lowercaseExtension = g_ascii_strdown(extension, -1);
free(extension);
char * filename = extractFileName(filePath);
char * extension = extractExtension(filename);
char * lowercaseExtension = g_ascii_strdown(extension, -1);
free(extension);
char appIdStr[20];
sprintf(appIdStr, "%d", appId);
char appIdStr[20];
char * outdir = g_build_filename(configFolder, MOD_FOLDER_NAME, appIdStr, filename, NULL);
char * outdir = g_build_filename(configFolder, MOD_FOLDER_NAME, appIdStr, filename, NULL);
g_mkdir_with_parents(outdir, 0755);
printf("Adding mod, this process can be slow depending on your hardware\n");
if(strcmp(lowercaseExtension, "rar") == 0) {
unrar(filePath, outdir);
} else if (strcmp(lowercaseExtension, "zip") == 0) {
unzip(filePath, outdir);
} else if (strcmp(lowercaseExtension, "7z") == 0) {
un7z(filePath, outdir);
} else {
printf("Unsupported format only zip/7z/rar are supported\n");
exit(EXIT_FAILURE);
}
g_mkdir_with_parents(outdir, 0755);
printf("Adding mod, this process can be slow depending on your hardware\n");
if(strcmp(lowercaseExtension, "rar") == 0) {
returnValue = unrar(filePath, outdir);
} else if (strcmp(lowercaseExtension, "zip") == 0) {
returnValue = unzip(filePath, outdir);
} else if (strcmp(lowercaseExtension, "7z") == 0) {
returnValue = un7z(filePath, outdir);
} else {
printf("Unsupported format only zip/7z/rar are supported\n");
returnValue = EXIT_FAILURE;
}
printf("Done\n");
printf("Done\n");
free(lowercaseExtension);
free(filename);
free(outdir);
free(lowercaseExtension);
free(filename);
free(outdir);
exit2:
free(configFolder);
exit:
return returnValue;
}