Why would I be using Windows and RoR? I am in the process of creating an automated build system for windows mobile builds, and windows mobile only builds on windows. There are quite a few things that behave different on windows vs linux or osx. The topic today is file access.
Follow up:
On linux and osx, file access behaves as one would usually expect. When reading and writing files, windows defaults to text only mode. Most the time that isn't a problem, but sometimes things are real nasty. I needed to MD5 a series of PNG files, and it wasn't working. I was always getting the same hash back for every one.
I had grabbed a snippet of code from here: http://snippets.dzone.com/posts/show/3858 to do the MD5 of a file, and all looked well. The code is as follows:
def md5(file_path)
hasher = Digest::MD5.new
open(file_path, "r") do |io|
counter = 0
while (!io.eof)
readBuf = io.readpartial($BUF_SIZE)
putc '.' if ((counter+=1) % 3 == 0)
hasher.update(readBuf)
end
end
return hasher.hexdigest
end
The problem here is trying to read binary files. In windows it treated the files as text only, so I was getting an EOF immediately after the PNG header. This would make all files look alike, and generate the same hash.
One character added fixes this problem:
def md5(file_path)
hasher = Digest::MD5.new
open(file_path, "rb") do |io|
counter = 0
while (!io.eof)
readBuf = io.readpartial($BUF_SIZE)
putc '.' if ((counter+=1) % 3 == 0)
hasher.update(readBuf)
end
end
return hasher.hexdigest
end
In case you don't see it, the 2nd argument to open changes() from "r" to "rb". This forces windows to open it as binary and the problem is solved.
One of my other main issues has been with threading, specifically that you cannot fork on windows. I'll address this in the future.
Recent Comments