Cdrom control and detection

OK,

I know how to eject/open the CD drive on a linux box from within Ruby:

system “eject”

How do I detect that the user has inserted the next cd so that my
program can continue cataloging cd contents?

I know it’s more of a Linux programming question, but maybe there is
some rubylicious way to do this? Or just the good old fashioned best
practice way will do.

Is there a good tutorial on detecting system and media changes?

It just occurred to me that most linux users today use automounting! So
using the sane method you should omit the last line BUT:

That is a slight problem because mounting (automatic or not) can take
several seconds. If you insert a disk and hit enter immediately, and
the program is not delayed by the system “mount” call, execution will
proceed altho the mount may not be completed.

So, either you put in a gap
sleep(5)
which should be long enough, or you do some kind of check for data in
the mount point directory.

David Thurston wrote:

How do I detect that the user has inserted the next cd so that my
program can continue cataloging cd contents?

You could do a loop with mount, system will return true if it succeeds
(and false if it fails!). However, that is not a very nice idea unless
you really, really want to completely minimize user interaction to
nothing. I’d recommend something like:

puts (“Insert DVD and hit enter.”)
gets
system “mount /dev/cdrom”

A little more sane.

Hi,

Mk 27 schrieb:

David Thurston wrote:

How do I detect that the user has inserted the next cd so that my
program can continue cataloging cd contents?

You could do a loop with mount, system will return true if it succeeds
(and false if it fails!). However, that is not a very nice idea unless
you really, really want to completely minimize user interaction to
nothing. I’d recommend something like:

better than polling the mount directory would be to observe it with
inotify http://raa.ruby-lang.org/project/ruby-inotify/

Because, this library includes a smal implementation error, you should
take a look at
http://www.mindbucket.com/2009/02/24/ruby-daemons-verifying-good-behavior/
and make the suggested change to the source before compile.

Cheers, detlef

better than polling the mount directory would be to observe it with
inotify http://raa.ruby-lang.org/project/ruby-inotify/

Cheers, detlef

Thanks Detlef!

Getting geekier :slight_smile:

I wonder if the Linux kernel sends up a flare or a flag or signal or
changes a constant or something in relation to removable media events?

Thanks Mk 27,

That will work.

I wonder if there is anything in udev or hal or /proc that I can listen
to?

(Always looking for a really ubergeeky answer)