Module: Puppet::FileSystem

Defined in:
lib/puppet/file_system.rb,
lib/puppet/file_system/path_pattern.rb

Defined Under Namespace

Classes: AbsolutePathPattern, FileImpl, MemoryFile, MemoryImpl, PathPattern, Posix, RelativePathPattern, Uniquefile, Windows

Class Method Summary collapse

Class Method Details

.assert_path(path) ⇒ Object

Asserts that the given path is of the expected type produced by #pathname

Raises:

  • (ArgumentError)

    when path is not of the expected type



338
339
340
# File 'lib/puppet/file_system.rb', line 338

def self.assert_path(path)
  @impl.assert_path(path)
end

.basename(path) ⇒ Object

Returns the name of the file as a opaque handle.



70
71
72
# File 'lib/puppet/file_system.rb', line 70

def self.basename(path)
  @impl.basename(assert_path(path))
end

.basename_string(path) ⇒ String

Returns the name of the file.



78
79
80
# File 'lib/puppet/file_system.rb', line 78

def self.basename_string(path)
  @impl.path_string(@impl.basename(assert_path(path)))
end

.binread(path) ⇒ String

Returns The binary contents of the file.



146
147
148
# File 'lib/puppet/file_system.rb', line 146

def self.binread(path)
  @impl.binread(assert_path(path))
end

.children(path) ⇒ Array<Object>

Returns references to all of the children of the given directory path, excluding . and ...



218
219
220
# File 'lib/puppet/file_system.rb', line 218

def self.children(path)
  @impl.children(assert_path(path))
end

.chmod(mode, path) ⇒ Object

Changes permission bits on the named path to the bit pattern represented by mode.

Raises:

  • (Errno::ENOENT)

    : path doesn’t exist



373
374
375
# File 'lib/puppet/file_system.rb', line 373

def self.chmod(mode, path)
  @impl.chmod(mode, path)
end

.compare_stream(path, stream) ⇒ Boolean

Compares the contents of this file against the contents of a stream.



314
315
316
# File 'lib/puppet/file_system.rb', line 314

def self.compare_stream(path, stream)
  @impl.compare_stream(assert_path(path), stream)
end

.dir(path) ⇒ Object

Returns The directory of this file as an opaque handle.



44
45
46
# File 'lib/puppet/file_system.rb', line 44

def self.dir(path)
  @impl.dir(assert_path(path))
end

.dir_exist?(path) ⇒ Boolean

Returns Does the directory of the given path exist?.



57
58
59
# File 'lib/puppet/file_system.rb', line 57

def self.dir_exist?(path)
  @impl.exist?(@impl.dir(assert_path(path)))
end

.dir_mkpath(path) ⇒ Object

Creates all directories down to (inclusive) the dir of the given path



62
63
64
# File 'lib/puppet/file_system.rb', line 62

def self.dir_mkpath(path)
  @impl.mkpath(@impl.dir(assert_path(path)))
end

.dir_string(path) ⇒ String

Returns The directory of this file as a String.



52
53
54
# File 'lib/puppet/file_system.rb', line 52

def self.dir_string(path)
  @impl.path_string(@impl.dir(assert_path(path)))
end

.directory?(path) ⇒ Boolean

Determines if a file is a directory.



166
167
168
# File 'lib/puppet/file_system.rb', line 166

def self.directory?(path)
  @impl.directory?(assert_path(path))
end

.each_line(path, &block) ⇒ Object

Processes each line of the file by yielding it to the given block



117
118
119
# File 'lib/puppet/file_system.rb', line 117

def self.each_line(path, &block)
  @impl.each_line(assert_path(path), &block)
end

.exclusive_create(path, mode, &block) ⇒ Object

Create and open a file for write only if it doesn’t exist.

Raises:

  • (Errno::EEXIST)

    path already exists.

See Also:

  • open


359
360
361
# File 'lib/puppet/file_system.rb', line 359

def self.exclusive_create(path, mode, &block)
  @impl.exclusive_create(assert_path(path), mode, &block)
end

