Documentation
¶
Index ¶
- Constants
- Variables
- func Copy(srcFile, dstFile string) error
- func CopyDirectory(srcDir, dest string) error
- func Exists(path string) (bool, error)
- func IsDevice(path string) bool
- func IsDir(path string) bool
- func IsFile(path string) bool
- func IsNamedPipe(path string) bool
- func IsRegular(path string) bool
- func IsSocket(path string) bool
- func IsSymlink(path string) bool
- func SafeCreate(root, path string, mode int64) (*os.File, error)
- func SafeOpen(root, path string) (*os.File, error)
- func SafeOpenFile(root, path string, flag int, mode int64) (*os.File, error)
- func SafePath(root, path string) (string, error)
- func SafeReadFile(root, path string) ([]byte, error)
- func ValidMode(mode int64) (os.FileMode, error)
Constants ¶
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 )
const OSWindows = "windows"
Variables ¶
Functions ¶
func CopyDirectory ¶
CopyDirectory copies a directory from src to dest
func Exists ¶
Exists returns true if the given path exists
It returns false and an error on any error, e.g. on insufficient permissions
func IsDevice ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
SafeCreate creates or truncates a file with the specified mode. It uses SafeOpenFile with write-only, create and truncate flags.
func SafeOpen ¶
SafeOpen opens a file for read-only access in a secure manner. It uses SafeOpenFile with read-only flag and default permissions.
func SafeOpenFile ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.