|
Assuming a file does not yet exist, a call to FileUtils.replace_file like the following will fail:
Puppet::Util.replace_file('/tmp/foo', 0440) { |of| FileUtils.copy_stream(StringIO.new("\u1234"), of) }
|
With an error like:
Errno::EACCES: Permission denied @ rb_sysopen - /tmp/foo20170323-24678-rg0fn9
|
from /usr/local/opt/rbenv/versions/2.1.9/lib/ruby/2.1.0/fileutils.rb:494:in `initialize'
|
This is because the replace_file code will use permissions of an existing file should it exist, but will try to chmod a file *before* yielding it back in the block if it does not yet exist. Therefore it is impossible to create a new read-only file like you might normally with Ruby itself like:
File.open('/tmp/foo.txt', 'w+', 0440) { |fh| fh.write('hello world') }
|
# => 11
|
Also note that the code path on Windows is slightly different and does not exhibit the same problem.
|