io

package
v0.22.5 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

package io implements various io utilities.

Index

Constants

View Source
const (
	DCP_SESSION_FOLDER           = "DCP_SESSION_FOLDER"           // Folder to delete when finished with a session
	DCP_PRESERVE_EXECUTABLE_LOGS = "DCP_PRESERVE_EXECUTABLE_LOGS" // If truthy ("1", "true", "on", "yes"), preserve logs from executable runs
)
View Source
const (
	FirstLogLineNumber = 1
)
View Source
const (
	MaxTailSize = 1000 * 1000 // Million rows
)

Variables

View Source
var (
	ErrClosedWriter = errors.New("writer is closed")
	ErrClosedReader = errors.New("reader is closed")
)
View Source
var (
	DcpSessionDir func() string
)
View Source
var (
	DcpTempDir func() string
)

Functions

func CleanupSessionFolderIfNeeded

func CleanupSessionFolderIfNeeded()

func CreateTempFolder

func CreateTempFolder(name string, perm os.FileMode) (string, error)

func NewBufferedPipe

func NewBufferedPipe() (io.ReadCloser, io.WriteCloser)

NewBufferedPipe is like io.Pipe(), except it includes an automatically-expanding buffer, so writers are never blocked. It is also goroutine-safe. Inspiration/reference: https://github.com/golang/go/issues/28790, https://github.com/golang/go/issues/34502, https://github.com/acomagu/bufpipe

func NewBufferedPipeWithMaxSize added in v0.22.2

func NewBufferedPipeWithMaxSize(maxSize uint) (io.ReadCloser, io.WriteCloser)

NewBufferedPipeWithMaxSize is like NewBufferedPipe, but with an optional maximum buffer size. If maxSize is 0, the buffer can grow without limit (same as NewBufferedPipe). If maxSize is > 0, writers will block when the buffer reaches the maximum size, waiting for readers to consume data before more can be written.

func NewTimestampAwareReader

func NewTimestampAwareReader(inner io.Reader, opts TimestampAwareReaderOptions) io.ReadCloser

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

func OpenFile

func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)

func OpenTempFile

func OpenTempFile(name string, flag int, perm os.FileMode) (*os.File, error)

func PreserveSessionFolder

func PreserveSessionFolder()

func WriteFile

func WriteFile(name string, data []byte, perm os.FileMode) error

Types

type BufferedPipeReader

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

func (*BufferedPipeReader) Close

func (bpr *BufferedPipeReader) Close() error

Closes the reader half of the pipe, subsequent readers will receive io.ErrClosedPipe.

func (*BufferedPipeReader) CloseWithError

func (bpr *BufferedPipeReader) CloseWithError(err error) error

Closes the reader half of the pipe, subsequent readers will receive the passed error. PipeReader spec requires that CloseWithError() never overwrites the previous error and always returns nil.

func (*BufferedPipeReader) Read

func (bpr *BufferedPipeReader) Read(p []byte) (int, error)

type BufferedPipeWriter

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

func (*BufferedPipeWriter) Close

func (bpw *BufferedPipeWriter) Close() error

Closes the writer half of the pipe, subsequent writers will receive io.ErrClosedPipe. Subsequent readers will receive io.EOF.

func (*BufferedPipeWriter) CloseWithError

func (bpw *BufferedPipeWriter) CloseWithError(err error) error

Closes the writer half of the pipe, subsequent writers will receive the passed error. Subsequent readers will receive io.EOF. PipeWriter spec requires that CloseWithError() never overwrites the previous error and always returns nil.

func (*BufferedPipeWriter) Write

func (bpw *BufferedPipeWriter) Write(p []byte) (int, error)

type BufferedWrappingWriter

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

func NewBufferedWrappingWriter

func NewBufferedWrappingWriter() *BufferedWrappingWriter

Creates a new BufferedWrappingWriter.

func (*BufferedWrappingWriter) Close

func (bww *BufferedWrappingWriter) Close() error

func (*BufferedWrappingWriter) SetTarget

func (bww *BufferedWrappingWriter) SetTarget(writer io.Writer) error

func (*BufferedWrappingWriter) Write

func (bww *BufferedWrappingWriter) Write(p []byte) (int, error)

Writes the given bytes to the target writer, or buffers them if the target writer is not yet available.

type ContextReader

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

ContextReader is a reader that will read from an inner reader until the context is cancelled.

func NewContextReader

func NewContextReader(ctx context.Context, r io.Reader, leverageReadCloser bool) *ContextReader

Creates a new ContextReader instance If leverageReadCloser is true, the ContextReader will take advantage of the fact that the reader is also a ReadCloser and will call Close() on the reader when the context is cancelled. This allows the ContextReader to work without extra worker goroutine. The user of the ContextReader must ensure that the reader is not closed by other means.

func (*ContextReader) Read

func (cr *ContextReader) Read(p []byte) (int, error)

type FollowWriter

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

func NewFollowWriter

func NewFollowWriter(ctx context.Context, source io.Reader, dest io.Writer) *FollowWriter

