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

Change-Id: Ib613e74597d4bdd689043ba93aeb6a87ec80cc14
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/66646
Original-Reviewed-by: Evan Benn <evanbenn@google.com>
Original-Reviewed-by: Sam McNally <sammc@google.com>
Original-Reviewed-by: Thomas Heijligen <src@posteo.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72348
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/helpers_fileio.c b/helpers_fileio.c
index 3b809c6..e3a4993 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,55 @@
 	return ret;
 #endif
 }
+
+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
+}