diff options
author | Edward O'Callaghan <quasisec@google.com> | 2022-08-15 10:53:32 +1000 |
---|---|---|
committer | Edward O'Callaghan <quasisec@chromium.org> | 2022-08-25 00:25:41 +0000 |
commit | c9ebfad95fed1dedde2d15d62299adba573f34c7 (patch) | |
tree | 2b2cba4ef766529c86ee498c4e6f37a09891018b | |
parent | 3c1aa889d867669ac98a204de7b7dbe2778f814a (diff) | |
download | flashrom-c9ebfad95fed1dedde2d15d62299adba573f34c7.tar.gz flashrom-c9ebfad95fed1dedde2d15d62299adba573f34c7.tar.bz2 flashrom-c9ebfad95fed1dedde2d15d62299adba573f34c7.zip |
flashrom.c: Move write_buf_to_file() to helpers_fileio.c
Constructing a written file from a buffer is auxiliary
functionality to the core flashrom algorithms. Move
aside to decrease the overall complexity of flashrom.c
BUG=b:242246291
TEST=builds
Change-Id: Ib613e74597d4bdd689043ba93aeb6a87ec80cc14
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/66646
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Evan Benn <evanbenn@google.com>
Reviewed-by: Sam McNally <sammc@google.com>
Reviewed-by: Thomas Heijligen <src@posteo.de>
-rw-r--r-- | flashrom.c | 64 | ||||
-rw-r--r-- | helpers_fileio.c | 61 |
2 files changed, 61 insertions, 64 deletions
@@ -21,10 +21,6 @@ #include <stdio.h> #include <sys/types.h> -#ifndef __LIBPAYLOAD__ -#include <fcntl.h> -#include <sys/stat.h> -#endif #include <string.h> #include <unistd.h> #include <stdlib.h> @@ -910,66 +906,6 @@ int read_buf_from_include_args(const struct flashrom_layout *const layout, unsig } /** - * @brief Writes passed data buffer into a file - * - * @param buf Buffer with data to write - * @param size Size of buffer - * @param filename File path to write to - * @return 0 on success - */ -int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename) -{ -#ifdef __LIBPAYLOAD__ - msg_gerr("Error: No file I/O support in libpayload\n"); - return 1; -#else - FILE *image; - int ret = 0; - - if (!filename) { - msg_gerr("No filename specified.\n"); - return 1; - } - if ((image = fopen(filename, "wb")) == NULL) { - msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno)); - return 1; - } - - unsigned long numbytes = fwrite(buf, 1, size, image); - if (numbytes != size) { - msg_gerr("Error: file %s could not be written completely.\n", filename); - ret = 1; - goto out; - } - if (fflush(image)) { - msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno)); - ret = 1; - } - // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW). -#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1) - struct stat image_stat; - if (fstat(fileno(image), &image_stat) != 0) { - msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno)); - ret = 1; - goto out; - } - if (S_ISREG(image_stat.st_mode)) { - if (fsync(fileno(image))) { - msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno)); - ret = 1; - } - } -#endif -out: - if (fclose(image)) { - msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno)); - ret = 1; - } - return ret; -#endif -} - -/** * @brief Writes content from buffer to one or more files. * * Writes content from supplied buffer to files. If a filename is specified for diff --git a/helpers_fileio.c b/helpers_fileio.c index 550d98d2..cb7675db 100644 --- a/helpers_fileio.c +++ b/helpers_fileio.c @@ -21,6 +21,7 @@ #include <errno.h> #ifndef __LIBPAYLOAD__ +#include <fcntl.h> #include <sys/stat.h> #endif @@ -69,3 +70,63 @@ out: return ret; #endif } + +/** + * @brief Writes passed data buffer into a file + * + * @param buf Buffer with data to write + * @param size Size of buffer + * @param filename File path to write to + * @return 0 on success + */ +int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename) +{ +#ifdef __LIBPAYLOAD__ + msg_gerr("Error: No file I/O support in libpayload\n"); + return 1; +#else + FILE *image; + int ret = 0; + + if (!filename) { + msg_gerr("No filename specified.\n"); + return 1; + } + if ((image = fopen(filename, "wb")) == NULL) { + msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno)); + return 1; + } + + unsigned long numbytes = fwrite(buf, 1, size, image); + if (numbytes != size) { + msg_gerr("Error: file %s could not be written completely.\n", filename); + ret = 1; + goto out; + } + if (fflush(image)) { + msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno)); + ret = 1; + } + // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW). +#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1) + struct stat image_stat; + if (fstat(fileno(image), &image_stat) != 0) { + msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno)); + ret = 1; + goto out; + } + if (S_ISREG(image_stat.st_mode)) { + if (fsync(fileno(image))) { + msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno)); + ret = 1; + } + } +#endif +out: + if (fclose(image)) { + msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno)); + ret = 1; + } + return ret; +#endif +} |