summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>2009-11-15 19:13:12 (EST)
committer graham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>2009-11-15 19:13:12 (EST)
commit9dd3216ffcf2c95e35ef097a0c8c3f2898bbde62 (patch)
treec61840a69a28f4b6f7770ee0537add8507bd6e09
parentec04e0fa180e0ad7c3ba118c2e40bb56663b3903 (diff)
Add a function for recursive directory removal and use that instead of xsystem.
git-svn-id: http://opkg.googlecode.com/svn/trunk@311 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358
-rw-r--r--libopkg/file_util.c81
-rw-r--r--libopkg/file_util.h1
-rw-r--r--libopkg/opkg_cmd.c6
-rw-r--r--libopkg/opkg_conf.c7
4 files changed, 84 insertions, 11 deletions
diff --git a/libopkg/file_util.c b/libopkg/file_util.c
index 902b8c9..08c801a 100644
--- a/libopkg/file_util.c
+++ b/libopkg/file_util.c
@@ -18,6 +18,7 @@
#include "includes.h"
#include <sys/types.h>
#include <sys/stat.h>
+#include <dirent.h>
#include "sprintf_alloc.h"
#include "file_util.h"
@@ -224,3 +225,83 @@ char *file_sha256sum_alloc(const char *file_name)
}
#endif
+
+
+int
+rm_r(const char *path)
+{
+ int ret = 0;
+ DIR *dir;
+ struct dirent *dent;
+
+ dir = opendir(path);
+ if (dir == NULL) {
+ perror_msg("%s: opendir(%s)", __FUNCTION__, path);
+ return -1;
+ }
+
+ if (fchdir(dirfd(dir)) == -1) {
+ perror_msg("%s: fchdir(%s)", __FUNCTION__, path);
+ closedir(dir);
+ return -1;
+ }
+
+ while (1) {
+ errno = 0;
+ if ((dent = readdir(dir)) == NULL) {
+ if (errno) {
+ perror_msg("%s: readdir(%s)",
+ __FUNCTION__, path);
+ ret = -1;
+ }
+ break;
+ }
+
+ if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
+ continue;
+
+#ifdef _BSD_SOURCE
+ if (dent->d_type == DT_DIR) {
+ if ((ret = rm_r(dent->d_name)) == -1)
+ break;
+ continue;
+ } else if (dent->d_type == DT_UNKNOWN)
+#endif
+ {
+ struct stat st;
+ if ((ret = lstat(dent->d_name, &st)) == -1) {
+ perror_msg("%s: lstat(%s)",
+ __FUNCTION__, dent->d_name);
+ break;
+ }
+ if (S_ISDIR(st.st_mode)) {
+ if ((ret = rm_r(dent->d_name)) == -1)
+ break;
+ continue;
+ }
+ }
+
+ if ((ret = unlink(dent->d_name)) == -1) {
+ perror_msg("%s: unlink(%s)",
+ __FUNCTION__, dent->d_name);
+ break;
+ }
+ }
+
+ if (chdir("..") == -1) {
+ ret = -1;
+ perror_msg("%s: chdir(%s/..)", __FUNCTION__, path);
+ }
+
+ if (rmdir(path) == -1 ) {
+ ret = -1;
+ perror_msg("%s: rmdir(%s)", __FUNCTION__, path);
+ }
+
+ if (closedir(dir) == -1) {
+ ret = -1;
+ perror_msg("%s: closedir(%s)", __FUNCTION__, path);
+ }
+
+ return ret;
+}
diff --git a/libopkg/file_util.h b/libopkg/file_util.h
index 184e04d..cfad551 100644
--- a/libopkg/file_util.h
+++ b/libopkg/file_util.h
@@ -26,5 +26,6 @@ int file_copy(const char *src, const char *dest);
int file_mkdir_hier(const char *path, long mode);
char *file_md5sum_alloc(const char *file_name);
char *file_sha256sum_alloc(const char *file_name);
+int rm_r(const char *path);
#endif
diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c
index 9a49082..c40694e 100644
--- a/libopkg/opkg_cmd.c
+++ b/libopkg/opkg_cmd.c
@@ -323,7 +323,6 @@ static opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
static int opkg_finalize_intercepts(opkg_intercept_t ctx)
{
- char *cmd;
DIR *dir;
int err = 0;
@@ -349,10 +348,7 @@ static int opkg_finalize_intercepts(opkg_intercept_t ctx)
} else
perror (ctx->statedir);
- sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
- err = xsystem (cmd);
- free (cmd);
-
+ rm_r(ctx->statedir);
free (ctx->statedir);
free (ctx);
diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c
index 425c72b..bd73317 100644
--- a/libopkg/opkg_conf.c
+++ b/libopkg/opkg_conf.c
@@ -341,12 +341,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
void opkg_conf_deinit(opkg_conf_t *conf)
{
- int err;
- char *cmd;
-
- sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
- err = xsystem(cmd);
- free(cmd);
+ rm_r(conf->tmp_dir);
free(conf->tmp_dir);
free(conf->lists_dir);