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