Help us shape the future of Go! Take the 2026 Go Developer Survey

privatefiles

package
v0.17.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

Package privatefiles owns private file creation, access checks, and bounded record I/O.

Index

Constants

View Source
const (
	// DirectoryMode excludes group and other access on POSIX.
	DirectoryMode fs.FileMode = 0o700
	// FileMode excludes group and other access on POSIX.
	FileMode fs.FileMode = 0o600
)
View Source
const (
	// PublicationDirectoryName identifies the reserved metadata child for private record publication.
	PublicationDirectoryName = ".record-publications"
)
View Source
const RetainedPublicationMaxBytes = publicationJournalMaxBytes

RetainedPublicationMaxBytes bounds one captured publication journal.

Variables

This section is empty.

Functions

func CheckEntry

func CheckEntry(root *os.Root, name string, directory bool, want EntryReceipt) error

CheckEntry rejects a different native identity or access policy.

func CheckRecord

func CheckRecord(root *os.Root, name string, want RecordReceipt) error

CheckRecord rejects changed ownership, metadata, identity, or contents.

func CreateChild

func CreateChild(root *os.Root, name string) error

CreateChild exclusively creates one private child directory.

func CreateDirectory

func CreateDirectory(directory string) error

CreateDirectory creates private children through verified directory handles. Existing directories retain their permissions. Every parent must pass the ancestor policy.

func CreateFile

func CreateFile(root *os.Root, name string) (*os.File, error)

CreateFile exclusively creates one private regular file.

func InspectRetainedPublications

func InspectRetainedPublications(ctx context.Context, files map[string]RetainedFile, read RetainedRecordReader, accepts func(parent, destination, prefix string) bool) ([]string, error)

InspectRetainedPublications selects native journals and matching staging files to keep inactive. The caller supplies a complete verified inventory and a destination owner check. It must preserve every selected file in inactive recovery evidence. Destination records remain active and require separate owner validation. This check never promotes staging files. This result does not authorize native cleanup or admission.

func ReadFile

func ReadFile(root *os.Root, name string, limit int64) ([]byte, error)

ReadFile reads one bounded private regular file under an open root. The caller owns directory access policy and closes the root.

func ReadServiceConfiguration

func ReadServiceConfiguration(root *os.Root, name string, limit int64) ([]byte, error)

ReadServiceConfiguration reads a bounded primary input with trusted ownership and protected writes. The caller must explicitly select this policy and validate the selected path's ancestors. This exception does not change private state or dotenv readers and never changes permissions.

func ValidateACL

func ValidateACL(_ *os.Root, _ string, _ fs.FileInfo, _ string) error

ValidateACL checks native grants on the expected file without changing its ACL.

func ValidateAncestors

func ValidateAncestors(path string) error

ValidateAncestors checks existing parents and selected symlink routes without creating paths. Linux and macOS require root or effective-user ownership and protected directory entries. Trusted sticky directories permit shared creation. macOS also checks native ACL grants.

func ValidateMetadata

func ValidateMetadata(info fs.FileInfo, field string) error

ValidateMetadata checks private mode bits and native ownership where supported.

Types

type Directory

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

Directory binds a private directory to its original filesystem identity. Each operation reopens and validates it. No open handle survives the operation.

func ExistingDirectory

func ExistingDirectory(path string) (*Directory, error)

ExistingDirectory binds an existing private directory without creating any paths.

func NewDirectory

func NewDirectory(path string) (*Directory, error)

NewDirectory validates or creates one private directory without changing existing permissions. Linux, macOS, and Windows also validate ancestor ownership and access.

func NewExclusiveDirectory

func NewExclusiveDirectory(path string) (_ *Directory, resultErr error)

NewExclusiveDirectory creates one private directory beneath an existing trusted parent. An existing target causes an existence error. Failures preserve uncertain filesystem entries.

func (*Directory) CheckNoPendingPublications

func (d *Directory) CheckNoPendingPublications(ctx context.Context) error

CheckNoPendingPublications inspects existing receipts without deleting files or creating metadata. Migration must recover pending publications before recording a source inventory.

func (*Directory) Child

func (d *Directory) Child(name string) (*Directory, error)

Child validates or privately creates one child under the bound directory handle.

func (*Directory) CompareAndPublishFileContext

func (d *Directory) CompareAndPublishFileContext(ctx context.Context, name string, previous, data []byte, prefix string) error

CompareAndPublishFileContext requires the previously read bytes to remain current. A nil previous value requires an absent destination. An empty non-nil value requires an empty file.

func (*Directory) CompareAndRemoveFileContext

func (d *Directory) CompareAndRemoveFileContext(ctx context.Context, name string, previous []byte) (bool, error)

CompareAndRemoveFileContext removes a private record whose bytes match previous. It shares the PublishFileContext writer lock and verifies identity and access before removal. A missing file permits an idempotent retry. A nil previous value is invalid. The result reports visible removal even when directory synchronization fails.

func (*Directory) CompletePublication

