memfs

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2025 License: MIT Imports: 7 Imported by: 0

README

The memfs package

The memfs package provides in-memory filesystem.

The File

The File structure implements multiple I/O interfaces plus fs.File interface. Its purpose is to be a versatile in-memory buffer.

type File interface {
    io.Seeker
    io.Reader
    io.ReaderAt
    io.Closer
    io.ReaderFrom
    io.Writer
    io.WriterAt
    io.StringWriter
    fs.File
    
    Truncate(size int64) error
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrOutOfBounds = errors.New("offset out of bounds")

ErrOutOfBounds is returned for invalid offsets.

Functions

func WithFileAppend

func WithFileAppend(fil *File)

WithFileAppend is a File constructor function option setting the offset to the end of the file. This option must be the last option on the option list.

When File.Truncate is used, the offset will be set to the end of the File.

func WithFileFlag

func WithFileFlag(flag int) func(*File)

WithFileFlag is a File constructor function option setting flags. Flags are the same as for os.OpenFile.

Currently only os.O_APPEND flag is supported.

func WithFileName

func WithFileName(name string) func(*File)

WithFileName is a File constructor function option setting file name.

func WithFileOffset

func WithFileOffset(off int) func(*File)

WithFileOffset is a File constructor function option setting the offset.

Types

type File

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

A File is a variable-sized buffer of bytes. Its zero value is an empty buffer ready to use.

Example
package main

import (
	"fmt"
	"io"

	"github.com/ctx42/testing/pkg/kit/memfs"
)

func main() {
	buf := &memfs.File{}

	_, _ = buf.Write([]byte{0, 1, 2, 3})
	_, _ = buf.Seek(-2, io.SeekEnd)
	_, _ = buf.Write([]byte{4, 5})
	_, _ = buf.Seek(0, io.SeekStart)

	data, _ := io.ReadAll(buf)
	fmt.Println(data)

}
Output:

[0 1 4 5]

func FileWith

func FileWith(content []byte, opts ...func(*File)) *File

FileWith creates a new instance of File initialized with content. The created instance takes ownership of the content slice, and the caller must not use it after passing it to this function. FileWith is intended to prepare the File instance to read existing data. It can also be used to set the initial size of the internal buffer for writing. To do that, the content slice should have the desired capacity but a length of zero. It will panic with ErrOutOfBounds if the WithFileOffset option sets the offset to a negative number or beyond sata slice length.

func NewFile

func NewFile(opts ...func(buffer *File)) *File

NewFile returns a new instance of File. The difference between using this function and using the zero value is that this function will initialize the buffer with capacity of bytes.MinRead. It will panic with ErrOutOfBounds if the WithFileOffset option sets offset to a negative number or greater than bytes.MinRead.

func (*File) Cap

func (fil *File) Cap() int

Cap returns the buffer capacity, that is, the total space allocated for the buffer's data.

func (*File) Close

func (fil *File) Close() error

Close sets offset to zero and zero put the buffer. It always returns nil error.

func (*File) Grow

func (fil *File) Grow(n int)

Grow grows the buffer's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, Grow will panic. If the buffer can't grow, it will panic with bytes.ErrTooLarge.

func (*File) Len

func (fil *File) Len() int

Len returns the buffer length.

func (*File) Offset

func (fil *File) Offset() int

Offset returns the current offset.

func (*File) Read

func (fil *File) Read(p []byte) (int, error)

Read reads the next len(p) bytes from the buffer at the current offset or until the buffer is drained. The return value is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.

func (*File) ReadAt

func (fil *File) ReadAt(p []byte, off int64) (int, error)

ReadAt reads len(p) bytes from the buffer at the current offset. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(p). It does not change the offset.

func (*File) ReadByte

func (fil *File) ReadByte() (byte, error)

ReadByte reads and returns the next byte from the buffer at the current offset or returns an error. If ReadByte returns an error, no input byte was consumed, and the returned byte value is undefined.

func (*File) ReadFrom

func (fil *File) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads data from r until EOF and appends it to the buffer at the current offset, growing the buffer as needed. The return value is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with bytes.ErrTooLarge.

func (*File) Release

func (fil *File) Release() []byte

Release releases ownership of the underlying buffer, the caller should not use this instance after this call.

func (*File) Seek

func (fil *File) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Read or Write on the buffer to the offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error (only if calculated offset < 0). Returns non-nil error of type fs.PathError where fs.PathError.Path field is an empty string unless WithFileName option was used during instance creation.

func (*File) SeekEnd

func (fil *File) SeekEnd() int64

SeekEnd is a convenience method setting the buffer's offset to the buffer length and returning the value it had before the method was called.

func (*File) SeekStart

func (fil *File) SeekStart() int64

SeekStart is a convenience method setting the buffer's offset to zero and returning the value it had before the method was called.

func (*File) Stat

func (fil *File) Stat() (fs.FileInfo, error)

Stat returns information about the in-memory file where the name is empty (unless, WithFileName option was used), the size is the length of the underlying buffer, mode is always 0444, modification time is always zero value time and fs.FileInfo.Sys always returns nil.

func (*File) String

func (fil *File) String() string

String returns string representation of the buffer starting at the current offset. Calling this method is considered as reading the buffer and advances offset to the end of the buffer.

func (*File) Truncate

func (fil *File) Truncate(size int64) error

Truncate changes the size of the buffer discarding bytes at the offsets greater than size. It does not change the offset unless a WithFileAppend option was used, then it sets the offset to the end of the buffer. Returns an error only when size is negative. The error is of type fs.PathError where fs.PathError.Path field is an empty string unless WithFileName option was used during instance creation.

func (*File) Write

func (fil *File) Write(p []byte) (n int, err error)

Write writes the contents of p to the underlying buffer at the current offset, growing the buffer as needed. The return value n is the length of p; err is always nil.

func (*File) WriteAt

func (fil *File) WriteAt(p []byte, off int64) (n int, err error)

WriteAt writes len(p) bytes to the underlying buffer starting at the current offset. It returns the number of bytes written; err is always nil. It does not change the offset.

func (*File) WriteByte

func (fil *File) WriteByte(c byte) error

WriteByte writes a byte c to the underlying buffer at the current offset.

func (*File) WriteString

func (fil *File) WriteString(s string) (int, error)

WriteString writes string s to the buffer at the current offset.

func (*File) WriteTo

func (fil *File) WriteTo(w io.Writer) (int64, error)

WriteTo writes data to w starting at the current offset until there are no more bytes to write or when an error occurs. The int64 return value is the number of bytes written. When an error occurred during the operation, it is also returned.

type FileInfo

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

FileInfo implements fs.FileInfo interface.

func (FileInfo) IsDir

func (fi FileInfo) IsDir() bool

func (FileInfo) ModTime

func (fi FileInfo) ModTime() time.Time

func (FileInfo) Mode

func (fi FileInfo) Mode() fs.FileMode

func (FileInfo) Name

func (fi FileInfo) Name() string

func (FileInfo) Size

func (fi FileInfo) Size() int64

func (FileInfo) Sys

func (fi FileInfo) Sys() any

Jump to

Keyboard shortcuts

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