summaryrefslogtreecommitdiff
path: root/src/shared/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-06-16 13:21:07 +0200
committerLennart Poettering <lennart@poettering.net>2014-06-16 13:21:07 +0200
commit2e78fa79bbaebb358d2657c397180d2d08d69b12 (patch)
tree4c128734dff3fc78c49c1240c432f55cb62fc203 /src/shared/util.c
parent45c196a76b2d883552c90807386d9bed40da822b (diff)
tmpfiles: add new "L+" command as stronger version of "L", that removes the destination before creating a symlink
Also, make use of this for mtab as long as mount insists on creating it even if we invoke it with "-n".
Diffstat (limited to 'src/shared/util.c')
-rw-r--r--src/shared/util.c79
1 files changed, 53 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;
+}