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