Class: Train::File::Remote::Windows

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

Constant Summary

Constants inherited from Train::File

DATA_FIELDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Train::File::Remote

#stat

Methods inherited from Train::File

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

Constructor Details

This class inherits a constructor from Train::File

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/train/file/remote/windows.rb', line 5

def path
  @path
end

Instance Method Details

#basename(suffix = nil, sep = "\\") ⇒ Object



15
16
17
# File 'lib/train/file/remote/windows.rb', line 15

def basename(suffix = nil, sep = "\\")
  super(suffix, sep)
end

#contentObject



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

def content
  return @content if defined?(@content)

  @content = @backend.run_command("Get-Content(\"#{@spath}\") | Out-String").stdout
  return @content unless @content.empty?

  @content = nil if directory? # or size.nil? or size > 0
  @content
end

#content=(new_content) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/train/file/remote/windows.rb', line 29

def content=(new_content)
  win_cmd = format('[IO.File]::WriteAllBytes("%<file>s", [Convert]::FromBase64String("%<base64>s"))',
                   base64: Base64.strict_encode64(new_content),
                   file: @spath)

  @backend.run_command(win_cmd)

  @content = new_content
end

#exist?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/train/file/remote/windows.rb', line 39

def exist?
  return @exist if defined?(@exist)

  @exist = @backend.run_command(
    "(Test-Path -Path \"#{@spath}\").ToString()"
  ).stdout.chomp == "True"
end

#file_versionObject



80
81
82
83
84
# File 'lib/train/file/remote/windows.rb', line 80

def file_version
  @file_version ||= @backend.run_command(
    "[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").FileVersion"
  ).stdout.chomp
end


94
95
96
# File 'lib/train/file/remote/windows.rb', line 94

def link_path
  nil
end

#mountedObject



102
103
104
# File 'lib/train/file/remote/windows.rb', line 102

def mounted
  nil
end

#ownerObject



47
48
49
50
51
52
53
54
# File 'lib/train/file/remote/windows.rb', line 47

def owner
  owner = @backend.run_command(
    "Get-Acl \"#{@spath}\" | select -expand Owner"
  ).stdout.strip
  return if owner.empty?

  owner
end

#product_versionObject



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

def product_version
  @product_version ||= @backend.run_command(
    "[System.Diagnostics.FileVersionInfo]::GetVersionInfo(\"#{@spath}\").ProductVersion"
  ).stdout.chomp
end

#sanitize_filename(path) ⇒ Object

Ensures we do not use invalid characters for file names



8
9
10
11
12
13
# File 'lib/train/file/remote/windows.rb', line 8

def sanitize_filename(path)
  return if path.nil?

  # we do not filter :, backslash and forward slash, since they are part of the path
  @spath = path.gsub(/[<>"|?*]/, "")
end


98
99
100
# File 'lib/train/file/remote/windows.rb', line 98

def shallow_link_path
  nil
end

#sizeObject



68
69
70
71
72
# File 'lib/train/file/remote/windows.rb', line 68

def size
  if file?
    @backend.run_command("((Get-Item '#{@spath}').Length)").stdout.strip.to_i
  end
end

#typeObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/train/file/remote/windows.rb', line 56

def type
  if attributes.include?("Archive") && !attributes.include?("Directory")
    return :file
  elsif attributes.include?("ReparsePoint")
    return :symlink
  elsif attributes.include?("Directory")
    return :directory
  end

  :unknown
end