Class: Puppet::FileSystem::FileImpl Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/file_system/file_impl.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.

Abstract implementation of the Puppet::FileSystem

API:

  • private

Direct Known Subclasses

Posix

Instance Method Summary collapse

Instance Method Details

#assert_path(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.

API:

  • private



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

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.

API:

  • private



38
39
40
# File 'lib/puppet/file_system/file_impl.rb', line 38

def basename(path)
  path.basename.to_s
end

#binread(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.

Raises:

API:

  • private



92
93
94
# File 'lib/puppet/file_system/file_impl.rb', line 92

def binread(path)
  raise NotImplementedError
end

#children(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.

API:

  • private



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

def children(path)
  path.children
end

#chmod(mode, 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.

API:

  • private



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

def chmod(mode, path)
  FileUtils.chmod(mode, path)
end

#compare_stream(path, stream) ⇒ 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.

API:

  • private



152
153
154
# File 'lib/puppet/file_system/file_impl.rb', line 152

def compare_stream(path, stream)
  open(path, 0, 'rb') { |this| FileUtils.compare_stream(this, stream) }
end

#dir(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.

API:

  • private



34
35
36
# File 'lib/puppet/file_system/file_impl.rb', line 34

def dir(path)
  path.dirname
end

#directory?(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.

Returns:

API:

  • private



100
101
102
# File 'lib/puppet/file_system/file_impl.rb', line 100

def directory?(path)
  ::File.directory?(path)
end

#each_line(path, &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.

API:

  • private



74
75
76
77
78
79
80
# File 'lib/puppet/file_system/file_impl.rb', line 74

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

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.

API:

  • private



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

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.

API:

  • private



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet/file_system/file_impl.rb', line 51

def exclusive_open(path, mode, options = 'r', timeout = 300, &block)
  wait = 0.001 + (Kernel.rand / 1000)
  written = false
  while !written
    ::File.open(path, options, mode) do |rf|
      if rf.flock(::File::LOCK_EX|::File::LOCK_NB)
        Puppet.debug{ _("Locked '%{path}'") % { path: path } }
        yield rf
        written = true
        Puppet.debug{ _("Unlocked '%{path}'") % { path: path } }
      else
        Puppet.debug{ "Failed to lock '%s' retrying in %.2f milliseconds" % [path, wait * 1000] }
        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

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:

API:

  • private



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

def executable?(path)
  ::File.executable?(path)
end

#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.

Returns:

API:

  • private



96
97
98
# File 'lib/puppet/file_system/file_impl.rb', line 96

def exist?(path)
  ::File.exist?(path)
end

#expand_path(path, dir_string = nil) ⇒ 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.

API:

  • private



25
26
27
28
# File 'lib/puppet/file_system/file_impl.rb', line 25

def expand_path(path, dir_string = nil)
  # ensure `nil` values behave like underlying File.expand_path
  ::File.expand_path(path.nil? ? nil : path_string(path), dir_string)
end

#file?(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.

Returns:

API:

  • private



104
105
106
# File 'lib/puppet/file_system/file_impl.rb', line 104

def file?(path)
  ::File.file?(path)
end

#lstat(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.

API:

  • private



148
149
150
# File 'lib/puppet/file_system/file_impl.rb', line 148

def lstat(path)
  ::File.lstat(path)
end

#mkpath(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.

API:

  • private



120
121
122
# File 'lib/puppet/file_system/file_impl.rb', line 120

def mkpath(path)
  path.mkpath
end

#open(path, 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.

API:

  • private



30
31
32
# File 'lib/puppet/file_system/file_impl.rb', line 30

def open(path, mode, options, &block)
  ::File.open(path, options, mode, &block)
end

#path_string(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.

API:

  • private



21
22
23
# File 'lib/puppet/file_system/file_impl.rb', line 21

def path_string(path)
  path.to_s
end

#pathname(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.

API:

  • private



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

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.

API:

  • private



82
83
84
# File 'lib/puppet/file_system/file_impl.rb', line 82

def read(path, opts = {})
  path.read(**opts)
end

#read_preserve_line_endings(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.

API:

  • private



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

def read_preserve_line_endings(path)
  default_encoding = Encoding.default_external.name
  encoding = default_encoding.downcase.start_with?('utf-') ? "bom|#{default_encoding}" : default_encoding
  read(path, encoding: encoding)
end

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.

API:

  • private



136
137
138
# File 'lib/puppet/file_system/file_impl.rb', line 136

def readlink(path)
  ::File.readlink(path)
end

#replace_file(path, mode = nil) ⇒ 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.

API:

  • private



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/puppet/file_system/file_impl.rb', line 160

def replace_file(path, mode = nil)
  begin
    stat = lstat(path)
    gid = stat.gid
    uid = stat.uid
    mode ||= stat.mode & 07777
  rescue Errno::ENOENT
    mode ||= 0640
  end

  tempfile = Puppet::FileSystem::Uniquefile.new(Puppet::FileSystem.basename_string(path), Puppet::FileSystem.dir_string(path))
  begin
    begin
      yield tempfile
      tempfile.flush
      tempfile.fsync
    ensure
      tempfile.close
    end

    tempfile_path = tempfile.path
    FileUtils.chown(uid, gid, tempfile_path) if uid && gid
    chmod(mode, tempfile_path)
    ::File.rename(tempfile_path, path_string(path))
  ensure
    tempfile.close!
  end
end

#size(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.

API:

  • private



42
43
44
# File 'lib/puppet/file_system/file_impl.rb', line 42

def size(path)
  path.size
end

#stat(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.

API:

  • private



144
145
146
# File 'lib/puppet/file_system/file_impl.rb', line 144

def stat(path)
  ::File.stat(path)
end

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.

API:

  • private



128
129
130
# File 'lib/puppet/file_system/file_impl.rb', line 128

def symlink(path, dest, options = {})
  FileUtils.symlink(path, dest, **options)
end

#symlink?(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.

Returns:

API:

  • private



132
133
134
# File 'lib/puppet/file_system/file_impl.rb', line 132

def symlink?(path)
  ::File.symlink?(path)
end

#touch(path, mtime: nil) ⇒ 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.

API:

  • private



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

def touch(path, mtime: nil)
  ::FileUtils.touch(path, mtime: mtime)
end

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.

API:

  • private



140
141
142
# File 'lib/puppet/file_system/file_impl.rb', line 140

def unlink(*paths)
  ::File.unlink(*paths)
end

#writable?(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.

Returns:

API:

  • private



112
113
114
# File 'lib/puppet/file_system/file_impl.rb', line 112

def writable?(path)
  path.writable?
end