ioutils

package
v1.55.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: Apache-2.0 Imports: 10 Imported by: 148

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed is returned when Write is called on a closed BytesPipe.
	ErrClosed = errors.New("write to closed BytesPipe")
)

Functions

func AtomicWriteFile

func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error

func AtomicWriteFileWithOpts added in v0.46.1

func AtomicWriteFileWithOpts(filename string, data []byte, perm os.FileMode, opts *AtomicFileWriterOptions) error

AtomicWriteFile atomically writes data to a file named by filename.

func HashData

func HashData(src io.Reader) (string, error)

HashData returns the sha256 sum of src.

func NewCancelReadCloser

func NewCancelReadCloser(ctx context.Context, in io.ReadCloser) io.ReadCloser

NewCancelReadCloser creates a wrapper that closes the ReadCloser when the context is cancelled. The returned io.ReadCloser must be closed when it is no longer needed.

func NewReadCloserWrapper

func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser

NewReadCloserWrapper returns a new io.ReadCloser.

func NewReaderErrWrapper

func NewReaderErrWrapper(r io.Reader, closer func()) io.Reader

NewReaderErrWrapper returns a new io.Reader.

func NewWriteCloserWrapper

func NewWriteCloserWrapper(r io.Writer, closer func() error) io.WriteCloser

NewWriteCloserWrapper returns a new io.WriteCloser.

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

NopWriteCloser returns a nopWriteCloser.

func SetDefaultOptions

func SetDefaultOptions(opts AtomicFileWriterOptions)

SetDefaultOptions overrides the default options used when creating an atomic file writer.

func TempDir

func TempDir(dir, prefix string) (string, error)

TempDir on Unix systems is equivalent to os.MkdirTemp.

Types

type AtomicFileWriterOptions

type AtomicFileWriterOptions struct {
	// NoSync specifies whether the sync call must be skipped for the file.
	// If NoSync is not specified, the file is synced to the
	// storage after it has been written and before it is moved to
	// the specified path.
	NoSync bool
	// On successful return from Close() this is set to the mtime of the
	// newly written file.
	ModTime time.Time
	// Specifies whether Commit() must be explicitly called to write state
	// to the destination. This allows an application to preserve the original
	// file when an error occurs during processing (and not just during write)
	// The default is false, which will auto-commit on Close
	ExplicitCommit bool
}

AtomicFileWriterOptions specifies options for creating the atomic file writer.

type AtomicWriteSet

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

AtomicWriteSet is used to atomically write a set of files and ensure they are visible at the same time. Must be committed to a new directory.

func NewAtomicWriteSet

func NewAtomicWriteSet(tmpDir string) (*AtomicWriteSet, error)

NewAtomicWriteSet creates a new atomic write set to atomically create a set of files. The given directory is used as the base directory for storing files before commit. If no temporary directory is given the system default is used.

func (*AtomicWriteSet) Cancel

func (ws *AtomicWriteSet) Cancel() error

Cancel cancels the set and removes all temporary data created in the set.

func (*AtomicWriteSet) Commit

func (ws *AtomicWriteSet) Commit(target string) error

Commit moves all created files to the target directory. The target directory must not exist and the parent of the target directory must exist.

func (*AtomicWriteSet) FileWriter

func (ws *AtomicWriteSet) FileWriter(name string, flag int, perm os.FileMode) (io.WriteCloser, error)

FileWriter opens a file writer inside the set. The file should be synced and closed before calling commit.

func (*AtomicWriteSet) String

func (ws *AtomicWriteSet) String() string

String returns the location the set is writing to.

func (*AtomicWriteSet) WriteFile

func (ws *AtomicWriteSet) WriteFile(filename string, data []byte, perm os.FileMode) error

WriteFile writes a file to the set, guaranteeing the file has been synced.

type BytesPipe

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

BytesPipe is io.ReadWriteCloser which works similarly to pipe(queue). All written data may be read at most once. Also, BytesPipe allocates and releases new byte slices to adjust to current needs, so the buffer won't be overgrown after peak loads.

func NewBytesPipe

func NewBytesPipe() *BytesPipe

NewBytesPipe creates new BytesPipe, initialized by specified slice. If buf is nil, then it will be initialized with slice which cap is 64. buf will be adjusted in a way that len(buf) == 0, cap(buf) == cap(buf).

