]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
util: Add util function to remove an entire directory tree (dangerous?).
authorAdam Sutton <dev@adamsutton.me.uk>
Fri, 2 Nov 2012 16:32:28 +0000 (16:32 +0000)
committerAdam Sutton <dev@adamsutton.me.uk>
Wed, 28 Nov 2012 09:43:13 +0000 (09:43 +0000)
src/tvheadend.h
src/utils.c

index 64a3318d358e0d06999db8bccfc80f6e1f2d5b46..f03ae96d0db2d01e3a2a8db7cfc4371a3f13a90f 100644 (file)
@@ -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
index 7aaca13abcae01a45c4d1c3605fc9d931c8c8c96..0ed2181daf7aeeda717d11af97ca9ecabe16e681 100644 (file)
@@ -22,6 +22,9 @@
 #include <assert.h>
 #include <openssl/md5.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <unistd.h>
 #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;
+}