fileperm

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: MIT Imports: 4 Imported by: 4

README

fileperm - file permission tests for Go

Go Reference Go Report Card codecov REUSE status buy ma a coffee

The problem

Go does not offer a trivial way to get the permissions of a specific file in the context of the current user. While we get tools like os.Stat to get general file permissions, this does not help to identify how our current user is affected by these permissions.

Other languages like the Shell or Perl provide a simple test mechanism, to allow the program to validate if the user, that the program is executed as does have permissions on the specific file.

Sh:

$ chmod 400 test.txt && test -r test.txt && echo "We can read the file" || echo "We cannot read the file"
We can read the file

$ chmod 100 test.txt && test -r test.txt && echo "We can read the file" || echo "We cannot read the file"
We cannot read the file

Perl:

#!/usr/bin/env perl

if(-r "./test.txt") {
    print "We can"
}
else {
    print "We cannot"
}
print " read the file\n"';

go-fileperm aims to provide a similar mechanism in Go. The module offers methods to check if a provided file is accessible to the user (readable, writable, executable and combinations of those).

Usage

First, we create a new UserPerm instance by calling the New method together with the file in question as an argument:

package main

import (
	"fmt"
	"os"

	"github.com/wneessen/go-fileperm"
)

func main() {
	up, err := fileperm.New("/var/tmp/foo.txt")
	if err != nil {
		fmt.Print("failed to create new filepe:", err)
		os.Exit(1)
	}
}

Once the UserPerm instance is ready in your hands, we can use different methods to check the permissions of the file for the context of the user that our program or process is running in:

package main

import (
	"fmt"
	"os"

	"github.com/wneessen/go-fileperm"
)

func main() {
	up, err := fileperm.New("/var/tmp/foo.txt")
	if err != nil {
		fmt.Print("failed to create new filepe:", err)
		os.Exit(1)
	}
	fmt.Printf("User can write to file: %t", up.UserWritable())
}

Performance

go-fileperm is quite fast and works allocation free. The single checks like UserReadable, UserWritable and UserExecutable works in the 140-150ns range, while the combined ones take a little longer (180-210ns).

goos: darwin
goarch: arm64
pkg: github.com/wneessen/go-fileperm
BenchmarkPermUser_UserReadable
BenchmarkPermUser_UserReadable-8                 7364846               143.6 ns/op             0 B/op          0 allocs/op
BenchmarkPermUser_UserWritable
BenchmarkPermUser_UserWritable-8                 7803267               154.9 ns/op             0 B/op          0 allocs/op
BenchmarkPermUser_UserExecutable
BenchmarkPermUser_UserExecutable-8               7922624               149.2 ns/op             0 B/op          0 allocs/op
BenchmarkPermUser_UserWriteReadable
BenchmarkPermUser_UserWriteReadable-8            6494815               186.1 ns/op             0 B/op          0 allocs/op
BenchmarkPermUser_UserWriteExecutable
BenchmarkPermUser_UserWriteExecutable-8          6590229               181.0 ns/op             0 B/op          0 allocs/op
BenchmarkPermUser_UserReadExecutable
BenchmarkPermUser_UserReadExecutable-8           6190532               184.7 ns/op             0 B/op          0 allocs/op
BenchmarkPermUser_UserWriteReadExecutable
BenchmarkPermUser_UserWriteReadExecutable-8      5728713               208.8 ns/op             0 B/op          0 allocs/op
PASS

Documentation

Index

Constants

View Source
const (
	OsRead       = 0o4
	OsWrite      = 0o2
	OsEx         = 0o1
	OsUserShift  = 6
	OsGroupShift = 3
	OsOthShift   = 0

	OsUserR = OsRead << OsUserShift
	OsUserW = OsWrite << OsUserShift
	OsUserX = OsEx << OsUserShift

	OsGroupR = OsRead << OsGroupShift
	OsGroupW = OsWrite << OsGroupShift
	OsGroupX = OsEx << OsGroupShift

	OsOthR = OsRead << OsOthShift
	OsOthW = OsWrite << OsOthShift
	OsOthX = OsEx << OsOthShift
)

List of different OS permission bits

Variables

This section is empty.

Functions

This section is empty.

Types

type PermUser added in v0.2.0

type PermUser struct {
	Path        string
	Stat        os.FileInfo
	UID         uint32
	GID         uint32
	CurUserUID  int64
	CurUserGIDs []int64
}

PermUser implements the main struct of the fileperm packages. All methods are based on it

func New

func New(f string) (PermUser, error)

New returns a new PermUser struct. NewFileUserPerm expects a file path string as input and will return an error if the initial operations failed

func (*PermUser) UserExecutable added in v0.2.0

func (p *PermUser) UserExecutable() bool

UserExecutable returns true if the filepath is executable by the current user

func (*PermUser) UserReadExecutable added in v0.2.0

func (p *PermUser) UserReadExecutable() bool

UserReadExecutable returns true if the filepath is read- and executable by the current user

func (*PermUser) UserReadable added in v0.2.0

func (p *PermUser) UserReadable() bool

UserReadable returns true if the filepath is readable by the current user

func (*PermUser) UserWritable added in v0.2.0

func (p *PermUser) UserWritable() bool

UserWritable returns true if the filepath is writable by the current user

func (*PermUser) UserWriteExecutable added in v0.2.0

func (p *PermUser) UserWriteExecutable() bool

UserWriteExecutable returns true if the filepath is write- and executable by the current user

func (*PermUser) UserWriteReadExecutable added in v0.2.0

func (p *PermUser) UserWriteReadExecutable() bool

UserWriteReadExecutable returns true if the filepath is write- and read- and executable by the current user

func (*PermUser) UserWriteReadable added in v0.2.0

func (p *PermUser) UserWriteReadable() bool

UserWriteReadable returns true if the filepath is write- and readable by the current user

Jump to

Keyboard shortcuts

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