2016-03-28 13:26:04 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2024-06-29 15:15:29 +00:00
|
|
|
"fmt"
|
2016-03-28 13:26:04 +00:00
|
|
|
"os"
|
2017-11-20 20:32:25 +00:00
|
|
|
"time"
|
2016-03-28 13:26:04 +00:00
|
|
|
)
|
|
|
|
|
2016-08-21 08:41:02 +00:00
|
|
|
// Mkdir creates a new directory with the specified name and permission bits.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func Mkdir(name string, perm os.FileMode) error {
|
|
|
|
return os.Mkdir(fixpath(name), perm)
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:54:53 +00:00
|
|
|
// MkdirAll creates a directory named path, along with any necessary parents,
|
|
|
|
// and returns nil, or else returns an error. The permission bits perm are used
|
|
|
|
// for all directories that MkdirAll creates. If path is already a directory,
|
|
|
|
// MkdirAll does nothing and returns nil.
|
|
|
|
func MkdirAll(path string, perm os.FileMode) error {
|
|
|
|
return os.MkdirAll(fixpath(path), perm)
|
|
|
|
}
|
|
|
|
|
2016-08-21 08:41:02 +00:00
|
|
|
// Readlink returns the destination of the named symbolic link.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func Readlink(name string) (string, error) {
|
|
|
|
return os.Readlink(fixpath(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the named file or directory.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func Remove(name string) error {
|
|
|
|
return os.Remove(fixpath(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll removes path and any children it contains.
|
|
|
|
// It removes everything it can but returns the first error
|
|
|
|
// it encounters. If the path does not exist, RemoveAll
|
|
|
|
// returns nil (no error).
|
|
|
|
func RemoveAll(path string) error {
|
|
|
|
return os.RemoveAll(fixpath(path))
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:22:51 +00:00
|
|
|
// Rename renames (moves) oldpath to newpath.
|
|
|
|
// If newpath already exists, Rename replaces it.
|
|
|
|
// OS-specific restrictions may apply when oldpath and newpath are in different directories.
|
|
|
|
// If there is an error, it will be of type *LinkError.
|
|
|
|
func Rename(oldpath, newpath string) error {
|
|
|
|
return os.Rename(fixpath(oldpath), fixpath(newpath))
|
|
|
|
}
|
|
|
|
|
2016-08-21 08:41:02 +00:00
|
|
|
// Symlink creates newname as a symbolic link to oldname.
|
|
|
|
// If there is an error, it will be of type *LinkError.
|
|
|
|
func Symlink(oldname, newname string) error {
|
2020-08-05 18:16:37 +00:00
|
|
|
return os.Symlink(oldname, fixpath(newname))
|
2016-08-21 08:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 23:14:20 +00:00
|
|
|
// Link creates newname as a hard link to oldname.
|
|
|
|
// If there is an error, it will be of type *LinkError.
|
|
|
|
func Link(oldname, newname string) error {
|
|
|
|
return os.Link(fixpath(oldname), fixpath(newname))
|
|
|
|
}
|
|
|
|
|
2016-08-21 08:41:02 +00:00
|
|
|
// Stat returns a FileInfo structure describing the named file.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func Stat(name string) (os.FileInfo, error) {
|
|
|
|
return os.Stat(fixpath(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat returns the FileInfo structure describing the named file.
|
|
|
|
// If the file is a symbolic link, the returned FileInfo
|
|
|
|
// describes the symbolic link. Lstat makes no attempt to follow the link.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func Lstat(name string) (os.FileInfo, error) {
|
|
|
|
return os.Lstat(fixpath(name))
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:21:22 +00:00
|
|
|
// Open opens a file for reading.
|
|
|
|
func Open(name string) (File, error) {
|
|
|
|
return os.Open(fixpath(name))
|
|
|
|
}
|
|
|
|
|
2016-08-21 08:41:02 +00:00
|
|
|
// OpenFile is the generalized open call; most users will use Open
|
|
|
|
// or Create instead. It opens the named file with specified flag
|
|
|
|
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
|
|
|
|
// methods on the returned File can be used for I/O.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
|
|
|
|
return os.OpenFile(fixpath(name), flag, perm)
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:32:25 +00:00
|
|
|
// Chtimes changes the access and modification times of the named file,
|
|
|
|
// similar to the Unix utime() or utimes() functions.
|
|
|
|
//
|
|
|
|
// The underlying filesystem may truncate or round the values to a less
|
|
|
|
// precise time unit. If there is an error, it will be of type *PathError.
|
|
|
|
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
|
|
return os.Chtimes(fixpath(name), atime, mtime)
|
|
|
|
}
|
2024-02-23 00:31:20 +00:00
|
|
|
|
|
|
|
// IsAccessDenied checks if the error is due to permission error.
|
|
|
|
func IsAccessDenied(err error) bool {
|
|
|
|
return os.IsPermission(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetPermissions resets the permissions of the file at the specified path
|
|
|
|
func ResetPermissions(path string) error {
|
|
|
|
// Set the default file permissions
|
2024-07-29 18:10:58 +00:00
|
|
|
if err := os.Chmod(fixpath(path), 0600); err != nil {
|
2024-02-23 00:31:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-29 15:15:29 +00:00
|
|
|
|
|
|
|
// Readdirnames returns a list of file in a directory. Flags are passed to fs.OpenFile. O_RDONLY is implied.
|
|
|
|
func Readdirnames(filesystem FS, dir string, flags int) ([]string, error) {
|
|
|
|
f, err := filesystem.OpenFile(dir, O_RDONLY|flags, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("openfile for readdirnames failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := f.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close()
|
|
|
|
return nil, fmt.Errorf("readdirnames %v failed: %w", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|