summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2022-12-22 14:26:29 +0100
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-02-20 12:43:31 +0100
commit361bcb2097cd9c163f780863c9bd254b4c9c91c8 (patch)
tree32ecd0d60bb0ebeed5b254c10f386e06dd30397c
parente8858f1104d87179abd8d9c413292e42f1eaf7c0 (diff)
downloadsystemd-361bcb2097cd9c163f780863c9bd254b4c9c91c8.tar.gz
env-file: Add write_env_file_at()
-rw-r--r--src/basic/env-file.c9
-rw-r--r--src/basic/env-file.h6
2 files changed, 10 insertions, 5 deletions
diff --git a/src/basic/env-file.c b/src/basic/env-file.c
index 45b0d901c5..16de727c09 100644
--- a/src/basic/env-file.c
+++ b/src/basic/env-file.c
@@ -578,14 +578,15 @@ static void write_env_var(FILE *f, const char *v) {
fputc_unlocked('\n', f);
}
-int write_env_file(const char *fname, char **l) {
+int write_env_file_at(int dir_fd, const char *fname, char **l) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int r;
+ assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(fname);
- r = fopen_temporary(fname, &f, &p);
+ r = fopen_temporary_at(dir_fd, fname, &f, &p);
if (r < 0)
return r;
@@ -596,12 +597,12 @@ int write_env_file(const char *fname, char **l) {
r = fflush_and_check(f);
if (r >= 0) {
- if (rename(p, fname) >= 0)
+ if (renameat(dir_fd, p, dir_fd, fname) >= 0)
return 0;
r = -errno;
}
- (void) unlink(p);
+ (void) unlinkat(dir_fd, p, 0);
return r;
}
diff --git a/src/basic/env-file.h b/src/basic/env-file.h
index 2448d943cd..dc38b7a5c9 100644
--- a/src/basic/env-file.h
+++ b/src/basic/env-file.h
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
+#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
@@ -16,4 +17,7 @@ int load_env_file_pairs(FILE *f, const char *fname, char ***ret);
int merge_env_file(char ***env, FILE *f, const char *fname);
-int write_env_file(const char *fname, char **l);
+int write_env_file_at(int dir_fd, const char *fname, char **l);
+static inline int write_env_file(const char *fname, char **l) {
+ return write_env_file_at(AT_FDCWD, fname, l);
+}