class Puppet::FileSystem::FileImpl

Abstract implementation of the Puppet::FileSystem

Public Instance Methods

assert_path(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
10 def assert_path(path)
11   return path if path.is_a?(Pathname)
12 
13   # Some paths are string, or in the case of WatchedFile, it pretends to be
14   # one by implementing to_str.
15   if path.respond_to?(:to_str)
16     Pathname.new(path)
17   else
18     raise ArgumentError, _("FileSystem implementation expected Pathname, got: '%{klass}'") % { klass: path.class }
19   end
20 end
basename(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
39 def basename(path)
40   path.basename.to_s
41 end
binread(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
93 def binread(path)
94   raise NotImplementedError
95 end
children(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
125 def children(path)
126   path.children
127 end
chmod(mode, path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
157 def chmod(mode, path)
158   FileUtils.chmod(mode, path)
159 end
compare_stream(path, stream) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
153 def compare_stream(path, stream)
154   open(path, 0, 'rb') { |this| FileUtils.compare_stream(this, stream) }
155 end
dir(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
35 def dir(path)
36   path.dirname
37 end
directory?(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
101 def directory?(path)
102   ::File.directory?(path)
103 end
each_line(path) { |line| ... } click to toggle source
   # File lib/puppet/file_system/file_impl.rb
75 def each_line(path, &block)
76   ::File.open(path) do |f|
77     f.each_line do |line|
78       yield line
79     end
80   end
81 end
exclusive_create(path, mode, &block) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
47 def exclusive_create(path, mode, &block)
48   opt = File::CREAT | File::EXCL | File::WRONLY
49   self.open(path, mode, opt, &block)
50 end
exclusive_open(path, mode, options = 'r', timeout = 300) { |rf| ... } click to toggle source
   # File lib/puppet/file_system/file_impl.rb
52 def exclusive_open(path, mode, options = 'r', timeout = 300, &block)
53   wait = 0.001 + (Kernel.rand / 1000)
54   written = false
55   while !written
56     ::File.open(path, options, mode) do |rf|
57       if rf.flock(::File::LOCK_EX|::File::LOCK_NB)
58         Puppet.debug{ _("Locked '%{path}'") % { path: path } }
59         yield rf
60         written = true
61         Puppet.debug{ _("Unlocked '%{path}'") % { path: path } }
62       else
63         Puppet.debug{ "Failed to lock '%s' retrying in %.2f milliseconds" % [path, wait * 1000] }
64         sleep wait
65         timeout -= wait
66         wait *= 2
67         if timeout < 0
68           raise Timeout::Error, _("Timeout waiting for exclusive lock on %{path}") % { path: path }
69         end
70       end
71     end
72   end
73 end
executable?(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
109 def executable?(path)
110   ::File.executable?(path)
111 end
exist?(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
97 def exist?(path)
98   ::File.exist?(path)
99 end
expand_path(path, dir_string = nil) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
26 def expand_path(path, dir_string = nil)
27   # ensure `nil` values behave like underlying File.expand_path
28   ::File.expand_path(path.nil? ? nil : path_string(path), dir_string)
29 end
file?(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
105 def file?(path)
106   ::File.file?(path)
107 end
lstat(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
149 def lstat(path)
150   ::File.lstat(path)
151 end
mkpath(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
121 def mkpath(path)
122   path.mkpath
123 end
open(path, mode, options, &block) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
31 def open(path, mode, options, &block)
32   ::File.open(path, options, mode, &block)
33 end
path_string(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
22 def path_string(path)
23   path.to_s
24 end
pathname(path) click to toggle source
  # File lib/puppet/file_system/file_impl.rb
6 def pathname(path)
7   path.is_a?(Pathname) ? path : Pathname.new(path)
8 end
read(path, opts = {}) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
83 def read(path, opts = {})
84   path.read(**opts)
85 end
read_preserve_line_endings(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
87 def read_preserve_line_endings(path)
88   default_encoding = Encoding.default_external.name
89   encoding = default_encoding.downcase.start_with?('utf-') ? "bom|#{default_encoding}" : default_encoding
90   read(path, encoding: encoding)
91 end
replace_file(path, mode = nil) { |tempfile| ... } click to toggle source
    # File lib/puppet/file_system/file_impl.rb
161 def replace_file(path, mode = nil)
162   begin
163     stat = lstat(path)
164     gid = stat.gid
165     uid = stat.uid
166     mode ||= stat.mode & 07777
167   rescue Errno::ENOENT
168     mode ||= 0640
169   end
170 
171   tempfile = Puppet::FileSystem::Uniquefile.new(Puppet::FileSystem.basename_string(path), Puppet::FileSystem.dir_string(path))
172   begin
173     begin
174       yield tempfile
175       tempfile.flush
176       tempfile.fsync
177     ensure
178       tempfile.close
179     end
180 
181     tempfile_path = tempfile.path
182     FileUtils.chown(uid, gid, tempfile_path) if uid && gid
183     chmod(mode, tempfile_path)
184     ::File.rename(tempfile_path, path_string(path))
185   ensure
186     tempfile.close!
187   end
188 end
size(path) click to toggle source
   # File lib/puppet/file_system/file_impl.rb
43 def size(path)
44   path.size
45 end
stat(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
145 def stat(path)
146   ::File.stat(path)
147 end
touch(path, mtime: nil) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
117 def touch(path, mtime: nil)
118   ::FileUtils.touch(path, mtime: mtime)
119 end
writable?(path) click to toggle source
    # File lib/puppet/file_system/file_impl.rb
113 def writable?(path)
114   path.writable?
115 end