Permalink
Browse files

fs-util: introduce mkfifoat_atomic() helper

  • Loading branch information...
fbuihuu committed Apr 27, 2018
1 parent 43231f0 commit 4fe3828c587e4324184fbf153606277058dbe33a
Showing with 26 additions and 0 deletions.
  1. +25 −0 src/basic/fs-util.c
  2. +1 −0 src/basic/fs-util.h
@@ -435,6 +435,31 @@ int mkfifo_atomic(const char *path, mode_t mode) {
return 0;
}

int mkfifoat_atomic(int dirfd, const char *path, mode_t mode) {
_cleanup_free_ char *t = NULL;
int r;

assert(path);

if (path_is_absolute(path))
return mkfifo_atomic(path, mode);

/* We're only interested in the (random) filename. */
r = tempfn_random_child("", NULL, &t);
if (r < 0)
return r;

if (mkfifoat(dirfd, t, mode) < 0)
return -errno;

if (renameat(dirfd, t, dirfd, path) < 0) {
unlink_noerrno(t);
return -errno;
}

return 0;
}

int get_files_in_directory(const char *path, char ***list) {
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
@@ -42,6 +42,7 @@ int symlink_idempotent(const char *from, const char *to);
int symlink_atomic(const char *from, const char *to);
int mknod_atomic(const char *path, mode_t mode, dev_t dev);
int mkfifo_atomic(const char *path, mode_t mode);
int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);

int get_files_in_directory(const char *path, char ***list);

0 comments on commit 4fe3828

Please sign in to comment.