Class: Puppet::FileSystem::FileImpl
- Defined in:
- lib/puppet/file_system/file_impl.rb
Overview
Abstract implementation of the Puppet::FileSystem
Direct Known Subclasses
Instance Method Summary collapse
- #assert_path(path) ⇒ Object
- #basename(path) ⇒ Object
- #binread(path) ⇒ Object
- #children(path) ⇒ Object
- #chmod(mode, path) ⇒ Object
- #compare_stream(path, stream) ⇒ Object
- #dir(path) ⇒ Object
- #directory?(path) ⇒ Boolean
- #each_line(path, &block) ⇒ Object
- #exclusive_create(path, mode, &block) ⇒ Object
- #exclusive_open(path, mode, options = 'r', timeout = 300, &block) ⇒ Object
- #executable?(path) ⇒ Boolean
- #exist?(path) ⇒ Boolean
- #expand_path(path, dir_string = nil) ⇒ Object
- #file?(path) ⇒ Boolean
- #lstat(path) ⇒ Object
- #mkpath(path) ⇒ Object
- #open(path, mode, options, &block) ⇒ Object
- #path_string(path) ⇒ Object
- #pathname(path) ⇒ Object
- #read(path, opts = {}) ⇒ Object
- #read_preserve_line_endings(path) ⇒ Object
- #readlink(path) ⇒ Object
- #size(path) ⇒ Object
- #stat(path) ⇒ Object
- #symlink(path, dest, options = {}) ⇒ Object
- #symlink?(path) ⇒ Boolean
- #touch(path) ⇒ Object
- #unlink(*paths) ⇒ Object
- #writable?(path) ⇒ Boolean
Instance Method Details
#assert_path(path) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/puppet/file_system/file_impl.rb', line 9 def assert_path(path) return path if path.is_a?(Pathname) # Some paths are string, or in the case of WatchedFile, it pretends to be # one by implementing to_str. if path.respond_to?(:to_str) Pathname.new(path) else raise ArgumentError, _("FileSystem implementation expected Pathname, got: '%{klass}'") % { klass: path.class } end end |
#basename(path) ⇒ Object
38 39 40 |
# File 'lib/puppet/file_system/file_impl.rb', line 38 def basename(path) path.basename.to_s end |
#binread(path) ⇒ Object
87 88 89 |
# File 'lib/puppet/file_system/file_impl.rb', line 87 def binread(path) raise NotImplementedError end |
#children(path) ⇒ Object
119 120 121 |
# File 'lib/puppet/file_system/file_impl.rb', line 119 def children(path) path.children end |
#chmod(mode, path) ⇒ Object
151 152 153 |
# File 'lib/puppet/file_system/file_impl.rb', line 151 def chmod(mode, path) FileUtils.chmod(mode, path) end |
#compare_stream(path, stream) ⇒ Object
147 148 149 |
# File 'lib/puppet/file_system/file_impl.rb', line 147 def compare_stream(path, stream) open(path, 0, 'rb') { |this| FileUtils.compare_stream(this, stream) } end |
#dir(path) ⇒ Object
34 35 36 |
# File 'lib/puppet/file_system/file_impl.rb', line 34 def dir(path) path.dirname end |
#directory?(path) ⇒ Boolean
95 96 97 |
# File 'lib/puppet/file_system/file_impl.rb', line 95 def directory?(path) ::File.directory?(path) end |
#each_line(path, &block) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/puppet/file_system/file_impl.rb', line 71 def each_line(path, &block) ::File.open(path) do |f| f.each_line do |line| yield line end end end |
#exclusive_create(path, mode, &block) ⇒ Object
46 47 48 49 |
# File 'lib/puppet/file_system/file_impl.rb', line 46 def exclusive_create(path, mode, &block) opt = File::CREAT | File::EXCL | File::WRONLY self.open(path, mode, opt, &block) end |
#exclusive_open(path, mode, options = 'r', timeout = 300, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/puppet/file_system/file_impl.rb', line 51 def exclusive_open(path, mode, = 'r', timeout = 300, &block) wait = 0.001 + (Kernel.rand / 1000) written = false while !written ::File.open(path, , mode) do |rf| if rf.flock(::File::LOCK_EX|::File::LOCK_NB) yield rf written = true else sleep wait timeout -= wait wait *= 2 if timeout < 0 raise Timeout::Error, _("Timeout waiting for exclusive lock on %{path}") % { path: @path } end end end end end |
#executable?(path) ⇒ Boolean
103 104 105 |
# File 'lib/puppet/file_system/file_impl.rb', line 103 def executable?(path) ::File.executable?(path) end |
#exist?(path) ⇒ Boolean
91 92 93 |
# File 'lib/puppet/file_system/file_impl.rb', line 91 def exist?(path) ::File.exist?(path) end |
#expand_path(path, dir_string = nil) ⇒ Object
25 26 27 28 |
# File 'lib/puppet/file_system/file_impl.rb', line 25 def (path, dir_string = nil) # ensure `nil` values behave like underlying File.expand_path ::File.(path.nil? ? nil : path_string(path), dir_string) end |
#file?(path) ⇒ Boolean
99 100 101 |
# File 'lib/puppet/file_system/file_impl.rb', line 99 def file?(path) ::File.file?(path) end |
#lstat(path) ⇒ Object
143 144 145 |
# File 'lib/puppet/file_system/file_impl.rb', line 143 def lstat(path) File.lstat(path) end |
#mkpath(path) ⇒ Object
115 116 117 |
# File 'lib/puppet/file_system/file_impl.rb', line 115 def mkpath(path) path.mkpath end |
#open(path, mode, options, &block) ⇒ Object
30 31 32 |
# File 'lib/puppet/file_system/file_impl.rb', line 30 def open(path, mode, , &block) ::File.open(path, , mode, &block) end |
#path_string(path) ⇒ Object
21 22 23 |
# File 'lib/puppet/file_system/file_impl.rb', line 21 def path_string(path) path.to_s end |
#pathname(path) ⇒ Object
5 6 7 |
# File 'lib/puppet/file_system/file_impl.rb', line 5 def pathname(path) path.is_a?(Pathname) ? path : Pathname.new(path) end |
#read(path, opts = {}) ⇒ Object
79 80 81 |
# File 'lib/puppet/file_system/file_impl.rb', line 79 def read(path, opts = {}) path.read(opts) end |
#read_preserve_line_endings(path) ⇒ Object
83 84 85 |
# File 'lib/puppet/file_system/file_impl.rb', line 83 def read_preserve_line_endings(path) read(path) end |
#readlink(path) ⇒ Object
131 132 133 |
# File 'lib/puppet/file_system/file_impl.rb', line 131 def readlink(path) File.readlink(path) end |
#size(path) ⇒ Object
42 43 44 |
# File 'lib/puppet/file_system/file_impl.rb', line 42 def size(path) path.size end |
#stat(path) ⇒ Object
139 140 141 |
# File 'lib/puppet/file_system/file_impl.rb', line 139 def stat(path) File.stat(path) end |
#symlink(path, dest, options = {}) ⇒ Object
123 124 125 |
# File 'lib/puppet/file_system/file_impl.rb', line 123 def symlink(path, dest, = {}) FileUtils.symlink(path, dest, ) end |
#symlink?(path) ⇒ Boolean
127 128 129 |
# File 'lib/puppet/file_system/file_impl.rb', line 127 def symlink?(path) File.symlink?(path) end |
#touch(path) ⇒ Object
111 112 113 |
# File 'lib/puppet/file_system/file_impl.rb', line 111 def touch(path) ::FileUtils.touch(path) end |
#unlink(*paths) ⇒ Object
135 136 137 |
# File 'lib/puppet/file_system/file_impl.rb', line 135 def unlink(*paths) File.unlink(*paths) end |
#writable?(path) ⇒ Boolean
107 108 109 |
# File 'lib/puppet/file_system/file_impl.rb', line 107 def writable?(path) path.writable? end |