Kanrisuru

Mode

Kanrisuru::Mode class models the mode permissions for users and groups on a unix-like system. The mode class helps translate between numeric and symbolic representation of permission bits set on a filesytem.

A file permission mode is characterized as having 3 distinct access types, owner, group, and other. The owner is a user that the file belongs to. The group type is which group the file / directoy belongs to. Other is anyone that isn’t either the owner or the group, that has file access permissions.

For each ownership type, a file can be marked as having the following permission bits set:

  • Read: Can the file be read from.
  • Write: Can changes be made to a file
  • Execute: Can the file be executed as a program on the linux sytem.

The 3 permission bits can be set based on their positioning in the octal format, eg rwx, where each permission setting can be marked as a 1 for active, and 0 for inactive. The 3 permission bits can then be represented as either a symoblic string, namely: rwx, or a numeric value, 7.

The 3 ownership types combined form a symoblic mode permission that is commonly found on linux systems

-rwxr--r--


The first slot designates if the permission is for a directory or a file, then the owner, group and other.

Namespace

Mode::Permission

Public Class Methods

new

Instanties a new mode instance. The mode can either be in a symbolic or numeric string type, as well as an octal integer type.

mode = Kanrisuru::Mode.new("644")
mode.symbolic
"-rw-r--r--"

mode = Kanrisuru::Mode.new("drwxrwxrwx")
mode.numeric
"777"

mode = Kanrisuru::Mode.new(0o644)
mode.to_s
"-rw-r--r--"

Public Instance Methods

directory?

Returns if the permission bits is marked as directory or not.

Kanrisuru::Mode.new("drwxrwxrwx").directory?
true

Kanrisuru::Mode.new("644").directory?
false

symbolic

Returns the symbolic representation of the mode permission.

mode = Kanrisuru::Mode.new("600")
mode.symbolic
"-rw-------"

symbolic=

Set mode permission bits via symbolic representation. Can either be set on the entire owner, group and other modes, as well as adding permission or removing permission on a single type.

# Setting the entire permission chain.
mode = Kanrisuru::Mode.new("400")
mode.symbolic = "-rw-r--rw-"
mode.numeric
"646"

# Change bits per ownership type, using traditional notation
mode.symbolic = "u=rwx,g+w" 
mode.symbolic
"-rwxrw-rw-"

# Change all bits, using traditional notation
mode.symbolic = "a-x"
mode.symbolic
"-rw-rw-rw"

numeric

Returns the numeric octal format as a string.

mode = Kanrisuru::Mode.new("drwxrwxrwx")
mode.numeric
"777"

numeric=

Set the file permission bits via numeric notation. Accepts either a string representation of the numeric format, or an octal integer type.

mode = Kanrisuru::Mode.new("drw-rw-rw-")
mode.numeric = "600"
mode.symbolic
"drw-------"

mode.numeric = 0o644
mode.symbolic
"drw-r--r--"

inspect

Returns a string version of the instance.

mode = Kanrisuru::Mode.new("777")
mode.inspect
"#<Kanrisuru::Mode:0x800 @numeric=777 @symbolic=-rwxrwxrwx>"

to_s

Returns the symbolic version of the mode permission

Kanrisuru::Mode.new(0o444).to_s
"-r--r--r--"

to_i

Returns an integer numeric version of the mode permission. The value will be converted into base10, so be careful when inspecting and testing this value

mode = Kanrisuru::Mode.new("-rwx--x--x")
mode.to_i
457

mode.to_i.to_s(8)
"711"