Forum: Ruby cdrom control and detection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David T. (Guest)
on 2009-05-21 23:13
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?
Mk 2. (Guest)
on 2009-05-22 00:08
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.
Mk 2. (Guest)
on 2009-05-22 00:20
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.
Detlef R. (Guest)
on 2009-05-22 01:16
(Received via mailing list)
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-...
and make the suggested change to the source before compile.

Cheers, detlef
David T. (Guest)
on 2009-05-22 01:17
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)
David T. (Guest)
on 2009-05-22 01:59

> 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 :)

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?
This topic is locked and can not be replied to.