fsx

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 4 Imported by: 1 module, 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 fsx holds file system helpers the standard library leaves out.

WriteFile replaces a file atomically, the way a configuration file, a generated script or a cache should be written: a reader sees the old contents or the new ones, never part of either, also when the program is interrupted halfway. A file that holds a credential is written with secret.WriteFile instead, which is the same with the mode fixed to 0600.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrSymlink = errors.New("fsx: refusing to replace a symbolic link")

ErrSymlink reports a symbolic link at the path WriteFile was asked to write, which it refuses to replace.

Functions

func WriteFile

func WriteFile(path string, data []byte, perm fs.FileMode) error

WriteFile writes data to path and replaces an existing file atomically: it writes a temporary file in the same directory, syncs it to disk and renames it into place. A reader sees the old contents or the new ones, never part of either, and an interrupted write leaves the old file as it was.

The new file gets mode perm, as it is given: unlike os.WriteFile, the umask does not apply, and a file that already exists does not keep its mode. The mode is set before any data reaches the file, so data written with mode 0600 is never readable by others. On Windows only the read-only bit, set when perm has no write permission for the owner, can be set.

The directory must exist. A symbolic link at path is refused with an error wrapping ErrSymlink, as secret.WriteFile refuses one: the rename would replace the link instead of the file it points to, which is rarely what whoever made the link wanted.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/mittwald/cli-kit/fsx"
)

func main() {
	dir, err := os.MkdirTemp("", "example")
	if err != nil {
		panic(err)
	}
	defer os.RemoveAll(dir)

	path := filepath.Join(dir, "config.yaml")
	if err := fsx.WriteFile(path, []byte("theme: dark\n"), 0o644); err != nil {
		panic(err)
	}
	data, _ := os.ReadFile(path)
	fmt.Print(string(data))
}
Output:
theme: dark

Types

This section is empty.

Jump to

Keyboard shortcuts

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