From a886dac94e8466c640e4aa23e1629467a941f439 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Fri, 11 Jan 2013 17:38:52 +0100 Subject: [PATCH] Add write_file function write_file() will write a buffer to a file. The file is created if necessary. Signed-off-by: Sascha Hauer --- fs/fs.c | 19 +++++++++++++++++++ include/fs.h | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/fs/fs.c b/fs/fs.c index 04331fcd0..f84051613 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -67,6 +67,25 @@ err_out: EXPORT_SYMBOL(read_file); +int write_file(const char *filename, void *buf, size_t size) +{ + int fd, ret; + + fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT); + if (fd < 0) + return fd; + + ret = write_full(fd, buf, size); + + close(fd); + + if (ret < 0) + return ret; + + return 0; +} +EXPORT_SYMBOL(write_file); + char *mkmodestr(unsigned long mode, char *str) { static const char *l = "xwr"; diff --git a/include/fs.h b/include/fs.h index 8ff730091..919daab67 100644 --- a/include/fs.h +++ b/include/fs.h @@ -166,6 +166,11 @@ char *mkmodestr(unsigned long mode, char *str); */ void *read_file(const char *filename, size_t *size); +/* + * Write a buffer to a file. This file is newly created. + */ +int write_file(const char *filename, void *buf, size_t size); + /* * This function turns 'path' into an absolute path and removes all occurrences * of "..", "." and double slashes. The returned string must be freed wit free().