proof of concept
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include "file.h"
|
||||
#include <stdio.h>
|
||||
|
||||
u_int32_t countSetBits(u_int32_t n) {
|
||||
// base case
|
||||
if (n == 0)
|
||||
return 0;
|
||||
else
|
||||
// if last bit set add 1 else add 0
|
||||
return (n & 1) + countSetBits(n >> 1);
|
||||
}
|
||||
|
||||
//simplest way to copy a file in c(linux)
|
||||
int copy(const char * path, const char * dest, u_int32_t flags) {
|
||||
int flagCount = countSetBits(flags);
|
||||
if(flagCount > 3) {
|
||||
printf("Invalid flags for cp command\n");
|
||||
return -1;
|
||||
}
|
||||
//flags + cp + path + dest
|
||||
const char * args[flagCount + 4];
|
||||
memset(args, 0, (flagCount + 4) * sizeof(char *));
|
||||
args[0] = "/bin/cp";
|
||||
args[1] = path;
|
||||
args[2] = dest;
|
||||
int argIndex = 3;
|
||||
|
||||
if(flags & CP_LINK) {
|
||||
args[argIndex] = "--link";
|
||||
argIndex += 1;
|
||||
}
|
||||
if(flags & CP_RECURSIVE) {
|
||||
args[argIndex] = "-r";
|
||||
argIndex += 1;
|
||||
}
|
||||
if(flags & CP_NO_TARGET_DIR) {
|
||||
args[argIndex] = "-T";
|
||||
argIndex += 1;
|
||||
}
|
||||
|
||||
|
||||
int pid = fork();
|
||||
if(pid == 0) {
|
||||
//discard the const. since we are in a fork we don't care.
|
||||
execv("/bin/cp", (char **)args);
|
||||
return 0;
|
||||
} else {
|
||||
int returnValue;
|
||||
waitpid(pid, &returnValue, 0);
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
|
||||
int delete(const char * path, bool recursive) {
|
||||
int pid = fork();
|
||||
if(pid == 0) {
|
||||
if(recursive) {
|
||||
execl("/bin/rm", "/bin/rm", "-r", path, NULL);
|
||||
} else {
|
||||
execl("/bin/rm", "/bin/rm", path, NULL);
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
int returnValue;
|
||||
waitpid(pid, &returnValue, 0);
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
|
||||
int move(const char * source, const char * destination) {
|
||||
int pid = fork();
|
||||
if(pid == 0) {
|
||||
execl("/bin/mv", "/bin/mv", source, destination, NULL);
|
||||
return 0;
|
||||
} else {
|
||||
int returnValue;
|
||||
waitpid(pid, &returnValue, 0);
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifndef __FILE_H__
|
||||
#define __FILE_H__
|
||||
|
||||
#define CP_LINK 1
|
||||
#define CP_RECURSIVE 2
|
||||
#define CP_NO_TARGET_DIR 4
|
||||
|
||||
int copy(const char * path, const char * dest, u_int32_t flags);
|
||||
int delete(const char * path, bool recursive);
|
||||
int move(const char * source, const char * destination);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <libaudit.h>
|
||||
#include <pwd.h>
|
||||
#include <string.h>
|
||||
|
||||
char * getHome() {
|
||||
//not getting home from the env enable us to use sudo
|
||||
struct passwd *pw = getpwuid(audit_getloginuid());
|
||||
return strdup(pw->pw_dir);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
char * getHome();
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <glib.h>
|
||||
#include "install.h"
|
||||
#include "main.h"
|
||||
|
||||
//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);
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if(index <= 0) 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, '.');
|
||||
}
|
||||
|
||||
char * extractFileName(const char * filePath) {
|
||||
return extractLastPart(filePath, '/');
|
||||
}
|
||||
|
||||
|
||||
void addMod(const char * filePath, int appId) {
|
||||
if (access(filePath, F_OK) != 0) {
|
||||
printf("File not found\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 * 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);
|
||||
}
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
free(lowercaseExtension);
|
||||
free(filename);
|
||||
free(outdir);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef __INSTALL_H__
|
||||
#define __INSTALL_H__
|
||||
#define INSTALLED_FLAG_FILE "__DO_NOT_REMOVE__"
|
||||
void addMod(const char * filePath, int appId);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef __MACRO_H__
|
||||
#define __MACRO_H__
|
||||
|
||||
//cannot be empty it's made for fixed size array
|
||||
#define LEN(array) sizeof(array) / sizeof(array[0])
|
||||
|
||||
#endif
|
||||
+425
@@ -0,0 +1,425 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/wait.h>
|
||||
#include <libaudit.h>
|
||||
|
||||
#include "overlayfs.h"
|
||||
#include "install.h"
|
||||
#include "getHome.h"
|
||||
#include "steam.h"
|
||||
#include "main.h"
|
||||
#include "file.h"
|
||||
|
||||
GHashTable * gamePaths;
|
||||
|
||||
bool isRoot() {
|
||||
return getuid() == 0;
|
||||
}
|
||||
|
||||
GList * listFilesInFolder(const char * path) {
|
||||
GList * list = NULL;
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
d = opendir(path);
|
||||
if (d) {
|
||||
while ((dir = readdir(d)) != NULL) {
|
||||
//removes .. & . from the list
|
||||
if(strcmp(dir->d_name, "..") != 0 && strcmp(dir->d_name, ".") != 0) {
|
||||
list = g_list_append(list, strdup(dir->d_name));
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
int noRoot() {
|
||||
printf("Don't run this argument as root\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int needRoot() {
|
||||
printf("Root is needed to bind with the game files\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int usage() {
|
||||
printf("Use --list-games or -l to list compatible games\n");
|
||||
printf("Use --add or -a <APPID> <FILENAME> to add a mod to a game\n");
|
||||
printf("Use --list-mods or -m <APPID> to list all mods for a game\n");
|
||||
printf("Use --install or -i <APPID> <MODID> to add a mod to a game\n");
|
||||
printf("Use --remove or -r <APPID> <MODID> to remove a mod from a game\n");
|
||||
printf("Use --deploy or -d <APPID> to deploy the mods for the game\n");
|
||||
printf("Use --unbind or -u <APPID> to rollback a deployment\n");
|
||||
//TODO: as bonus
|
||||
//printf("Use --start-game <APPID> to deploy the mods and launch the game through steam\n");
|
||||
//printf("Use --steam in team cmdline options to deploy directly on game startup\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int validateAppId(const char * appIdStr) {
|
||||
//strtoul set EINVAL(after C99) if the string is invalid
|
||||
u_int32_t appid = strtoul(appIdStr, NULL, 10);
|
||||
if(errno == EINVAL) {
|
||||
printf("Appid has to be a valid number\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int gameId = getGameIdFromAppId(appid);
|
||||
if(gameId < 0) {
|
||||
printf("Game is not compatible\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(!g_hash_table_contains(gamePaths, &gameId)) {
|
||||
printf("Game not found\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return appid;
|
||||
}
|
||||
|
||||
int listGames(int argc, char ** argv) {
|
||||
if(argc != 2) return usage();
|
||||
GList * gamesIds = g_hash_table_get_keys(gamePaths);
|
||||
if(g_list_length(gamesIds) == 0) {
|
||||
printf("No game found\n");
|
||||
} else {
|
||||
while(gamesIds != NULL) {
|
||||
int * gameIndex = (int*)gamesIds->data;
|
||||
printf("%d: %s\n", GAMES_APPIDS[*gameIndex], GAMES_NAMES[*gameIndex]);
|
||||
gamesIds = g_list_next(gamesIds);
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int add(int argc, char ** argv) {
|
||||
if(argc != 4) return usage();
|
||||
char * appIdStr = argv[2];
|
||||
|
||||
u_int32_t appid = validateAppId(appIdStr);
|
||||
if(appid < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
addMod(argv[3], appid);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int listMods(int argc, char ** argv) {
|
||||
if(argc != 3) return usage();
|
||||
char * appIdStr = argv[2];
|
||||
if(validateAppId(appIdStr) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//might crash if no mods were installed
|
||||
char * modFolder = g_build_filename(getenv("HOME"), MANAGER_FILES, MOD_FOLDER_NAME, appIdStr, NULL);
|
||||
GList * mods = listFilesInFolder(modFolder);
|
||||
unsigned short index = 0;
|
||||
|
||||
printf("Id | Installed | Name\n");
|
||||
while(mods != NULL) {
|
||||
char * modName = (char*)mods->data;
|
||||
char * modPath = g_build_filename(modFolder, modName, INSTALLED_FLAG_FILE, NULL);
|
||||
|
||||
if(access(modPath, F_OK) == 0) {
|
||||
printf("\033[0;32m %d | ✓ | %s\n", index, modName);
|
||||
} else {
|
||||
printf("\033[0;31m %d | ✕ | %s\n", index, modName);
|
||||
}
|
||||
|
||||
index++;
|
||||
mods = g_list_next(mods);
|
||||
}
|
||||
|
||||
|
||||
free(modFolder);
|
||||
free(mods);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* THIS WILL NOT INSTALL THE MOD
|
||||
* this will just flag the mod as needed to be deployed
|
||||
* it's the deploying process that handle the rest
|
||||
*/
|
||||
int installAndRemoveMod(int argc, char ** argv, bool install) {
|
||||
if(argc != 4) return usage();
|
||||
char * appIdStr = argv[2];
|
||||
u_int32_t appid = validateAppId(appIdStr);
|
||||
if(appid < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//strtoul set EINVAL(after C99) if the string is invalid
|
||||
u_int32_t modId = strtoul(argv[3], NULL, 10);
|
||||
if(errno == EINVAL) {
|
||||
printf("Appid has to be a valid number\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//might crash if no mods were installed
|
||||
char * modFolder = g_build_filename(getenv("HOME"), MANAGER_FILES, MOD_FOLDER_NAME, appIdStr, NULL);
|
||||
GList * mods = listFilesInFolder(modFolder);
|
||||
GList * modsFirstPointer = mods;
|
||||
|
||||
for(int i = 0; i < modId; i++) {
|
||||
mods = g_list_next(mods);
|
||||
}
|
||||
|
||||
if(mods == NULL) {
|
||||
printf("Mod not found\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
char * modName = (char*)mods->data;
|
||||
char * modFlag = g_build_filename(modFolder, modName, INSTALLED_FLAG_FILE, NULL);
|
||||
|
||||
if(install) {
|
||||
if(access(modFlag, F_OK) == 0) {
|
||||
printf("The mod is already activated \n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
//Create activated file
|
||||
FILE * fd = fopen(modFlag, "w");
|
||||
fwrite("", 1, 1, fd);
|
||||
fclose(fd);
|
||||
} else {
|
||||
if(access(modFlag, F_OK) != 0) {
|
||||
printf("The mod is not activated \n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if(unlink(modFlag) != 0) {
|
||||
printf("Error could not disable the mod.\n");
|
||||
printf("please remove this file '%s' as root\n", modFlag);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
free(modFolder);
|
||||
g_list_free(modsFirstPointer);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int deploy(int argc, char ** argv) {
|
||||
if(argc != 3) return usage();
|
||||
|
||||
char * appIdStr = argv[2];
|
||||
int appid = validateAppId(appIdStr);
|
||||
if(appid < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
char * home = getHome();
|
||||
char * gameFolder = g_build_filename(home, MANAGER_FILES, GAME_FOLDER_NAME, appIdStr, NULL);
|
||||
|
||||
if(access(gameFolder, F_OK) != 0) {
|
||||
printf("Please run '%s --setup %d' first", APP_NAME, appid);
|
||||
}
|
||||
|
||||
//unmount everything beforhand
|
||||
//might crash if no mods were installed
|
||||
char * modFolder = g_build_filename(home, MANAGER_FILES, MOD_FOLDER_NAME, appIdStr, NULL);
|
||||
|
||||
GList * mods = listFilesInFolder(modFolder);
|
||||
//since the priority is by alphabetical order
|
||||
//and we need to bind the least important first
|
||||
//and the most important last
|
||||
//we have to reverse the list
|
||||
mods = g_list_reverse(mods);
|
||||
|
||||
//probably over allocating but this doesn't matter that much
|
||||
char * modsToInstall[g_list_length(mods) + 2];
|
||||
int modCount = 0;
|
||||
|
||||
while(true) {
|
||||
if(mods == NULL)break;
|
||||
char * modName = (char *)mods->data;
|
||||
char * modPath = g_build_path("/", modFolder, modName, NULL);
|
||||
char * modFlag = g_build_filename(modPath, INSTALLED_FLAG_FILE, NULL);
|
||||
|
||||
//if the mod is not marked to be deployed then don't
|
||||
if(access(modFlag, F_OK) != 0) {
|
||||
printf("ignoring mod %s\n", modName);
|
||||
mods = g_list_next(mods);
|
||||
continue;
|
||||
}
|
||||
|
||||
//this is made to leave the first case empty so i can put lowerdir in it before feeding it to g_strjoinv
|
||||
printf("Installing %s\n", modName);
|
||||
modsToInstall[modCount] = modPath;
|
||||
modCount += 1;
|
||||
|
||||
mods = g_list_next(mods);
|
||||
free(modFlag);
|
||||
}
|
||||
|
||||
//const char * data = "lowerdir=gameFolder,gameFolder2,gameFolder3..."
|
||||
modsToInstall[modCount] = gameFolder;
|
||||
modsToInstall[modCount + 1] = NULL;
|
||||
printf("Mounting the overlay\n");
|
||||
int gameId = getGameIdFromAppId(appid);
|
||||
const char * path = g_hash_table_lookup(gamePaths, &gameId);
|
||||
char * steamGameFolder = g_build_path("/", path, "steamapps/common", GAMES_NAMES[gameId], "Data", NULL);
|
||||
printf("%s\n", steamGameFolder);
|
||||
|
||||
char * gameUpperDir = g_build_filename(home, MANAGER_FILES, GAME_UPPER_DIR_NAME, appIdStr, NULL);
|
||||
char * gameWorkDir = g_build_filename(home, MANAGER_FILES, GAME_WORK_DIR_NAME, appIdStr, NULL);
|
||||
|
||||
//unmount the game folder
|
||||
//DETACH + FORCE allow us to be sure it will be unload.
|
||||
//it might crash / corrupt game file if the user do it while the game is running
|
||||
//but it's still very unlikely
|
||||
while(umount2(steamGameFolder, MNT_FORCE | MNT_DETACH) == 0);
|
||||
int status = overlayMount(modsToInstall, steamGameFolder, gameUpperDir, gameWorkDir);
|
||||
if(status == 0) {
|
||||
printf("Everything is ready, just launch the game\n");
|
||||
} else if(status == -1) {
|
||||
printf("Could not mount the mods overlay, try to install fuse-overlay\n");
|
||||
return EXIT_FAILURE;
|
||||
} else if(status == -2) {
|
||||
printf("Could not mount the mods overlay\n");
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
printf("%d take a screenshot and go insult the dev that let this passthrough\n", __LINE__);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int setup(int argc, char ** argv) {
|
||||
if(argc != 3 ) return usage();
|
||||
char * appIdStr = argv[2];
|
||||
int appid = validateAppId(appIdStr);
|
||||
if(appid < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
int gameId = getGameIdFromAppId(appid);
|
||||
char * home = getHome();
|
||||
|
||||
char * gameUpperDir = g_build_filename(home, MANAGER_FILES, GAME_UPPER_DIR_NAME, appIdStr, NULL);
|
||||
char * gameWorkDir = g_build_filename(home, MANAGER_FILES, GAME_WORK_DIR_NAME, appIdStr, NULL);
|
||||
g_mkdir_with_parents(gameUpperDir, 0755);
|
||||
g_mkdir_with_parents(gameWorkDir, 0755);
|
||||
free(gameUpperDir);
|
||||
free(gameWorkDir);
|
||||
|
||||
const char * path = g_hash_table_lookup(gamePaths, &gameId);
|
||||
char * steamGameFolder = g_build_path("/", path, "steamapps/common", GAMES_NAMES[gameId], "Data", NULL);
|
||||
|
||||
char * gameFolder = g_build_filename(home, MANAGER_FILES, GAME_FOLDER_NAME, appIdStr, NULL);
|
||||
if(access(gameFolder, F_OK) == 0) {
|
||||
//if the game folder alredy exists just delete it
|
||||
//this will allow the removal of dlcs and language change
|
||||
delete(gameFolder, true);
|
||||
}
|
||||
g_mkdir_with_parents(gameFolder, 0755);
|
||||
|
||||
//links don't conflict with overlayfs and avoid coping 17Gb of files.
|
||||
//but links require the files to be on the same filesystem
|
||||
const int returnValue = copy(steamGameFolder, gameFolder, CP_RECURSIVE | CP_NO_TARGET_DIR | CP_LINK);
|
||||
if(returnValue < 0) copy(steamGameFolder, gameFolder, CP_RECURSIVE | CP_NO_TARGET_DIR);
|
||||
|
||||
free(steamGameFolder);
|
||||
free(gameFolder);
|
||||
printf("Done\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int unbind(int argc, char ** argv) {
|
||||
if(argc != 3 ) return usage();
|
||||
char * appIdStr = argv[2];
|
||||
int appid = validateAppId(appIdStr);
|
||||
if(appid < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
int gameId = getGameIdFromAppId(appid);
|
||||
const char * path = g_hash_table_lookup(gamePaths, &gameId);
|
||||
char * steamGameFolder = g_build_path("/", path, "steamapps/common", GAMES_NAMES[gameId], "Data", NULL);
|
||||
while(umount2(steamGameFolder, MNT_FORCE | MNT_DETACH));
|
||||
|
||||
free(steamGameFolder);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
if(argc < 2 ) return usage();
|
||||
|
||||
|
||||
if(audit_getloginuid() == 0) {
|
||||
printf("Root user should not be using this\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
gamePaths = search_games();
|
||||
|
||||
|
||||
|
||||
|
||||
char * configFolder = g_build_filename(getHome(), MANAGER_FILES, NULL);
|
||||
if(access(configFolder, F_OK) != 0) {
|
||||
//check to NOT run this as root
|
||||
//creating folder as root will causes lots of headache
|
||||
if(getuid() == 0) {
|
||||
printf("For the first please run without sudo\n");
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
//leading 0 == octal
|
||||
int i = g_mkdir_with_parents(configFolder, 0755);
|
||||
if(i < 0) {
|
||||
printf("Could not create configs, check access rights for this path: %s", configFolder);
|
||||
}
|
||||
//try to enable casefold
|
||||
//failure would have no impact
|
||||
char * chattrcommand = g_strjoin("", "chattr +F ",configFolder, " 2> /dev/null", NULL);
|
||||
system(chattrcommand);
|
||||
free(chattrcommand);
|
||||
}
|
||||
}
|
||||
free(configFolder);
|
||||
|
||||
|
||||
if(strcmp(argv[1], "--list-games") == 0 || strcmp(argv[1], "-l") == 0) {
|
||||
if(isRoot()) return noRoot();
|
||||
return listGames(argc, argv);
|
||||
} else if(strcmp(argv[1], "--add") == 0 || strcmp(argv[1], "-a") == 0) {
|
||||
if(isRoot()) return noRoot();
|
||||
return add(argc, argv);
|
||||
} else if(strcmp(argv[1], "--list-mods") == 0 || strcmp(argv[1], "-m") == 0) {
|
||||
if(isRoot()) return noRoot();
|
||||
return listMods(argc, argv);
|
||||
} else if(strcmp(argv[1], "--install") == 0 || strcmp(argv[1], "-i") == 0) {
|
||||
if(isRoot()) return noRoot();
|
||||
return installAndRemoveMod(argc, argv, true);
|
||||
} else if(strcmp(argv[1], "--remove") == 0 || strcmp(argv[1], "-r") == 0) {
|
||||
if(isRoot()) return noRoot();
|
||||
return installAndRemoveMod(argc, argv, false);
|
||||
} else if(strcmp(argv[1], "--deploy") == 0 || strcmp(argv[1], "-d") == 0) {
|
||||
if(!isRoot()) return needRoot();
|
||||
return deploy(argc, argv);
|
||||
} else if(strcmp(argv[1], "--unbind") == 0 || strcmp(argv[1], "-u") == 0){
|
||||
if(!isRoot()) return needRoot();
|
||||
return unbind(argc, argv);
|
||||
} else if(strcmp(argv[1], "--setup") == 0) {
|
||||
if(isRoot()) return noRoot();
|
||||
return setup(argc, argv);
|
||||
} else {
|
||||
return usage();
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#define APP_NAME "modmanager"
|
||||
//relative to home the url is not preprocessed so ../ might prose some issues
|
||||
// in c "A" "B" is the same as "AB"
|
||||
#define MANAGER_FILES ".local/share/" APP_NAME
|
||||
#define MOD_FOLDER_NAME "MOD_FOLDER"
|
||||
#define GAME_FOLDER_NAME "GAME_FOLDER"
|
||||
#define GAME_UPPER_DIR_NAME "UPPER_DIRS"
|
||||
#define GAME_WORK_DIR_NAME "WORK_DIRS"
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <sys/mount.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
#include <glib.h>
|
||||
#include "overlayfs.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int overlayMount(char ** sources, const char * dest, const char * upperdir, const char * workdir) {
|
||||
char * lowerdir = g_strjoinv(":", sources);
|
||||
char * mountData = g_strjoin("", "lowerdir=", lowerdir, ",workdir=", workdir, ",upperdir=", upperdir, NULL);
|
||||
int result = mount("overlay", "none", "overlay", MS_RDONLY, mountData);
|
||||
if(result < 0) {
|
||||
//this is very important for a lot of filesystems
|
||||
//even ext4 can be incompatible with the kernel's implementation
|
||||
//with some flags such as casefold
|
||||
if(access("/usr/bin/fuse-overlayfs", F_OK) == 0) {
|
||||
|
||||
int pid = fork();
|
||||
if(pid == 0) {
|
||||
//exit is used to get the return value when using waitpid
|
||||
exit(execl("/usr/bin/fuse-overlayfs", "/usr/bin/fuse-overlayfs", "-r", "-o", mountData, dest, NULL));
|
||||
} else {
|
||||
|
||||
int returnValue = 0;
|
||||
waitpid(pid, &returnValue, 0);
|
||||
free(lowerdir);
|
||||
free(mountData);
|
||||
if(returnValue != 0) {
|
||||
return -2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} else {
|
||||
free(lowerdir);
|
||||
free(mountData);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
free(lowerdir);
|
||||
free(mountData);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef __OVERLAY_H__
|
||||
#define __OVERLAY_H__
|
||||
|
||||
int overlayMount(char ** sources, const char * dest, const char * upperdir, const char * workdir);
|
||||
|
||||
#endif
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
#include "steam.h"
|
||||
#include "macro.h"
|
||||
#include "getHome.h"
|
||||
#include <unistd.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <wordexp.h>
|
||||
|
||||
//TODO: optimise and replace reallocs
|
||||
|
||||
enum FieldIds { FIELD_PATH, FIELD_LABEL, FIELD_CONTENT_ID, FIELD_TOTAL_SIZE, FIELD_CLEAN_BYTES, FIELD_CORRUPTION, FIELD_APPS };
|
||||
|
||||
int getFiledId(const char * field) {
|
||||
//replace with a hash + switch
|
||||
if(strcmp(field, "path") == 0) {
|
||||
return FIELD_PATH;
|
||||
} else if(strcmp(field, "label") == 0) {
|
||||
return FIELD_LABEL;
|
||||
} else if(strcmp(field, "contentid") == 0) {
|
||||
return FIELD_CONTENT_ID;
|
||||
} else if(strcmp(field, "totalsize") == 0) {
|
||||
return FIELD_TOTAL_SIZE;
|
||||
} else if(strcmp(field, "update_clean_bytes_tally") == 0) {
|
||||
return FIELD_CLEAN_BYTES;
|
||||
} else if(strcmp(field, "time_last_update_corruption") == 0) {
|
||||
return FIELD_CORRUPTION;
|
||||
} else if(strcmp(field, "apps") == 0) {
|
||||
return FIELD_APPS;
|
||||
} else {
|
||||
printf("unknown field");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
ValveLibraries_t * parseVDF(const char * path, size_t * size) {
|
||||
FILE * fd = fopen(path, "r");
|
||||
char * line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t read;
|
||||
|
||||
|
||||
|
||||
bool inQuotes = false;
|
||||
|
||||
ValveLibraries_t * libraries = NULL;
|
||||
*size = 0;
|
||||
|
||||
//skip the "libraryfolders" label & the first opening brace
|
||||
getline(&line, &len, fd);
|
||||
getline(&line, &len, fd);
|
||||
|
||||
int braceDepth = 0;
|
||||
|
||||
//can support up to 1024 car in quotes;
|
||||
char buffer[1024];
|
||||
bool isAppId = TRUE;
|
||||
int bufferIndex = 0;
|
||||
int nextFieldToFill = -1;
|
||||
|
||||
while (getline(&line, &len, fd) != -1) {
|
||||
for(int i = 0; line[i] != '\0'; i++) {
|
||||
switch (line[i]) {
|
||||
case '"':
|
||||
if(braceDepth == 0) {
|
||||
//this is a library id so we don't care.
|
||||
bufferIndex = 0;
|
||||
} else {
|
||||
//actual data
|
||||
if(inQuotes) {
|
||||
inQuotes = FALSE;
|
||||
buffer[bufferIndex] = '\0';
|
||||
if(nextFieldToFill == -1) {
|
||||
nextFieldToFill = getFiledId(buffer);
|
||||
|
||||
} else {
|
||||
char * value = strndup(buffer, bufferIndex);
|
||||
ValveLibraries_t * library = &libraries[*size - 1];
|
||||
switch (nextFieldToFill) {
|
||||
case FIELD_PATH:
|
||||
library->path = value;
|
||||
break;
|
||||
|
||||
case FIELD_LABEL:
|
||||
library->label = value;
|
||||
break;
|
||||
|
||||
case FIELD_CONTENT_ID:
|
||||
library->contentId = value;
|
||||
break;
|
||||
|
||||
case FIELD_TOTAL_SIZE:
|
||||
library->totalSize = strtoul(value, NULL, 0);
|
||||
break;
|
||||
|
||||
case FIELD_CLEAN_BYTES:
|
||||
library->update_clean_bytes_tally = value;
|
||||
break;
|
||||
|
||||
case FIELD_CORRUPTION:
|
||||
library->time_last_update_corruption = value;
|
||||
break;
|
||||
|
||||
case FIELD_APPS:
|
||||
if(isAppId) {
|
||||
library->appsCount++;
|
||||
library->apps = realloc(library->apps, library->appsCount * sizeof(ValveApp_t));
|
||||
unsigned int appid = strtoul(value, NULL, 10);
|
||||
library->apps[library->appsCount - 1].appid = appid;
|
||||
} else {
|
||||
library->apps[library->appsCount - 1].update = strtoul(value, NULL, 10);
|
||||
}
|
||||
isAppId = !isAppId;
|
||||
break;
|
||||
}
|
||||
//field apps is using braces so the syntax is different
|
||||
if(nextFieldToFill != FIELD_APPS)nextFieldToFill = -1;
|
||||
}
|
||||
} else {
|
||||
inQuotes = TRUE;
|
||||
}
|
||||
bufferIndex = 0;
|
||||
|
||||
}
|
||||
break;
|
||||
case '{':
|
||||
braceDepth++;
|
||||
if(braceDepth == 1) {
|
||||
*size += 1;
|
||||
libraries = realloc(libraries, sizeof(ValveLibraries_t) * (*size));
|
||||
memset(&libraries[*size - 1], 0, sizeof(ValveLibraries_t));
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
if(inQuotes) {
|
||||
printf("Syntax error in VDF\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(nextFieldToFill == FIELD_APPS) {
|
||||
nextFieldToFill = -1;
|
||||
}
|
||||
braceDepth--;
|
||||
break;
|
||||
|
||||
default:
|
||||
if(inQuotes) {
|
||||
buffer[bufferIndex++] = line[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fd);
|
||||
return libraries;
|
||||
}
|
||||
|
||||
GHashTable* search_games() {
|
||||
ValveLibraries_t * libraries = NULL;
|
||||
size_t size = 0;
|
||||
const char * HOME = getHome();
|
||||
|
||||
for(int i = 0; i < LEN(steamLibraries); i++) {
|
||||
char * path = g_build_filename(HOME, steamLibraries[i], "steamapps/libraryfolders.vdf", NULL);
|
||||
if (access(path, F_OK) == 0) {
|
||||
libraries = parseVDF(path, &size);
|
||||
break;
|
||||
}
|
||||
|
||||
g_free(path);
|
||||
}
|
||||
|
||||
GHashTable* table = g_hash_table_new(g_int_hash, g_int_equal);
|
||||
|
||||
//fill the table
|
||||
for(int i = 0; i < size; i++) {
|
||||
for(int j = 0; j < libraries[i].appsCount; j++) {
|
||||
int gameId = getGameIdFromAppId(libraries[i].apps[j].appid);
|
||||
if(gameId >= 0) {
|
||||
int * key = malloc(sizeof(int));
|
||||
*key = gameId;
|
||||
g_hash_table_insert(table, key, strdup(libraries[i].path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//free used up memory
|
||||
for(int i = 0; i < size; i++) {
|
||||
free(libraries[i].path);
|
||||
free(libraries[i].label);
|
||||
free(libraries[i].contentId);
|
||||
free(libraries[i].update_clean_bytes_tally);
|
||||
free(libraries[i].time_last_update_corruption);
|
||||
free(libraries[i].apps);
|
||||
}
|
||||
|
||||
if(libraries != NULL) free(libraries);
|
||||
return table;
|
||||
}
|
||||
|
||||
|
||||
int getGameIdFromAppId(u_int32_t id) {
|
||||
for(int k = 0; k < LEN(GAMES_APPIDS); k++) {
|
||||
if(id == GAMES_APPIDS[k]) {
|
||||
return k;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
#ifndef __STEAM_H__
|
||||
#define __STEAM_H__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <glib.h>
|
||||
|
||||
typedef struct ValveApp {
|
||||
unsigned int appid;
|
||||
unsigned int update;
|
||||
} ValveApp_t;
|
||||
|
||||
typedef struct ValveLibraries {
|
||||
char * path;
|
||||
char * label;
|
||||
char * contentId;
|
||||
unsigned long totalSize;
|
||||
char * update_clean_bytes_tally;
|
||||
char * time_last_update_corruption;
|
||||
ValveApp_t * apps;
|
||||
size_t appsCount;
|
||||
} ValveLibraries_t;
|
||||
|
||||
// relative to the home directory
|
||||
const static char * steamLibraries[] = {
|
||||
"/.steam/root/",
|
||||
"/.steam/steam/",
|
||||
"/.local/share/steam",
|
||||
//flatpack steam.
|
||||
"/.var/app/com.valvesoftware.Steam/.local/share/Steam"
|
||||
};
|
||||
|
||||
//todo add the older games
|
||||
// order has to be the same as in GAMES_NAMES
|
||||
const static u_int32_t GAMES_APPIDS[] = {
|
||||
489830,
|
||||
};
|
||||
|
||||
//the name of the game in the steamapps/common folder
|
||||
const static char * GAMES_NAMES[] = {
|
||||
"Skyrim Special Edition",
|
||||
};
|
||||
|
||||
GHashTable * search_games();
|
||||
int getGameIdFromAppId(u_int32_t id);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user