Class: Knj::Mount

Inherits:
Object show all
Defined in:
lib/knj/mount.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Mount

Returns a new instance of Mount.



83
84
85
# File 'lib/knj/mount.rb', line 83

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



81
82
83
# File 'lib/knj/mount.rb', line 81

def data
  @data
end

Class Method Details

.ensure(args) ⇒ Object



74
75
76
77
78
79
# File 'lib/knj/mount.rb', line 74

def self.ensure(args)
  list = Knj::Mount.list("to_search" => args["to"])
  return false if !list.empty?
  Knj::Mount.mount(args)
  return true
end

.list(args = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/knj/mount.rb', line 2

def self.list(args = {})
  mount_output = Knj::Os.shellcmd("mount")
  ret = []
  
  mount_output.scan(/^(.+?) on (.+?) type (.+) \((.+?)\)$/) do |match|
    type = match[2]
    folder_from = match[0]
    folder_to = match[1]
    opts = match[3].split(",")
    
    folder_from = nil if folder_from == "none"
    #raise "The folder doesnt exist?" if !File.exists?(folder_to)
    
    add = true
    add = false if args.key?("to") and args["to"] != folder_to
    add = false if args.key?("from") and args["from"] != folder_from
    
    if args["from_search"]
      Knj::Strings.searchstring(args["from_search"]).each do |str|
        add = false if !folder_from or folder_from.index(str) == nil
      end
    end
    
    if args["to_search"]
      Knj::Strings.searchstring(args["to_search"]).each do |str|
        add = false if !folder_to or folder_to.index(str) == nil
      end
    end
    
    if add
      ret << Knj::Mount.new(
        :type => type,
        :from => folder_from,
        :to => folder_to,
        :opts => opts
      )
    end
  end
  
  return ret
end

.mount(args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/knj/mount.rb', line 44

def self.mount(args)
  cmd = "mount"
  cmd << " -t #{Knj::Strings.unixsafe(args["type"])}" if args.key?("type")
  cmd << " --bind" if args["bind"]
  cmd << " #{Knj::Strings.unixsafe(args["from"])} #{Knj::Strings.unixsafe(args["to"])}"
  
  if args.key?("opts")
    raise "opts argument must be an array." if !args["opts"].is_a?(Array)
    
    cmd << "-O "
    
    first = true
    args["opts"].each do |opt|
      cmd << "," if !first
      first = false if first
      
      if opt.is_a?(Array)
        raise "Array-opt must have a length of 2." if opt.length != 2
        cmd << "#{Knj::Strings.unixsafe(opt[0])}=#{Knj::Strings.unixsafe(opt[1])}"
      elsif arg.is_a?(String)
        cmd << "#{Knj::Strings.unixsafe(opt)}"
      else
        raise "Unknown class: #{opt.class.name}."
      end
    end
  end
  
  Knj::Os.shellcmd(cmd)
end

Instance Method Details

#[](key) ⇒ Object



87
88
89
90
# File 'lib/knj/mount.rb', line 87

def [](key)
  raise "Invalid key: #{key}." if !@data.key?(key)
  return @data[key]
end

#access?(args = {}) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/knj/mount.rb', line 98

def access?(args = {})
  args["timeout"] = 2 if !args.key?("timeout")
  access = false
  
  begin
    Timeout.timeout(args["timeout"]) do
      Dir.new(@data[:to]).each do |file|
        access = true
        break
      end
    end
  rescue Timeout::Error => e
    return false
  end
  
  return access
end

#unmountObject Also known as: umount



92
93
94
# File 'lib/knj/mount.rb', line 92

def unmount
  Knj::Os.shellcmd("umount #{Knj::Strings.unixsafe(@data[:to])}")
end