From: Eleanor McHugh Date: 2009-11-27T20:42:38+09:00 Subject: Re: Ruby byte access to disk sectors like dd does On 27 Nov 2009, at 03:46, Gary Wright wrote: > On Nov 26, 2009, at 4:07 PM, Gary Hasson wrote: > >> I have been unable to find any reference to Ruby methods that provide >> raw disk access the way dd does. I frequently use the Linux utility >> program, dd, and would like to be able to do the same type of byte-level >> and sector-level access to the hard drive with Ruby. (What I am looking >> for is not file access.) Are there Ruby methods to provide this >> capability (other than having Ruby access dd)? > > dd doesn't ever have 'raw disk access' or 'sector' level access to a disk drive so I'm not sure how to respond. > > A raw disk device just presents the underlying drive as a file. You use seek and read and write to accomplish I/O but it is generally a mistake to think that you are accessing particular 'sectors' when you do this. Modern disks drives have complex geometries that aren't visible unless you are working at the device driver level. > > To utilize 'raw' I/O with Ruby look at IO#sysread, IO#syswrite, and IO#sysseek. This would be analogous to a C program utilizing the similarly named system calls. You'll also want to open the device file in binary mode and also be sure that you don't use any of the buffered I/O methods (i.e. don't mix and match). For a gentle intro to the more general topic of using Ruby for systems programming on a Unix box, take a look at the Ruby Plumber's Guide presentations linked from my signature. Also buy yourself a copy of Marc Rochkind's "Advanced Unix Programming" which is as light a read as is possible given the nature of the subject matter ;) Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason