From: Adam Sutton Date: Fri, 2 Nov 2012 16:32:28 +0000 (+0000) Subject: util: Add util function to remove an entire directory tree (dangerous?). X-Git-Tag: v3.5~259 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41f1c671f07af57e590c23ea2d4ab7607e8c4df7;p=thirdparty%2Ftvheadend.git util: Add util function to remove an entire directory tree (dangerous?). --- diff --git a/src/tvheadend.h b/src/tvheadend.h index 64a3318d3..f03ae96d0 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -482,6 +482,8 @@ char *md5sum ( const char *str ); int makedirs ( const char *path, int mode ); +int rmtree ( const char *path ); + /* printing */ #if __SIZEOF_LONG__ == 8 #define PRItime_t PRId64 diff --git a/src/utils.c b/src/utils.c index 7aaca13ab..0ed2181da 100644 --- a/src/utils.c +++ b/src/utils.c @@ -22,6 +22,9 @@ #include #include #include +#include +#include +#include #include "tvheadend.h" /** @@ -373,3 +376,30 @@ makedirs ( const char *inpath, int mode ) } return 0; } + +int +rmtree ( const char *path ) +{ + int err; + struct dirent de, *der; + struct stat st; + char buf[512]; + DIR *dir = opendir(path); + if (!dir) return -1; + while (!readdir_r(dir, &de, &der) && der) { + if (!strcmp("..", de.d_name) || !strcmp(".", de.d_name)) + continue; + snprintf(buf, sizeof(buf), "%s/%s", path, de.d_name); + err = stat(buf, &st); + if (err) break; + if (S_ISDIR(st.st_mode)) + err = rmtree(buf); + else + err = unlink(buf); + if (err) break; + } + closedir(dir); + if (!err) + err = rmdir(path); + return err; +}