From fd3ecff9ec5e45bf66d27da979c58bf23cd307da Mon Sep 17 00:00:00 2001 From: Uwe Kleine-König Date: Fri, 1 Jun 2018 20:07:48 +0200 Subject: libfile: implement new helper write_file_flash() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compared to write_file() this new function also calls erase() to be suitable for flash devices. Signed-off-by: Uwe Kleine-König Signed-off-by: Sascha Hauer --- include/libfile.h | 1 + lib/libfile.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/libfile.h b/include/libfile.h index fd2fadeaa8..2c5eef71f1 100644 --- a/include/libfile.h +++ b/include/libfile.h @@ -12,6 +12,7 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, loff_t max_size); int write_file(const char *filename, const void *buf, size_t size); +int write_file_flash(const char *filename, const void *buf, size_t size); int copy_file(const char *src, const char *dst, int verbose); diff --git a/lib/libfile.c b/lib/libfile.c index b7db22d694..d22519b8f4 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -250,6 +250,39 @@ int write_file(const char *filename, const void *buf, size_t size) } EXPORT_SYMBOL(write_file); +/** + * write_file_flash - write a buffer to a file backed by flash + * @filename: The filename to write + * @size: The size of the buffer + * + * Functional this is identical to write_file but calls erase() before writing. + * + * Return: 0 for success or negative error value + */ +int write_file_flash(const char *filename, const void *buf, size_t size) +{ + int fd, ret; + + fd = open(filename, O_WRONLY); + if (fd < 0) + return fd; + + ret = erase(fd, size, 0); + if (ret < 0) + goto out_close; + + ret = write_full(fd, buf, size); + +out_close: + close(fd); + + if (ret < 0) + return ret; + + return 0; +} +EXPORT_SYMBOL(write_file_flash); + /** * copy_file - Copy a file * @src: The source filename -- cgit v1.2.3