Documentation ¶
Overview ¶
Package fsutil are convenience utilities for working with filesystem I/O.
Index ¶
- Variables
- func Abs(path string) (string, error)
- func AppendFile(path string, data []byte, mode fs.FileMode) error
- func Dir(path, ext string, maxfile int) ([]string, error)
- func HomeDir() string
- func IsEmptyDir(path string) (bool, error)
- func IsPath(path string, t ...PathType) bool
- func ReadFile(path string, maxBytes int64) ([]byte, error)
- func RemoveEmptyDir(path string) error
- func RemoveFile(path string, deleteEmptyDir bool) error
- func Subdir(path string, maxfile int) ([]string, error)
- func WriteFile(path string, data []byte, mode fs.FileMode) error
- type PathType
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotDir = errors.New("not a directory") ErrHomeDir = errors.New("cannot resolve home directory") )
var ( ErrNotFile = errors.New("not a file") ErrFileTooBig = errors.New("file is too large") )
Functions ¶
func AppendFile ¶
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 ¶
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 ¶
IsEmptyDir returns true if the directory is empty. Returns ErrNotDir if path is not a directory.
func ReadFile ¶
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 ¶
RemoveEmptyDir removes the directory at path if it is empty. Returns ErrNotDir if path is not a directory.
func RemoveFile ¶
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 ¶
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 ¶
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 )