mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-26 14:42:36 +02:00
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>
This commit is contained in:
parent
3c1aa889d8
commit
c9ebfad95f
64
flashrom.c
64
flashrom.c
@ -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>
|
||||
@ -909,66 +905,6 @@ int read_buf_from_include_args(const struct flashrom_layout *const layout, unsig
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user