[pacman-dev] [PATCH 1/5] common: move rmrf to util-common

Jonas Witschel diabonas at gmx.de
Wed Jul 17 20:06:49 UTC 2019


We will need to remove temporary directories in libalpm, therefore move
this helper function to the common utilities.

Signed-off-by: Jonas Witschel <diabonas at gmx.de>
---
 src/common/util-common.c | 42 ++++++++++++++++++++++++++++++++++++++++
 src/common/util-common.h |  2 ++
 src/pacman/util.c        | 40 --------------------------------------
 src/pacman/util.h        |  1 -
 4 files changed, 44 insertions(+), 41 deletions(-)

diff --git a/src/common/util-common.c b/src/common/util-common.c
index 3aa0eac9..098ea890 100644
--- a/src/common/util-common.c
+++ b/src/common/util-common.c
@@ -21,6 +21,8 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <dirent.h>
+#include <unistd.h>

 #include "util-common.h"

@@ -197,3 +199,43 @@ char *strndup(const char *s, size_t n)
 	return (char *)memcpy(new, s, len);
 }
 #endif
+
+/* does the same thing as 'rm -rf' */
+int rmrf(const char *path)
+{
+	int errflag = 0;
+	struct dirent *dp;
+	DIR *dirp;
+
+	if(!unlink(path)) {
+		return 0;
+	} else {
+		switch(errno) {
+		case ENOENT:
+			return 0;
+		case EPERM:
+		case EISDIR:
+			break;
+		default:
+			/* not a directory */
+			return 1;
+		}
+
+		dirp = opendir(path);
+		if(!dirp) {
+			return 1;
+		}
+		for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
+			if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
+				char name[PATH_MAX];
+				snprintf(name, PATH_MAX, "%s/%s", path, dp->d_name);
+				errflag += rmrf(name);
+			}
+		}
+		closedir(dirp);
+		if(rmdir(path)) {
+			errflag++;
+		}
+		return errflag;
+	}
+}
diff --git a/src/common/util-common.h b/src/common/util-common.h
index 3434e1eb..6ab1150a 100644
--- a/src/common/util-common.h
+++ b/src/common/util-common.h
@@ -36,6 +36,8 @@ size_t strtrim(char *str);
 char *strndup(const char *s, size_t n);
 #endif

+int rmrf(const char *path);
+
 #define ARRAYSIZE(a) (sizeof (a) / sizeof (a[0]))

 #endif /* PM_UTIL_COMMON_H */
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 8f6290db..14fce9e6 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -247,46 +247,6 @@ unsigned short getcols(void)
 	return c;
 }

-/* does the same thing as 'rm -rf' */
-int rmrf(const char *path)
-{
-	int errflag = 0;
-	struct dirent *dp;
-	DIR *dirp;
-
-	if(!unlink(path)) {
-		return 0;
-	} else {
-		switch(errno) {
-		case ENOENT:
-			return 0;
-		case EPERM:
-		case EISDIR:
-			break;
-		default:
-			/* not a directory */
-			return 1;
-		}
-
-		dirp = opendir(path);
-		if(!dirp) {
-			return 1;
-		}
-		for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
-			if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
-				char name[PATH_MAX];
-				snprintf(name, PATH_MAX, "%s/%s", path, dp->d_name);
-				errflag += rmrf(name);
-			}
-		}
-		closedir(dirp);
-		if(rmdir(path)) {
-			errflag++;
-		}
-		return errflag;
-	}
-}
-
 /* output a string, but wrap words properly with a specified indentation
  */
 void indentprint(const char *str, unsigned short indent, unsigned short cols)
diff --git a/src/pacman/util.h b/src/pacman/util.h
index a1fbef46..1dd36882 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -53,7 +53,6 @@ int check_syncdbs(size_t need_repos, int check_valid);
 int sync_syncdbs(int level, alpm_list_t *syncs);
 unsigned short getcols(void);
 void columns_cache_reset(void);
-int rmrf(const char *path);
 void indentprint(const char *str, unsigned short indent, unsigned short cols);
 char *strreplace(const char *str, const char *needle, const char *replace);
 void string_display(const char *title, const char *string, unsigned short cols);
--
2.22.0


More information about the pacman-dev mailing list