Class: Train::File

Inherits:
Object
  • Object
show all
Defined in:
lib/train/file.rb,
lib/train/file/local.rb,
lib/train/file/remote.rb,
lib/train/file/local/unix.rb,
lib/train/file/remote/aix.rb,
lib/train/file/remote/qnx.rb,
lib/train/file/remote/unix.rb,
lib/train/file/remote/linux.rb,
lib/train/file/local/windows.rb,
lib/train/file/remote/windows.rb

Direct Known Subclasses

Local, Remote, Transports::Mock::Connection::File

Defined Under Namespace

Classes: Local, Remote

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
}.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of File.



20
21
22
23
24
25
26
# File 'lib/train/file.rb', line 20

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

  sanitize_filename(path)
end

Instance Method Details

#block_device?Boolean

Returns:

  • (Boolean)


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

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

#character_device?Boolean

Returns:

  • (Boolean)


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

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

#directory?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/train/file.rb', line 124

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

#file?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/train/file.rb', line 116

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

#file_versionObject

file_version is primarily used by Windows operating systems only and will be overwritten in Windows-related classes. Since this field is returned for all file objects, the acceptable default value is nil



95
96
97
# File 'lib/train/file.rb', line 95

def file_version
  nil
end

#md5sumObject



57
58
59
60
61
62
63
# File 'lib/train/file.rb', line 57

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

#mounted?Boolean

if the OS-specific file class supports inquirying as to whether the file/device is mounted, the #mounted method should return a command object whose stdout will not be nil if indeed the device is mounted.

if the OS-specific file class does not support checking for mount status, the method should not be implemented and this method will return false.

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/train/file.rb', line 151

def mounted?
  return false unless respond_to?(:mounted)

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

#owned_by?(sth) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/train/file.rb', line 132

def owned_by?(sth)
  owner == sth
end

#pathObject



136
137
138
139
140
141
142
# File 'lib/train/file.rb', line 136

def path
  if symlink? && @follow_symlink
    link_path
  else
    @path
  end
end

#pipe?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/train/file.rb', line 112

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

#product_versionObject

product_version is primarily used by Windows operating systems only and will be overwritten in Windows-related classes. Since this field is returned for all file objects, the acceptable default value is nil



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

def product_version
  nil
end

#sanitize_filename(_path) ⇒ Object

This method gets override by particular os class.



29
30
31
# File 'lib/train/file.rb', line 29

def sanitize_filename(_path)
  nil
end

#sha256sumObject



65
66
67
68
69
70
71
# File 'lib/train/file.rb', line 65

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

#socket?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/train/file.rb', line 120

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

#sourceObject



73
74
75
76
77
78
79
# File 'lib/train/file.rb', line 73

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

#source_pathObject



81
82
83
# File 'lib/train/file.rb', line 81

def source_path
  @path
end

#symlink?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/train/file.rb', line 128

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

#to_jsonObject



45
46
47
48
49
50
51
# File 'lib/train/file.rb', line 45

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



53
54
55
# File 'lib/train/file.rb', line 53

def type
  :unknown
end

#version?(version) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/train/file.rb', line 99

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