Skip to content

Commit 7864454

Browse files
committed
pkg/ioutils: move atomic file-writers to a separate (pkg/atomicwriter) package
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 641e2fc commit 7864454

15 files changed

Lines changed: 93 additions & 48 deletions

File tree

container/container.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
"github.com/docker/docker/layer"
3232
libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
3333
"github.com/docker/docker/oci"
34+
"github.com/docker/docker/pkg/atomicwriter"
3435
"github.com/docker/docker/pkg/idtools"
35-
"github.com/docker/docker/pkg/ioutils"
3636
"github.com/docker/docker/restartmanager"
3737
"github.com/docker/docker/volume"
3838
volumemounts "github.com/docker/docker/volume/mounts"
@@ -193,7 +193,7 @@ func (container *Container) toDisk() (*Container, error) {
193193
}
194194

195195
// Save container settings
196-
f, err := ioutils.NewAtomicFileWriter(pth, 0o600)
196+
f, err := atomicwriter.New(pth, 0o600)
197197
if err != nil {
198198
return nil, err
199199
}
@@ -273,7 +273,7 @@ func (container *Container) WriteHostConfig() (*containertypes.HostConfig, error
273273
return nil, err
274274
}
275275

276-
f, err := ioutils.NewAtomicFileWriter(pth, 0o600)
276+
f, err := atomicwriter.New(pth, 0o600)
277277
if err != nil {
278278
return nil, err
279279
}

daemon/cluster/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88

9-
"github.com/docker/docker/pkg/ioutils"
9+
"github.com/docker/docker/pkg/atomicwriter"
1010
)
1111

1212
// convertKVStringsToMap converts ["key=value"] to {"key":"value"}
@@ -44,7 +44,7 @@ func savePersistentState(root string, config nodeStartConfig) error {
4444
if err != nil {
4545
return err
4646
}
47-
return ioutils.AtomicWriteFile(filepath.Join(root, stateFile), dt, 0o600)
47+
return atomicwriter.WriteFile(filepath.Join(root, stateFile), dt, 0o600)
4848
}
4949

5050
func clearPersistentState(root string) error {

daemon/graphdriver/overlay2/overlay.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
"github.com/docker/docker/internal/containerfs"
2424
"github.com/docker/docker/internal/directory"
2525
"github.com/docker/docker/pkg/archive"
26+
"github.com/docker/docker/pkg/atomicwriter"
2627
"github.com/docker/docker/pkg/chrootarchive"
2728
"github.com/docker/docker/pkg/idtools"
28-
"github.com/docker/docker/pkg/ioutils"
2929
"github.com/docker/docker/pkg/parsers"
3030
"github.com/docker/docker/quota"
3131
"github.com/docker/go-units"
@@ -393,7 +393,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
393393
}
394394

395395
// Write link id to link file
396-
if err := ioutils.AtomicWriteFile(path.Join(dir, "link"), []byte(lid), 0o644); err != nil {
396+
if err := atomicwriter.WriteFile(path.Join(dir, "link"), []byte(lid), 0o644); err != nil {
397397
return err
398398
}
399399

@@ -406,7 +406,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
406406
return err
407407
}
408408

409-
if err := ioutils.AtomicWriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0o600); err != nil {
409+
if err := atomicwriter.WriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0o600); err != nil {
410410
return err
411411
}
412412

@@ -415,7 +415,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
415415
return err
416416
}
417417
if lower != "" {
418-
if err := ioutils.AtomicWriteFile(path.Join(dir, lowerFile), []byte(lower), 0o644); err != nil {
418+
if err := atomicwriter.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0o644); err != nil {
419419
return err
420420
}
421421
}

daemon/id.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"os"
55
"path/filepath"
66

