fsutil

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: AGPL-3.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 fsutil holds small filesystem helpers shared across sqi.

AppleDouble files

macOS stores a file's extended attributes and resource fork in a companion file named "._<original>" whenever the underlying filesystem cannot hold them natively — exFAT, FAT32, SMB and NFS shares, and most USB media. A checkout on such a volume therefore sprouts a "._foo.yaml" beside every "foo.yaml".

Those companions are binary, carry no content sqi cares about, and — because they keep the original extension — are matched by every "*.yaml" glob and every `strings.HasSuffix(name, ".yaml")` filter. Code that scans a directory then tries to parse one and fails on a control character, reporting a corrupt file that does not exist. The guard lives here so every subsystem shares it.

Index

Constants

View Source
const SecretFileMode os.FileMode = 0o600

SecretFileMode is the POSIX mode every secret this package writes carries: readable and writable by the owner, nothing for group or other.

Variables

This section is empty.

Functions

func CreateSecretExclusive added in v0.4.0

func CreateSecretExclusive(path string) error

CreateSecretExclusive creates an empty file at path, restricted to the writing user as WriteSecret restricts one, for a caller whose content is then written by something else into the file it names (a database backup written by SQLite's VACUUM INTO or by pg_dump). The restriction is in place before the first byte lands. It fails with an error matching fs.ErrExist when path already exists, so it never replaces, or widens, a file.

func FilterDirEntries

func FilterDirEntries(entries []fs.DirEntry) []fs.DirEntry

FilterDirEntries returns entries with AppleDouble companions removed, preserving order. It is the guard for os.ReadDir and fs.ReadDir results.

func FilterPaths

func FilterPaths(paths []string) []string

FilterPaths returns paths with AppleDouble companions removed, preserving order. It is the guard for filepath.Glob results, which match "._x.yaml" against "*.yaml" like any other file.

func Glob

func Glob(pattern string) ([]string, error)

Glob is filepath.Glob with AppleDouble companions removed. A "*.yaml" pattern matches "._foo.yaml" like any other file, so every caller globbing for data files wants this rather than the stdlib version.

func HideAppleDouble

func HideAppleDouble(inner fs.FS) fs.FS

HideAppleDouble wraps an fs.FS so AppleDouble companions are invisible to both directory listings and Open calls. Use it when a package hands an fs.FS to code it does not control.

func IsAppleDouble

func IsAppleDouble(name string) bool

IsAppleDouble reports whether a path's base name marks it as a macOS AppleDouble companion file. It accepts a bare name or a full path.

Both separators are recognized on every platform: filepath.Base only splits on the host's separator, so a Windows-style path checked on Linux would otherwise be treated as one long base name and slip through.

func IsRestricted added in v0.4.0

func IsRestricted(path string) (bool, error)

IsRestricted reports whether path is readable only by its owner and the platform's administrative principals, the property WriteSecret establishes.

It is exported because it is the only portable way for a test to assert that property: `Mode().Perm() == 0o600` says nothing about a file's protection on Windows. A caller may also use it as a runtime self-check on operator-provisioned key material sqi did not write itself.

func MkdirSecret added in v0.4.0

func MkdirSecret(dir string) error

MkdirSecret creates dir and every missing parent, restricting dir itself to the creating user the same way WriteSecret restricts a file.

Only the leaf is restricted. Intermediate parents are ordinary directories: a secret's confidentiality comes from its own DACL (or mode), never from an ancestor being unreadable, and tightening a shared parent an operator already created is the widening/narrowing anti-pattern this codebase avoids elsewhere. An already-existing dir is left as the operator configured it: os.MkdirAll does not revisit an existing directory's mode, and this does not revisit its ACL.

func RestrictSecret added in v0.4.0

func RestrictSecret(f *os.File) error

RestrictSecret restricts an already-open file to the writing user, for callers that must create the descriptor themselves. Call it immediately after the create and before the first write, for the ordering reason in WriteSecret's doc. On Windows the handle must carry WRITE_DAC (see restrictSecret).

func WriteSecret added in v0.4.0

func WriteSecret(path string, data []byte) error

WriteSecret writes data to path so that only the writing user, plus the platform's administrative principals, can read it back.

Why not os.WriteFile(path, data, 0o600)

sqi writes its private key material through this: the farm CA key and the server/client TLS keys (internal/certgen, behind `sqi-server tls init` and `tls issue`) and the NATS broker nkey seed (internal/brokerauth).

On Windows a POSIX mode is close to a no-op. os.Chmod maps only to the read-only attribute (it cannot deny read access to anyone) and os.MkdirAll ignores its mode argument. A file created with 0o600 therefore lands with whatever DACL it inherits from its parent, which for a data directory under %ProgramData% or a user profile is often readable by every local account.

What "restricted" means per platform

  • POSIX: mode 0600.
  • Windows: a protected DACL (inheritance stripped) granting full control to exactly three trustees: the writing user, LocalSystem, and BUILTIN\Administrators, the closest available analog of 0600. SYSTEM and Administrators are included for the same reason internal/worker/isolation includes them: a service must be able to read what it wrote, and an operator must not be locked out of a key on their own machine. On POSIX, root can read a 0600 file too.

The DACL is applied to the handle before any secret byte is written, so the file is never observable at an inherited, permissive ACL with key material already in it. That ordering is why this cannot be "os.WriteFile then fix up the ACL".

An existing file at path is replaced. WriteSecret does not create parent directories: call MkdirSecret first when the directory may be absent, so the directory's own restriction is an explicit decision rather than a side effect.

Types

This section is empty.

Directories

Path Synopsis
Package fsutiltest makes filesystem links for tests.
Package fsutiltest makes filesystem links for tests.

Jump to

Keyboard shortcuts

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