Module: XDo::Drive

Defined in:
lib/xdo/drive.rb

Overview

Some methods to interact with disk drives. The value of the drive parameter of many methods can be either a mount point like /media/my_cdrom, a device file like scd0 or the default name “cdrom” for the default drive.

If you don’t pass in a drive name, the return value of #default will be used.

Class Method Summary collapse

Class Method Details

.close(drive = default) ⇒ Object

Closes a drive.

Parameters

drive

(default()) The drive to close.

Return value

Undefined.

Raises

XError

eject failed.

Example

XDo::Drive.eject("scd0")
XDo::Drive.close

XDo::Drive.eject("/dev/my_cdrom")
XDo::Drive.close("scd0") #A mount point doesn't make sense here

Raises:



52
53
54
55
56
# File 'lib/xdo/drive.rb', line 52

def close(drive = default)        
  err = ""
  Open3.popen3("eject -t #{drive}"){|stdin, stdout, stderr| err << stderr.read}
  raise(XDo::XError, err) unless err.empty?
end

.defaultObject

Returns the mount point of the default drive. You can use it as a value for a drive parameter.

Return value

The default drive’s name. Usually "cdrom".

Raises

XError

eject failed.

Example

p XDo::Drive.default #=> "cdrom"
XDo::Drive.eject(XDo::Drive.default)

Raises:



67
68
69
70
71
72
73
# File 'lib/xdo/drive.rb', line 67

def default
  err = ""
  out = ""
  Open3.popen3("#{XDo::EJECT} -d"){|stdin, stdout, stderr| out << stdout.read; err << stderr.read}
  raise(XDo::XError, err) unless err.empty?
  out.match(/`(.*)'/)[1]
end

.eject(drive = default) ⇒ Object

Opens a drive.

Parameters

drive

(default()) The drive to open.

Return value

True.

Raises

XError

You’re using a laptop whose drive has to be closed manually.

XError

eject failed.

Example

XDo::Drive.eject("scd0")
XDo::Drive.eject("/media/my_cdrom")

Remarks

This method may silently fail if the device is blocked by e.g. a CD burning program. Have a look at #release if you want to force it to open.

Raises:



32
33
34
35
36
37
# File 'lib/xdo/drive.rb', line 32

def eject(drive = default)
  err = ""
  Open3.popen3("#{XDo::EJECT} #{drive}"){|stdin, stdout, stderr| err << stderr.read}
  raise(XDo::XError, err) unless err.empty?
  true
end

.lock(drive = default) ⇒ Object

Locks a drive, so that it can’t be opened by using the eject button or the eject command.

Parameters

drive

(default()) The drive to lock.

Return value

true.

Raises

XError

eject failed.

Example

XDo::Drive.lock("scd0")
XDo::Drive.eject # fails
XDo::Drive.release
XDo::Drive.eject("scd0") #succeeds

Remarks

Note that the lock doesn’t get released if your process exits. You should probably register a at_exit handler to avoid confusion when your program exits with an exception.

Raises:



92
93
94
95
96
97
# File 'lib/xdo/drive.rb', line 92

def lock(drive = default)
  err = ""
  Open3.popen3("#{XDo::EJECT} -i on #{drive}"){|stdin, stdout, stderr| err << stderr.read}
  raise(XDo::XError, err) unless err.empty?
  true
end

.release(drive = default) ⇒ Object

Unlocks a drive, so that it can be opened by neither the eject button nor the eject command.

Parameters

drive

The drive to remove the lock from.

Return value

true.

Raises

XError

eject failed.

Example

XDo::Drive.lock("scd0")
XDo::Drive.eject # fails
XDo::Drive.release
XDo::Drive.eject("scd0") #succeeds

Remarks

Use with caution. If a burning program locked the drive and you force it to open, the resulting CD-ROM is garbage.

Raises:



115
116
117
118
119
120
121
# File 'lib/xdo/drive.rb', line 115

def release(drive = default)
  drive = default unless drive
  err = ""
  Open3.popen3("#{XDo::EJECT} -i off #{drive}"){|stdin, stdout, stderr| err << stderr.read}
  raise(XDo::XError,err) unless err.empty?
  true
end