filemode

package
v0.304.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package filemode contains helpers when you need to work with file modes.

Index

Constants

View Source
const (
	UserR   = read << shiftUser
	UserW   = write << shiftUser
	UserX   = execute << shiftUser
	UserRW  = UserR | UserW
	UserRWX = UserRW | UserX

	GroupR   = read << shiftGroup
	GroupW   = write << shiftGroup
	GroupX   = execute << shiftGroup
	GroupRW  = GroupR | GroupW
	GroupRWX = GroupRW | GroupX

	OtherR   = read << shiftOther
	OtherW   = write << shiftOther
	OtherX   = execute << shiftOther
	OtherRW  = OtherR | OtherW
	OtherRWX = OtherRW | OtherX

	AllR   = UserR | GroupR | OtherR
	AllW   = UserW | GroupW | OtherW
	AllX   = UserX | GroupX | OtherX
	AllRW  = AllR | AllW
	AllRWX = AllRW | AllX
)

Variables

This section is empty.

Functions

func Contains

func Contains(perm, has os.FileMode) bool

Types

type Octal

type Octal os.FileMode

Octal represents Unix file permissions in octal (base-8) notation.

Octal notation is the numeric representation of POSIX file permissions, where each digit (0-7) represents a combination of read (4), write (2), and execute (1) permissions for user, group, and other respectively.

Format:

  • 3-digit format: Standard permissions (e.g., 755, 644, 777)
  • 4-digit format: Special bits + standard permissions (e.g., 4755, 2644, 1777)

Standard 3-digit format:

  • 2 to left digit: User/owner permissions
  • 1 to left digit: Group permissions
  • 0 to left digit: Other/world permissions

Each digit is calculated by summing the permission values:

  • Read (r): 4
  • Write (w): 2
  • Execute (x): 1

Permission value table:

0 (000): --- (no permissions)
1 (001): --x (execute only)
2 (010): -w- (write only)
3 (011): -wx (write + execute)
4 (100): r-- (read only)
5 (101): r-x (read + execute)
6 (110): rw- (read + write)
7 (111): rwx (read + write + execute)

Examples:

755 = -rwxr-xr-x (user: rwx, group: r-x, other: r-x)
644 = -rw-r--r-- (user: rw-, group: r--, other: r--)
777 = -rwxrwxrwx (user: rwx, group: rwx, other: rwx)
600 = -rw------- (user: rw-, group: ---, other: ---)

Special permission bits (4-digit format):

  • First digit represents special bits: 4000: setuid bit (s in user execute position) 2000: setgid bit (s in group execute position) 1000: sticky bit (t in other execute position)

Examples with special bits:

4755 = rwsr-xr-x (setuid + rwxr-xr-x)
2755 = rwxr-sr-x (setgid + rwxr-xr-x)
1777 = rwxrwxrwt (sticky + rwxrwxrwx)

Conversion from os.FileMode:

mode.Perm() returns the permission bits as os.FileMode
Use Octal(mode.Perm()) to convert to octal representation

Standards:

  • Defined by POSIX.1 standard
  • Supported by chmod, stat, and umask commands
  • Compatible with all Unix-like systems (Linux, macOS, BSD)

func (Octal) Contains

func (o Octal) Contains(oth Octal) bool

func (Octal) Group

func (o Octal) Group() Permission

func (Octal) Other

func (o Octal) Other() Permission

func (Octal) Perm

func (o Octal) Perm() Octal

Perm returns the Unix permission bits in m (m & [ModePerm]).

func (Octal) SymbolicNotation

func (o Octal) SymbolicNotation() string

SymbolicNotation returns a POSIX compliant symbolic notation for a given file mode.

func (Octal) Type

func (o Octal) Type() Octal

Type returns type bits in m (m & [ModeType]).

func (Octal) User

func (o Octal) User() Permission

type Permission

type Permission struct {
	Class   string `enum:"user,group,other"`
	Read    bool
	Write   bool
	Execute bool
}

func (Permission) Contains

func (p Permission) Contains(o Permission) bool

func (Permission) IsZero

func (p Permission) IsZero() bool

func (Permission) Octal

func (p Permission) Octal() Octal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL