File: windows.rb

package info (click to toggle)
ruby-train 3.13.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,208 kB
  • sloc: ruby: 10,002; sh: 17; makefile: 8
file content (118 lines) | stat: -rw-r--r-- 3,189 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module Train
  class File
    class Remote
      class Windows < Train::File::Remote
        attr_reader :path
        # Ensures we do not use invalid characters for file names
        # @see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions
        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

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

        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

        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

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

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

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

          owner
        end

        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

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

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

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

        %w{
          mode group uid gid mtime selinux_label
        }.each do |field|
          define_method field.to_sym do
            nil
          end
        end

        def link_path
          nil
        end

        def shallow_link_path
          nil
        end

        def mounted
          nil
        end

        private

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

          @attributes = @backend.run_command(
            "(Get-ItemProperty -Path \"#{@spath}\").attributes.ToString()"
          ).stdout.chomp.split(/\s*,\s*/)
        end
      end
    end
  end
end