Class: Train::File::Local

Inherits:
Train::File show all
Defined in:
lib/train/file/local.rb,
lib/train/file/local/unix.rb,
lib/train/file/local/windows.rb

Direct Known Subclasses

Unix, Windows

Defined Under Namespace

Classes: Unix, Windows

Constant Summary

Constants inherited from Train::File

DATA_FIELDS

Instance Method Summary collapse

Methods inherited from Train::File

#directory?, #file?, #file_version, #initialize, #md5sum, #mounted?, #owned_by?, #path, #pipe?, #product_version, #sanitize_filename, #sha256sum, #socket?, #source, #source_path, #symlink?, #to_json, #version?

Constructor Details

This class inherits a constructor from Train::File

Instance Method Details

#block_device?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/train/file/local.rb', line 41

def block_device?
  ::File.blockdev?(@path)
end

#character_device?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/train/file/local.rb', line 45

def character_device?
  ::File.chardev?(@path)
end

#contentObject



12
13
14
15
16
# File 'lib/train/file/local.rb', line 12

def content
  @content ||= ::File.read(@path, encoding: "UTF-8")
rescue StandardError => _
  nil
end

#content=(new_content) ⇒ Object



18
19
20
21
22
# File 'lib/train/file/local.rb', line 18

def content=(new_content)
  ::File.open(@path, "w", encoding: "UTF-8") { |fp| fp.write(new_content) }

  @content = new_content
end


24
25
26
27
28
29
30
31
32
33
# File 'lib/train/file/local.rb', line 24

def link_path
  return nil unless symlink?

  begin
    @link_path ||= ::File.realpath(@path)
  rescue Errno::ELOOP => _
    # Leave it blank on symbolic loop, same as readlink
    @link_path = ""
  end
end

#linked_to?(dst) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/train/file/local.rb', line 76

def linked_to?(dst)
  link_path == dst
end

#mode?(sth) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/train/file/local.rb', line 72

def mode?(sth)
  mode == sth
end


35
36
37
38
39
# File 'lib/train/file/local.rb', line 35

def shallow_link_path
  return nil unless symlink?

  @link_path ||= ::File.readlink(@path)
end

#typeObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/train/file/local.rb', line 49

def type
  case ::File.ftype(@path)
  when "blockSpecial"
    :block_device
  when "characterSpecial"
    :character_device
  when "link"
    :symlink
  when "fifo"
    :pipe
  else
    ::File.ftype(@path).to_sym
  end
end