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
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

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
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