Class: Wright::Util::FilePermissions

Inherits:
Object
  • Object
show all
Defined in:
lib/wright/util/file_permissions.rb

Overview

Internal: Helper class to manage file permissions.

Constant Summary collapse

VALID_FILETYPES =

Internal: Supported filetypes.

[:file, :directory]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, filetype) ⇒ FilePermissions

Internal: Initialize a FilePermissions object.

filename - The file’s name. filetype - The file’s type, typically :file or :directory.



42
43
44
45
46
47
48
# File 'lib/wright/util/file_permissions.rb', line 42

def initialize(filename, filetype)
  unless VALID_FILETYPES.include?(filetype)
    fail ArgumentError, "Invalid filetype '#{filetype}'"
  end
  @filename = filename
  @filetype = filetype
end

Instance Attribute Details

#filenameObject

Internal: Get/Set the target file’s name.



24
25
26
# File 'lib/wright/util/file_permissions.rb', line 24

def filename
  @filename
end

#groupObject

Internal: Get/Set the file’s target group.



27
28
29
# File 'lib/wright/util/file_permissions.rb', line 27

def group
  @group
end

#modeObject

Internal: Get/Set the file’s target mode.



30
31
32
# File 'lib/wright/util/file_permissions.rb', line 30

def mode
  @mode
end

#ownerObject

Internal: Get the file’s target owner.



33
34
35
# File 'lib/wright/util/file_permissions.rb', line 33

def owner
  @owner
end

Class Method Details

.create_from_resource(resource, filetype) ⇒ Object

Internal: Create a FilePermissions from a Wright::Resource.

resource - The resource object. filetype - The file’s type, typically :file or :directory.

Returns a Wright::Util::FilePermissions object.



14
15
16
17
18
19
20
21
# File 'lib/wright/util/file_permissions.rb', line 14

def self.create_from_resource(resource, filetype)
  filepath = ::File.expand_path(resource.name)
  p = Wright::Util::FilePermissions.new(filepath, filetype)
  p.owner = resource.owner
  p.group = resource.group
  p.mode = resource.mode
  p
end

Instance Method Details

#current_groupObject

Internal: Get the file’s current group.



101
102
103
# File 'lib/wright/util/file_permissions.rb', line 101

def current_group
  Wright::Util::File.file_group(@filename)
end

#current_modeObject

Internal: Get the file’s current mode.



91
92
93
# File 'lib/wright/util/file_permissions.rb', line 91

def current_mode
  Wright::Util::File.file_mode(@filename)
end

#current_ownerObject

Internal: Get the file’s current owner.



96
97
98
# File 'lib/wright/util/file_permissions.rb', line 96

def current_owner
  Wright::Util::File.file_owner(@filename)
end

#updateObject

Internal: Update the file’s owner, group and mode.



85
86
87
88
# File 'lib/wright/util/file_permissions.rb', line 85

def update
  ::File.chmod(@mode, @filename) if @mode
  ::File.chown(@owner, @group, @filename) if @owner || @group
end

#uptodate?Boolean

Internal: Check if the file’s owner, group and mode are up-to-date.

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/wright/util/file_permissions.rb', line 76

def uptodate?
  if ::File.exist?(@filename)
    owner_uptodate? && group_uptodate? && mode_uptodate?
  else
    false
  end
end