> 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. Specifically, we need to check if it's available on MS Windows and (if not) make this code conditional on OS :(. Other than that, code looks right and it's a good catch. -- Markus ----------------------------------------------------------- The power of accurate observation is commonly called cynicism by those who have not got it. ~George Bernard Shaw ------------------------------------------------------------ -- 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.
