Class: Puppet::FileSystem::Windows Private
- Defined in:
- lib/puppet/file_system/windows.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Constant Summary collapse
- FULL_CONTROL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Puppet::Util::Windows::File::FILE_ALL_ACCESS
- FILE_READ =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Puppet::Util::Windows::File::FILE_GENERIC_READ
- FILE_NOT_FOUND =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
docs.microsoft.com/en-us/windows/desktop/debug/system-error-codes–0-499-
2
- ACCESS_DENIED =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
5
- SHARING_VIOLATION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
32
- LOCK_VIOLATION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
33
Instance Method Summary collapse
- #chmod(mode, path) ⇒ Object private
- #exist?(path) ⇒ Boolean private
- #expand_path(path, dir_string = nil) ⇒ Object private
- #lstat(path) ⇒ Object private
- #open(path, mode, options, &block) ⇒ Object private
- #read_preserve_line_endings(path) ⇒ Object private
- #readlink(path) ⇒ Object private
- #replace_file(path, mode = nil) ⇒ Object private
- #stat(path) ⇒ Object private
- #symlink(path, dest, options = {}) ⇒ Object private
- #symlink?(path) ⇒ Boolean private
- #unlink(*file_names) ⇒ Object private
Methods inherited from Posix
Methods inherited from FileImpl
#assert_path, #basename, #binread, #children, #compare_stream, #dir, #directory?, #each_line, #exclusive_create, #exclusive_open, #executable?, #file?, #mkpath, #path_string, #pathname, #read, #size, #touch, #writable?
Instance Method Details
#chmod(mode, path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
107 108 109 |
# File 'lib/puppet/file_system/windows.rb', line 107 def chmod(mode, path) Puppet::Util::Windows::Security.set_mode(mode, path.to_s) end |
#exist?(path) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
35 36 37 |
# File 'lib/puppet/file_system/windows.rb', line 35 def exist?(path) return Puppet::Util::Windows::File.exist?(path) end |
#expand_path(path, dir_string = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/puppet/file_system/windows.rb', line 16 def (path, dir_string = nil) # ensure `nil` values behave like underlying File.expand_path string_path = ::File.(path.nil? ? nil : path_string(path), dir_string) # if no tildes, nothing to expand, no need to call Windows API, return original string return string_path if !string_path.index('~') begin # no need to do existence check up front as GetLongPathName implies that check is performed # and it should be the exception that files aren't actually present string_path = Puppet::Util::Windows::File.get_long_pathname(string_path) rescue Puppet::Util::Windows::Error => e # preserve original File.expand_path behavior for file / path not found by returning string raise if (e.code != Puppet::Util::Windows::File::ERROR_FILE_NOT_FOUND && e.code != Puppet::Util::Windows::File::ERROR_PATH_NOT_FOUND) end string_path end |
#lstat(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
100 101 102 103 104 105 |
# File 'lib/puppet/file_system/windows.rb', line 100 def lstat(path) if ! Puppet.features.manages_symlinks? return Puppet::Util::Windows::File.stat(path) end Puppet::Util::Windows::File.lstat(path) end |
#open(path, mode, options, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 9 10 11 12 13 14 |
# File 'lib/puppet/file_system/windows.rb', line 8 def open(path, mode, , &block) # PUP-6959 mode is explicitly ignored until it can be implemented # Ruby on Windows uses mode for setting file attributes like read-only and # archived, not for setting permissions like POSIX raise TypeError.new('mode must be specified as an Integer') if mode && !mode.is_a?(Numeric) ::File.open(path, , nil, &block) end |
#read_preserve_line_endings(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
111 112 113 114 115 116 117 |
# File 'lib/puppet/file_system/windows.rb', line 111 def read_preserve_line_endings(path) contents = path.read( :mode => 'rb', :encoding => 'bom|utf-8') contents = path.read( :mode => 'rb', :encoding => "bom|#{Encoding::default_external.name}") unless contents.valid_encoding? contents = path.read unless contents.valid_encoding? contents end |
#readlink(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
65 66 67 68 |
# File 'lib/puppet/file_system/windows.rb', line 65 def readlink(path) raise_if_symlinks_unsupported Puppet::Util::Windows::File.readlink(path) end |
#replace_file(path, mode = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/puppet/file_system/windows.rb', line 125 def replace_file(path, mode = nil) if directory?(path) raise Errno::EISDIR, _("Is a directory: %{directory}") % { directory: path } end current_sid = Puppet::Util::Windows::SID.name_to_sid(Puppet::Util::Windows::ADSI::User.current_user_name) current_sid = Puppet::Util::Windows::SID.name_to_sid(Puppet::Util::Windows::ADSI::User.current_sam_compatible_user_name) unless current_sid dacl = case mode when 0644 dacl = secure_dacl(current_sid) dacl.allow(Puppet::Util::Windows::SID::BuiltinUsers, FILE_READ) dacl when 0660, 0640, 0600, 0440 secure_dacl(current_sid) when nil get_dacl_from_file(path) || secure_dacl(current_sid) else raise ArgumentError, "#{mode} is invalid: Only modes 0644, 0640, 0660, and 0440 are allowed" end tempfile = Puppet::FileSystem::Uniquefile.new(Puppet::FileSystem.basename_string(path), Puppet::FileSystem.dir_string(path)) begin tempdacl = Puppet::Util::Windows::AccessControlList.new tempdacl.allow(current_sid, FULL_CONTROL) set_dacl(tempfile.path, tempdacl) begin yield tempfile tempfile.flush tempfile.fsync ensure tempfile.close end set_dacl(tempfile.path, dacl) if dacl ::File.rename(tempfile.path, path_string(path)) ensure tempfile.close! end rescue Puppet::Util::Windows::Error => e case e.code when ACCESS_DENIED, SHARING_VIOLATION, LOCK_VIOLATION raise Errno::EACCES.new(path_string(path), e) else raise SystemCallError.new(e.) end end |
#stat(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 |
# File 'lib/puppet/file_system/windows.rb', line 96 def stat(path) Puppet::Util::Windows::File.stat(path) end |
#symlink(path, dest, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/puppet/file_system/windows.rb', line 39 def symlink(path, dest, = {}) raise_if_symlinks_unsupported dest_exists = exist?(dest) # returns false on dangling symlink dest_stat = Puppet::Util::Windows::File.stat(dest) if dest_exists # silent fail to preserve semantics of original FileUtils return 0 if dest_exists && dest_stat.ftype == 'directory' if dest_exists && dest_stat.ftype == 'file' && [:force] != true raise(Errno::EEXIST, _("%{dest} already exists and the :force option was not specified") % { dest: dest }) end if [:noop] != true ::File.delete(dest) if dest_exists # can only be file Puppet::Util::Windows::File.symlink(path, dest) end 0 end |
#symlink?(path) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
60 61 62 63 |
# File 'lib/puppet/file_system/windows.rb', line 60 def symlink?(path) return false if ! Puppet.features.manages_symlinks? Puppet::Util::Windows::File.symlink?(path) end |
#unlink(*file_names) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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 |
# File 'lib/puppet/file_system/windows.rb', line 70 def unlink(*file_names) if ! Puppet.features.manages_symlinks? return ::File.unlink(*file_names) end file_names.each do |file_name| file_name = file_name.to_s # handle PathName stat = Puppet::Util::Windows::File.stat(file_name) rescue nil # sigh, Ruby + Windows :( if !stat ::File.unlink(file_name) rescue Dir.rmdir(file_name) elsif stat.ftype == 'directory' if Puppet::Util::Windows::File.symlink?(file_name) Dir.rmdir(file_name) else raise Errno::EPERM.new(file_name) end else ::File.unlink(file_name) end end file_names.length end |