diff options
Diffstat (limited to 'src/lib/util.cc')
| -rw-r--r-- | src/lib/util.cc | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/lib/util.cc b/src/lib/util.cc index 5eba8d73a..595c7e76e 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -784,13 +784,30 @@ increment_eyes (Eyes e) } void +checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::path path) +{ + size_t N = fwrite (ptr, 1, size, stream); + if (N != size) { + if (ferror(stream)) { + fclose (stream); + throw FileError (String::compose("fwrite error %1", errno), path); + } else { + fclose (stream); + throw FileError ("Unexpected short write", path); + } + } +} + +void checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path path) { size_t N = fread (ptr, 1, size, stream); if (N != size) { if (ferror(stream)) { + fclose (stream); throw FileError (String::compose("fread error %1", errno), path); } else { + fclose (stream); throw FileError ("Unexpected short read", path); } } |
