Class: Remotebackup::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/node.rb

Constant Summary collapse

Month =
{"Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8,
"Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12}
LS_INFO =
{
  :redhat => {
    :ls_file => /([-a-z]*)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d*)\s+([A-Z][a-z]*)\s+(\d+)\s+([\d:]*)\s+(.*)/,
    :ls_link => /([-a-z]*)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d*)\s+([A-Z][a-z]*)\s+(\d+)\s+([\d:]*)\s+(.*)\s+->\s+(.*)/,
    :position => 
      {:access=>1,:link_num=>2,:user=>3,:group=>4,:size=>5,:month=>6,:day=>7,:yt=>8,:name=>9,:arrow=>10,:source=>11}
  },
  :debian => {
    :ls_file => /([-a-z]*)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d*)\s+([-0-9]*)\s+([\d:]*)\s+(.*)/,
    :ls_link => /([-a-z]*)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d*)\s+([-0-9]*)\s+([\d:]*)\s+(.*)\s+->\s+(.*)/,
    :position => 
      {:access=>1,:link_num=>2,:user=>3,:group=>4,:size=>5,:day =>6,:time =>7,:name=>8,:arrow=>9,:source=>10}
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, out) ⇒ Node



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/node.rb', line 62

def initialize(type,out)
  @type = type
  case out
  when /^-/
    if m = ls_file.match(out)
      @ftype = :file
      @name = m[position[:name]]
      @size = m[position[:size]].to_i
      @date = to_date_time(m)
      @make_map = lambda{ return {"size" => @size, "date" => @date}}
    end
  when /^l/
    if m = ls_link.match(out)
      @ftype = :symbolic
      @name = m[position[:name]]
      @source = m[position[:source]]
      @make_map = lambda{ return {"source" => @source}}
    end
  when /^d/
    if m = ls_file.match(out)
      @ftype = :directory
      @name = m[position[:name]]
    end
  else
    @ftype = :special
    $log.error("#{out} is not regular file. ignore.")
  end
end

Instance Attribute Details

#ftypeObject

Returns the value of attribute ftype.



3
4
5
# File 'lib/node.rb', line 3

def ftype
  @ftype
end

#make_mapObject

Returns the value of attribute make_map.



3
4
5
# File 'lib/node.rb', line 3

def make_map
  @make_map
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/node.rb', line 3

def name
  @name
end

Class Method Details

.build(out) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/node.rb', line 21

def self.build(out)
  LS_INFO.each do|key,val|
    if val[:ls_file].match(out)
      return Node.new(key,out)
    end
  end
  nil
end

Instance Method Details

#ls_fileObject



50
51
52
# File 'lib/node.rb', line 50

def ls_file
  LS_INFO[@type][:ls_file]
end


54
55
56
# File 'lib/node.rb', line 54

def ls_link
  LS_INFO[@type][:ls_link]
end

#positionObject



58
59
60
# File 'lib/node.rb', line 58

def position
  LS_INFO[@type][:position]
end

#to_date_time(match) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/node.rb', line 30

def to_date_time(match)
  if @type == :redhat
    month = Month[match[position[:month]]]
    day = match[position[:day]].to_i
    hour = 0
    minute = 0
    tmp_time = match[position[:yt]]
    if t = /(.*):(.*)/.match(tmp_time)
      year = DateTime.now.year
      hour = t[1].to_i
      minute = t[2].to_i
    else
      year = tmp_time.to_i
    end
    DateTime.new(year,month,day,hour,minute).to_s
  else
    DateTime.parse(match[position[:time]]).to_s
  end
end