fileutil

package
v1.20210731.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2021 License: MIT Imports: 12 Imported by: 2

Documentation

Overview

Package fileutil provides helpers for reading and writing files.

Index

Constants

View Source
const (
	// Kilobyte represents the bytes in a kilobyte.
	Kilobyte int64 = 1 << 10
	// Megabyte represents the bytes in a megabyte.
	Megabyte int64 = Kilobyte << 10
	// Gigabyte represents the bytes in a gigabyte.
	Gigabyte int64 = Megabyte << 10
	//Terrabyte represents the bytes in a terrabyte.
	Terrabyte int64 = Gigabyte << 10
)
View Source
const (
	ErrWatchStopped          ex.Class = "watch file should stop"
	DefaultWatchPollInterval          = 500 * time.Millisecond
)

Watch constants

Variables

This section is empty.

Functions

func ETag

func ETag(contents []byte) (string, error)

ETag creates an etag for a given blob.

func FormatFileSize

func FormatFileSize(sizeBytes int64) string

FormatFileSize returns a string representation of a file size in bytes.

func ParseFileSize

func ParseFileSize(fileSizeValue string) (int64, error)

ParseFileSize parses a file size

func ReadChunks

func ReadChunks(filePath string, chunkSize int, handler func([]byte) error) error

ReadChunks reads a file in `chunkSize` pieces, dispatched to the handler.

func ReadLines

func ReadLines(filePath string, handler func(string) error) error

ReadLines reads a file and calls the handler for each line.

func RemoveMany

func RemoveMany(filePaths ...string) error

RemoveMany removes an array of files.

func Watch

func Watch(path string, action WatchAction) error

Watch watches a file for changes and calls the action if there are changes. It does this by polling the file for ModTime changes every 500ms. It is not designed for watching a large number of files. This function blocks, and you should probably call this with its own goroutine. The action takes a direct file handle, and is _NOT_ responsible for closing the file; the watcher will do that when the action has completed.

Types

type Temp

type Temp struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Temp is a file that deletes itself when closed. It does not hold a file handle open, so no guarantees are made around the file persisting for the lifetime of the object.

func NewTemp

func NewTemp(contents []byte) (*Temp, error)

NewTemp creates a new temp file with given contents.

func (*Temp) Close

func (tf *Temp) Close() error

Close closes the file reference and deletes the file.

func (*Temp) Name

func (tf *Temp) Name() string

Name returns the fully qualified file path.

func (*Temp) Read

func (tf *Temp) Read(buffer []byte) (int, error)

Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.

func (*Temp) ReadAt

func (tf *Temp) ReadAt(buffer []byte, off int64) (int, error)

ReadAt reads len(b) bytes from the File starting at byte offset off. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(b). At end of file, that error is io.EOF.

func (*Temp) Stat

func (tf *Temp) Stat() (os.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

func (*Temp) Write

func (tf *Temp) Write(contents []byte) (int, error)

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

func (*Temp) WriteAt

func (tf *Temp) WriteAt(contents []byte, off int64) (int, error)

WriteAt writes len(b) bytes to the File starting at byte offset off. It returns the number of bytes written and an error, if any. WriteAt returns a non-nil error when n != len(b).

func (*Temp) WriteString

func (tf *Temp) WriteString(contents string) (int, error)

WriteString is like Write, but writes the contents of string s rather than a slice of bytes.

type WatchAction

type WatchAction func(*os.File) error

WatchAction is an action for the file watcher.

type Watcher

type Watcher struct {
	*async.Latch

	Path         string
	PollInterval time.Duration
	Action       func(*os.File) error
	Errors       chan error
}

Watcher watches a file for changes and calls the action.

func NewWatcher

func NewWatcher(path string, action WatchAction, opts ...WatcherOption) *Watcher

NewWatcher returns a new watcher.

func (Watcher) PollIntervalOrDefault

func (w Watcher) PollIntervalOrDefault() time.Duration

PollIntervalOrDefault returns the polling interval or a default.

func (Watcher) Watch

func (w Watcher) Watch()

Watch watches a given file.

type WatcherOption

type WatcherOption func(*Watcher)

WatcherOption is an option for a watcher.

Jump to

Keyboard shortcuts

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