Repository: moby/moby
File: pkg/ioutils/fswriters_deprecated.go (Go)

Code before:
package ioutils

import (
	"io"
	"os"

	"github.com/docker/docker/pkg/fswriter"
)

// NewAtomicFileWriter returns WriteCloser so that writing to it writes to a
// temporary file and closing it atomically changes the temporary file to
// destination path. Writing and closing concurrently is not allowed.
// NOTE: umask is not considered for the file's permissions.
//
// Deprecated: use [fswriter.NewAtomicFileWriter] instead.
func NewAtomicFileWriter(filename string, perm os.FileMode) (io.WriteCloser, error) {
	return fswriter.NewAtomicFileWriter(filename, perm)
}

// AtomicWriteFile atomically writes data to a file named by filename and with the specified permission bits.
// NOTE: umask is not considered for the file's permissions.
//
// Deprecated: use [fswriter.AtomicWriteFile] instead.
func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error {
	return fswriter.AtomicWriteFile(filename, data, perm)
}

// AtomicWriteSet is used to atomically write a set
// of files and ensure they are visible at the same time.
// Must be committed to a new directory.
//
// Deprecated: use [fswriter.AtomicWriteSet] instead.
type AtomicWriteSet = fswriter.AtomicWriteSet

// NewAtomicWriteSet creates a new atomic write set to
// atomically create a set of files. The given directory
// is used as the base directory for storing files before
// commit. If no temporary directory is given the system
// default is used.
//
// Deprecated: use [fswriter.NewAtomicWriteSet] instead.
func NewAtomicWriteSet(tmpDir string) (*fswriter.AtomicWriteSet, error) {
	return fswriter.NewAtomicWriteSet(tmpDir)
}


Review comment (suggestion):
Could use some feedback / thoughts  on this;

I was originally (some time ago) considering to move these to a `atomic` package, for a slightly nicer interface;

- `atomic.NewFileWriter` or even `atomic.NewWriter`
- `atomic.WriteFile`
- `atomic.NewWriteSet`

But I wasn't sure if that would be too easily confused / conflict with Go's `atomic` packages.

FWIW; there's some existing projects handling atomic write operations as well that we could even consider adopting;

- https://github.com/tailscale/tailscale/blob/ff095606ccff083160eb01a8a4cc062cacfe1a33/atomicfile/atomicfile.go
- https://github.com/natefinch/atomic
- (mostly Linux, not Windows); https://github.com/google/renameio
- (forked from k8s, but using a temp-directory, which may not work well in all cases if that dir is on a different filesystem) https://github.com/gomodules/atomic-writer



Code after:
package ioutils

import (
	"io"
	"os"

	"github.com/docker/docker/pkg/atomicwriter"
)

// NewAtomicFileWriter returns WriteCloser so that writing to it writes to a
// temporary file and closing it atomically changes the temporary file to
// destination path. Writing and closing concurrently is not allowed.
// NOTE: umask is not considered for the file's permissions.
//
// Deprecated: use [atomicwriter.New] instead.
func NewAtomicFileWriter(filename string, perm os.FileMode) (io.WriteCloser, error) {
	return atomicwriter.New(filename, perm)
}

// AtomicWriteFile atomically writes data to a file named by filename and with the specified permission bits.
// NOTE: umask is not considered for the file's permissions.
//
// Deprecated: use [atomicwriter.WriteFile] instead.
func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error {
	return atomicwriter.WriteFile(filename, data, perm)
}

// AtomicWriteSet is used to atomically write a set
// of files and ensure they are visible at the same time.
// Must be committed to a new directory.
//
// Deprecated: use [atomicwriter.WriteSet] instead.
type AtomicWriteSet = atomicwriter.WriteSet

// NewAtomicWriteSet creates a new atomic write set to
// atomically create a set of files. The given directory
// is used as the base directory for storing files before
// commit. If no temporary directory is given the system
// default is used.
//
// Deprecated: use [atomicwriter.NewWriteSet] instead.
func NewAtomicWriteSet(tmpDir string) (*atomicwriter.WriteSet, error) {
	return atomicwriter.NewWriteSet(tmpDir)
}