jffs2reset: fix recursive file/directory removal
authorFelix Fietkau <nbd@openwrt.org>
Sun, 11 Jan 2015 16:52:46 +0000 (17:52 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 11 Jan 2015 16:52:46 +0000 (17:52 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
jffs2reset.c

index 56848723a0a005530188d5cc9c26d329c651dc70..13edf1e2a8ec83c5cf4885310a6ef00839c54b8b 100644 (file)
 static int
 handle_rmdir(const char *dir)
 {
-       struct stat s;
-       struct dirent **namelist;
-       int n;
-
-       n = scandir(dir, &namelist, NULL, NULL);
+       struct dirent *dt;
+       struct stat st;
+       DIR *d;
+       int fd;
 
-       if (n < 1)
+       d = opendir(dir);
+       if (!d)
                return -1;
 
-       while (n--) {
-               char file[256];
+       fd = dirfd(d);
+
+       while ((dt = readdir(d)) != NULL) {
+               if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
+                       continue;
 
-               snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
-               if (!lstat(file, &s) && !S_ISDIR(s.st_mode))
-                       unlink(file);
-               free(namelist[n]);
+               unlinkat(fd, dt->d_name, 0);
        }
-       free(namelist);
 
+       closedir(d);
        rmdir(dir);
 
        return 0;