.exclusive_open(path, 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 acquire the lock the process will wait ever increasing amounts of time in order to prevent multiple processes from wasting resources.

Yields:

  • The file handle, in read-write mode

Raises:

  • (Timeout::Error)

    If the timeout is exceeded while waiting to acquire the lock



109
110
111
# File 'lib/puppet/file_system.rb', line 109

def self.exclusive_open(path, mode, options = 'r', timeout = 300, &block)
  @impl.exclusive_open(assert_path(path), mode, options, timeout, &block)
end

.executable?(path) ⇒ Boolean

TODO:

Should this take into account extensions on the windows platform?

Determines if a file is executable.



187
188
189
# File 'lib/puppet/file_system.rb', line 187

def self.executable?(path)
  @impl.executable?(assert_path(path))
end

.exist?(path) ⇒ Boolean

Determines if a file exists by verifying that the file can be stat’d. Will follow symlinks and verify that the actual target path exists.



157
158
159
# File 'lib/puppet/file_system.rb', line 157

def self.exist?(path)
  @impl.exist?(assert_path(path))
end

.file?(path) ⇒ Boolean

Determines if a file is a file.



175
176
177
# File 'lib/puppet/file_system.rb', line 175

def self.file?(path)
  @impl.file?(assert_path(path))
end

.lstat(path) ⇒ File::Stat

link. Instead, reports on the link itself.



303
304
305
# File 'lib/puppet/file_system.rb', line 303

def self.lstat(path)
  @impl.lstat(assert_path(path))
end

.mkpath(path) ⇒ Object

Creates directories for all parts of the given path.



211
212
213
# File 'lib/puppet/file_system.rb', line 211

def self.mkpath(path)
  @impl.mkpath(assert_path(path))
end

.open(path, mode, options, &block) ⇒ Object

Opens the given path with given mode, and options and optionally yields it to the given block.



36
37
38
# File 'lib/puppet/file_system.rb', line 36

def self.open(path, mode, options, &block)
  @impl.open(assert_path(path), mode, options, &block)
end

.overlay(*files, &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.

Allows overriding the filesystem for the duration of the given block. The filesystem will only contain the given file(s).



24
25
26
27
28
29
30
# File 'lib/puppet/file_system.rb', line 24

def self.overlay(*files, &block)
  old_impl = @impl
  @impl = Puppet::FileSystem::MemoryImpl.new(*files)
  yield
ensure
  @impl = old_impl
end

.path_string(path) ⇒ String

Produces a string representation of the opaque path handle.



347
348
349
# File 'lib/puppet/file_system.rb', line 347

def self.path_string(path)
  @impl.path_string(path)
end

.pathname(path) ⇒ Object

Produces an opaque pathname “handle” object representing the given path. Different implementations of the underlying file system may use different runtime objects. The produced “handle” should be used in all other operations that take a “path”. No operation should be directly invoked on the returned opaque object



328
329
330
# File 'lib/puppet/file_system.rb', line 328

def self.pathname(path)
  @impl.pathname(path)
end

.read(path) ⇒ String

Returns The contents of the file.



125
126
127
# File 'lib/puppet/file_system.rb', line 125

def self.read(path)
  @impl.read(assert_path(path))
end

.read_preserve_line_endings(path) ⇒ String

Read a file keeping the original line endings intact. This attempts to open files using binary mode using some encoding overrides and falling back to IO.read when none of the encodings are valid.



138
139
140
# File 'lib/puppet/file_system.rb', line 138

def self.read_preserve_line_endings(path)
  @impl.read_preserve_line_endings(assert_path(path))
end

Returns the name of the file referenced by the given link.



265
266
267
# File 'lib/puppet/file_system.rb', line 265

def self.readlink(path)
  @impl.readlink(assert_path(path))
end

.size(path) ⇒ Integer

Returns the size of the file.



86
87
88
# File 'lib/puppet/file_system.rb', line 86

def self.size(path)
  @impl.size(assert_path(path))
end

.stat(path) ⇒ File::Stat

Returns object for the named file.



286
287
288
# File 'lib/puppet/file_system.rb', line 286

def self.stat(path)
  @impl.stat(assert_path(path))
end

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

Options Hash (options):

  • :force (Boolean)

    overwrite dest

  • :noop (Boolean)

    do not perform the operation

  • :verbose (Boolean)

    verbose output

Raises:

  • (Errno::EEXIST)

    dest already exists as a file and, :force is not set



249
250
251
# File 'lib/puppet/file_system.rb', line 249

def self.symlink(path, dest, options = {})
  @impl.symlink(assert_path(path), dest, options)
end

.symlink?(path) ⇒ Boolean

Returns true if the file is a symbolic link.



257
258
259
# File 'lib/puppet/file_system.rb', line 257

def self.symlink?(path)
  @impl.symlink?(assert_path(path))
end

.touch(path) ⇒ Object

Touches the file. On most systems this updates the mtime of the file.



203
204
205
# File 'lib/puppet/file_system.rb', line 203

def self.touch(path)
  @impl.touch(assert_path(path))
end

Deletes the given paths, returning the number of names passed as arguments. See also Dir::rmdir.

Raises:

  • an exception on any error.



278
279
280
# File 'lib/puppet/file_system.rb', line 278

def self.unlink(*paths)
  @impl.unlink(*(paths.map {|p| assert_path(p) }))
end

.writable?(path) ⇒ Boolean

Returns Whether the file is writable by the current process.



195
196
197
# File 'lib/puppet/file_system.rb', line 195

def self.writable?(path)
  @impl.writable?(assert_path(path))
end