library

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ManifestName       = "manifest.json"
	EmoticonsDirectory = "emoticons"
	MaxManifestBytes   = 8 << 20
	MaxImageBytes      = 32 << 20
	MaxManifestItems   = 20_000
	MaxCaptionBytes    = 4 << 10
)

Variables

This section is empty.

Functions

func OpenRelative

func OpenRelative(ctx context.Context, root, relative string) (*os.File, error)

OpenRelative opens a file beneath root without following symbolic links in any path component. The caller owns and must close the returned file.

func ReadRelative

func ReadRelative(ctx context.Context, root, relative string, limit int64) ([]byte, error)

ReadRelative reads a bounded file beneath root using the platform's secure relative-open implementation. Callers must pass a relative path and handle the returned error according to their own boundary contract.

func ValidateManifest

func ValidateManifest(manifest Manifest, limits Limits) error

ValidateManifest validates metadata without reading the referenced files.

func VerifyFile

func VerifyFile(ctx context.Context, path string, item Item, limits Limits) error

VerifyFile validates one standard manifest item at an already resolved path. The caller remains responsible for resolving that path beneath its source root.

Types

type Error

type Error struct {
	Kind      string
	Subtype   string
	Message   string
	Hint      string
	Retryable bool
	Committed bool
	Err       error
}

Error is a stable, machine-readable error returned by the library layer. Callers should branch on Kind and Subtype rather than Message.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Hooks

type Hooks struct {
	BeforeRename   func(string) error
	AfterRename    func(string) error
	SyncDirectory  func(string) error
	BeforeManifest func() error
}

Hooks make filesystem failure boundaries testable without changing production behavior.

type Item

type Item struct {
	MD5      string `json:"md5"`
	SHA256   string `json:"sha256"`
	Filename string `json:"filename"`
	Format   string `json:"format"`
	Size     int64  `json:"size"`
	Caption  string `json:"caption,omitempty"`
}

Item is one standard v1 manifest entry. Filename is relative to the library root.

func ReadImage

func ReadImage(ctx context.Context, path string) (Item, []byte, error)

ReadImage reads and validates one local original image. The returned item uses the standard emoticons/<md5>.<format> filename and has an empty caption. The returned bytes are a snapshot of the opened file and can be staged for a later manifest update even when the source path is subsequently moved.

type Library

type Library struct {
	Root   string
	Limits Limits
	Hooks  Hooks
}

Library confines all paths used by this package to Root.

func New

func New(root string) (*Library, error)

func (*Library) CreateRelativeTempDirectory

func (l *Library) CreateRelativeTempDirectory(relative, pattern string) (string, error)

CreateRelativeTempDirectory creates a private temporary directory beneath an existing directory inside the library root. Unix implementations create it through an anchored directory descriptor to avoid path-swap races.

func (*Library) EnsureRelativeDirectory

func (l *Library) EnsureRelativeDirectory(relative string) error

EnsureRelativeDirectory creates a directory beneath the library root while rejecting symlinked path components. It is used by higher-level operations for their private staging and state directories before they publish files.

func (*Library) EnsureRelativeDirectoryNoState

func (l *Library) EnsureRelativeDirectoryNoState(relative string) error

EnsureRelativeDirectoryNoState creates a directory beneath an already existing library root without creating the library's private .sticker state directory. It is intended for temporary material roots that must contain only standard library files.

func (*Library) ItemPath

func (l *Library) ItemPath(item Item) (string, error)

ItemPath resolves a manifest item's standard filename beneath the library root. It checks path components but does not read the image; use ReadItem when content integrity must be established before returning a usable path.

func (*Library) OpenVerified

func (l *Library) OpenVerified(ctx context.Context, item Item) (*os.File, string, error)

OpenVerified opens one standard item only after all declared integrity checks have passed. The caller owns the returned file and must close it. The file offset is reset to the beginning before returning.

func (*Library) ReadItem

func (l *Library) ReadItem(ctx context.Context, id string) (Item, string, error)

ReadItem locates and verifies one item, returning its absolute path only after all declared integrity checks have passed. It performs no image reads for other items.

