Class: OsCtld::Assets::BaseFile

Inherits:
Base
  • Object
show all
Defined in:
lib/osctld/assets/base_file.rb

Direct Known Subclasses

Directory, File, Symlink, UnixSocket

Instance Attribute Summary

Attributes inherited from Base

#errors, #opts, #path

Instance Method Summary collapse

Methods inherited from Base

#add_error, register, #state, type, #type, #validate, #validate?

Constructor Details

#initialize(path, opts) ⇒ BaseFile

Returns a new instance of BaseFile

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • user (Integer, nil)
  • group (Integer, nil)
  • mode (Integer, nil)
  • optional (Boolean)


12
13
14
# File 'lib/osctld/assets/base_file.rb', line 12

def initialize(path, opts)
  super
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/osctld/assets/base_file.rb', line 38

def exist?
  File.exist?(path)
end

#modeObject (protected)



47
48
49
50
# File 'lib/osctld/assets/base_file.rb', line 47

def mode
  # Extract permission bits, see man inode(7)
  stat.mode & 07777
end

#statObject (protected)



43
44
45
# File 'lib/osctld/assets/base_file.rb', line 43

def stat
  @stat ||= File.stat(path)
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/osctld/assets/base_file.rb', line 16

def valid?
  return true if !exist? && opts[:optional]

  if opts[:user] && stat.uid != opts[:user]
    add_error("invalid owner: expected #{opts[:user]}, got #{stat.uid}")
  end

  if opts[:group] && stat.gid != opts[:group]
    add_error("invalid group: expected #{opts[:group]}, got #{stat.gid}")
  end

  if opts[:mode] && mode != opts[:mode]
    add_error("invalid mode: expected #{opts[:mode].to_s(8)}, got #{mode.to_s(8)}")
  end

  super

rescue Errno::ENOENT
  add_error('does not exist')
  false
end