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

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