diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/util.c | 52 | ||||
-rw-r--r-- | src/shared/util.h | 1 |
2 files changed, 51 insertions, 2 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index 254b5637ae..ee95a4b6f7 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -6977,6 +6977,14 @@ int tempfn_xxxxxx(const char *p, char **ret) { assert(p); assert(ret); + /* + * Turns this: + * /foo/bar/waldo + * + * Into this: + * /foo/bar/.waldoXXXXXX + */ + fn = basename(p); if (!filename_is_valid(fn)) return -EINVAL; @@ -6987,7 +6995,7 @@ int tempfn_xxxxxx(const char *p, char **ret) { strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), "."), fn), "XXXXXX"); - *ret = t; + *ret = path_kill_slashes(t); return 0; } @@ -7000,6 +7008,14 @@ int tempfn_random(const char *p, char **ret) { assert(p); assert(ret); + /* + * Turns this: + * /foo/bar/waldo + * + * Into this: + * /foo/bar/.waldobaa2a261115984a9 + */ + fn = basename(p); if (!filename_is_valid(fn)) return -EINVAL; @@ -7018,7 +7034,39 @@ int tempfn_random(const char *p, char **ret) { *x = 0; - *ret = t; + *ret = path_kill_slashes(t); + return 0; +} + +int tempfn_random_child(const char *p, char **ret) { + char *t, *x; + uint64_t u; + unsigned i; + + assert(p); + assert(ret); + + /* Turns this: + * /foo/bar/waldo + * Into this: + * /foo/bar/waldo/.3c2b6219aa75d7d0 + */ + + t = new(char, strlen(p) + 2 + 16 + 1); + if (!t) + return -ENOMEM; + + x = stpcpy(stpcpy(t, p), "/."); + + u = random_u64(); + for (i = 0; i < 16; i++) { + *(x++) = hexchar(u & 0xF); + u >>= 4; + } + + *x = 0; + + *ret = path_kill_slashes(t); return 0; } diff --git a/src/shared/util.h b/src/shared/util.h index 9a878ca1ac..a8ccf20bb8 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -1018,6 +1018,7 @@ int fflush_and_check(FILE *f); int tempfn_xxxxxx(const char *p, char **ret); int tempfn_random(const char *p, char **ret); +int tempfn_random_child(const char *p, char **ret); bool is_localhost(const char *hostname); |