summaryrefslogtreecommitdiff
path: root/tools.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools.c')
-rw-r--r--tools.c52
1 files changed, 48 insertions, 4 deletions
diff --git a/tools.c b/tools.c
index cd2c60f..a3b27e5 100644
--- a/tools.c
+++ b/tools.c
@@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
- * $Id: tools.c 1.27 2001/01/13 15:35:02 kls Exp $
+ * $Id: tools.c 1.30 2001/02/11 14:44:22 kls Exp $
*/
#define _GNU_SOURCE
@@ -51,7 +51,7 @@ char *strreplace(char *s, char c1, char c2)
{
char *p = s;
- while (*p) {
+ while (p && *p) {
if (*p == c1)
*p = c2;
p++;
@@ -237,8 +237,10 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
}
}
dsyslog(LOG_INFO, "removing %s", FileName);
- if (remove(FileName) == 0)
- return true;
+ if (remove(FileName) < 0) {
+ LOG_ERROR_STR(FileName);
+ return false;
+ }
}
else if (errno != ENOENT) {
LOG_ERROR_STR(FileName);
@@ -247,6 +249,48 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
return true;
}
+bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
+{
+ DIR *d = opendir(DirName);
+ if (d) {
+ bool empty = true;
+ struct dirent *e;
+ while ((e = readdir(d)) != NULL) {
+ if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) {
+ char *buffer;
+ asprintf(&buffer, "%s/%s", DirName, e->d_name);
+ struct stat st;
+ if (stat(buffer, &st) == 0) {
+ if (S_ISDIR(st.st_mode)) {
+ if (!RemoveEmptyDirectories(buffer, true))
+ empty = false;
+ }
+ else
+ empty = false;
+ }
+ else {
+ LOG_ERROR_STR(buffer);
+ delete buffer;
+ return false;
+ }
+ delete buffer;
+ }
+ }
+ closedir(d);
+ if (RemoveThis && empty) {
+ dsyslog(LOG_INFO, "removing %s", DirName);
+ if (remove(DirName) < 0) {
+ LOG_ERROR_STR(DirName);
+ return false;
+ }
+ }
+ return empty;
+ }
+ else
+ LOG_ERROR_STR(DirName);
+ return false;
+}
+
char *ReadLink(const char *FileName)
{
char RealName[_POSIX_PATH_MAX];