class Puppet::FileSystem::Windows
Constants
- ACCESS_DENIED
- FILE_NOT_FOUND
docs.microsoft.com/en-us/windows/desktop/debug/system-error-codes–0-499-
- FILE_READ
- FULL_CONTROL
- LOCK_VIOLATION
- SHARING_VIOLATION
Public Instance Methods
chmod(mode, path)
click to toggle source
# File lib/puppet/file_system/windows.rb 107 def chmod(mode, path) 108 Puppet::Util::Windows::Security.set_mode(mode, path.to_s) 109 end
exist?(path)
click to toggle source
# File lib/puppet/file_system/windows.rb 35 def exist?(path) 36 return Puppet::Util::Windows::File.exist?(path) 37 end
expand_path(path, dir_string = nil)
click to toggle source
# File lib/puppet/file_system/windows.rb 16 def expand_path(path, dir_string = nil) 17 # ensure `nil` values behave like underlying File.expand_path 18 string_path = ::File.expand_path(path.nil? ? nil : path_string(path), dir_string) 19 # if no tildes, nothing to expand, no need to call Windows API, return original string 20 return string_path if !string_path.index('~') 21 22 begin 23 # no need to do existence check up front as GetLongPathName implies that check is performed 24 # and it should be the exception that files aren't actually present 25 string_path = Puppet::Util::Windows::File.get_long_pathname(string_path) 26 rescue Puppet::Util::Windows::Error => e 27 # preserve original File.expand_path behavior for file / path not found by returning string 28 raise if (e.code != Puppet::Util::Windows::File::ERROR_FILE_NOT_FOUND && 29 e.code != Puppet::Util::Windows::File::ERROR_PATH_NOT_FOUND) 30 end 31 32 string_path 33 end
lstat(path)
click to toggle source
# File lib/puppet/file_system/windows.rb 100 def lstat(path) 101 if ! Puppet.features.manages_symlinks? 102 return Puppet::Util::Windows::File.stat(path) 103 end 104 Puppet::Util::Windows::File.lstat(path) 105 end
open(path, mode, options, &block)
click to toggle source
# File lib/puppet/file_system/windows.rb 8 def open(path, mode, options, &block) 9 # PUP-6959 mode is explicitly ignored until it can be implemented 10 # Ruby on Windows uses mode for setting file attributes like read-only and 11 # archived, not for setting permissions like POSIX 12 raise TypeError.new('mode must be specified as an Integer') if mode && !mode.is_a?(Numeric) 13 ::File.open(path, options, nil, &block) 14 end
read_preserve_line_endings(path)
click to toggle source
# File lib/puppet/file_system/windows.rb 111 def read_preserve_line_endings(path) 112 contents = path.read( :mode => 'rb', :encoding => 'bom|utf-8') 113 contents = path.read( :mode => 'rb', :encoding => "bom|#{Encoding::default_external.name}") unless contents.valid_encoding? 114 contents = path.read unless contents.valid_encoding? 115 116 contents 117 end
readlink(path)
click to toggle source
# File lib/puppet/file_system/windows.rb 65 def readlink(path) 66 raise_if_symlinks_unsupported 67 Puppet::Util::Windows::File.readlink(path) 68 end
replace_file(path, mode = nil) { |tempfile| ... }
click to toggle source
# File lib/puppet/file_system/windows.rb 125 def replace_file(path, mode = nil) 126 if Puppet::FileSystem.directory?(path) 127 raise Errno::EISDIR, _("Is a directory: %{directory}") % { directory: path } 128 end 129 130 current_sid = Puppet::Util::Windows::SID.name_to_sid(Puppet::Util::Windows::ADSI::User.current_user_name) 131 current_sid = Puppet::Util::Windows::SID.name_to_sid(Puppet::Util::Windows::ADSI::User.current_sam_compatible_user_name) unless current_sid 132 133 dacl = case mode 134 when 0644 135 dacl = secure_dacl(current_sid) 136 dacl.allow(Puppet::Util::Windows::SID::BuiltinUsers, FILE_READ) 137 dacl 138 when 0660, 0640, 0600, 0440 139 secure_dacl(current_sid) 140 when nil 141 get_dacl_from_file(path) || secure_dacl(current_sid) 142 else 143 raise ArgumentError, "#{mode} is invalid: Only modes 0644, 0640, 0660, and 0440 are allowed" 144 end 145 146 147 tempfile = Puppet::FileSystem::Uniquefile.new(Puppet::FileSystem.basename_string(path), Puppet::FileSystem.dir_string(path)) 148 begin 149 tempdacl = Puppet::Util::Windows::AccessControlList.new 150 tempdacl.allow(current_sid, FULL_CONTROL) 151 set_dacl(tempfile.path, tempdacl) 152 153 begin 154 yield tempfile 155 tempfile.flush 156 tempfile.fsync 157 ensure 158 tempfile.close 159 end 160 161 set_dacl(tempfile.path, dacl) if dacl 162 File.rename(tempfile.path, Puppet::FileSystem.path_string(path)) 163 ensure 164 tempfile.close! 165 end 166 rescue Puppet::Util::Windows::Error => e 167 case e.code 168 when ACCESS_DENIED, SHARING_VIOLATION, LOCK_VIOLATION 169 raise Errno::EACCES.new(Puppet::FileSystem.path_string(path), e) 170 else 171 raise SystemCallError.new(e.message) 172 end 173 end
stat(path)
click to toggle source
# File lib/puppet/file_system/windows.rb 96 def stat(path) 97 Puppet::Util::Windows::File.stat(path) 98 end
symlink(path, dest, options = {})
click to toggle source
# File lib/puppet/file_system/windows.rb 39 def symlink(path, dest, options = {}) 40 raise_if_symlinks_unsupported 41 42 dest_exists = exist?(dest) # returns false on dangling symlink 43 dest_stat = Puppet::Util::Windows::File.stat(dest) if dest_exists 44 45 # silent fail to preserve semantics of original FileUtils 46 return 0 if dest_exists && dest_stat.ftype == 'directory' 47 48 if dest_exists && dest_stat.ftype == 'file' && options[:force] != true 49 raise(Errno::EEXIST, _("%{dest} already exists and the :force option was not specified") % { dest: dest }) 50 end 51 52 if options[:noop] != true 53 ::File.delete(dest) if dest_exists # can only be file 54 Puppet::Util::Windows::File.symlink(path, dest) 55 end 56 57 0 58 end
symlink?(path)
click to toggle source
# File lib/puppet/file_system/windows.rb 60 def symlink?(path) 61 return false if ! Puppet.features.manages_symlinks? 62 Puppet::Util::Windows::File.symlink?(path) 63 end
unlink(*file_names)
click to toggle source
# File lib/puppet/file_system/windows.rb 70 def unlink(*file_names) 71 if ! Puppet.features.manages_symlinks? 72 return ::File.unlink(*file_names) 73 end 74 75 file_names.each do |file_name| 76 file_name = file_name.to_s # handle PathName 77 stat = Puppet::Util::Windows::File.stat(file_name) rescue nil 78 79 # sigh, Ruby + Windows :( 80 if !stat 81 ::File.unlink(file_name) rescue Dir.rmdir(file_name) 82 elsif stat.ftype == 'directory' 83 if Puppet::Util::Windows::File.symlink?(file_name) 84 Dir.rmdir(file_name) 85 else 86 raise Errno::EPERM.new(file_name) 87 end 88 else 89 ::File.unlink(file_name) 90 end 91 end 92 93 file_names.length 94 end
Private Instance Methods
get_dacl_from_file(path)
click to toggle source
# File lib/puppet/file_system/windows.rb 195 def get_dacl_from_file(path) 196 sd = Puppet::Util::Windows::Security.get_security_descriptor(Puppet::FileSystem.path_string(path)) 197 sd.dacl 198 rescue Puppet::Util::Windows::Error => e 199 raise e unless e.code == FILE_NOT_FOUND 200 end
raise_if_symlinks_unsupported()
click to toggle source
# File lib/puppet/file_system/windows.rb 202 def raise_if_symlinks_unsupported 203 if ! Puppet.features.manages_symlinks? 204 msg = _("This version of Windows does not support symlinks. Windows Vista / 2008 or higher is required.") 205 raise Puppet::Util::Windows::Error.new(msg) 206 end 207 208 if ! Puppet::Util::Windows::Process.process_privilege_symlink? 209 Puppet.warning _("The current user does not have the necessary permission to manage symlinks.") 210 end 211 end
secure_dacl(current_sid)
click to toggle source
# File lib/puppet/file_system/windows.rb 183 def secure_dacl(current_sid) 184 dacl = Puppet::Util::Windows::AccessControlList.new 185 [ 186 Puppet::Util::Windows::SID::LocalSystem, 187 Puppet::Util::Windows::SID::BuiltinAdministrators, 188 current_sid 189 ].uniq.map do |sid| 190 dacl.allow(sid, FULL_CONTROL) 191 end 192 dacl 193 end
set_dacl(path, dacl)
click to toggle source
# File lib/puppet/file_system/windows.rb 177 def set_dacl(path, dacl) 178 sd = Puppet::Util::Windows::Security.get_security_descriptor(path) 179 new_sd = Puppet::Util::Windows::SecurityDescriptor.new(sd.owner, sd.group, dacl, true) 180 Puppet::Util::Windows::Security.set_security_descriptor(path, new_sd) 181 end