Class: Train::Extras::FileCommon

Inherits:
Object
  • Object
show all
Defined in:
lib/train/extras/file_common.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

UnixFile, WindowsFile

Instance Method Summary collapse

Constructor Details

#initialize(backend, path, follow_symlink = true) ⇒ FileCommon

Returns a new instance of FileCommon.



21
22
23
24
25
# File 'lib/train/extras/file_common.rb', line 21

def initialize(backend, path, follow_symlink = true)
  @backend = backend
  @path = path || ''
  @follow_symlink = follow_symlink
end

Instance Method Details

#basename(suffix = nil, sep = '/') ⇒ Object



131
132
133
134
# File 'lib/train/extras/file_common.rb', line 131

def basename(suffix = nil, sep = '/')
  fail 'Not yet supported: Suffix in file.basename' unless suffix.nil?
  @basename ||= detect_filename(path, sep || '/')
end

#block_device?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/train/extras/file_common.rb', line 56

def block_device?
  type == :block_device
end

#character_device?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/train/extras/file_common.rb', line 60

def character_device?
  type == :character_device
end

#directory?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/train/extras/file_common.rb', line 68

def directory?
  type == :directory
end

#file?Boolean

Additional methods for convenience

Returns:

  • (Boolean)


52
53
54
# File 'lib/train/extras/file_common.rb', line 52

def file?
  type == :file
end

#grouped_into?(sth) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/train/extras/file_common.rb', line 100

def grouped_into?(sth)
  group == sth
end


108
109
110
# File 'lib/train/extras/file_common.rb', line 108

def link_path
  symlink? ? path : nil
end

#linked_to?(dst) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/train/extras/file_common.rb', line 104

def linked_to?(dst)
  link_path == dst
end

#md5sumObject

The following methods can be overwritten by a derived class if desired, to e.g. achieve optimizations.



34
35
36
37
38
39
40
# File 'lib/train/extras/file_common.rb', line 34

def md5sum
  res = Digest::MD5.new
  res.update(content)
  res.hexdigest
rescue TypeError => _
  nil
end

#mode?(sth) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/train/extras/file_common.rb', line 92

def mode?(sth)
  mode == sth
end

#mounted?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/train/extras/file_common.rb', line 127

def mounted?
  !mounted.nil? && !mounted.stdout.nil? && !mounted.stdout.empty?
end

#owned_by?(sth) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/train/extras/file_common.rb', line 96

def owned_by?(sth)
  owner == sth
end

#pipe?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/train/extras/file_common.rb', line 88

def pipe?
  type == :pipe
end

#sha256sumObject



42
43
44
45
46
47
48
# File 'lib/train/extras/file_common.rb', line 42

def sha256sum
  res = Digest::SHA256.new
  res.update(content)
  res.hexdigest
rescue TypeError => _
  nil
end

#socket?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/train/extras/file_common.rb', line 64

def socket?
  type == :socket
end

#sourceObject



80
81
82
83
84
85
86
# File 'lib/train/extras/file_common.rb', line 80

def source
  if @follow_symlink
    self.class.new(@backend, @path, false)
  else
    self
  end
end

#source_pathObject



76
77
78
# File 'lib/train/extras/file_common.rb', line 76

def source_path
  @path
end

#symlink?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/train/extras/file_common.rb', line 72

def symlink?
  source.type == :symlink
end

#typeObject



27
28
29
# File 'lib/train/extras/file_common.rb', line 27

def type
  :unknown
end

#unix_mode_mask(owner, type) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/train/extras/file_common.rb', line 117

def unix_mode_mask(owner, type)
  o = UNIX_MODE_OWNERS[owner.to_sym]
  return nil if o.nil?

  t = UNIX_MODE_TYPES[type.to_sym]
  return nil if t.nil?

  t & o
end

#version?(version) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/train/extras/file_common.rb', line 112

def version?(version)
  product_version == version or
    file_version == version
end