tempfile

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package tempfile stages output in the destination directory and publishes it with an atomic rename. Failed or canceled work removes the staged file, leaving the final path untouched.

The creator owns cleanup. Defer the returned value's Discard method immediately after New or NewExternal; Discard is a no-op after Commit succeeds.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Scratch

func Scratch(dir, pattern string) (f *os.File, cleanup func() error, err error)

Scratch creates an unnamed temporary file in dir (or the OS temp dir if dir is "") and returns it with a cleanup func that closes and removes it. Use it for staging input that has no final destination (e.g. a downloaded source staged for ffmpeg). The cleanup is idempotent.

Types

type External

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

External stages output written by another process, such as ffmpeg. It reserves a temp path in the destination directory, then renames that path into place on Commit. Unlike File, External does not keep the file open for writing.

Reserve a name with NewExternal, pass Path to the process, then call Commit to publish or Discard to remove the temp.

func NewExternal

func NewExternal(finalPath string) (*External, error)

NewExternal reserves a temp path next to finalPath for an external writer.

The temp name preserves finalPath's extension (for example, out.flac.*.flac), because tools like ffmpeg infer the output container from the filename extension.

func (*External) Commit

func (e *External) Commit() error

Commit atomically renames the temp path to the final path. After a successful Commit, Discard is a no-op.

func (*External) Discard

func (e *External) Discard() error

Discard removes the temp path. It is safe to call multiple times and is a no-op after a successful Commit.

func (*External) Final

func (e *External) Final() string

Final returns the destination path (valid after Commit).

func (*External) Path

func (e *External) Path() string

Path returns the temp path reserved for the external writer.

type File

type File struct {
	*os.File
	// contains filtered or unexported fields
}

File is a staged output: write to it, then Commit (atomic rename to the final path) or Discard (remove the temp). The temp is created in the final path's directory so the rename stays on one filesystem and is therefore atomic.

func New

func New(finalPath string) (*File, error)

New creates a staging file for eventual atomic rename to finalPath. The returned *File embeds *os.File, so callers write to it directly.

func (*File) Commit

func (f *File) Commit() error

Commit flushes and closes the temp, then atomically renames it to the final path. After a successful Commit, Discard is a no-op.

func (*File) Discard

func (f *File) Discard() error

Discard closes and removes the temp file. It is safe to call multiple times and is a no-op after a successful Commit.

func (*File) Path

func (f *File) Path() string

Path returns the final destination path (valid only after Commit).

Jump to

Keyboard shortcuts

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