[PATCH 4/7] libfile: implement a function to cache a file

Sascha Hauer s.hauer at pengutronix.de
Tue Jan 23 23:45:31 PST 2018


Due to the nature of TFTP which can't lseek and due to the silliness
of our filesystem implementation which can't cache accesses we have to
manually cache files on TFTP filesystems sometimes. Make it easier
for them by providing a cache_file() function which copies the file
from TFTP to RAM.

Signed-off-by: Sascha Hauer <s.hauer at 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 6dbb81a241..beec7cff79 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -28,4 +28,6 @@ int unlink_recursive(const char *path, char **failedpath);
 
 char *make_temp(const char *template);
 
+int cache_file(const char *path, char **newpath);
+
 #endif /* __LIBFILE_H */
diff --git a/lib/libfile.c b/lib/libfile.c
index 79054eb5ac..738ff1287d 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -512,3 +512,30 @@ char *make_temp(const char *template)
 
 	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.
+ *
+ * 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;
+}
-- 
2.11.0




More information about the barebox mailing list