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:



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

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



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

def eject(volume_name)
  device, uhelper = device_mount_info(volume_name)
  # Kindle第10世代への対応
  # https://github.com/whiteleaf7/narou/issues/314
  device_chopped = device.gsub(/\d+$/, "")

  case uhelper
  when "udisks"
    commands = ["udisks --unmount #{device}",
                "udisks --detach #{device_chopped}"]
  when "udisks2"
    commands = ["udisksctl unmount -b #{device} --no-user-interaction",
                "udisksctl power-off -b #{device_chopped} --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)


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

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



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

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