permbits

package module
v0.0.0-...-39d7c58 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2019 License: MIT Imports: 2 Imported by: 58

README

GoDoc CircleCI Maintainability Test Coverage patreon flattr

PermBits

Easy file permissions for golang. Easily get and set file permission bits.

This package makes it a breeze to check and modify file permission bits in Linux, Mac, and other Unix systems.

Example

permissions, err := permbits.Stat("/path/to/my/file")
if err != nil {
	return err
}

// Check to make sure the group can write to the file
// If they can't write, update the permissions so they can
if !permissions.GroupWrite() {
	permissions.SetGroupWrite(true)
	err := permbits.Chmod("/path/to/my/file", permissions)
	if err != nil {
		return errors.New("error setting permission on file", err)
	}
}

// Also works well with os.File
fileInfo, err := file.Stat()
if err != nil {
	return err
}
fileMode := fileInfo.Mode()
permissions := permbits.FileMode(fileMode)

// Disable write access to the file for everyone but the user
permissions.SetGroupWrite(false)
permissions.SetOtherWrite(false)
permbits.UpdateFileMode(&fileMode, permissions)

// You can also work with octets directly
if permissions != 0777 {
	return fmt.Errorf("Permissions on file are incorrect. Should be 777, got %o", permissions)
}

Contributors

  1. Patrick Hayes (linkedin) (github) - Available for hire.

Documentation

Overview

Easy file permissions for golang. Easily get and set file permission bits.

This package makes it a breeze to check and modify file permission bits in Linux, Mac, and other Unix systems.

permissions, err := permbits.Stat("/path/to/my/file")
if err != nil {
	return err
}

// Check to make sure the group can write to the file
// If they can't write, update the permissions so they can
if !permissions.GroupWrite() {
permissions.SetGroupWrite(true)
	err := permbits.Chmod("/path/to/my/file", permissions)
	if err != nil {
		return errors.New("error setting permission on file", err)
	}
}
// Also works well with os.File
fileInfo, err := file.Stat()
if err != nil {
	return err
}
fileMode := fileInfo.Mode()
permissions := permbits.FileMode(fileMode)

// Disable write access to the file for everyone but the user
permissions.SetGroupWrite(false)
permissions.SetOtherWrite(false)
permbits.UpdateFileMode(&fileMode, permissions)

// You can also work with octets directly
if permissions != 0777 {
	return fmt.Errorf("Permissions on file are incorrect. Should be 777, got %o", permissions)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chmod

func Chmod(filepath string, b PermissionBits) error

Given a filepath, set it's permission bits directly

func UpdateFileMode

func UpdateFileMode(fm *os.FileMode, b PermissionBits)

Given an os.FileMode object, update it's permissions

Types

type PermissionBits

type PermissionBits uint32
const (
	Setuid PermissionBits = 1 << (12 - 1 - iota)
	Setgid
	Sticky
	UserRead
	UserWrite
	UserExecute
	GroupRead
	GroupWrite
	GroupExecute
	OtherRead
	OtherWrite
	OtherExecute
)

func FileMode

func FileMode(fm os.FileMode) PermissionBits

Given a FileMode from the os package, get it's permission bits

func Stat

func Stat(filepath string) (PermissionBits, error)

Given a filepath, get it's permission bits

func (PermissionBits) GroupExecute

func (b PermissionBits) GroupExecute() bool

func (PermissionBits) GroupRead

func (b PermissionBits) GroupRead() bool

func (PermissionBits) GroupWrite

func (b PermissionBits) GroupWrite() bool

func (PermissionBits) OtherExecute

func (b PermissionBits) OtherExecute() bool

func (PermissionBits) OtherRead

func (b PermissionBits) OtherRead() bool

func (PermissionBits) OtherWrite

func (b PermissionBits) OtherWrite() bool

func (*PermissionBits) SetGroupExecute

func (b *PermissionBits) SetGroupExecute(set bool)

func (*PermissionBits) SetGroupRead

func (b *PermissionBits) SetGroupRead(set bool)

func (*PermissionBits) SetGroupWrite

func (b *PermissionBits) SetGroupWrite(set bool)

func (*PermissionBits) SetOtherExecute

func (b *PermissionBits) SetOtherExecute(set bool)

func (*PermissionBits) SetOtherRead

func (b *PermissionBits) SetOtherRead(set bool)

func (*PermissionBits) SetOtherWrite

func (b *PermissionBits) SetOtherWrite(set bool)

func (*PermissionBits) SetSetgid

func (b *PermissionBits) SetSetgid(set bool)

func (*PermissionBits) SetSetuid

func (b *PermissionBits) SetSetuid(set bool)

func (*PermissionBits) SetSticky

func (b *PermissionBits) SetSticky(set bool)

func (*PermissionBits) SetUserExecute

func (b *PermissionBits) SetUserExecute(set bool)

func (*PermissionBits) SetUserRead

func (b *PermissionBits) SetUserRead(set bool)

func (*PermissionBits) SetUserWrite

func (b *PermissionBits) SetUserWrite(set bool)

func (PermissionBits) Setgid

func (b PermissionBits) Setgid() bool

func (PermissionBits) Setuid

func (b PermissionBits) Setuid() bool

func (PermissionBits) Sticky

func (b PermissionBits) Sticky() bool

func (PermissionBits) String

func (b PermissionBits) String() string

func (PermissionBits) UserExecute

func (b PermissionBits) UserExecute() bool

func (PermissionBits) UserRead

func (b PermissionBits) UserRead() bool

func (PermissionBits) UserWrite

func (b PermissionBits) UserWrite() bool

Jump to

Keyboard shortcuts

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