local

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: AGPL-3.0 Imports: 13 Imported by: 2

Documentation

Overview

Package local provides functions to operate local files and local filesystems.

For better performance, all functions in this package are unsafe for concurrency unless otherwise specified.

Index

Constants

This section is empty.

Variables

View Source
var ErrContainsPathSeparator = errors.AutoNewCustom(
	"prefix/suffix contains path separator",
	errors.PrependFullPkgName,
	0,
)

ErrContainsPathSeparator is an error indicating that the prefix or the suffix contains a path separator.

The client should use errors.Is to test whether an error is ErrContainsPathSeparator.

Functions

func Checksum added in v0.6.0

func Checksum(filename string, upper bool, newHashes ...func() hash.Hash) (
	checksums []string, err error)

Checksum calculates hash checksums of a local file, and returns the result in hexadecimal representation and any error encountered during opening and reading the file.

If the file is a directory, Checksum reports filesys.ErrIsDir and returns nil checksums. (To test whether err is filesys.ErrIsDir, use function errors.Is.)

upper indicates whether to use uppercase in hexadecimal representation.

newHashes are functions that create new hash functions (e.g., crypto/sha256.New, crypto.SHA256.New).

The length of the returned checksums is the same as that of newHashes. The hash result of newHashes[i] is checksums[i], encoded in hexadecimal. In particular, if newHashes[i] is nil or returns nil, checksums[i] is an empty string. If len(newHashes) is 0, checksums is nil.

func Read

func Read(name string, opts *filesys.ReadOptions) (
	r filesys.Reader, err error)

Read opens a file with specified name and options opts for reading.

If the file is a directory, Read reports filesys.ErrIsDir and returns a nil Reader. (To test whether err is filesys.ErrIsDir, use function errors.Is.)

If opts are nil, a zero-value filesys.ReadOptions is used.

The file is closed when the returned reader is closed.

If the file is a symlink, it is evaluated by filepath.EvalSymlinks.

The file is opened by os.Open; the associated file descriptor has mode os.O_RDONLY.

func Tmp

func Tmp(dir, prefix, suffix string, perm fs.FileMode) (f *os.File, err error)

Tmp creates and opens a new temporary file in the directory dir, with specified permission perm (before umask), for reading and writing (the associated file descriptor has mode os.O_RDWR).

The filename is generated by concatenating prefix, a random string, and suffix. Both prefix and suffix must not contain a path separator. If prefix or suffix contains a path separator, it returns a nil f and an error ErrContainsPathSeparator. (To test whether err is ErrContainsPathSeparator, use function errors.Is.)

If dir is empty, it uses the default directory for temporary files (as returned by os.TempDir) instead.

Calling this function simultaneously does not choose the same file.

The client can use f.Name() to find the pathname of the file. The client is responsible for removing the file when no longer needed.

func TmpDir

func TmpDir(dir, prefix, suffix string, perm fs.FileMode) (
	name string, err error)

TmpDir creates a new temporary directory in the directory dir, with specified permission perm (before umask), and returns the pathname of the new directory.

The new directory's name is generated by concatenating prefix, a random string, and suffix. Both prefix and suffix must not contain a path separator. If prefix or suffix contains a path separator, it returns an empty name and an error ErrContainsPathSeparator. (To test whether err is ErrContainsPathSeparator, use function errors.Is.)

If dir is empty, it uses the default directory for temporary files (as returned by os.TempDir) instead.

Calling this function simultaneously does not choose the same directory.

The client is responsible for removing the directory when no longer needed.

func VerifyChecksum

func VerifyChecksum(filename string, hvs ...filesys.HashVerifier) bool

VerifyChecksum verifies a local file by hash checksum.

It returns true if the file can be read and matches all filesys.HashVerifier in hvs (nil and duplicate filesys.HashVerifier are ignored). In particular, it returns true if there is no non-nil filesys.HashVerifier in hvs and the file can be opened for reading. In this case, the file is not read.

Note that VerifyChecksum does not reset the hash state of anyone in hvs. The client should use new filesys.HashVerifier returned by filesys.NewHashVerifier or call the Reset method of filesys.HashVerifier before calling this function if needed.

func WriteAppend added in v0.5.0

func WriteAppend(
	name string,
	perm fs.FileMode,
	mkDirs bool,
	opts *filesys.WriteOptions,
) (w filesys.Writer, err error)

WriteAppend creates (if necessary) and opens a file with specified name and options opts for writing.

If the file exists, new data is appended to the file. If the file does not exist, it is created with specified permission perm (before umask).

mkDirs indicates whether to make necessary directories before opening the file.

opts are handled the same as in function github.com/donyori/gogo/filesys.Write.

The file is closed when the returned writer is closed.

func WriteExcl added in v0.5.0

func WriteExcl(
	name string,
	perm fs.FileMode,
	mkDirs bool,
	opts *filesys.WriteOptions,
) (w filesys.Writer, err error)

WriteExcl creates and opens a file with specified name and options opts for writing.

The file is created with specified permission perm (before umask). If the file exists, it reports an error that satisfies errors.Is(err, fs.ErrExist) is true.

mkDirs indicates whether to make necessary directories before opening the file.

opts are handled the same as in function github.com/donyori/gogo/filesys.Write.

The file is closed when the returned writer is closed.

func WriteTrunc added in v0.5.0

func WriteTrunc(
	name string,
	perm fs.FileMode,
	mkDirs bool,
	opts *filesys.WriteOptions,
) (w filesys.Writer, err error)

WriteTrunc creates (if necessary) and opens a file with specified name and options opts for writing.

If the file exists, it is truncated. If the file does not exist, it is created with specified permission perm (before umask).

mkDirs indicates whether to make necessary directories before opening the file.

opts are handled the same as in function github.com/donyori/gogo/filesys.Write.

The file is closed when the returned writer is closed.

Types

type CaptureToStringFunc added in v0.12.1

type CaptureToStringFunc func() (s string, err error, first bool)

CaptureToStringFunc is a function that stops capturing and returns the captured content as a string.

CaptureToStringFunc is safe for concurrent use. The first call to the CaptureToStringFunc returns the captured content, any error encountered, and true. The subsequent calls to the CaptureToStringFunc return the same content and error, but false.

func CaptureStderrToString added in v0.12.1

func CaptureStderrToString() (f CaptureToStringFunc, err error)

CaptureStderrToString captures the standard error stream to a string.

It returns a CaptureToStringFunc and any error encountered. The CaptureToStringFunc stops capturing and retrieves the captured content.

If the returned CaptureToStringFunc is not nil, the client is responsible for calling it to stop capturing and restore the standard error stream when capturing is no longer needed.

CaptureStderrToString reports an error if the standard error stream has already been captured elsewhere.

func CaptureStdoutToString added in v0.12.1

func CaptureStdoutToString() (f CaptureToStringFunc, err error)

CaptureStdoutToString captures the standard output stream to a string.

It returns a CaptureToStringFunc and any error encountered. The CaptureToStringFunc stops capturing and retrieves the captured content.

If the returned CaptureToStringFunc is not nil, the client is responsible for calling it to stop capturing and restore the standard output stream when capturing is no longer needed.

CaptureStdoutToString reports an error if the standard output stream has already been captured elsewhere.

Jump to

Keyboard shortcuts

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