diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2022-07-22 21:02:27 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2022-07-22 21:02:27 +0200 |
commit | 6dcffd4f688c22b1015d473e059c4af2ce704db5 (patch) | |
tree | 9833383dfd9de04485ef75de6ea3334aa3bc98b7 | |
parent | 4e4db5b28319c51a8621cec49ea1aaf8ff78ac2a (diff) | |
download | polecat-6dcffd4f688c22b1015d473e059c4af2ce704db5.tar.gz polecat-6dcffd4f688c22b1015d473e059c4af2ce704db5.zip |
move filesystem logic into separate file
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/common.c | 112 | ||||
-rw-r--r-- | src/common.h | 7 | ||||
-rw-r--r-- | src/dxvk.c | 5 | ||||
-rw-r--r-- | src/fs.c | 136 | ||||
-rw-r--r-- | src/fs.h | 20 | ||||
-rw-r--r-- | src/lutris.c | 3 | ||||
-rw-r--r-- | src/wine.c | 7 |
8 files changed, 168 insertions, 125 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index de3d40c..4b45fec 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,9 @@ list(APPEND ${CMAKE_CURRENT_SOURCE_DIR}/common.h ${CMAKE_CURRENT_SOURCE_DIR}/config.c ${CMAKE_CURRENT_SOURCE_DIR}/config.h + ${CMAKE_CURRENT_SOURCE_DIR}/defines.h + ${CMAKE_CURRENT_SOURCE_DIR}/fs.c + ${CMAKE_CURRENT_SOURCE_DIR}/fs.h ${CMAKE_CURRENT_SOURCE_DIR}/main.c ${CMAKE_CURRENT_SOURCE_DIR}/main.h ${CMAKE_CURRENT_SOURCE_DIR}/net.c diff --git a/src/common.c b/src/common.c index f12b3d6..59e6fa6 100644 --- a/src/common.c +++ b/src/common.c @@ -75,115 +75,3 @@ void print_help(const struct Command* commands, const size_t commands_size, } } } - -struct stat getStat(const char* path) -{ - // fill with 0s by default in the case stat fails - struct stat sb = {0}; - - // the return value signifies if stat failes (e.g. file not found) - // unimportant for us if it fails it won't touch sb - stat(path, &sb); - - return sb; -} - -int isFile(const char* path) -{ - struct stat sb = getStat(path); - - return S_ISREG(sb.st_mode) -#ifndef _WIN32 - || S_ISLNK(sb.st_mode) -#endif - ; -} - -int isDir(const char* path) -{ - struct stat sb = getStat(path); - - return S_ISDIR(sb.st_mode) -#ifndef _WIN32 - || S_ISLNK(sb.st_mode) -#endif - ; -} - -int makeDir(const char* path) -{ - char pathcpy[PATH_MAX]; - char *index; - - strncpy(pathcpy, path, PATH_MAX-1); // make a mutable copy of the path - - for(index = pathcpy+1; *index; ++index) - { - - if (*index == '/') - { - *index = '\0'; - - if (mkdir(pathcpy, 0755) != 0) - { - if (errno != EEXIST) - return -1; - } - - *index = '/'; - } - } - - return mkdir(path, 0755); -} - -int removeDir(const char *path) -{ - DIR *d = opendir(path); - size_t path_len = strlen(path); - int r = -1; - - if (d) { - struct dirent *p; - - r = 0; - while (!r && (p=readdir(d))) { - char *buf; - size_t len; - - // Skip the names "." and ".." as we don't want to recurse on them. - if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) - continue; - - len = path_len + strlen(p->d_name) + 2; - buf = malloc(len); - - if (buf) { - struct stat statbuf = {0}; - - snprintf(buf, len, "%s/%s", path, p->d_name); - if (!stat(buf, &statbuf)) { - if (S_ISDIR(statbuf.st_mode)) - r = removeDir(buf); -#ifndef _WIN32 - else if (S_ISLNK(statbuf.st_mode)) - r = unlink(buf); -#endif - else - r = remove(buf); - } - else // it is very likely that we found a dangling symlink which is not detected by stat - { - r = unlink(buf); - } - free(buf); - } - } - closedir(d); - } - - if (!r) - r = rmdir(path); - - return r; -} diff --git a/src/common.h b/src/common.h index d4cce1d..d57fdb4 100644 --- a/src/common.h +++ b/src/common.h @@ -43,11 +43,4 @@ int set_no_net(UNUSED int argc, UNUSED char** argv); void print_help(const struct Command*, size_t, const struct Flag*, size_t); -struct stat getStat(const char* path); -int isFile(const char*); -int isDir(const char*); - -int makeDir(const char* path); -int removeDir(const char *path); - #endif @@ -6,10 +6,11 @@ #include <dirent.h> #include <json.h> -#include "dxvk.h" +#include "config.h" +#include "fs.h" #include "net.h" #include "tar.h" -#include "config.h" +#include "dxvk.h" static const struct Command dxvk_commands[] = { { .name = "download", .func = dxvk_download, .description = "download and install a dxvk version" }, diff --git a/src/fs.c b/src/fs.c new file mode 100644 index 0000000..0c4a79d --- /dev/null +++ b/src/fs.c @@ -0,0 +1,136 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> +#include <limits.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> + +#include "fs.h" + +#ifdef _WIN32 +#define mkdir(path, perm) mkdir(path) +#endif + +static struct stat getStat(const char* path) +{ + // fill with 0s by default in the case stat fails + struct stat sb = {0}; + + // the return value signifies if stat failes (e.g. file not found) + // unimportant for us if it fails it won't touch sb + stat(path, &sb); + + return sb; +} + +int isFile(const char* path) +{ + struct stat sb = getStat(path); + +#ifndef _WIN32 + if (S_ISLNK(sb.st_mode)) + { + char buf[PATH_MAX]; + readlink(path, buf, sizeof(buf)); + + return isFile(buf); + } +#endif + return S_ISREG(sb.st_mode); +} + +int isDir(const char* path) +{ + struct stat sb = getStat(path); + +#ifndef _WIN32 + if (S_ISLNK(sb.st_mode)) + { + char buf[PATH_MAX]; + readlink(path, buf, sizeof(buf)); + + return isDir(buf); + } +#endif + return S_ISDIR(sb.st_mode); +} + +int makeDir(const char* path) +{ + char pathcpy[PATH_MAX]; + char *index; + + strncpy(pathcpy, path, PATH_MAX-1); // make a mutable copy of the path + + for(index = pathcpy+1; *index; ++index) + { + + if (*index == OS_PATH_SEP) + { + *index = '\0'; + + if (mkdir(pathcpy, 0755) != 0) + { + if (errno != EEXIST) + return -1; + } + + *index = OS_PATH_SEP; + } + } + + return mkdir(path, 0755); +} + +int removeDir(const char* path) +{ + DIR *d = opendir(path); + size_t path_len = strlen(path); + int r = -1; + + if (d) { + struct dirent *p; + + r = 0; + while (!r && (p = readdir(d))) { + char *buf; + size_t len; + + // Skip the names "." and ".." as we don't want to recurse on them. + if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) + continue; + + len = path_len + strlen(p->d_name) + 2; + buf = malloc(len); + + if (buf) { + struct stat statbuf = {0}; + + snprintf(buf, len, "%s/%s", path, p->d_name); + if (!stat(buf, &statbuf)) { + if (S_ISDIR(statbuf.st_mode)) + r = removeDir(buf); +#ifndef _WIN32 + else if (S_ISLNK(statbuf.st_mode)) + r = unlink(buf); +#endif + else + r = remove(buf); + } + else // it is very likely that we found a dangling symlink which is not detected by stat + { + r = unlink(buf); + } + free(buf); + } + } + closedir(d); + } + + if (!r) + r = rmdir(path); + + return r; +} diff --git a/src/fs.h b/src/fs.h new file mode 100644 index 0000000..73b5d90 --- /dev/null +++ b/src/fs.h @@ -0,0 +1,20 @@ +#ifndef FS_H +#define FS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define OS_PATH_SEP '/' + +int isFile(const char*); +int isDir(const char*); + +int makeDir(const char*); +int removeDir(const char*); + +#ifdef __cplusplus +} +#endif + +#endif
\ No newline at end of file diff --git a/src/lutris.c b/src/lutris.c index fdf470c..8107ada 100644 --- a/src/lutris.c +++ b/src/lutris.c @@ -6,8 +6,9 @@ #include <libgen.h> #include <sys/stat.h> -#include "lutris.h" +#include "fs.h" #include "net.h" +#include "lutris.h" #define PERCENT_SPACE "%20" @@ -8,11 +8,12 @@ #include <sys/utsname.h> #include <json.h> -#include "wine.h" +#include "common.h" +#include "config.h" +#include "fs.h" #include "net.h" #include "tar.h" -#include "config.h" -#include "common.h" +#include "wine.h" static const struct Command winecmd_commands[] = { { .name = "download", .func = winecmd_download, .description = "download and extract a wine version" }, |