Class: Wright::Util::FilePermissions Private

Inherits:
Object
  • Object
show all
Defined in:
lib/wright/util/file_permissions.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.

Helper class to manage file permissions.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, filetype) ⇒ FilePermissions

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.

Initializes a FilePermissions object.

Parameters:

  • filename (String)

    the file's name

  • filetype (Symbol)

    the file's type (:file or :directory)


46
47
48
49
50
51
52
# File 'lib/wright/util/file_permissions.rb', line 46

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

Instance Attribute Details

#filenameString

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 the filename.

Returns:

  • (String)

    the filename


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

def filename
  @filename
end

#gidInteger

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 the file's intended gid.

Returns:

  • (Integer)

    the file's intended gid


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

def gid
  @gid
end

#modeInteger

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 the file's intended mode.

Returns:

  • (Integer)

    the file's intended mode


37
38
39
# File 'lib/wright/util/file_permissions.rb', line 37

def mode
  @mode
end

#uidInteger

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 the file's intended uid.

Returns:

  • (Integer)

    the file's intended uid


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

def uid
  @uid
end

Class Method Details

.create_from_resource(resource, filetype) ⇒ Wright::Util::FilePermissions

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.

Creates a FilePermissions object from a Resource::File or Resource::Directory.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if the user or group are invalid


18
19
20
21
22
23
24
25
# File 'lib/wright/util/file_permissions.rb', line 18

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

Instance Method Details

#current_gidInteger

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 the file's current group's gid.

Returns:

  • (Integer)

    the file's current group's gid


99
100
101
# File 'lib/wright/util/file_permissions.rb', line 99

def current_gid
  Wright::Util::File.file_group(filename)
end

#current_modeInteger

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 the file's current mode.

Returns:

  • (Integer)

    the file's current mode


89
90
91
# File 'lib/wright/util/file_permissions.rb', line 89

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

#current_uidInteger

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 the file's current owner's uid.

Returns:

  • (Integer)

    the file's current owner's uid


94
95
96
# File 'lib/wright/util/file_permissions.rb', line 94

def current_uid
  Wright::Util::File.file_owner(filename)
end

#updatevoid

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.

This method returns an undefined value.

Updates the file's uid, gid and mode.


83
84
85
86
# File 'lib/wright/util/file_permissions.rb', line 83

def update
  ::File.chmod(mode, filename) if mode
  ::File.chown(uid, gid, filename) if uid || gid
end

#uptodate?Bool

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.

Checks if the file's uid, gid and mode are up-to-date

Returns:

  • (Bool)

    true if the file is up to date, false otherwise


72
73
74
75
76
77
78
# File 'lib/wright/util/file_permissions.rb', line 72

def uptodate?
  if ::File.exist?(filename)
    uid_uptodate? && gid_uptodate? && mode_uptodate?
  else
    false
  end
end