filesystem

package
v0.0.0-...-f557d2e Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: Apache-2.0 Imports: 15 Imported by: 34

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRootSentinel = errors.New("hit root sentinel")

ErrRootSentinel is wrapped and then returned from GetCommonAncestor when it encounters one of the provided rootSentinels.

Functions

func AbsPath

func AbsPath(base *string) error

AbsPath is a convenience wrapper around filepath.Abs that accepts a string pointer, base, and updates it on successful resolution.

func Copy

func Copy(outfile, infile string, mode os.FileMode) (err error)

Copy makes a copy of the file.

func CreateDirectories

func CreateDirectories(baseDirectory string, files []string) error

CreateDirectories creates the directory structure needed by the given list of files.

func GetCommonAncestor

func GetCommonAncestor(paths []string, rootSentinels []string) (string, error)

GetCommonAncestor returns the smallest path which is the ancestor of all provided paths (which must actually exist on the filesystem).

All paths here are converted to absolute paths before calculating the ancestor, and the returned path will also be an absolute path. Note that this doesn't do anything special with symlinks; the caller can resolve them if necessary.

This function works correctly on case-insensitive filesystems, or on filesystems with a mix of case-sensitive and case-insensitive paths, but you can get some wild filesystems out there, so this will probably break on exotic setups. Note that the case of the path you get back will be derived from the shortest input path (after making them absolute); this function makes no attempt to "canonicalize" the case of any paths (but may do so accidentally, depending on the operating system). This function does not attempt to resolve symlinks.

If a given path points to a file, the file's containing directory will be considered instead (i.e. GetCommonAncestor("a/b.ext") will return the absolute path of "a").

`rootSentinels` is a list of sub paths to look for to stop walking up the directory hierarchy. A typical value would be something like []string{".git"}. If one of these is found, this function returns "" with a wrapped ErrRootSentinel. Use errors.Is to identify this.

Returns an error if any of the provided paths does not exist. If successful, will return a path ending with PathSeparator.

If no paths are prodvided, returns ("", nil)

func GetFilenameNoExt

func GetFilenameNoExt(path string) string

GetFilenameNoExt returns the base file name without the extension.

func GetFreeSpace

func GetFreeSpace(path string) (uint64, error)

GetFreeSpace returns the number of free bytes.

On POSIX platforms, this returns the free space as visible by the current user. The returned value is what is usable, and it can be lower than the actual free disk space. For example on linux there's by default a 5% that is reserved to the root user.

func HardlinkRecursively

func HardlinkRecursively(src, dst string) error

HardlinkRecursively efficiently copies a file or directory from src to dst.

`src` may be a file, directory, or a symlink to a file or directory. All symlinks are replaced with their targets, so the resulting directory structure in `dst` will never have any symlinks.

To increase speed, HardlinkRecursively hardlinks individual files into the (newly created) directory structure if possible.

func IsDir

func IsDir(path string) (bool, error)

IsDir to see whether |path| is a directory. This is just a thin wrapper around os.Stat(...). If this returns True, |path| is a directory. If this returns False with nil err, |path| is not a directory. If this returns non-nil error, failed to determine |path| is a drectory.

func IsEmptyDir

func IsEmptyDir(dir string) (bool, error)

IsEmptyDir returns whether |dir| is empty or not. This returns error if |dir| is not directory, or find some error during checking.

func IsNotExist

func IsNotExist(err error) bool

IsNotExist calls os.IsNotExist on the unwrapped err.

func MakeDirs

func MakeDirs(path string) error

MakeDirs is a convenience wrapper around os.MkdirAll that applies a 0755 mask to all created directories.

func MakePathUserWritable

func MakePathUserWritable(path string, fi os.FileInfo) error

MakePathUserWritable updates the filesystem metadata on a single file or directory to make it user-writable.

fi is optional. If nil, os.Stat will be called on path. Otherwise, fi will be regarded as the results of calling os.Stat on path. This is provided as an optimization, since some filesystem operations automatically yield a FileInfo.

func MakeReadOnly

func MakeReadOnly(path string, filter func(string) bool) error

MakeReadOnly recursively iterates through all of the files and directories starting at path and marks them read-only.

func MakeTreeFilesReadOnly

func MakeTreeFilesReadOnly(ctx context.Context, root string) error

MakeTreeFilesReadOnly makes all the files in the directories read only but not the directories themselves. This means files can be created or deleted.

func MakeTreeReadOnly

func MakeTreeReadOnly(ctx context.Context, root string) error

MakeTreeReadOnly makes all the files in the directories read only. Also makes the directories read only, only if it makes sense on the platform. This means no file can be created or deleted.

func MakeTreeWritable

func MakeTreeWritable(ctx context.Context, root string) error

MakeTreeWritable makes all the files in the directories writeable. Also makes the directories writeable, only if it makes sense on the platform.

func ReadableCopy

func ReadableCopy(outfile, infile string) error

ReadableCopy makes a copy of the file that is readable by everyone.

func RemoveAll

func RemoveAll(path string) error

RemoveAll is a fork of os.RemoveAll that attempts to deal with read only files and directories by modifying permissions as necessary.

If the specified path does not exist, RemoveAll will return nil.

Note that RemoveAll will not modify permissions on parent directory of the provided path, even if it is read only and preventing deletion of the path on POSIX system.

Copied from https://go.googlesource.com/go/+/b86e76681366447798c94abb959bb60875bcc856/src/os/path.go#63

func RenamingRemoveAll

func RenamingRemoveAll(path, renameToDir string) (renamedToPath string, err error)

RenamingRemoveAll opportunistically renames a path first, and then removes it.

The advantage over RemoveAll is, if renaming succeeds, lower chance of interference from other writers/readers of the filesystem. If renaming fails, removes the original path via RemoveAll.

If renameToDir is given, a new temp directory will be created in it. Else, a new temp directory is placed within the path's parent dir. After this, a file/dir represented by the path is moved into the temp dir.

In case of any failures during the temp dir creation or the move, default to RemoveAll of path in place.

Returned renamedToPath is the renamed path if renaming succeeded and "" otherwise. Returned error is the one from RemoveAll call.

func ResolveSymlink(path string) (string, os.FileInfo, error)

ResolveSymlink recursively resolves simlink and returns absolute path that is not symlink with stat.

func SetReadOnly

func SetReadOnly(path string, readOnly bool) error

SetReadOnly sets or resets the write bit on a file or directory. Zaps out access to 'group' and 'others'.

func Touch

func Touch(path string, when time.Time, mode os.FileMode) error

Touch creates a new, empty file at the specified path.

If when is zero-value, time.Now will be used.

Types

type TempDir

type TempDir struct {
	// Dir is the base diectory. If empty, the default will be used (see
	// ioutil.TempDir)
	Dir string

	// Prefix is the prefix to apply to the temporary directory. If empty, a
	// default will be used (see ioutil.TempDir).
	Prefix string

	// OnCleanupErr, if not nil, will be called if TempDir cleanup fails.
	//
	// If nil, cleanup errors will be silently discarded.
	CleanupErrFunc func(tdir string, err error)
}

TempDir configures a temporary directory.

func (*TempDir) With

func (td *TempDir) With(fn func(string) error) error

With creates a temporary directory and passes it to fn. After fn exits, the directory and all of its contents is deleted.

Any error that happens during setup or execution of the callback is returned. If an error occurs during cleanup, the optional CleanupErrFunc will be called.

Jump to

Keyboard shortcuts

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