7-
"github.com/docker/docker/pkg/ioutils"
7+
"github.com/docker/docker/pkg/atomicwriter"
88
"github.com/google/uuid"
99
"github.com/pkg/errors"
1010
)
@@ -24,7 +24,7 @@ func LoadOrCreateID(root string) (string, error) {
2424
idb, err := os.ReadFile(idPath)
2525
if os.IsNotExist(err) {
2626
id = uuid.New().String()
27-
if err := ioutils.AtomicWriteFile(idPath, []byte(id), os.FileMode(0o600)); err != nil {
27+
if err := atomicwriter.WriteFile(idPath, []byte(id), os.FileMode(0o600)); err != nil {
2828
return "", errors.Wrap(err, "error saving ID file")
2929
}
3030
} else if err != nil {

daemon/runtime_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/docker/docker/daemon/config"
2222
"github.com/docker/docker/errdefs"
2323
"github.com/docker/docker/libcontainerd/shimopts"
24-
"github.com/docker/docker/pkg/ioutils"
24+
"github.com/docker/docker/pkg/atomicwriter"
2525
"github.com/opencontainers/runtime-spec/specs-go/features"
2626
"github.com/pkg/errors"
2727
)
@@ -191,7 +191,7 @@ func wrapRuntime(dir, name, binary string, args []string) (string, error) {
191191
// containers.
192192
suffix := base32Disemvoweled.EncodeToString(sum.Sum(nil))
193193
scriptPath := filepath.Join(dir, name+"."+suffix)
194-
if err := ioutils.AtomicWriteFile(scriptPath, wrapper.Bytes(), 0o700); err != nil {
194+
if err := atomicwriter.WriteFile(scriptPath, wrapper.Bytes(), 0o700); err != nil {
195195
return "", err
196196
}
197197
return scriptPath, nil

distribution/metadata/metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"path/filepath"
66
"sync"
77

8-
"github.com/docker/docker/pkg/ioutils"
8+
"github.com/docker/docker/pkg/atomicwriter"
99
)
1010

1111
// Store implements a K/V store for mapping distribution-related IDs
@@ -60,7 +60,7 @@ func (store *FSMetadataStore) Set(namespace, key string, value []byte) error {
6060
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
6161
return err
6262
}
63-
return ioutils.AtomicWriteFile(path, value, 0o644)
63+
return atomicwriter.WriteFile(path, value, 0o644)
6464
}
6565

6666
// Delete removes data indexed by namespace and key. The data file named after

image/fs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"sync"
99

1010
"github.com/containerd/log"
11-
"github.com/docker/docker/pkg/ioutils"
11+
"github.com/docker/docker/pkg/atomicwriter"
1212
"github.com/opencontainers/go-digest"
1313
"github.com/pkg/errors"
1414
)
@@ -118,7 +118,7 @@ func (s *fs) Set(data []byte) (digest.Digest, error) {
118118
}
119119

120120
dgst := digest.FromBytes(data)
121-
if err := ioutils.AtomicWriteFile(s.contentFile(dgst), data, 0o600); err != nil {
121+
if err := atomicwriter.WriteFile(s.contentFile(dgst), data, 0o600); err != nil {
122122
return "", errors.Wrap(err, "failed to write digest data")
123123
}
124124

@@ -148,7 +148,7 @@ func (s *fs) SetMetadata(dgst digest.Digest, key string, data []byte) error {
148148
if err := os.MkdirAll(baseDir, 0o700); err != nil {
149149
return err
150150
}
151-
return ioutils.AtomicWriteFile(filepath.Join(s.metadataDir(dgst), key), data, 0o600)
151+
return atomicwriter.WriteFile(filepath.Join(s.metadataDir(dgst), key), data, 0o600)
152152
}
153153

154154
// GetMetadata returns metadata for a given digest.

layer/filestore.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/containerd/log"
1414
"github.com/docker/distribution"
15+
"github.com/docker/docker/pkg/atomicwriter"
1516
"github.com/docker/docker/pkg/ioutils"
1617
"github.com/opencontainers/go-digest"
1718
"github.com/pkg/errors"
@@ -29,7 +30,7 @@ type fileMetadataStore struct {
2930

3031
type fileMetadataTransaction struct {
3132
store *fileMetadataStore
32-
ws *ioutils.AtomicWriteSet
33+
ws *atomicwriter.WriteSet
3334
}
3435

3536
// newFSMetadataStore returns an instance of a metadata store
@@ -66,7 +67,7 @@ func (fms *fileMetadataStore) StartTransaction() (*fileMetadataTransaction, erro
6667
if err := os.MkdirAll(tmpDir, 0o755); err != nil {
6768
return nil, err
6869
}
69-
ws, err := ioutils.NewAtomicWriteSet(tmpDir)
70+
ws, err := atomicwriter.NewWriteSet(tmpDir)
7071
if err != nil {
7172
return nil, err
7273
}

libnetwork/internal/resolvconf/resolvconf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"text/template"
3232

3333
"github.com/containerd/log"
34-
"github.com/docker/docker/pkg/ioutils"
34+
"github.com/docker/docker/pkg/atomicwriter"
3535
"github.com/opencontainers/go-digest"
3636
"github.com/pkg/errors"
3737
)
@@ -370,7 +370,7 @@ func (rc *ResolvConf) WriteFile(path, hashPath string, perm os.FileMode) error {
370370

371371
// Write the hash file.
372372
if hashPath != "" {
373-
hashFile, err := ioutils.NewAtomicFileWriter(hashPath, perm)
373+
hashFile, err := atomicwriter.New(hashPath, perm)
374374
if err != nil {
375375
return errSystem{err}
376376
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package ioutils // import "github.com/docker/docker/pkg/ioutils"
1+
package atomicwriter
22

33
import (
44
"io"
55
"os"
66
"path/filepath"
77
)
88

9-
// NewAtomicFileWriter returns WriteCloser so that writing to it writes to a
9+
// New returns a WriteCloser so that writing to it writes to a
1010
// temporary file and closing it atomically changes the temporary file to
1111
// destination path. Writing and closing concurrently is not allowed.
1212
// NOTE: umask is not considered for the file's permissions.
13-
func NewAtomicFileWriter(filename string, perm os.FileMode) (io.WriteCloser, error) {
13+
func New(filename string, perm os.FileMode) (io.WriteCloser, error) {
1414
f, err := os.CreateTemp(filepath.Dir(filename), ".tmp-"+filepath.Base(filename))
1515
if err != nil {
1616
return nil, err
@@ -27,10 +27,10 @@ func NewAtomicFileWriter(filename string, perm os.FileMode) (io.WriteCloser, err
2727
}, nil
2828
}
2929

30-
// AtomicWriteFile atomically writes data to a file named by filename and with the specified permission bits.
30+
// WriteFile atomically writes data to a file named by filename and with the specified permission bits.
3131
// NOTE: umask is not considered for the file's permissions.
32-
func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error {
33-
f, err := NewAtomicFileWriter(filename, perm)
32+
func WriteFile(filename string, data []byte, perm os.FileMode) error {
33+
f, err := New(filename, perm)
3434
if err != nil {
3535
return err
3636
}
@@ -82,32 +82,32 @@ func (w *atomicFileWriter) Close() (retErr error) {
8282
return nil
8383
}
8484

85-
// AtomicWriteSet is used to atomically write a set
85+
// WriteSet is used to atomically write a set
8686
// of files and ensure they are visible at the same time.
8787
// Must be committed to a new directory.
88-
type AtomicWriteSet struct {
88+
type WriteSet struct {
8989
root string
9090
}
9191

92-
// NewAtomicWriteSet creates a new atomic write set to
92+
// NewWriteSet creates a new atomic write set to
9393
// atomically create a set of files. The given directory
9494
// is used as the base directory for storing files before
9595
// commit. If no temporary directory is given the system
9696
// default is used.
97-
func NewAtomicWriteSet(tmpDir string) (*AtomicWriteSet, error) {
97+
func NewWriteSet(tmpDir string) (*WriteSet, error) {
9898
td, err := os.MkdirTemp(tmpDir, "write-set-")
9999
if err != nil {
100100
return nil, err
101101
}
102102

103-
return &AtomicWriteSet{
103+
return &WriteSet{
104104
root: td,
105105
}, nil
106106
}
107107

108108
// WriteFile writes a file to the set, guaranteeing the file
109109
// has been synced.
110-
func (ws *AtomicWriteSet) WriteFile(filename string, data []byte, perm os.FileMode) error {
110+
func (ws *WriteSet) WriteFile(filename string, data []byte, perm os.FileMode) error {
111111
f, err := ws.FileWriter(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
112112
if err != nil {
113113
return err
@@ -136,7 +136,7 @@ func (w syncFileCloser) Close() error {
136136

137137
// FileWriter opens a file writer inside the set. The file
138138
// should be synced and closed before calling commit.
139-
func (ws *AtomicWriteSet) FileWriter(name string, flag int, perm os.FileMode) (io.WriteCloser, error) {
139+
func (ws *WriteSet) FileWriter(name string, flag int, perm os.FileMode) (io.WriteCloser, error) {
140140
f, err := os.OpenFile(filepath.Join(ws.root, name), flag, perm)
141141
if err != nil {
142142
return nil, err
@@ -146,18 +146,18 @@ func (ws *AtomicWriteSet) FileWriter(name string, flag int, perm os.FileMode) (i
146146

147147
// Cancel cancels the set and removes all temporary data
148148
// created in the set.
149-
func (ws *AtomicWriteSet) Cancel() error {
149+
func (ws *WriteSet) Cancel() error {
150150
return os.RemoveAll(ws.root)
151151
}
152152

153153
// Commit moves all created files to the target directory. The
154154
// target directory must not exist and the parent of the target
155155
// directory must exist.
156-
func (ws *AtomicWriteSet) Commit(target string) error {
156+
func (ws *WriteSet) Commit(target string) error {
157157
return os.Rename(ws.root, target)
158158
}
159159

160160
// String returns the location the set is writing to.
161-
func (ws *AtomicWriteSet) String() string {
161+
func (ws *WriteSet) String() string {
162162
return ws.root
163163
}

0 commit comments

Comments
 (0)