diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2018-02-08 09:04:27 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2018-02-08 09:04:27 +0100 |
commit | 0dce5375d799e727bd1aa5ccdb28156d9bcd4d17 (patch) | |
tree | e1c6dd036a69ab954616219dfa0cee8312bb43f8 /lib | |
parent | 6f826bfe45298721a92bbe93d32a6a0eb76d46ee (diff) | |
parent | 74cdc34323ee23b9b195866c56699e268f05841a (diff) | |
download | barebox-0dce5375d799e727bd1aa5ccdb28156d9bcd4d17.tar.gz barebox-0dce5375d799e727bd1aa5ccdb28156d9bcd4d17.tar.xz |
Merge branch 'for-next/tftp-workaround'
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libfile.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/libfile.c b/lib/libfile.c index 1face96c82..b7db22d694 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -20,6 +20,7 @@ #include <malloc.h> #include <libfile.h> #include <progress.h> +#include <stdlib.h> #include <linux/stat.h> /* @@ -485,3 +486,57 @@ int open_and_lseek(const char *filename, int mode, loff_t pos) return fd; } + +/** + * make_temp - create a name for a temporary file + * @template: The filename prefix + * + * This function creates a name for a temporary file. @template is used as a + * template for the name which gets appended a 8-digit hexadecimal number to + * create a unique filename. + * + * Return: This function returns a filename which can be used as a temporary + * file later on. The returned filename must be freed by the caller. + */ +char *make_temp(const char *template) +{ + char *name = NULL; + struct stat s; + int ret; + + do { + free(name); + name = basprintf("/tmp/%s-%08x", template, random32()); + ret = stat(name, &s); + } while (!ret); + + return name; +} + +/** + * cache_file - Cache a file in /tmp + * @path: The file to cache + * @newpath: The return path where the file is copied to + * + * This function copies a given file to /tmp and returns its name in @newpath. + * @newpath is dynamically allocated and must be freed by the caller. + * + * Return: 0 for success, negative error code otherwise. + */ +int cache_file(const char *path, char **newpath) +{ + char *npath; + int ret; + + npath = make_temp("filecache"); + + ret = copy_file(path, npath, 0); + if (ret) { + free(npath); + return ret; + } + + *newpath = npath; + + return 0; +} |