Module: Device::Library::Linux

Defined in:
lib/device/library/linux.rb

Constant Summary collapse

@@mount_roots =
%w(/media /mnt)

Instance Method Summary collapse

Instance Method Details

#device_mount_info(volume_name) ⇒ Object

端末のデバイスファイル名と uhelper オプションの値を取得

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/device/library/linux.rb', line 29

def device_mount_info(volume_name)
  device_root = get_device_root_dir(volume_name)
  raise Device::CantEject, "端末が接続されていません" unless device_root

  pattern = %r!^(/dev/[^ ]+) .* #{device_root} .*\Wuhelper=(\w+)!
  open("|mount") do |io|
    while line = io.gets
      if line =~ pattern
        return [$1, $2]
      end
    end
  end
  [nil, nil]
end

#eject(volume_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/device/library/linux.rb', line 55

def eject(volume_name)
  device, uhelper = device_mount_info(volume_name)

  case uhelper
  when "udisks"
    commands = ["udisks --unmount #{device}",
                "udisks --detach #{device.chop}"]
  when "udisks2"
    commands = ["udisksctl unmount -b #{device} --no-user-interaction",
                "udisksctl power-off -b #{device.chop} --no-user-interaction"]
  else
    raise Device::CantEject, "udisks または udisks2 によって自動マウントされたデバイスではありません。"
  end

  commands.each do |command|
    status, _stdout, stderr = systemu(command)
    unless status.success?
      raise Device::CantEject, "#{command}: #{stderr.strip}"
    end
  end
end

#ejectable?(volume_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/device/library/linux.rb', line 44

def ejectable?(volume_name)
  case device_mount_info(volume_name)[1]
  when "udisks", "udisks2"
    true
  else
    false
  end
rescue Device::CantEject
  false
end

#get_device_root_dir(volume_name) ⇒ Object

:reek:UtilityFunction



18
19
20
21
22
23
24
25
26
# File 'lib/device/library/linux.rb', line 18

def get_device_root_dir(volume_name)
  @@mount_roots.each do |mount_root|
    path = File.join(mount_root, volume_name)
    if File.directory?(path)
      return path
    end
  end
  nil
end