
Hello, I wrote a simple program to read the contents of a cdrom: module Main where import Text.Printf import System.IO import System.Posix.Types import System.Posix.IO main = do fd <- openFd "/dev/cdrom" ReadOnly Nothing defaultFileFlags readCdRom fd 4096 closeFd fd readCdRom fd byteCount = do (buf, actualByteCount) = fdRead fd bytecount readCdRom fd byteCount When it executed it read thousands of 4096 blocks as I expected. It then got a "hardware error" which I suspect was the "end of the CD", i.e. the begininng of the "unlasered" part of the CD. Is there a better and more graceful way to detect and handle "end of data"? Thanks, ,Vasili

2008/2/1 Galchin Vasili
I wrote a simple program to read the contents of a cdrom:
(Note that this is a terribly inefficient way of reading large amounts of binary data. Of course, if this is just meant as an example, that's fine. Otherwise, see the struff about ByteStrings at http://www.haskell.org/haskellwiki/DealingWithBinaryData) The error you are seeing comes from the operating system. You could run the resulting Haskell program under strace to see the exact error, but what's happening is that the kernel is getting an error from the CD drive itself. This is pretty much unique to CDs I guess. If you were reading a hard drive, the kernel knows exactly how large the disk is (see /proc/partitions) and would return EOF (zero bytes). As an aside, hitting EOF in your code would result in an infinite loop, since you don't handle actualByteCount == 0. You can catch and handle the resulting exception with the functions in: http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.htm... AGL -- Adam Langley agl@imperialviolet.org http://www.imperialviolet.org 650-283-9641

On Feb 1, 2008 1:42 PM, Bryan O'Sullivan
No, it's the Haskell runtime turning a -1 return from read into an exception. You need to call hIsEOF to check whether you've hit EOF, then break out of the loop.
(assuming you meant 0, not -1) My bad, I thought that the Posix.IO stuff was a closer wrapping than that. It does, indeed, throw an exception on 0. How unfortunate. AGL -- Adam Langley agl@imperialviolet.org http://www.imperialviolet.org 650-283-9641

Thank you Adam and Bradley. My program is my getting a feel of how to open a
Linux and do "block" reads. Just conceptual
Vasili
On 2/1/08, Adam Langley
On Feb 1, 2008 1:42 PM, Bryan O'Sullivan
wrote: No, it's the Haskell runtime turning a -1 return from read into an exception. You need to call hIsEOF to check whether you've hit EOF, then break out of the loop.
(assuming you meant 0, not -1)
My bad, I thought that the Posix.IO stuff was a closer wrapping than that. It does, indeed, throw an exception on 0. How unfortunate.
AGL
-- Adam Langley agl@imperialviolet.org http://www.imperialviolet.org 650-283-9641
participants (3)
-
Adam Langley
-
Bryan O'Sullivan
-
Galchin Vasili