HomePhorge

Fix #4923 - close process race when truncating existing file
9ba0c8a22c6fUnpublished

Unpublished Commit ยท Learn More

Repository Importing: This repository is still importing.

Description

Fix #4923 - close process race when truncating existing file

Using File.open(file, "w") calls open(2) with O_CREAT|O_TRUNC which
means when the file exists it is immediately truncated.
But the file is not locked yet, so another process can either write or
read to the file, leading to file corruption.

The fix is to truncate only when the file is exclusively locked. This can
be done on some operating system with O_EXLOCK open(2) flag.
I chose the more portable option of:

  • open
  • flock
  • truncate
  • write
  • close

It might also be good to flush and fsync the file after writing it,
otherwise in case of crash an incomplete file can stay on disk.

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>

Details

Provenance
Brice Figureau <brice-puppet@daysofwonder.com>Authored on
James Turnbull <james@lovedthanlost.net>Committed on Nov 10 2010, 2:56 AM
vanmeeuwenPushed on Jun 2 2015, 2:22 PM
Parents
rPUcb16d3dcbad4: Puppet-load: better and safer error reporting
Branches
Unknown
Tags
Unknown

Event Timeline

James Turnbull <james@lovedthanlost.net> committed rPU9ba0c8a22c6f: Fix #4923 - close process race when truncating existing file (authored by Brice Figureau <brice-puppet@daysofwonder.com>).Nov 10 2010, 2:56 AM