I started learning Chef to manage our servers and I stumbled in a very weird (in my opinion) behavior in Ruby. I do not know Ruby so this might be just a misunderstanding from my part.
The error I was getting was
`delete': Permission denied - [some path]/metadata.json (Errno::EACCES)
Since I knew for sure that it was not actually about permissions, the next logical thing was to check for file locking. After digging a bit through the relevant code, I discovered that there is a method that produces a checksum for each file.
def checksum_file(file, digest)
File.open(file, 'rb') { |f| checksum_io(f, digest) }
end
def checksum_io(io, digest)
while chunk = io.read(1024 * 8)
digest.update(chunk)
end
digest.hexdigest
end
Having found that, I searched a bit and found an answer about closing files in Ruby and it seemed that the code was actually fine... but it was not. I tried to change the method to the "block format" and it worked without error:
def checksum_file(file, digest)
File.open(file, 'rb') do |f|
checksum_io(f, digest)
end
end
Can someone please explain the difference between the two versions of the code?
-- Edit --
It seems that this problem occurs only in Windows and maybe only when using the ruby provided by ChefDK 0.3.0 :
ruby 2.0.0p451 (2014-02-24) [i386-mingw32]