summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/util.c79
-rw-r--r--src/shared/util.h3
2 files changed, 56 insertions, 26 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index d93a9680fd..d840dedfc6 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -4013,7 +4013,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
assert(_f);
assert(_temp_path);
- t = strappend(path, ".XXXXXX");
+ t = tempfn_xxxxxx(path);
if (!t)
return -ENOMEM;
@@ -4125,42 +4125,21 @@ int vt_disallocate(const char *name) {
}
int symlink_atomic(const char *from, const char *to) {
- char *x;
- _cleanup_free_ char *t;
- const char *fn;
- size_t k;
- uint64_t u;
- unsigned i;
- int r;
+ _cleanup_free_ char *t = NULL;
assert(from);
assert(to);
- t = new(char, strlen(to) + 1 + 16 + 1);
+ t = tempfn_random(to);
if (!t)
return -ENOMEM;
- fn = basename(to);
- k = fn-to;
- memcpy(t, to, k);
- t[k] = '.';
- x = stpcpy(t+k+1, fn);
-
- u = random_u64();
- for (i = 0; i < 16; i++) {
- *(x++) = hexchar(u & 0xF);
- u >>= 4;
- }
-
- *x = 0;
-
if (symlink(from, t) < 0)
return -errno;
if (rename(t, to) < 0) {
- r = -errno;
- unlink(t);
- return r;
+ unlink_noerrno(t);
+ return -errno;
}
return 0;
@@ -6669,3 +6648,51 @@ int fflush_and_check(FILE *f) {
return 0;
}
+
+char *tempfn_xxxxxx(const char *p) {
+ const char *fn;
+ char *t;
+ size_t k;
+
+ assert(p);
+
+ t = new(char, strlen(p) + 1 + 6 + 1);
+ if (!t)
+ return NULL;
+
+ fn = basename(p);
+ k = fn - p;
+
+ strcpy(stpcpy(stpcpy(mempcpy(t, p, k), "."), fn), "XXXXXX");
+
+ return t;
+}
+
+char *tempfn_random(const char *p) {
+ const char *fn;
+ char *t, *x;
+ uint64_t u;
+ size_t k;
+ unsigned i;
+
+ assert(p);
+
+ t = new(char, strlen(p) + 1 + 16 + 1);
+ if (!t)
+ return NULL;
+
+ fn = basename(p);
+ k = fn - p;
+
+ x = stpcpy(stpcpy(mempcpy(t, p, k), "."), fn);
+
+ u = random_u64();
+ for (i = 0; i < 16; i++) {
+ *(x++) = hexchar(u & 0xF);
+ u >>= 4;
+ }
+
+ *x = 0;
+
+ return t;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 1796014f26..e8552410c0 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -948,3 +948,6 @@ int umount_recursive(const char *target, int flags);
int bind_remount_recursive(const char *prefix, bool ro);
int fflush_and_check(FILE *f);
+
+char *tempfn_xxxxxx(const char *p);
+char *tempfn_random(const char *p);