savefile

package
v0.4.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 8 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

Overview

Package savefile provides core-free atomic file staging, backup rotation, directory syncing, stale temporary-file cleanup, and portable filename validation shared by pkg/save and pkg/online/gameserver.

Index

Constants

View Source
const StaleTempAge = time.Hour

StaleTempAge is how old a .save-*.tmp file must be before SweepStaleTemps treats it as left behind by an interrupted write rather than one in progress.

Variables

This section is empty.

Functions

func DirSyncUnsupported

func DirSyncUnsupported(err error) bool

DirSyncUnsupported reports whether err from syncing a directory means the file system does not support it rather than that it failed.

func SweepStaleTemps

func SweepStaleTemps(dir string)

SweepStaleTemps removes .save-*.tmp staging files older than StaleTempAge. Errors are ignored: a file that cannot be removed now is tried again later.

func SyncDir

func SyncDir(dir string) error

SyncDir flushes directory metadata after a rename or removal so the change survives a crash. Windows cannot sync a directory handle and needs no separate sync for renames, so it is skipped there; anywhere else a failure is returned, except that a file system that cannot sync a directory at all (EINVAL or ENOTSUP, as on some network and FUSE mounts) is treated as having nothing to sync.

func ValidateName

func ValidateName(name string) error

ValidateName rejects empty names, overly long names, path separators, traversal sequences, leading or trailing dots or spaces, control characters, and Windows-reserved filenames.

func WriteAtomic

func WriteAtomic(dir, target string, data []byte) error

WriteAtomic writes data to target inside dir by staging a synced temporary file, renaming it over target, and syncing dir.

func WriteStaged

func WriteStaged(dir, primary string, data []byte, beforeRename func() error) error

WriteStaged stages data into a synced temporary file in dir, runs beforeRename (if non-nil) before touching primary, atomically renames the temporary file over primary, and syncs dir. If staging, beforeRename, or rename fails, the staged temporary file is removed and primary is unchanged.

func WriteTemp

func WriteTemp(dir string, data []byte) (string, error)

WriteTemp writes data to a new temporary file in dir, syncs it to disk and closes it, returning its path.

func WriteWithBackup

func WriteWithBackup(dir, primary, backup string, data []byte, valid func([]byte) bool) error

WriteWithBackup stages data in dir, preserves any existing primary that passes valid (or any existing primary when valid is nil) in backup via WriteAtomic, renames the staged file over primary, and syncs dir.

Types

This section is empty.

Jump to

Keyboard shortcuts

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