0

I am using the following code fragment in Powershell:

$fd = [System.IO.File]::OpenRead($file)
$buf = new-object byte[] (8MB)
$read_len = $fd.Read($buf,0,$buf.length)

Is there any chance the data that shows up in the buffer might not be what is actually on the drive? I haven't had a specific problem but I'm imagining possible issues with reading from USB or network drives.

The reason I ask is that I am reading the entire file and writing each buffer to a new file. I compute the MD5 hash as I read the file. But, if somehow the buffer was not actually the source file contents then the new file would have this same corruption and its MD5 would match the one I calculated while reading the original. This would give me the false impression that the copy was successful.

So, in short, is read buffer corruption even possible?

macq
  • 19
  • 1
  • 4
  • You read what's on disk, so actually the contents of the buffer reflect what's on disk. Yet... you better might consider computing your MD5 hash with the integrated .NET functions. See this [question](http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell) to get you started... – Peter Schneider May 10 '15 at 06:33
  • Peter, I am basing my MD5 calcs on that link - specifically Casey's answer although including the file copy as part of the first loop that calculates the MD5 of the source. My concern was more what happens when there is a drive error (rare I know but still possible). Will the read throw an exception? Could it possibly get corrupt data in the read buffer? What happens exactly when a hardware error or network error prevents the data from being read? – macq May 10 '15 at 13:43

0 Answers0