Class: Puppet::FileSystem::File Private
- Defined in:
- lib/puppet/file_system/file.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
An abstraction over the ruby file system operations for a single file.
For the time being this is being kept private so that we can evolve it for a while.
Constant Summary collapse
- IMPL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
if RUBY_VERSION =~ /^1\.8/ require 'puppet/file_system/file18' Puppet::FileSystem::File18 elsif Puppet::Util::Platform.windows? require 'puppet/file_system/file19windows' Puppet::FileSystem::File19Windows else require 'puppet/file_system/file19' Puppet::FileSystem::File19 end
Instance Attribute Summary collapse
- #path ⇒ Object readonly private
Class Method Summary collapse
-
.exist?(path) ⇒ Boolean
private
Determine if a file exists by verifying that the file can be stat’d.
-
.forget(file) ⇒ Object
private
Forget a remembered file.
- .new(path) ⇒ Object private
-
.overlay(file, &block) ⇒ Object
private
Run a block of code with a file accessible in the filesystem.
-
.remember(file) ⇒ Object
private
Create a binding between a filename and a particular instance of a file object.
-
.unlink(*file_names) ⇒ Integer
private
Deletes the named files, returning the number of names passed as arguments.
Instance Method Summary collapse
-
#basename ⇒ String
The name of the file.
-
#binread ⇒ String
private
The binary contents of the file.
-
#compare_stream(stream) ⇒ Boolean
private
Compare the contents of this file against the contents of a stream.
-
#dir ⇒ Puppet::FileSystem::File
The directory of this file.
- #each_line(&block) ⇒ Object private
-
#exclusive_open(mode, options = 'r', timeout = 300) { ... } ⇒ Void
Allows exclusive updates to a file to be made by excluding concurrent access using flock.
-
#executable? ⇒ Boolean
private
Determine if a file is executable.
-
#exist? ⇒ Boolean
private
Determine if a file exists by verifying that the file can be stat’d.
-
#initialize(path) ⇒ File
constructor
private
A new instance of File.
-
#lstat ⇒ File::Stat
private
link.
-
#mkpath ⇒ Object
private
Create the entire path as directories.
- #open(mode, options, &block) ⇒ Object private
-
#read ⇒ String
private
The contents of the file.
-
#readlink ⇒ String
private
The name of the file referenced by the given link.
-
#size ⇒ Num
The size of this file.
-
#stat ⇒ File::Stat
private
Object for the named file.
-
#symlink(dest, options = {}) ⇒ Integer
private
Creates a symbolic link dest which points to the current file.
-
#symlink? ⇒ Boolean
private
True if the file is a symbolic link.
- #to_s ⇒ Object private
-
#touch ⇒ Object
private
Touches the file.
-
#unlink ⇒ Integer
private
Deletes the file.
-
#writable? ⇒ Boolean
private
process.
Constructor Details
#initialize(path) ⇒ File
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of File.
63 64 65 66 67 68 69 |
# File 'lib/puppet/file_system/file.rb', line 63 def initialize(path) if path.is_a?(Pathname) @path = path else @path = Pathname.new(path) end end |
Instance Attribute Details
#path ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 9 10 |
# File 'lib/puppet/file_system/file.rb', line 8 def path @path end |
Class Method Details
.exist?(path) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determine if a file exists by verifying that the file can be stat’d. Will follow symlinks and verify that the actual target path exists.
151 152 153 154 |
# File 'lib/puppet/file_system/file.rb', line 151 def self.exist?(path) return IMPL.exist?(path) if IMPL.method(:exist?) != self.method(:exist?) File.exist?(path) end |
.forget(file) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This API should only be used for testing
Forget a remembered file
59 60 61 |
# File 'lib/puppet/file_system/file.rb', line 59 def self.forget(file) @remembered.delete(file.path.to_s) end |
.new(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
23 24 25 26 27 28 29 30 31 |
# File 'lib/puppet/file_system/file.rb', line 23 def self.new(path) if @remembered.include?(path.to_s) @remembered[path.to_s] else file = IMPL.allocate file.send(:initialize, path) file end end |
.overlay(file, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This API should only be used for testing
Run a block of code with a file accessible in the filesystem.
38 39 40 41 42 43 |
# File 'lib/puppet/file_system/file.rb', line 38 def self.(file, &block) remember(file) yield ensure forget(file) end |
.remember(file) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This API should only be used for testing
Create a binding between a filename and a particular instance of a file object.
50 51 52 |
# File 'lib/puppet/file_system/file.rb', line 50 def self.remember(file) @remembered[file.path.to_s] = file end |
.unlink(*file_names) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deletes the named files, returning the number of names passed as arguments. See also Dir::rmdir.
233 234 235 236 |
# File 'lib/puppet/file_system/file.rb', line 233 def self.unlink(*file_names) return IMPL.unlink(*file_names) if IMPL.method(:unlink) != self.method(:unlink) File.unlink(*file_names) end |
Instance Method Details
#basename ⇒ String
Returns the name of the file.
83 84 85 |
# File 'lib/puppet/file_system/file.rb', line 83 def basename @path.basename.to_s end |
#binread ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The binary contents of the file.
143 144 145 |
# File 'lib/puppet/file_system/file.rb', line 143 def binread raise NotImplementedError end |
#compare_stream(stream) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compare the contents of this file against the contents of a stream.
262 263 264 265 266 |
# File 'lib/puppet/file_system/file.rb', line 262 def compare_stream(stream) open(0, 'rb') do |this| FileUtils.compare_stream(this, stream) end end |
#dir ⇒ Puppet::FileSystem::File
Returns The directory of this file.
77 78 79 |
# File 'lib/puppet/file_system/file.rb', line 77 def dir Puppet::FileSystem::File.new(@path.dirname) end |
#each_line(&block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
129 130 131 132 133 134 135 |
# File 'lib/puppet/file_system/file.rb', line 129 def each_line(&block) ::File.open(@path) do |f| f.each_line do |line| yield line end end end |
#exclusive_open(mode, options = 'r', timeout = 300) { ... } ⇒ Void
Allows exclusive updates to a file to be made by excluding concurrent access using flock. This means that if the file is on a filesystem that does not support flock, this method will provide no protection.
While polling to aquire the lock the process will wait ever increasing amounts of time in order to prevent multiple processes from wasting resources.
(defaults to read-only mode)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/puppet/file_system/file.rb', line 109 def exclusive_open(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}" end end end end end |
#executable? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Should this take into account extensions on the windows platform?
Determine if a file is executable.
169 170 171 |
# File 'lib/puppet/file_system/file.rb', line 169 def executable? ::File.executable?(@path) end |
#exist? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determine if a file exists by verifying that the file can be stat’d. Will follow symlinks and verify that the actual target path exists.
160 161 162 |
# File 'lib/puppet/file_system/file.rb', line 160 def exist? self.class.exist?(@path) end |
#lstat ⇒ File::Stat
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
link. Instead, reports on the link itself.
255 256 257 |
# File 'lib/puppet/file_system/file.rb', line 255 def lstat File.lstat(@path) end |
#mkpath ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create the entire path as directories
185 186 187 |
# File 'lib/puppet/file_system/file.rb', line 185 def mkpath @path.mkpath end |
#open(mode, options, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 |
# File 'lib/puppet/file_system/file.rb', line 71 def open(mode, , &block) ::File.open(@path, , mode, &block) end |
#read ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The contents of the file.
138 139 140 |
# File 'lib/puppet/file_system/file.rb', line 138 def read @path.read end |
#readlink ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the name of the file referenced by the given link.
223 224 225 |
# File 'lib/puppet/file_system/file.rb', line 223 def readlink File.readlink(@path) end |
#size ⇒ Num
Returns The size of this file.
89 90 91 |
# File 'lib/puppet/file_system/file.rb', line 89 def size @path.size end |
#stat ⇒ File::Stat
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns object for the named file.
249 250 251 |
# File 'lib/puppet/file_system/file.rb', line 249 def stat File.stat(@path) end |
#symlink(dest, options = {}) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a symbolic link dest which points to the current file. If dest already exists:
-
and is a file, will raise Errno::EEXIST
-
and is a directory, will return 0 but perform no action
-
and is a symlink referencing a file, will raise Errno::EEXIST
-
and is a symlink referencing a directory, will return 0 but perform no action
With the :force option set to true, when dest already exists:
-
and is a file, will replace the existing file with a symlink (DANGEROUS)
-
and is a directory, will return 0 but perform no action
-
and is a symlink referencing a file, will modify the existing symlink
-
and is a symlink referencing a directory, will return 0 but perform no action
213 214 215 |
# File 'lib/puppet/file_system/file.rb', line 213 def symlink(dest, = {}) FileUtils.symlink(@path, dest, ) end |
#symlink? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if the file is a symbolic link.
218 219 220 |
# File 'lib/puppet/file_system/file.rb', line 218 def symlink? File.symlink?(@path) end |
#to_s ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
268 269 270 |
# File 'lib/puppet/file_system/file.rb', line 268 def to_s @path.to_s end |
#touch ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Touches the file. On most systems this updates the mtime of the file.
180 181 182 |
# File 'lib/puppet/file_system/file.rb', line 180 def touch ::FileUtils.touch(@path) end |
#unlink ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deletes the file. See also Dir::rmdir.
244 245 246 |
# File 'lib/puppet/file_system/file.rb', line 244 def unlink self.class.unlink(@path) end |
#writable? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
process
175 176 177 |
# File 'lib/puppet/file_system/file.rb', line 175 def writable? @path.writable? end |