aboutsummaryrefslogtreecommitdiff
path: root/src/common.c
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2020-12-11 21:27:41 +0100
committerJan200101 <sentrycraft123@gmail.com>2020-12-11 21:27:41 +0100
commit2da479e7a54e367e13e4a1f70987104a22372ebd (patch)
treeedc4c3828ff0dfbf2ef66d6cada8f76270bcdfcc /src/common.c
parent8dc996f6d89652d71c6c4fff87f7923bbfbb9a34 (diff)
downloadpolecat-2da479e7a54e367e13e4a1f70987104a22372ebd.tar.gz
polecat-2da479e7a54e367e13e4a1f70987104a22372ebd.zip
add remove commands to dxvk and wine
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c56
1 files changed, 54 insertions, 2 deletions
diff --git a/src/common.c b/src/common.c
index c5d9435..9b0ab51 100644
--- a/src/common.c
+++ b/src/common.c
@@ -3,6 +3,9 @@
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <dirent.h>
#include "common.h"
@@ -49,14 +52,63 @@ bool isDir(const char* path)
return S_ISDIR(sb.st_mode) || S_ISLNK(sb.st_mode);
}
-void makeDir(const char* path)
+int makeDir(const char* path)
{
// we do not care about the contents but what stat returns
struct stat sb;
if (stat(path, &sb) < 0)
{
- mkdir(path, 0755);
+ return mkdir(path, 0755);
}
+
+ return 0; // directory exists, pretend we made it
}
+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);
+ else if (S_ISLNK(statbuf.st_mode))
+ r = unlink(buf);
+ 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;
+} \ No newline at end of file