Class: Puppet::FileSystem::FileImpl

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/file_system/file_impl.rb

Overview

Abstract implementation of the Puppet::FileSystem

Direct Known Subclasses

File18, File19

Instance Method Summary collapse

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: '#{path.class}'"
  end
end

#basename(path) ⇒ Object



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

def basename(path)
  path.basename.to_s
end

#binread(path) ⇒ Object

Raises:

  • (NotImplementedError)


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

def binread(path)
  raise NotImplementedError
end

#children(path) ⇒ Object



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

def children(path)
  path.children
end

#chmod(mode, path) ⇒ Object



142
143
144
# File 'lib/puppet/file_system/file_impl.rb', line 142

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

#compare_stream(path, stream) ⇒ Object



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

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

#dir(path) ⇒ Object



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

def dir(path)
  path.dirname
end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#each_line(path, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/puppet/file_system/file_impl.rb', line 66

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



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

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/file_system/file_impl.rb', line 46

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)
        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?(path) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/puppet/file_system/file_impl.rb', line 94

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

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/puppet/file_system/file_impl.rb', line 90

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

#lstat(path) ⇒ Object



134
135
136
# File 'lib/puppet/file_system/file_impl.rb', line 134

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

#mkpath(path) ⇒ Object



106
107
108
# File 'lib/puppet/file_system/file_impl.rb', line 106

def mkpath(path)
  path.mkpath
end

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



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

def open(path, mode, options, &block)
  ::File.open(path, options, 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) ⇒ Object



74
75
76
# File 'lib/puppet/file_system/file_impl.rb', line 74

def read(path)
  path.read
end


122
123
124
# File 'lib/puppet/file_system/file_impl.rb', line 122

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

#size(path) ⇒ Object



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

def size(path)
  path.size
end

#stat(path) ⇒ Object



130
131
132
# File 'lib/puppet/file_system/file_impl.rb', line 130

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


114
115
116
# File 'lib/puppet/file_system/file_impl.rb', line 114

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

#symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#touch(path) ⇒ Object



102
103
104
# File 'lib/puppet/file_system/file_impl.rb', line 102

def touch(path)
  ::FileUtils.touch(path)
end


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

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

#writable?(path) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/puppet/file_system/file_impl.rb', line 98

def writable?(path)
  path.writable?
end