Preserve File error state once error occurs.

This commit is contained in:
Jared Boone 2016-05-12 21:54:13 -07:00
parent 0f1ae96ba4
commit b21758781c
2 changed files with 35 additions and 11 deletions

View file

@ -23,7 +23,11 @@
#include <algorithm> #include <algorithm>
File::File(const std::string& filename, openmode mode) { File::File(
const std::string& filename,
openmode mode
) : err { FR_OK }
{
BYTE fatfs_mode = 0; BYTE fatfs_mode = 0;
if( mode & openmode::in ) { if( mode & openmode::in ) {
fatfs_mode |= FA_READ; fatfs_mode |= FA_READ;
@ -38,9 +42,11 @@ File::File(const std::string& filename, openmode mode) {
fatfs_mode |= FA_OPEN_ALWAYS; fatfs_mode |= FA_OPEN_ALWAYS;
} }
if( f_open(&f, filename.c_str(), fatfs_mode) == FR_OK ) { err = f_open(&f, filename.c_str(), fatfs_mode);
if( err == FR_OK ) {
if( mode & openmode::ate ) { if( mode & openmode::ate ) {
if( f_lseek(&f, f_size(&f)) != FR_OK ) { err = f_lseek(&f, f_size(&f));
if( err != FR_OK ) {
f_close(&f); f_close(&f);
} }
} }
@ -52,20 +58,33 @@ File::~File() {
} }
bool File::read(void* const data, const size_t bytes_to_read) { bool File::read(void* const data, const size_t bytes_to_read) {
if( err != FR_OK ) {
return false;
}
UINT bytes_read = 0; UINT bytes_read = 0;
const auto result = f_read(&f, data, bytes_to_read, &bytes_read); err = f_read(&f, data, bytes_to_read, &bytes_read);
return (result == FR_OK) && (bytes_read == bytes_to_read); return (err == FR_OK);
} }
bool File::write(const void* const data, const size_t bytes_to_write) { bool File::write(const void* const data, const size_t bytes_to_write) {
if( err != FR_OK ) {
return false;
}
UINT bytes_written = 0; UINT bytes_written = 0;
const auto result = f_write(&f, data, bytes_to_write, &bytes_written); err = f_write(&f, data, bytes_to_write, &bytes_written);
return (result == FR_OK) && (bytes_written == bytes_to_write); return (err == FR_OK);
} }
uint64_t File::seek(const uint64_t new_position) { uint64_t File::seek(const uint64_t new_position) {
if( err != FR_OK ) {
return false;
}
const auto old_position = f_tell(&f); const auto old_position = f_tell(&f);
if( f_lseek(&f, new_position) != FR_OK ) { err = f_lseek(&f, new_position);
if( err != FR_OK ) {
f_close(&f); f_close(&f);
} }
if( f_tell(&f) != new_position ) { if( f_tell(&f) != new_position ) {
@ -80,8 +99,12 @@ bool File::puts(const std::string& string) {
} }
bool File::sync() { bool File::sync() {
const auto result = f_sync(&f); if( err != FR_OK ) {
return (result == FR_OK); return false;
}
err = f_sync(&f);
return (err == FR_OK);
} }
static std::string find_last_file_matching_pattern(const std::string& pattern) { static std::string find_last_file_matching_pattern(const std::string& pattern) {

View file

@ -126,7 +126,7 @@ public:
~File(); ~File();
bool is_open() const { bool is_open() const {
return f_error(&f) == 0; return err == FR_OK;
} }
bool read(void* const data, const size_t bytes_to_read); bool read(void* const data, const size_t bytes_to_read);
@ -145,6 +145,7 @@ public:
private: private:
FIL f; FIL f;
uint32_t err;
}; };
inline constexpr File::openmode operator|(File::openmode a, File::openmode b) { inline constexpr File::openmode operator|(File::openmode a, File::openmode b) {