diff --git a/src/tvheadend.h b/src/tvheadend.h index 64a3318d..f03ae96d 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 7aaca13a..0ed2181d 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; +}