fsutil

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2021 License: MIT Imports: 6 Imported by: 0

README

FS Utilities

A collection of convenience utilities for working with filesystem I/O.

Documentation

Overview

Package fsutil are convenience utilities for working with filesystem I/O.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotDir  = errors.New("not a directory")
	ErrHomeDir = errors.New("cannot resolve home directory")
)
View Source
var (
	ErrNotFile    = errors.New("not a file")
	ErrFileTooBig = errors.New("file is too large")
)

Functions

func Abs

func Abs(path string) (string, error)

func AppendFile

func AppendFile(path string, data []byte, mode fs.FileMode) error

AppendFile appends data to file at path. If the file does not exist, it is created with permission set to mode. If the file already exists, it mode is ignored. An error occurs if the file's parent directory does not exist.

func Dir

func Dir(path, ext string, maxfile int) ([]string, error)

Dir returns regular files directly in directory path up to maxfile entries. Use Subdir if you want to return sub-directories. If maxfile is <= 0, it returns all the entries. If ext is empty or *, entries are returned as-is. Otherwise, only entries with matching extension will be returned. Returns ErrNotDir if path is not a directory.

func HomeDir

func HomeDir() string

HomeDir returns the current user's home directory. It returns an empty string if the user's home directory cannot be determined.

func IsEmptyDir

func IsEmptyDir(path string) (bool, error)

IsEmptyDir returns true if the directory is empty. Returns ErrNotDir if path is not a directory.

func IsPath

func IsPath(path string, t ...PathType) bool

IsPath returns true if path is any of path type t.

func ReadFile

func ReadFile(path string, maxBytes int64) ([]byte, error)

ReadFile reads all content of file at path into memory. If maxBytes is larger than 0, returns (nil,error) if the file size is larger than maxBytes. Returns (nil,error) if path is not a file.

func RemoveEmptyDir

func RemoveEmptyDir(path string) error

RemoveEmptyDir removes the directory at path if it is empty. Returns ErrNotDir if path is not a directory.

func RemoveFile

func RemoveFile(path string, deleteEmptyDir bool) error

RemoveFile deletes file at path. It returns an error if path is not a file. Set deleteEmptyDir to true will also delete the file's parent directory if that parent directory will be empty after deleting file.

func Subdir

func Subdir(path string, maxfile int) ([]string, error)

Subdir returns sub-directories directly in directory path up to maxfile entries. If maxfile is <= 0, it returns all the entries. Use Dir if you want to return regular files. Returns ErrNotDir if path is not a directory.

func WriteFile

func WriteFile(path string, data []byte, mode fs.FileMode) error

WriteFile overwrites content of file at path with data, and then sets the file permission to mode. The file at path is created automatically if it does not exist, but an error occurs if the file's parent directory does not exist. If a file at path already exists, it is overwritten. Use AppendFile if you wish to append content instead.

Types

type PathType

type PathType uint32

PathType is the type of filesystem object.

const (
	// NotExist means the path does not exist.
	NotExist PathType = iota
	// Directory indicates a directory.
	Directory
	// File indicates a regular file.
	File
	// IrregularPath means the path exists but is an unsupported type.
	IrregularPath PathType = 54288
	// BadPath means the path exists, but more information about the path
	// cannot be determined.
	BadPath PathType = 0655
)

func TestPath

func TestPath(path string) PathType

TestPath returns the path type.

Jump to

Keyboard shortcuts

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