func (d *Directory) CompletePublication(ctx context.Context, name string, previous, data []byte, prefix string, check func(context.Context) error) error

CompletePublication finishes only an exact record after a host-owned completion check. It refuses foreign journals, changed staging identities, and incomplete staging evidence.

func (*Directory) CopyFile

func (d *Directory) CopyFile(ctx context.Context, name string, output io.Writer, limit int64) (copied int64, resultErr error)

CopyFile streams one private regular file into a caller-owned writer. A failure can follow partial output. The caller must discard that output. The operation verifies native access, file identity, size, and directory identity.

func (*Directory) CreateChild

func (d *Directory) CreateChild(name string) (*Directory, error)

CreateChild exclusively creates one private child directory.

func (*Directory) ExistingChild

func (d *Directory) ExistingChild(name string) (*Directory, error)

ExistingChild binds an existing child without creating any paths.

func (*Directory) InspectPublication

func (d *Directory) InspectPublication(ctx context.Context, name string, previous, data []byte, prefix string) ([]string, error)

InspectPublication checks only one exact publication without changing records. Returned stage names permit a host to inspect its original decision before completion.

func (*Directory) Open

func (d *Directory) Open() (*os.Root, error)

Open verifies directory identity, ownership, and private access before returning a confined root. The caller must close the root.

func (*Directory) PublishFileContext

func (d *Directory) PublishFileContext(ctx context.Context, name string, data []byte, prefix string) error

PublishFileContext publishes a private record with durable staging ownership. Recovery never treats an unrecorded temporary filename as proof of ownership. The method excludes concurrent publishers and recovers their complete abandoned receipts.

func (*Directory) ReadDir

func (d *Directory) ReadDir() ([]fs.DirEntry, error)

ReadDir lists the bound directory without following another directory at its original path.

func (*Directory) ReadFile

func (d *Directory) ReadFile(name string, limit int64) ([]byte, error)

ReadFile reads a bounded private regular file from the bound directory. Missing files return the filesystem absence error.

func (*Directory) RecoverPublications

func (d *Directory) RecoverPublications(ctx context.Context) error

RecoverPublications removes unchanged staging records under an exclusive writer lock. It preserves unknown files and incomplete receipts. A missing metadata directory requires no recovery.

func (*Directory) WriteFile

func (d *Directory) WriteFile(name string, data []byte, prefix string) error

WriteFile publishes complete private bytes through an exclusively created temporary file. Existing destinations must remain private regular files. The caller serializes writers.

func (*Directory) WriteFileContext

func (d *Directory) WriteFileContext(ctx context.Context, name string, data []byte, prefix string) error

WriteFileContext checks cancellation before access and immediately before destination publication. The caller serializes writers. Cancellation before publication preserves the previous destination. PublicationError identifies a visible record with unconfirmed durability.

func (*Directory) WriteFileContextWithSync

func (d *Directory) WriteFileContextWithSync(ctx context.Context, name string, data []byte, prefix string, syncDirectory func(*os.Root) error) error

WriteFileContextWithSync uses the supplied directory synchronizer after publication. A nil synchronizer selects the native filesystem operation. The caller serializes writers.

func (*Directory) WriteFileIfAbsentContext

func (d *Directory) WriteFileIfAbsentContext(ctx context.Context, name string, data []byte, prefix string) error

WriteFileIfAbsentContext publishes private bytes only when no destination exists. The write preserves a competing destination, including one created during publication.

type EntryReceipt

type EntryReceipt = publicationEntry

EntryReceipt binds a directory or regular file to its native identity and access policy. Directory receipts do not bind child contents or modification times.

func CaptureEntry

func CaptureEntry(root *os.Root, name string, directory bool) (EntryReceipt, error)

CaptureEntry records one private direct child under an open root.

type RecordReceipt

type RecordReceipt = publicationRecord

RecordReceipt also binds regular-file contents, size, mode, and modification time.

func CaptureRecord

func CaptureRecord(root *os.Root, name string, limit int64) (RecordReceipt, error)

CaptureRecord records one bounded private regular file under an open root.

type RetainedFile

type RetainedFile struct {
	Size   int64
	SHA256 string
}

RetainedFile identifies bytes in a verified backup. Names are slash-separated relative paths.

type RetainedPublication

type RetainedPublication struct {
	Destination string
	Prefix      string
	Stage       string
	HasRecord   bool
	Size        int64
	SHA256      string
}

RetainedPublication describes staging evidence in a captured publication journal. Native identities remain historical. This result cannot authorize native cleanup.

func InspectRetainedPublication

func InspectRetainedPublication(name string, raw []byte) (RetainedPublication, error)

InspectRetainedPublication validates a captured journal without accessing files. The caller must verify the backup, destination ownership, and any retained staging bytes. It must preserve the evidence and never promote staging bytes from this result.

type RetainedRecordReader

type RetainedRecordReader func(context.Context, string, int64) ([]byte, error)

RetainedRecordReader reads bounded bytes from a verified backup. An absent record must return an error that matches os.ErrNotExist.

Jump to

Keyboard shortcuts

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