Fix a memory leak if parent directory creation failed.
[oweals/busybox.git] / libbb / remove_file.c
index 52b3211e652eb652eb4c5df2f04d71df8e33890f..3b84680c4d03647903bf83d24fd186ca5940ce5c 100644 (file)
@@ -29,6 +29,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.h>
 #include <getopt.h>
 #include "libbb.h"
 
@@ -37,7 +38,7 @@ extern int remove_file(const char *path, int flags)
        struct stat path_stat;
        int path_exists = 1;
 
-       if (stat(path, &path_stat) < 0) {
+       if (lstat(path, &path_stat) < 0) {
                if (errno != ENOENT) {
                        perror_msg("unable to stat `%s'", path);
                        return -1;
@@ -110,6 +111,7 @@ extern int remove_file(const char *path, int flags)
                return status;
        } else {
                if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
+                                       !S_ISLNK(path_stat.st_mode) &&
                                        isatty(0)) ||
                                (flags & FILEUTILS_INTERACTIVE)) {
                        fprintf(stderr, "%s: remove `%s'? ", applet_name, path);