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

Constant Summary collapse

DATA_FIELDS =

interface methods: these fields should be implemented by every backend File

%w{
  exist? mode owner group uid gid content mtime size selinux_label path
  product_version file_version
}.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FileCommon.



23
24
25
26
27
# File 'lib/train/extras/file_common.rb', line 23

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

Instance Method Details

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



141
142
143
144
# File 'lib/train/extras/file_common.rb', line 141

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)


66
67
68
# File 'lib/train/extras/file_common.rb', line 66

def block_device?
  type.to_s == 'block_device'
end

#character_device?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/train/extras/file_common.rb', line 70

def character_device?
  type.to_s == 'character_device'
end

#directory?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/train/extras/file_common.rb', line 78

def directory?
  type.to_s == 'directory'
end

#file?Boolean

Additional methods for convenience

Returns:

  • (Boolean)


62
63
64
# File 'lib/train/extras/file_common.rb', line 62

def file?
  type.to_s == 'file'
end

#grouped_into?(sth) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/train/extras/file_common.rb', line 110

def grouped_into?(sth)
  group == sth
end


118
119
120
# File 'lib/train/extras/file_common.rb', line 118

def link_path
  symlink? ? path : nil
end

#linked_to?(dst) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



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

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

#mode?(sth) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/train/extras/file_common.rb', line 102

def mode?(sth)
  mode == sth
end

#mounted?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/train/extras/file_common.rb', line 137

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

#owned_by?(sth) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/train/extras/file_common.rb', line 106

def owned_by?(sth)
  owner == sth
end

#pipe?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/train/extras/file_common.rb', line 98

def pipe?
  type == :pipe
end

#sha256sumObject



52
53
54
55
56
57
58
# File 'lib/train/extras/file_common.rb', line 52

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

#socket?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/train/extras/file_common.rb', line 74

def socket?
  type.to_s == 'socket'
end

#sourceObject



90
91
92
93
94
95
96
# File 'lib/train/extras/file_common.rb', line 90

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

#source_pathObject



86
87
88
# File 'lib/train/extras/file_common.rb', line 86

def source_path
  @path
end

#symlink?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/train/extras/file_common.rb', line 82

def symlink?
  source.type.to_s == 'symlink'
end

#to_jsonObject



29
30
31
32
33
34
35
# File 'lib/train/extras/file_common.rb', line 29

def to_json
  res = Hash[DATA_FIELDS.map { |x| [x, method(x).call] }]
  # additional fields provided as input
  res['type'] = type
  res['follow_symlink'] = @follow_symlink
  res
end

#typeObject



37
38
39
# File 'lib/train/extras/file_common.rb', line 37

def type
  :unknown
end

#unix_mode_mask(owner, type) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/train/extras/file_common.rb', line 127

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)


122
123
124
125
# File 'lib/train/extras/file_common.rb', line 122

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