package tempdir

import "github.com/containers/storage/internal/tempdir"

Index

Functions

func CleanupTemporaryDirectories

func CleanupTemporaryDirectories(cleanFuncs ...CleanupTempDirFunc) error

CleanupTemporaryDirectories cleans up multiple temporary directories by calling their cleanup functions.

func RecoverStaleDirs

func RecoverStaleDirs(rootDir string) error

RecoverStaleDirs identifies and removes stale temporary directories in the root directory. A directory is considered stale if its lock file can be acquired (indicating no active use). The function attempts to remove both the directory and its lock file. If a directory's lock cannot be acquired, it is considered in use and is skipped.

Types

type CleanupTempDirFunc

type CleanupTempDirFunc func() error

CleanupTempDirFunc is a function type that can be returned by operations which need to perform cleanup actions later.

type TempDir

type TempDir struct {
	RootDir string
	// contains filtered or unexported fields
}

TempDir represents a temporary directory that is created in a specified root directory. It manages the lifecycle of the temporary directory, including creation, locking, and cleanup. Each TempDir instance is associated with a unique subdirectory in the root directory. Warning: The TempDir instance should be used in a single goroutine.

func NewTempDir

func NewTempDir(rootDir string) (*TempDir, error)

NewTempDir creates a TempDir and immediately creates both the temporary directory and its corresponding lock file in the specified RootDir. The RootDir itself will be created if it doesn't exist. Note: The caller MUST ensure that returned TempDir instance is cleaned up with .Cleanup().

func (*TempDir) Cleanup

func (td *TempDir) Cleanup() error

Cleanup removes the temporary directory and releases its instance lock. After cleanup, the TempDir instance becomes inactive and cannot be reused. Subsequent calls to StageDeletion() will fail. Multiple calls to Cleanup() are safe and will not return an error. Callers should typically defer Cleanup() to run after any application-level global locks are released to avoid holding those locks during potentially slow disk I/O.

func (*TempDir) StageDeletion

func (td *TempDir) StageDeletion(path string) error

StageDeletion moves the specified file into the instance's temporary directory. The temporary directory must already exist (created during NewTempDir). Files are renamed with a counter-based prefix (e.g., "0-filename", "1-filename") to ensure uniqueness. Note: 'path' must be on the same filesystem as the TempDir for os.Rename to work. The caller MUST ensure .Cleanup() is called. If the TempDir has been cleaned up, this method will return an error.

Source Files

tempdir.go

Version
v1.59.0 (latest)
Published
Jul 14, 2025
Platform
linux/amd64
Imports
7 packages
Last checked
2 days ago

Tools for package owners.