fsutil

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: Apache-2.0 Imports: 21 Imported by: 107

Documentation

Index

Constants

Variables

View Source
var (
	ErrorChecksumMismatch = errors.New("checksum mismatch")
)

Functions

func CompareFile

func CompareFile(buffer []byte, filename string) (bool, error)

CompareFile will read and compare the content of a file and buffer and will return true if the contents are the same else false.

func CompareFiles

func CompareFiles(leftFilename, rightFilename string) (bool, error)

CompareFiles will read and compare the content of two files and return true if they are the same else false.

func CopyFile

func CopyFile(destFilename, sourceFilename string, mode os.FileMode) error

CopyFile will create a new file, copies data from the sourceFilename to a tmpfile and then atomically renames the tmpfile to destFilename, ensuring that the file never has incomplete data. If there are any errors, then destFilename is unchanged. CopyFile is not safe to call concurrently for the same file.

func CopyFileExclusive added in v0.4.0

func CopyFileExclusive(destFilename, sourceFilename string,
	mode os.FileMode) error

CopyFileExclusive will exclusively create a new file, copies data from the sourceFilename to a tmpfile and then atomically moves the tmpfile to destFilename, ensuring that the file never has incomplete data. If there are any errors, then destFilename is unchanged.

func CopyFilesTree added in v0.8.0

func CopyFilesTree(destDir, sourceDir string) error

CopyFilesTree will copy a directory tree of regular files. Other inode types are ignored.

func CopyToFile

func CopyToFile(destFilename string, perm os.FileMode, reader io.Reader,
	length uint64) error

CopyToFile will create a new file, write length bytes from reader to a tmpfile and then atomically renames the tmpfile to destFilename, ensuring that the file never has incomplete data. If length is zero all remaining bytes from reader are written. If there are any errors, then destFilename is unchanged. CopyToFile is not safe to call concurrently for the same file.

func CopyToFileExclusive added in v0.4.0

func CopyToFileExclusive(destFilename string, perm os.FileMode,
	reader io.Reader, length uint64) error

CopyToFileExclusive will exclusively create a new file, write length bytes from reader to a tmpfile and then atomically moves the tmpfile to destFilename, ensuring that the file never has incomplete data. If length is zero all remaining bytes from reader are written. If there are any errors, then destFilename is unchanged.

func CopyTree

func CopyTree(destDir, sourceDir string) error

CopyTree will copy a directory tree.

func CopyTreeWithCopyFunc

func CopyTreeWithCopyFunc(destDir, sourceDir string,
	copyFunc func(destFilename, sourceFilename string,
		mode os.FileMode) error) error

CopyTreeWithCopyFunc is similar to CopyTree except it uses a specified copy function for copying regular files.

func Fallocate

func Fallocate(filename string, size uint64) error

Fallocate will allocate blocks for the file named filename, up to size specified in bytes.

func FallocateOrFill added in v0.4.0

func FallocateOrFill(filename string, size uint64,
	logger log.DebugLogger) error

FallocateOrFill will allocate blocks for the file named filename, up to size specified in bytes. If allocation is not supported/available, the file will be filled with zeros (and a debug message will be logged).

func ForceLink(oldname, newname string) error

ForceLink creates newname as a hard link to the oldname file. It first attempts to link using os.Link. If the first attempt fails due to a permission error, it blindly calls MakeMutable and then retries. If the first attempt fails due to newname existing, it blindly removes it and then retries.

func ForceRemove

func ForceRemove(name string) error

ForceRemove removes the named file or directory. It first attempts to remove using os.Remove and that fails, it blindly calls MakeMutable and then retries.

func ForceRemoveAll

func ForceRemoveAll(path string) error

ForceRemoveAll removes path and any children it contains. It first attempts to remove using os.RemoveAll and that fails, it blindly calls MakeMutable and then retries.

func ForceRename

func ForceRename(oldpath, newpath string) error

ForceRename renames (moves) a file. It first attempts to rename using os.Rename and if that fails due to a permission error, it blindly calls MakeMutable and then retries. If it fails because newpath is a directory, it calls ForceRemoveAll(newpath) and tries again.

func FsyncFile

func FsyncFile(file *os.File) error

FsyncFile will call file.Sync if it has not been called recently. This attempts to reduce the performance problems of fsync(2) by potentially sacrificing some file-system consistency.

func GetTreeSize added in v0.3.2

func GetTreeSize(dirname string) (uint64, error)

GetTreeSize will walk a directory tree and count the size of the files.

func LoadLines

func LoadLines(filename string) ([]string, error)

LoadLines will open a file and read lines from it. Comment lines (i.e. lines beginning with '#') are skipped.

func LoopbackDelete

func LoopbackDelete(loopDevice string) error

LoopbackDelete will disassociate (delete) a loopback block device from its backing file.

func LoopbackDeleteAndWaitForPartition added in v0.3.3

func LoopbackDeleteAndWaitForPartition(loopDevice, partition string,
	timeout time.Duration, logger log.DebugLogger) error

LoopbackDeleteAndWaitForPartition will disassociate (delete) a loopback block device from its backing file and wait for specified partition inode to disappear.

func LoopbackSetup

func LoopbackSetup(filename string) (string, error)

LoopbackSetup will associate a loopback block device with a regular file named filename. The name of the loop block device is returned.

func LoopbackSetupAndWaitForPartition added in v0.3.3

func LoopbackSetupAndWaitForPartition(filename, partition string,
	timeout time.Duration, logger log.DebugLogger) (string, error)

LoopbackSetupAndWaitForPartition will associate a loopback block device with a regular file named filename and wait for the specified partition block device node to become available. The timeout is limited to one hour. The name of the loop block device (excluding the partition) is returned.

func MakeMutable

