Class: Train::File::Remote::Unix

Inherits:
Train::File::Remote show all
Defined in:
lib/train/file/remote/unix.rb

Direct Known Subclasses

Aix, Linux, Qnx

Constant Summary

Constants inherited from Train::File

DATA_FIELDS

Instance Method Summary collapse

Methods inherited from Train::File::Remote

#basename, #stat

Methods inherited from Train::File

#block_device?, #character_device?, #directory?, #file?, #file_version, #initialize, #md5sum, #mounted?, #owned_by?, #pipe?, #product_version, #sha256sum, #socket?, #source, #source_path, #symlink?, #to_json, #type, #version?

Constructor Details

This class inherits a constructor from Train::File

Instance Method Details

#contentObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/train/file/remote/unix.rb', line 12

def content
  @content ||=
    if !exist? || directory?
      nil
    elsif size.nil? || size == 0
      ""
    else
      @backend.run_command("cat #{@spath}").stdout || ""
    end
end

#content=(new_content) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/train/file/remote/unix.rb', line 23

def content=(new_content)
  execute_result = @backend.run_command("base64 --help")
  if execute_result.exit_status != 0
    raise TransportError, "#{self.class} found no base64 binary for file writes"
  end

  unix_cmd = format("echo '%<base64>s' | base64 --decode > %<file>s",
                    base64: Base64.strict_encode64(new_content),
                    file: @spath)

  @backend.run_command(unix_cmd)

  @content = new_content
end

#exist?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/train/file/remote/unix.rb', line 38

def exist?
  @exist ||= begin
    f = @follow_symlink ? "" : " || test -L #{@spath}"
    if @backend.platform.solaris?
      # Solaris does not support `-e` flag in default `test`,
      # so we specify by running /usr/bin/test:
      # https://github.com/inspec/train/issues/587
      @backend.run_command("/usr/bin/test -e #{@spath}" + f)
    else
      @backend.run_command("test -e #{@spath}" + f)
    end.exit_status == 0
  end
end

#grouped_into?(sth) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/train/file/remote/unix.rb', line 69

def grouped_into?(sth)
  group == sth
end


77
78
79
# File 'lib/train/file/remote/unix.rb', line 77

def link_path
  symlink? ? path : nil
end

#linked_to?(dst) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/train/file/remote/unix.rb', line 73

def linked_to?(dst)
  link_path == dst
end

#mode?(sth) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/train/file/remote/unix.rb', line 65

def mode?(sth)
  mode == sth
end

#mountedObject



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

def mounted
  @mounted ||=
    @backend.run_command("mount | grep -- ' on #{@path} '")
end

#pathObject



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

def path
  return @path unless @follow_symlink && symlink?

  @link_path ||= read_target_path
end

#sanitize_filename(path) ⇒ Object



8
9
10
# File 'lib/train/file/remote/unix.rb', line 8

def sanitize_filename(path)
  @spath = Shellwords.escape(path) || @path
end


81
82
83
84
85
86
# File 'lib/train/file/remote/unix.rb', line 81

def shallow_link_path
  return nil unless symlink?

  @shallow_link_path ||=
    @backend.run_command("readlink #{@spath}").stdout.chomp
end

#unix_mode_mask(owner, type) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/train/file/remote/unix.rb', line 88

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