Creates a FollowWriter that reads content from the reader source and writes it to the writer destination. Keeps trying to read new content even after EOF until StopFollow() is called, after which the next EOF received will cause the reader and writer to stop. If the source is an io.Closer, it will be closed when the FollowWriter is cancelled.

func (*FollowWriter) Cancel

func (fw *FollowWriter) Cancel()

func (*FollowWriter) Done

func (fw *FollowWriter) Done() <-chan struct{}

func (*FollowWriter) Err

func (fw *FollowWriter) Err() error

func (*FollowWriter) StopFollow

func (fw *FollowWriter) StopFollow()

type IndexLine

type IndexLine struct {
	Timestamp time.Time
	Line      uint32
	Offset    uint64
}

type IndexingWriter

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

func NewIndexingWriter

func NewIndexingWriter(data, index WriteSyncerCloser) *IndexingWriter

func NewIndexingWriterWithStride

func NewIndexingWriterWithStride(data, index WriteSyncerCloser, indexStride uint32) *IndexingWriter

func (*IndexingWriter) Close

func (iw *IndexingWriter) Close() error

func (*IndexingWriter) IndexInvalid

func (iw *IndexingWriter) IndexInvalid() bool

func (*IndexingWriter) Sync

func (iw *IndexingWriter) Sync() error

func (*IndexingWriter) Write

func (iw *IndexingWriter) Write(p []byte) (int, error)

type NotifyWriteCloser

type NotifyWriteCloser interface {
	WriteSyncerCloser
	Closed() <-chan struct{}
}

NotifiyWriteCloser is a WriteCloser that can notify when it is closed.

type ParagraphWriter

type ParagraphWriter interface {
	io.WriteCloser
	NewParagraph()
}

ParagraphWriter is a WriteCloser that writes to an inner WriteCloser and has a concept of "paragraphs of data". A new paragraph is started by calling NewParagraph(). When that happens, the NEXT write (the first write of a new paragraph) will be preceded by a paragraph separator.

func NewParagraphWriter

func NewParagraphWriter(w io.WriteCloser, paragraphSeparator []byte) ParagraphWriter

type Syncer

type Syncer interface {
	Sync() error
}

type TailReader

type TailReader struct {
	// Set (frozen) when tailLines is filled with data and the reader stats are available.
	Filled    *concurrency.AutoResetEvent
	FillStats *atomic.Pointer[TailReaderStats] // The stats of the reader
	// contains filtered or unexported fields
}

TailReader is an io.ReadCloser that returns the last N lines of text from an inner reader. It reads all content from the inner reader until EOF, then allows reading only the last N lines from it. If the inner reader contains fewer than N lines, all lines will be returned.

TailReader remains usable after EOF is reached, i.e. it will just delegate to the inner reader once the last N lines are returned.

TailReader is not thread-safe, except from the Filled event and the Stats atomic pointer. The Stats value is valid only after the Filled event is set.

func NewTailReader

func NewTailReader(inner io.ReadCloser, tailSize int) *TailReader

func (*TailReader) Close

func (tr *TailReader) Close() error

Close implements the io.Closer interface.

func (*TailReader) Read

func (tr *TailReader) Read(p []byte) (int, error)

Read implements the io.Reader interface. On the first call, it reads all content from the inner reader, stores the last N lines, and then returns them on subsequent calls.

type TailReaderStats

type TailReaderStats struct {
	TotalLines int64 // How many lines have been read from the inner reader
	TailLines  int   // How many "tail" lines are available/will be returned to the reader
}

type TarWriter

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

func NewTarWriter

func NewTarWriter() *TarWriter

func (*TarWriter) Buffer

func (tw *TarWriter) Buffer() (*bytes.Buffer, error)

func (*TarWriter) CopyFile

func (tw *TarWriter) CopyFile(src io.Reader, size int64, name string, uid int32, gid int32, mode os.FileMode, modTime time.Time, changeTime time.Time, accessTime time.Time) error

func (*TarWriter) WriteDir

func (tw *TarWriter) WriteDir(name string, uid int32, gid int32, mode os.FileMode, modTime time.Time, changeTime time.Time, accessTime time.Time) error

func (*TarWriter) WriteFile

func (tw *TarWriter) WriteFile(contents []byte, name string, uid int32, gid int32, mode os.FileMode, modTime time.Time, changeTime time.Time, accessTime time.Time) error
func (tw *TarWriter) WriteSymlink(name string, linkTarget string, uid int32, gid int32, modTime time.Time, changeTime time.Time, accessTime time.Time) error

type TimestampAwareReaderOptions

type TimestampAwareReaderOptions struct {
	Timestamps  bool
	Limit       int64
	Skip        int64
	LineNumbers bool
}

type WriteSyncerCloser

type WriteSyncerCloser interface {
	io.WriteCloser
	Syncer
}

func NewTimestampWriter

func NewTimestampWriter(inner WriteSyncerCloser) WriteSyncerCloser

Creates a new TimestampWriter that wraps the given writer.

Jump to

Keyboard shortcuts

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