file

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: MIT Imports: 9 Imported by: 7

Documentation

Index

Constants

View Source
const (
	// FileModeNewDirectory is the default directory mode
	FileModeNewDirectory = 0o750

	// FileModeDefault is the default file mode
	FileModeNewFile = 0o640

	// FileModeMinValid is the minimum valid file mode
	FileModeMinValid = 0o000

	// FileModeMaxValid is the maximum valid file mode
	FileModeMaxValid = 0o777
)
View Source
const OSWindows = "windows"

Variables

View Source
var (
	ErrEmptyPath = errors.New("path cannot be empty")
	ErrNullByte  = errors.New("path contains null byte")
	ErrTraversal = errors.New("path traversal attempt detected")
	ErrSymlink   = errors.New("symlink traversal attempt detected")
)

Functions

func Copy

func Copy(srcFile, dstFile string) error

Copy copies a file from src to dest

func CopyDirectory

func CopyDirectory(srcDir, dest string) error

CopyDirectory copies a directory from src to dest

func Exists

func Exists(path string) (bool, error)

Exists returns true if the given path exists

It returns false and an error on any error, e.g. on insufficient permissions

func IsDevice

func IsDevice(path string) bool

IsDevice returns true if the given path is a device

It resolves all symbolic links It returns false on any error, e.g. if the file does not exist or on insufficient permissions

func IsDir

func IsDir(path string) bool

IsDir returns true if the given path is a directory

It returns false on any error, e.g. if the file does not exist or on insufficient permissions

func IsFile

func IsFile(path string) bool

IsFile returns true if the given path is a regular file, symlink, socket, or device

It returns false on any error, e.g. if the file does not exist or on insufficient permissions

func IsNamedPipe

func IsNamedPipe(path string) bool

IsNamedPipe returns true if the given path is a named pipe

It resolves all symbolic links It returns false on any error, e.g. if the file does not exist or on insufficient permissions

func IsRegular

func IsRegular(path string) bool

IsRegular returns true if the given path is a regular file

It resolves all symbolic links It returns false on any error, e.g. if the file does not exist or on insufficient permissions

func IsSocket

func IsSocket(path string) bool

IsSocket returns true if the given path is a socket

It resolves all symbolic links It returns false on any error, e.g. if the file does not exist or on insufficient permissions

func IsSymlink(path string) bool

IsSymlink returns true if the given path is a symlink

It does not resolve any symbolic links It returns false on any error, e.g. if the file does not exist or on insufficient permissions

func SafeCreate

func SafeCreate(root, path string, mode int64) (*os.File, error)

SafeCreate creates or truncates a file with the specified mode. It uses SafeOpenFile with write-only, create and truncate flags.

func SafeOpen

func SafeOpen(root, path string) (*os.File, error)

SafeOpen opens a file for read-only access in a secure manner. It uses SafeOpenFile with read-only flag and default permissions.

func SafeOpenFile

func SafeOpenFile(root, path string, flag int, mode int64) (*os.File, error)

SafeOpenFile opens a file with the specified path, base directory, flags, and mode. It ensures the file operation is secure by validating the mode and path. Returns a file handle and any error encountered.

func SafePath

func SafePath(root, path string) (string, error)

SafePath ensures the given path is safe to use within the specified root directory. It returns the cleaned absolute path and an error if the path is unsafe. The function performs the following checks and operations: 1. Validates that the path is not empty and does not contain null bytes. 2. Cleans and converts both the root and input paths to absolute paths. 3. Resolves any symlinks in the input path, even if the path does not exist. 4. Ensures the resolved path is within the root directory to prevent path traversal attacks. 5. Works on both Windows and Unix-like systems, handling platform-specific path separators and case sensitivity.

Parameters: - root: The root directory within which the path must be contained. - path: The input path to be validated and resolved. It can be either an absolute or relative path.

func SafeReadFile

func SafeReadFile(root, path string) ([]byte, error)

SafeReadFile reads the entire contents of a file securely. It ensures the file path is safe before reading. Returns the file contents and any error encountered.

func ValidMode

func ValidMode(mode int64) (os.FileMode, error)

ValidMode checks if the given mode is a valid file mode

Types

This section is empty.

Jump to

Keyboard shortcuts

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