func (*BytesPipe) Close

func (bp *BytesPipe) Close() error

Close causes further reads from a BytesPipe to return immediately.

func (*BytesPipe) CloseWithError

func (bp *BytesPipe) CloseWithError(err error) error

CloseWithError causes further reads from a BytesPipe to return immediately.

func (*BytesPipe) Read

func (bp *BytesPipe) Read(p []byte) (n int, err error)

Read reads bytes from BytesPipe. Data could be read only once.

func (*BytesPipe) Write

func (bp *BytesPipe) Write(p []byte) (int, error)

Write writes p to BytesPipe. It can allocate new []byte slices in a process of writing.

type CommittableWriter added in v0.46.1

type CommittableWriter interface {
	io.WriteCloser

	// Commit closes the temporary file associated with this writer, and
	// provided no errors (during commit or previously during write operations),
	// will publish the completed file under the intended destination.
	Commit() error
}

func NewAtomicFileWriter

func NewAtomicFileWriter(filename string, perm os.FileMode) (CommittableWriter, error)

NewAtomicFileWriterWithOpts returns a CommittableWriter, with auto-commit enabled. Writing to it writes to a temporary file and closing it atomically changes the temporary file to destination path. Writing and closing concurrently is not allowed.

func NewAtomicFileWriterWithOpts

func NewAtomicFileWriterWithOpts(filename string, perm os.FileMode, opts *AtomicFileWriterOptions) (CommittableWriter, error)

NewAtomicFileWriterWithOpts returns a CommittableWriter so that writing to it writes to a temporary file, which can later be committed to a destination path, either by Closing in the case of auto-commit, or manually calling commit if the ExplicitCommit option is enabled. Writing and closing concurrently is not allowed.

type NopFlusher

type NopFlusher struct{}

NopFlusher represents a type which flush operation is nop.

func (*NopFlusher) Flush

func (f *NopFlusher) Flush()

Flush is a nop operation.

type NopWriter

type NopWriter struct{}

NopWriter represents a type which write operation is nop.

func (*NopWriter) Write

func (*NopWriter) Write(buf []byte) (int, error)

type OnEOFReader

type OnEOFReader struct {
	Rc io.ReadCloser
	Fn func()
}

OnEOFReader wraps an io.ReadCloser and a function the function will run at the end of file or close the file.

func (*OnEOFReader) Close

func (r *OnEOFReader) Close() error

Close closes the file and run the function.

func (*OnEOFReader) Read

func (r *OnEOFReader) Read(p []byte) (n int, err error)

type WriteCounter

type WriteCounter struct {
	Count  int64
	Writer io.Writer
}

WriteCounter wraps a concrete io.Writer and hold a count of the number of bytes written to the writer during a "session". This can be convenient when write return is masked (e.g., json.Encoder.Encode())

func NewWriteCounter

func NewWriteCounter(w io.Writer) *WriteCounter

NewWriteCounter returns a new WriteCounter.

func (*WriteCounter) Write

func (wc *WriteCounter) Write(p []byte) (count int, err error)

type WriteFlusher

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

WriteFlusher wraps the Write and Flush operation ensuring that every write is a flush. In addition, the Close method can be called to intercept Read/Write calls if the targets lifecycle has already ended.

func NewWriteFlusher

func NewWriteFlusher(w io.Writer) *WriteFlusher

NewWriteFlusher returns a new WriteFlusher.

func (*WriteFlusher) Close

func (wf *WriteFlusher) Close() error

Close closes the write flusher, disallowing any further writes to the target. After the flusher is closed, all calls to write or flush will result in an error.

func (*WriteFlusher) Flush

func (wf *WriteFlusher) Flush()

Flush the stream immediately.

func (*WriteFlusher) Flushed

func (wf *WriteFlusher) Flushed() bool

Flushed returns the state of flushed. If it's flushed, return true, or else it return false.

func (*WriteFlusher) Write

func (wf *WriteFlusher) Write(b []byte) (n int, err error)

Notes

Bugs

  • Remove this method. Its use is inherently racy. Seems to be used to detect whether or a response code has been issued or not. Another hook should be used instead.

Jump to

Keyboard shortcuts

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