utils

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package utils provides commonly used utility fuctions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsFile

func AbsFile(path string) string

AbsFile returns an absolute representation of path. Works like filepath.Abs but do not return a second error return value

func AbsFileFromRoot

func AbsFileFromRoot(path string, root string) string

AbsFileFromRoot returns an absolute representation of path, using root as the cwd. Setting root to the empty string, uses the CWD

func EnsurePermissions

func EnsurePermissions(file string, maxPermissions int)

EnsurePermissions checks that the file permissions are less or equal than maxPermissions, aborting otherwise

func EnsureSafePermissions

func EnsureSafePermissions(file string)

EnsureSafePermissions checks that file permissions are safe (less or equal than 0700), aborting otherwise

func Exit

func Exit(code int, reason string, reasonArgs ...interface{})

Exit allows exiting the process with a code while providing a reason

func FileExists

func FileExists(path string) bool

FileExists returns true if path exists or false otherwise

func IsFile

func IsFile(path string) bool

IsFile returns true if path exists and is a file (or a link to a file) and false otherwise

func IsProcessRunning

func IsProcessRunning(pid int) bool

IsProcessRunning returns true if a process is running in the specified pid or false otherwhise

func IsReadable

func IsReadable(path string) bool

IsReadable returns true if the path exists and is redable and false otherwise

func IsReadableFile

func IsReadableFile(path string) bool

IsReadableFile returns true if path is a file and redable and false otherwise

func IsWritable

func IsWritable(path string) bool

IsWritable returns true if the path exists and is writable or if it can be created, and false otherwise

func IsWritableFile

func IsWritableFile(path string) bool

IsWritableFile returns true if path is a file and writable or does not exists but could be created, and false otherwise

func NewSecureFileAccessor

func NewSecureFileAccessor() interface {
	FileAccessor
}

NewSecureFileAccessor returns a new SecureFileAccessor

func OpenFileSecure

func OpenFileSecure(name string, flag int, perm os.FileMode) (*os.File, error)

OpenFileSecure allows opening a file ensure secure permissions

func ReadPid

func ReadPid(file string) (int, error)

ReadPid returns a parsed numeric pid from the given file. If the file does not exists, its contents do not contain a valid PID in the first line or any other error occurs, it will return -1 as the pid and the detailed error

func ReadSecure

func ReadSecure(file string) (string, error)

ReadSecure returns a string containing the contents of file The function will cause the process to exit if any user other than the owner has write permissions over it

func RoundDuration

func RoundDuration(duration time.Duration) time.Duration

RoundDuration returns a new duration truncated to seconds

func ValidatePidFilePath

func ValidatePidFilePath(path string) (err error)

ValidatePidFilePath returns an error unless path is valid to write a PID file. Specifically, it will ensure it is writable and, if it already exists, point to a file.

func WaitUntil

func WaitUntil(cb func() bool, args ...time.Duration) (success bool)

WaitUntil Waits until cb function returns true or the timeout is reached

func WritePid

func WritePid(file string, pid int) error

WritePid saves the provided pid into file with safe permissions (644) Returns an error in case of failure

func WriteSecure

func WriteSecure(file, data string) error

WriteSecure writes data into file ensuring the resulting permissions are 0644 (only write permissions for the owner)

Types

type FileAccessor

type FileAccessor interface {
	ReadFile(file string) (string, error)
	OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
	WriteFile(file, data string) error
}

FileAccessor is the interface that defines the operations that file openers must support

type SecureFileAccessor

type SecureFileAccessor struct {
	MaxPermissions          os.FileMode
	DefaultWritePermissions os.FileMode
}

SecureFileAccessor allows defining the acceptable permission used when reading and writing files

func (*SecureFileAccessor) EnsurePermissions

func (fa *SecureFileAccessor) EnsurePermissions(file string)

EnsurePermissions checks file and aborts the executing if it does not comply with the acceptable configured permissions

func (*SecureFileAccessor) OpenFile

func (fa *SecureFileAccessor) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)

OpenFile allows opening a file and ensuring its permissions are within the acceptable limits

func (*SecureFileAccessor) ReadFile

func (fa *SecureFileAccessor) ReadFile(file string) (string, error)

ReadFile allows reading a file if it complies with the required permissions

func (*SecureFileAccessor) WriteFile

func (fa *SecureFileAccessor) WriteFile(file, data string) error

WriteFile allows writing a file and enforcing its permissions to the configured ones

Jump to

Keyboard shortcuts

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