storagekit

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package storagekit holds the few helpers every storage.Store implementation shares, so the adapters (storage/local, storage/s3 and storagetest's memory store) cannot drift apart on them. It is internal: an adapter written elsewhere writes its own, and the conformance suite holds it to the same contract.

Index

Constants

View Source
const KeyAttempts = 3

KeyAttempts is how many generated keys a write tries before it reports a collision as storage.ErrExists.

Variables

This section is empty.

Functions

func Check

func Check(ctx context.Context, key string) error

Check is the preamble every call that names a key shares: the context, then the key's form.

func NormalizePrefix added in v0.7.0

func NormalizePrefix(prefix string) (string, error)

NormalizePrefix returns prefix as "" or "a/b/" after checking that it cannot name anything outside itself. The error names the offending prefix but not the adapter, which wraps it.

func Range

func Range(size, offset, length int64) (end int64, err error)

Range resolves an Open request against an object of size bytes: it returns the end of the range starting at offset. A negative length, or one that reaches or exceeds the remaining bytes (math.MaxInt64 included), reads to the end. An offset that is negative or beyond the end is storage.ErrInvalidRange. It never overflows, whatever length is.

func ValidSHA256 added in v0.7.0

func ValidSHA256(s string) bool

ValidSHA256 reports whether s is a lowercase hex SHA-256 digest, the form the adapters store as object metadata.

Types

type ContextReader

type ContextReader struct {
	Ctx context.Context
	R   io.Reader
}

ContextReader stops a copy as soon as Ctx is done.

func (*ContextReader) Read

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

Read implements io.Reader.

type FileOps added in v0.7.0

type FileOps struct {
	Sync   func(*os.File) error
	Close  func(*os.File) error
	Remove func(string) error
}

FileOps are the file operations a SpoolFile performs. The zero value uses the operating system; a test substitutes failing operations.

type SpoolFile added in v0.7.0

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

SpoolFile is a temporary file holding one upload body. Its life has three steps. Fill writes the body, syncs it and closes it: every failure of those is returned before any upload begins, so an upload never starts on bytes that did not reach the file. Open reopens the completed file read-only for the upload. Release closes what is still open and removes the file.

Release is best effort: it always tries to close and remove, but a failure it reports cannot undo an upload that has already committed, so a caller that has committed must not treat a Release error as a failed write.

func NewSpoolFile added in v0.7.0

func NewSpoolFile(prefix, dir, pattern string, ops FileOps) (*SpoolFile, error)

NewSpoolFile creates the file in dir. prefix names the adapter in errors.

func (*SpoolFile) Fill added in v0.7.0

func (s *SpoolFile) Fill(ctx context.Context, r io.Reader) error

Fill copies r into the file, then syncs and closes it. It reports the first failure of the copy, the sync or the close.

func (*SpoolFile) Open added in v0.7.0

func (s *SpoolFile) Open() (*os.File, error)

Open reopens the completed file read-only. Release closes the handle.

func (*SpoolFile) Release added in v0.7.0

func (s *SpoolFile) Release() error

Release closes any handle still open, then removes the file. It attempts both steps even when one fails, and returns the first failure.

Jump to

Keyboard shortcuts

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