Documentation ¶
Overview ¶
Package fileutil provides helpers for reading and writing files.
Index ¶
- Constants
- func ETag(contents []byte) (string, error)
- func FormatFileSize(sizeBytes int64) string
- func ParseFileSize(fileSizeValue string) (int64, error)
- func ReadChunks(filePath string, chunkSize int, handler func([]byte) error) error
- func ReadLines(filePath string, handler func(string) error) error
- func RemoveMany(filePaths ...string) error
- func Watch(path string, action WatchAction) error
- type Temp
- func (tf *Temp) Close() error
- func (tf *Temp) Name() string
- func (tf *Temp) Read(buffer []byte) (int, error)
- func (tf *Temp) ReadAt(buffer []byte, off int64) (int, error)
- func (tf *Temp) Stat() (os.FileInfo, error)
- func (tf *Temp) Write(contents []byte) (int, error)
- func (tf *Temp) WriteAt(contents []byte, off int64) (int, error)
- func (tf *Temp) WriteString(contents string) (int, error)
- type WatchAction
- type Watcher
- type WatcherOption
Constants ¶
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 )
const ( ErrWatchStopped ex.Class = "watch file should stop" DefaultWatchPollInterval = 500 * time.Millisecond )
Watch constants
Variables ¶
This section is empty.
Functions ¶
func FormatFileSize ¶
FormatFileSize returns a string representation of a file size in bytes.
func ParseFileSize ¶
ParseFileSize parses a file size
func ReadChunks ¶
ReadChunks reads a file in `chunkSize` pieces, dispatched to the handler.
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 ¶
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 (*Temp) Read ¶
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 ¶
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 ¶
Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.
func (*Temp) Write ¶
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).
type WatchAction ¶
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 ¶
PollIntervalOrDefault returns the polling interval or a default.