Add a function for recursive directory removal and use that instead of xsystem.
[oweals/opkg-lede.git] / libopkg / file_util.c
index 41762575e56d9e94960cc5079590bec0775dd2b0..08c801a1501c261968ba842319aac3fdeeb8cdbe 100644 (file)
@@ -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"
@@ -75,15 +76,11 @@ char *file_read_line_alloc(FILE *file)
        buf_len = strlen(buf);
        if (line) {
            line_size += buf_len;
-           line = realloc(line, line_size);
-           if (line == NULL) {
-               fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
-               break;
-           }
+           line = xrealloc(line, line_size);
            strcat(line, buf);
        } else {
            line_size = buf_len + 1;
-           line = strdup(buf);
+           line = xstrdup(buf);
        }
        if (buf[buf_len - 1] == '\n') {
            break;
@@ -147,24 +144,23 @@ char *file_md5sum_alloc(const char *file_name)
     char *md5sum_hex;
     unsigned char md5sum_bin[md5sum_bin_len];
 
-    md5sum_hex = calloc(1, md5sum_hex_len + 1);
-    if (md5sum_hex == NULL) {
-       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
-       return strdup("");
-    }
+    md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
 
     file = fopen(file_name, "r");
     if (file == NULL) {
        fprintf(stderr, "%s: Failed to open file %s: %s\n",
                __FUNCTION__, file_name, strerror(errno));
-       return strdup("");
+       free(md5sum_hex);
+       return NULL;
     }
 
     err = md5_stream(file, md5sum_bin);
     if (err) {
        fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
                __FUNCTION__, file_name, strerror(err));
-       return strdup("");
+       fclose(file);
+       free(md5sum_hex);
+       return NULL;
     }
 
     fclose(file);
@@ -197,24 +193,23 @@ char *file_sha256sum_alloc(const char *file_name)
     char *sha256sum_hex;
     unsigned char sha256sum_bin[sha256sum_bin_len];
 
-    sha256sum_hex = calloc(1, sha256sum_hex_len + 1);
-    if (sha256sum_hex == NULL) {
-       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
-       return strdup("");
-    }
+    sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
 
     file = fopen(file_name, "r");
     if (file == NULL) {
        fprintf(stderr, "%s: Failed to open file %s: %s\n",
                __FUNCTION__, file_name, strerror(errno));
-       return strdup("");
+       free(sha256sum_hex);
+       return NULL;
     }
 
     err = sha256_stream(file, sha256sum_bin);
     if (err) {
        fprintf(stderr, "%s: ERROR computing sha256sum for %s: %s\n",
                __FUNCTION__, file_name, strerror(err));
-       return strdup("");
+       fclose(file);
+       free(sha256sum_hex);
+       return NULL;
     }
 
     fclose(file);
@@ -230,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;
+}