summaryrefslogtreecommitdiff
path: root/util/nvmutil
diff options
context:
space:
mode:
Diffstat (limited to 'util/nvmutil')
-rw-r--r--util/nvmutil/include/common.h3
-rw-r--r--util/nvmutil/lib/file.c143
2 files changed, 12 insertions, 134 deletions
diff --git a/util/nvmutil/include/common.h b/util/nvmutil/include/common.h
index 67969832..a546e464 100644
--- a/util/nvmutil/include/common.h
+++ b/util/nvmutil/include/common.h
@@ -523,9 +523,6 @@ int close_on_eintr(int fd);
int fsync_on_eintr(int fd);
int fs_rename_at(int olddirfd, const char *old,
int newdirfd, const char *new);
-int fs_rm_rf_at(int dirfd, const char *path);
-int fs_mkdir_p(const char *path, mode_t mode);
-int fs_mkdir_p_at(int dirfd, const char *path, mode_t mode);
int fs_open(const char *path, int flags);
struct filesystem *rootfs(void);
int fs_resolve_at(int dirfd, const char *path, int flags);
diff --git a/util/nvmutil/lib/file.c b/util/nvmutil/lib/file.c
index a490b358..b605d488 100644
--- a/util/nvmutil/lib/file.c
+++ b/util/nvmutil/lib/file.c
@@ -936,11 +936,23 @@ mkhtemp_try_create(int dirfd,
if (fd_verify_dir_identity(dirfd, st_dir_initial) < 0)
goto err;
+ if (fstat(*fd, &st_open) < 0)
+ goto err;
+
+ if (!S_ISDIR(st_open.st_mode)) {
+ errno = ENOTDIR;
+ goto err;
+ }
+
*fd = openat2p(dirfd, fname_copy,
O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0);
if (*fd < 0)
goto err;
+ /* NOTE: could check nlink count here,
+ * but it's not very reliable here. skipped.
+ */
+
if (fd_verify_dir_identity(dirfd, st_dir_initial) < 0)
goto err;
@@ -1883,137 +1895,6 @@ fs_rename_at(int olddirfd, const char *old,
return renameat(olddirfd, old, newdirfd, new);
}
-int
-fs_rm_rf_at(int dirfd, const char *path)
-{
- int fd = -1;
- struct stat st;
-
- if (path == NULL) {
- errno = EFAULT;
- return -1;
- }
-
- fd = fs_resolve_at(dirfd, path, O_RDONLY | O_DIRECTORY);
- if (fd >= 0) {
- /* directory */
- /* iterate entries */
- /* recurse */
- /* unlinkat(dirfd, path, AT_REMOVEDIR) */
-
- return 0;
- }
-
- /* fallback: file */
- return unlinkat(dirfd, path, 0);
-}
-
-int
-fs_mkdir_p(const char *path, mode_t mode)
-{
- struct filesystem *fs;
-
- if (path == NULL) {
- errno = EFAULT;
- return -1;
- }
- if (path[0] != '/') {
- errno = EINVAL;
- return -1;
- }
-
- fs = rootfs();
- if (fs == NULL)
- return -1;
-
- return fs_mkdir_p_at(fs->rootfd, path + 1, mode);
-}
-
-/* implementation of: mkdir -p
- */
-int
-fs_mkdir_p_at(int dirfd, const char *path, mode_t mode)
-{
- const char *p = path;
- char name[256];
- int nextfd = -1;
- struct stat st_parent_initial;
- struct stat st_parent_now;
- int saved_errno = errno;
- int close_errno;
- int dir_created = 0;
- int r;
-
- if (path == NULL) {
-
- errno = EFAULT;
- return -1;
- }
-
- if (dirfd < 0 || *path == '\0') {
-
- errno = EINVAL;
- return -1;
- }
-
- if (fstat(dirfd, &st_parent_initial) < 0)
- return -1;
-
- for (;;) {
-
- r = fs_next_component(&p, name, sizeof(name));
- if (r < 0)
- goto err;
- if (r == 0)
- break;
-
- /* check parent integrity */
- if (fd_verify_identity(dirfd, &st_parent_initial,
- &st_parent_now) < 0)
- goto err;
-
- nextfd = openat2p(dirfd, name,
- O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0);
-
- if (nextfd < 0) {
- if (errno != ENOENT)
- goto err;
-
- if (mkdirat_on_eintr(dirfd, name, mode) < 0)
- goto err;
-
- dir_created = 1;
-
- nextfd = openat2p(dirfd, name,
- O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0);
- if (nextfd < 0)
- goto err;
- }
-
- close_errno = errno;
- (void)close_on_eintr(dirfd);
- errno = close_errno;
- dirfd = nextfd;
- nextfd = -1;
-
- st_parent_initial = st_parent_now;
- }
-
- errno = saved_errno;
- return (0);
-
-err:
- if (dirfd >= 0)
- (void) close_on_eintr(dirfd);
- if (nextfd >= 0)
- (void) close_on_eintr(nextfd);
- if (dir_created)
- (void) unlinkat(dirfd, name, AT_REMOVEDIR);
-
- errno = saved_errno;
- return (-1);
-}
-
/* secure open, based on
* relative path to root
*