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, #prefetch_zfs, register, #run_validation, #state, #type, #valid?, #validate?, #validate_block

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)
  • mode_bit_and (Integer, nil)
  • optional (Boolean)


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

def initialize(path, opts) # rubocop:disable all
  super
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/osctld/assets/base_file.rb', line 17

def exist?
  File.exist?(path)
end

#modeObject (protected)



53
54
55
56
# File 'lib/osctld/assets/base_file.rb', line 53

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

#statObject (protected)



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

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

#validate(run) ⇒ Object (protected)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/osctld/assets/base_file.rb', line 23

def validate(run)
  return if !exist? && opts[:optional]

  begin
    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

    if opts[:mode_bit_and] && (mode & opts[:mode_bit_and]) != opts[:mode_bit_and]
      add_error("invalid mode: bitwise and with #{opts[:mode_bit_and].to_s(8)} does not match")
    end
  rescue Errno::ENOENT
    add_error('does not exist')
  end

  super
end