func MakeMutable(pathname ...string) error

MakeMutable attempts to remove the "immutable" and "append-only" ext2 file-system attributes for one or more files. It is equivalent to calling the command-line programme "chattr -ai pathname...".

func ReadDirnames

func ReadDirnames(dirname string, ignoreMissing bool) ([]string, error)

ReadDirnames will open the directory named dirname and will read the entries in that directory. If ignoreMissing is true, no error is returned if the directory does not exist.

func ReadFileTree added in v0.3.3

func ReadFileTree(topdir, prefix string) (map[string][]byte, error)

ReadFileTree will traverse the specified directory tree rooted at topdir and reads the content of each file. The data are returned in a map with the keys being the filename (excluding the topdir and the specified prefix) and the data being the corresponding file contents.

func ReadLines

func ReadLines(reader io.Reader) ([]string, error)

ReadLines will read lines from a reader. Comment lines (i.e. lines beginning with '#') are skipped.

func UpdateFile

func UpdateFile(buffer []byte, filename string) (bool, error)

UpdateFile will read and compare the contents of a file and buffer and will update the file if different. It returns true if the contents were updated.

func WaitFile

func WaitFile(pathname string, timeout time.Duration) (io.ReadCloser, error)

WaitFile waits for the file given by pathname to become available to read and yields a io.ReadCloser when available, or an error if the timeout is exceeded or an error (other than file not existing) is encountered. A negative timeout indicates to wait forever. The io.ReadCloser must be closed after use.

func WaitForBlockAvailable added in v0.8.0

func WaitForBlockAvailable(pathname string,
	timeout time.Duration) (uint, uint, error)

WaitForBlockAvailable will wait for the specified block device node to become available, or return an error on timeout. The timeout is limited to one hour. The number of iterations and the number of successful Open(2) calls is returned. This is needed in enviroments where block devices such as partitions are dynamically created and there is a delay from creation to actual availability.

func WatchFile

func WatchFile(pathname string, logger log.Logger) <-chan io.ReadCloser

WatchFile watches the file given by pathname and yields a new io.ReadCloser when a new inode is found and it is a regular file. The io.ReadCloser must be closed after use. Any errors are logged to the logger if it is not nil.

func WatchFileStop

func WatchFileStop()

WatchFileStop stops all file watching and cleans up resources that would otherwise persist across syscall.Exec.

Types

type ChecksumReader

type ChecksumReader struct {
	// contains filtered or unexported fields
}

ChecksumReader uses the SHA512 hash to checksum data as they are read.

func NewChecksumReader

func NewChecksumReader(reader io.Reader) *ChecksumReader

NewChecksumReader creates a ChecksumReader using reader as the underlying source of data. Checksumming is enabled by default.

func (*ChecksumReader) EnableChecksumming added in v0.6.0

func (r *ChecksumReader) EnableChecksumming(enable bool)

EnableChecksumming enables checksumming if enable is true and clears any prior checksum data, else disables checksumming.

func (*ChecksumReader) GetChecksum

func (r *ChecksumReader) GetChecksum() []byte

GetChecksum returns the checksum of the data read so far. It returns nil if checksumming is disabled.

func (*ChecksumReader) Read

func (r *ChecksumReader) Read(p []byte) (int, error)

Read will read data into the buffer p.

func (*ChecksumReader) ReadByte

func (r *ChecksumReader) ReadByte() (byte, error)

ReadByte will read a single byte.

func (*ChecksumReader) VerifyChecksum

func (r *ChecksumReader) VerifyChecksum() error

VerifyChecksum will read a checksum from the underlying reader and will verify it matches the checksum that GetChecksum would return.

type ChecksumWriter

type ChecksumWriter struct {
	// contains filtered or unexported fields
}

ChecksumWriter uses the SHA512 hash to checksum data as they are written.

func NewChecksumWriter

func NewChecksumWriter(writer io.Writer) *ChecksumWriter

NewChecksumWriter creates a ChecksumWriter using writer as the underlying destination for data. Checksumming is enabled by default.

func (*ChecksumWriter) EnableChecksumming added in v0.6.0

func (w *ChecksumWriter) EnableChecksumming(enable bool)

EnableChecksumming enables checksumming if enable is true and clears any prior checksum data, else disables checksumming.

func (*ChecksumWriter) GetChecksum added in v0.6.0

func (w *ChecksumWriter) GetChecksum() []byte

GetChecksum returns the checksum of the data written so far. It returns nil if checksumming is disabled.

func (*ChecksumWriter) Write

func (w *ChecksumWriter) Write(p []byte) (int, error)

Write will write the data specified by p.

func (*ChecksumWriter) WriteChecksum

func (w *ChecksumWriter) WriteChecksum() error

WriteChecksum will write the checksum for the data written so far and will write it to the underlying writer.

type RenamingWriter

type RenamingWriter struct {
	*os.File
	// contains filtered or unexported fields
}

RenamingWriter is similar to a writable os.File, except that it attempts to ensure data integrity. A temporary file is used for writing, which is renamed during the Close method and an fsync(2) is attempted.

func CreateRenamingWriter

func CreateRenamingWriter(filename string, perm os.FileMode) (
	*RenamingWriter, error)

CreateRenamingWriter will create a temporary file for writing and will rename the temporary file to filename in the Close method if there are no write errors.

func (*RenamingWriter) Abort

func (w *RenamingWriter) Abort()

Abort will prevent file renaming during a subsequent Close method call.

func (*RenamingWriter) Close

func (w *RenamingWriter) Close() error

Close may attempt to fsync(2) the contents of the temporary file (if fsync(2) has not been called recently) and will then close and rename the temporary file if there were no Write errors or a call to the Abort method.

func (*RenamingWriter) Write

func (w *RenamingWriter) Write(p []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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