From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 3/8] libfile: implement make_temp
Date: Thu, 25 Jan 2018 08:45:15 +0100 [thread overview]
Message-ID: <20180125074520.10320-4-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20180125074520.10320-1-s.hauer@pengutronix.de>
Create a make_temp() function which creates a name for a temporary file.
Since we do not have any concurrency in barebox we do not need to create
the file right away and can leave that to the caller. Unlike unix
mktemp the resulting filename is dynamically allocated and must be
freed by the caller.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/libfile.h | 2 ++
lib/libfile.c | 27 +++++++++++++++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/include/libfile.h b/include/libfile.h
index dd0b00f988..6dbb81a241 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -26,4 +26,6 @@ int make_directory(const char *pathname);
int unlink_recursive(const char *path, char **failedpath);
+char *make_temp(const char *template);
+
#endif /* __LIBFILE_H */
diff --git a/lib/libfile.c b/lib/libfile.c
index 6b70306dbd..79054eb5ac 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,29 @@ 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 lateron. 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;
+}
--
2.15.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2018-01-25 7:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-25 7:45 [PATCH v2 0/8] Make TFTP detection more convenient Sascha Hauer
2018-01-25 7:45 ` [PATCH 1/8] startup: create /tmp Sascha Hauer
2018-01-25 7:45 ` [PATCH 2/8] fs: implement is_tftp_fs() Sascha Hauer
2018-01-25 7:45 ` Sascha Hauer [this message]
2018-01-25 7:45 ` [PATCH 4/8] libfile: implement a function to cache a file Sascha Hauer
2018-01-25 7:45 ` [PATCH 5/8] uimage: fix memory leak in error path Sascha Hauer
2018-01-25 7:45 ` [PATCH 6/8] uimage: Use is_tftp_fs() and cache_file() to ease TFTP workaround Sascha Hauer
2018-01-25 7:45 ` [PATCH 7/8] fs/uimagefs: " Sascha Hauer
2018-01-25 7:45 ` [PATCH 8/8] fs: remove now unused function can_lseek_backward() Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180125074520.10320-4-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox