Merge branch 'fsync-shadow-files-before-rename' of https://github.com/awelzel/zeek

* 'fsync-shadow-files-before-rename' of https://github.com/awelzel/zeek:
  logging/writers/ascii: shadow files: Add fsync() before rename()
This commit is contained in:
Johanna Amann 2021-10-15 09:21:42 +01:00
commit 1b3b9a3cfc
5 changed files with 45 additions and 3 deletions

View file

@ -2162,6 +2162,36 @@ bool safe_pwrite(int fd, const unsigned char* data, size_t len, size_t offset)
return true;
}
bool safe_fsync(int fd)
{
int r;
/*
* Failure cases of fsync(2) are EABDF, EINTR, ENOSPC, EROFS, EINVAL, EDQUOT.
*
* For EINTR, one can just retry until it is not interrupted. For the others
* we report an error.
*
* Note that we don't use the reporter here to allow use from different threads.
*/
do
{
r = fsync(fd);
} while ( r < 0 && errno == EINTR );
if ( r < 0 )
{
char buf[128];
zeek_strerror_r(errno, buf, sizeof(buf));
fprintf(stderr, "safe_fsync error %d: %s\n", errno, buf);
abort();
return false;
}
return true;
}
void safe_close(int fd)
{
/*