On 18/10/10 16:00, Markus Roberts wrote:
>> 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.
> 
> 
> According to PickAxe truncate is only available on some OSs as well,
> though I suspect it's more widely available than O_EXLOCK. 

It looks like truncate is available at least on POSIX.

> Specifically, we need to check if it's available on MS Windows and (if
> not) make this code conditional on OS :(.

It looks like (at least my ruby 1.8.7) uses chsize on win32, which is
hopefully supported since windows 95:
http://msdn.microsoft.com/en-us/library/dk925tyb(v=VS.80).aspx

> Other than that, code looks right and it's a good catch.

The issue is what to do if we find truncate not working everywhere?
The only remaining alternative would be to atomically rename a temporary
file as it was done a couple of years ago (with apparently some other
issues).
-- 
Brice Figureau
My Blog: http://www.masterzen.fr/

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to