diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2022-10-23 13:26:48 +0100 |
---|---|---|
committer | Michael Drake <mdrake.unique@gmail.com> | 2022-10-23 17:22:56 +0100 |
commit | ef00567b029ec007ceab342a2ed1addaa5f63be6 (patch) | |
tree | b1319d25b209aa20e6ed1f9a2c390367d015ae29 /utils | |
parent | 5fed462e88c8ec7ec25a668afa5b3f4b94eea4bb (diff) | |
download | netsurf-ef00567b029ec007ceab342a2ed1addaa5f63be6.tar.gz netsurf-ef00567b029ec007ceab342a2ed1addaa5f63be6.tar.bz2 |
utils: file: Use fstatat and unlinkat
Diffstat (limited to 'utils')
-rw-r--r-- | utils/file.c | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/utils/file.c b/utils/file.c index c460e49e9..3cf12d540 100644 --- a/utils/file.c +++ b/utils/file.c @@ -26,6 +26,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> +#include <fcntl.h> #include <errno.h> #include "desktop/gui_internal.h" @@ -322,12 +323,23 @@ netsurf_recursive_rm(const char *path) int nentries, ent; nserror ret = NSERROR_OK; struct stat ent_stat; /* stat result of leaf entry */ - char *leafpath = NULL; const char *leafname; + int dirfd; - nentries = scandir(path, &listing, 0, alphasort); + dirfd = open(path, O_DIRECTORY); + if (dirfd == -1) { + switch (errno) { + case ENOENT: + return NSERROR_NOT_FOUND; + default: + return NSERROR_UNKNOWN; + } + } + nentries = scandir(path, &listing, 0, alphasort); if (nentries < 0) { + close(dirfd); + switch (errno) { case ENOENT: return NSERROR_NOT_FOUND; @@ -341,21 +353,26 @@ netsurf_recursive_rm(const char *path) if (strcmp(leafname, ".") == 0 || strcmp(leafname, "..") == 0) continue; - ret = netsurf_mkpath(&leafpath, NULL, 2, path, leafname); - if (ret != NSERROR_OK) goto out; - if (stat(leafpath, &ent_stat) != 0) { + if (fstatat(dirfd, leafname, &ent_stat, + AT_SYMLINK_NOFOLLOW) != 0) { goto out_via_errno; } if (S_ISDIR(ent_stat.st_mode)) { + char *leafpath = NULL; + + ret = netsurf_mkpath(&leafpath, NULL, 2, path, leafname); + if (ret != NSERROR_OK) + goto out; + ret = netsurf_recursive_rm(leafpath); - if (ret != NSERROR_OK) goto out; + free(leafpath); + if (ret != NSERROR_OK) + goto out; } else { - if (unlink(leafpath) != 0) { + if (unlinkat(dirfd, leafname, 0) != 0) { goto out_via_errno; } } - free(leafpath); - leafpath = NULL; } if (rmdir(path) != 0) { @@ -380,9 +397,7 @@ out: free(listing); } - if (leafpath != NULL) { - free(leafpath); - } + close(dirfd); return ret; } |