Skip to content

Commit

Permalink
Merge pull request #2905 from realm/js/posix_fallocate-failures
Browse files Browse the repository at this point in the history
Make posix_fallocate robust against interruption, report right errors
  • Loading branch information
James Stone authored Oct 20, 2017
2 parents 485aaae + ecb81ae commit 91eb075
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

### Bugfixes

* Lorem ipsum.
* Make calls to posix_fallocate() robust against interruption and report
the correct error on failure.
PR [#2905](https://github.com/realm/realm-core/pull/2905).

### Breaking changes

Expand Down
16 changes: 12 additions & 4 deletions src/realm/util/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,19 @@ void File::prealloc_if_supported(SizeType offset, size_t size)
if (int_cast_with_overflow_detect(size, size2))
throw std::runtime_error("File size overflow");

if (::posix_fallocate(m_fd, offset, size2) == 0)
// posix_fallocate() does not set errno, it returns the error (if any) or zero.
// It is also possible for it to be interrupted by EINTR according to some man pages (ex fedora 24)
int status;
do {
status = ::posix_fallocate(m_fd, offset, size2);
} while (status == EINTR);

if (REALM_LIKELY(status == 0)) {
return;
int err = errno; // Eliminate any risk of clobbering
std::string msg = get_errno_msg("posix_fallocate() failed: ", err);
if (err == ENOSPC || err == EDQUOT) {
}

std::string msg = get_errno_msg("posix_fallocate() failed: ", status);
if (status == ENOSPC || status == EDQUOT) {
throw OutOfDiskSpace(msg);
}
throw std::runtime_error(msg);
Expand Down

0 comments on commit 91eb075

Please sign in to comment.