func (*Library) ReadManifest

func (l *Library) ReadManifest(ctx context.Context) (Manifest, error)

ReadManifest reads the root manifest. A missing manifest is an empty personal library; an existing malformed manifest is always an integrity error.

func (*Library) ReadManifestRequired

func (l *Library) ReadManifestRequired(ctx context.Context) (Manifest, error)

ReadManifestRequired reads a standard manifest that must exist. It is used for imports and other operations where a missing source is not an empty collection.

func (*Library) RemoveRelative

func (l *Library) RemoveRelative(ctx context.Context, relative string) (bool, error)

RemoveRelative removes one file beneath the library root without following symbolic links in path components. The operation is idempotent: a missing root, parent, or target returns (false, nil). A present target is removed in one filesystem operation and returns (true, nil).

func (*Library) RemoveRelativeDirectory

func (l *Library) RemoveRelativeDirectory(ctx context.Context, relative string) error

RemoveRelativeDirectory removes a directory tree beneath the library root without following symbolic links. A missing target is already clean. Callers should use it for private temporary directories, never for the library root itself.

func (*Library) UpdateManifest

func (l *Library) UpdateManifest(ctx context.Context, fn func(Manifest) (Manifest, error)) error

UpdateManifest executes fn under the cross-process write lock and atomically publishes one manifest.

func (*Library) Validate

func (l *Library) Validate(ctx context.Context, manifest Manifest) error

Validate checks every referenced file and its declared format, size, MD5, and SHA-256.

func (*Library) VerifyItem

func (l *Library) VerifyItem(ctx context.Context, item Item) error

VerifyItem validates one manifest item using the library root's secure relative-open implementation.

func (*Library) WithReadLock

func (l *Library) WithReadLock(ctx context.Context, fn func(Manifest) error) error

WithReadLock runs fn while holding the library's shared lock when one is present. The callback receives the manifest snapshot read under that lock, which lets read operations open referenced files without racing a manifest writer between the snapshot and the file handles.

func (*Library) WithWriteLock

func (l *Library) WithWriteLock(ctx context.Context, fn func(Manifest) error) error

WithWriteLock runs fn while holding the library's cross-process exclusive lock. The callback receives the latest personal manifest snapshot read under that lock, so callers can validate concurrent changes before publishing related private state.

func (*Library) WriteManifest

func (l *Library) WriteManifest(ctx context.Context, manifest Manifest) error

WriteManifest validates metadata and atomically replaces the root manifest.

func (*Library) WriteManifestLocked

func (l *Library) WriteManifestLocked(ctx context.Context, manifest Manifest) error

WriteManifestLocked validates and atomically publishes a manifest while the caller holds the lock acquired by WithWriteLock. It is intended for updates that publish the manifest together with related private metadata under one lock; callers must not invoke it outside that callback.

func (*Library) WriteRelativeAtomic

func (l *Library) WriteRelativeAtomic(ctx context.Context, relative string, data []byte) error

WriteRelativeAtomic publishes data at a path beneath the library root. Parent directories must already exist. The platform implementation anchors the write to the library root so a concurrent symlink swap cannot redirect the temporary file or rename outside the library.

func (*Library) WriteRelativeAtomicFrom

func (l *Library) WriteRelativeAtomicFrom(ctx context.Context, relative string, source io.Reader, limit int64, validate func(int64) error) error

WriteRelativeAtomicFrom streams bounded data into a root-anchored temporary file, validates it before rename, and atomically publishes it below the library root. The validator receives the number of bytes copied and runs before the destination becomes visible.

type Limits

type Limits struct {
	ManifestBytes int64
	ImageBytes    int64
	Items         int
	CaptionBytes  int
}

Limits bounds input accepted by the library. Zero fields use the defaults.

func DefaultLimits

func DefaultLimits() Limits

type Manifest

type Manifest struct {
	SchemaVersion int    `json:"schema_version"`
	Collection    string `json:"collection"`
	Items         []Item `json:"items"`
}

Manifest is the existing standard v1 format shared by public packs and personal libraries.

Jump to

Keyboard shortcuts

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