-
Notifications
You must be signed in to change notification settings - Fork 111
/
atomic_write.go
204 lines (173 loc) · 4.07 KB
/
atomic_write.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package fileutils
import (
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"syscall"
"go.uber.org/multierr"
)
const defaultCreateMode os.FileMode = 0644
type atomicWriteOpts struct {
mode os.FileMode
chmodMode os.FileMode
noSync bool
chown bool
chmod bool
uid int
gid int
}
// AtomicWriteOpt allows setting options for writing a file
type AtomicWriteOpt func(*atomicWriteOpts)
// WithAtomicWriteFileMode specifies the file mode the file must have
// This is affected by the umask
func WithAtomicWriteFileMode(mode os.FileMode) AtomicWriteOpt {
return func(opts *atomicWriteOpts) {
opts.mode = mode
}
}
func WithAtomicWriteChown(uid int, gid int) AtomicWriteOpt {
return func(opts *atomicWriteOpts) {
opts.uid = uid
opts.gid = gid
opts.chown = true
}
}
// WithAtomicWriteChmod specifies the file mode the file must have.
// This is not affected by the umask
func WithAtomicWriteChmod(mode os.FileMode) AtomicWriteOpt {
return func(opts *atomicWriteOpts) {
opts.chmod = true
opts.chmodMode = mode
}
}
// WithAtomicWriterNoSync specifies if sync should be skipped. Skipping
// sync is not safe.
func WithAtomicWriteNoSync(noSync bool) AtomicWriteOpt {
return func(opts *atomicWriteOpts) {
opts.noSync = noSync
}
}
// AtomicWrite reads the content of r into the file at path p atomically.
// This is done by first writing into a temporary file, and then renaming that into
// place.
func AtomicWrite(p string, r io.Reader, opts ...AtomicWriteOpt) error {
return AtomicWriter(p, func(w io.Writer) error {
_, err := io.Copy(w, r)
return err
}, opts...)
}
type atomicWriter struct {
finalPath string
tmpPath string
f *os.File
failed bool
noSync bool
closed bool
}
type WriteCloserFailer interface {
io.WriteCloser
Fail(error) error
}
func NewAtomicWriter(p string, opts ...AtomicWriteOpt) (WriteCloserFailer, error) {
dirname := filepath.Dir(p)
tmpPath := path.Join(dirname, fmt.Sprintf(".tmp.%s", filepath.Base(p)))
writeOpts := atomicWriteOpts{
mode: defaultCreateMode,
}
for _, o := range opts {
o(&writeOpts)
}
f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, writeOpts.mode)
if err != nil {
return nil, err
}
if writeOpts.chown {
err := os.Chown(tmpPath, writeOpts.uid, writeOpts.gid)
if err != nil {
f.Close() // nolint: errcheck
return nil, err
}
}
if writeOpts.chmod {
err := os.Chmod(tmpPath, writeOpts.chmodMode)
if err != nil {
f.Close() // nolint: errcheck
return nil, err
}
}
return &atomicWriter{
finalPath: p,
tmpPath: tmpPath,
f: f,
noSync: writeOpts.noSync,
}, nil
}
func (w *atomicWriter) Write(p []byte) (n int, err error) {
return w.f.Write(p)
}
func (w *atomicWriter) Fail(err error) error {
w.failed = true
return multierr.Combine(
err,
w.Close(),
)
}
func (w *atomicWriter) Close() error {
if w.closed {
return nil
}
w.closed = true
if !w.noSync {
if err := w.f.Sync(); err != nil {
return err
}
}
if err := w.f.Close(); err != nil {
return err
}
if !w.failed {
if err := os.Rename(w.tmpPath, w.finalPath); err != nil {
return err
}
if !w.noSync {
dirname := filepath.Dir(w.finalPath)
if err := syncDir(dirname); err != nil {
return err
}
}
}
return nil
}
// AtomicWriter writes a file atomically into p. It provides a writer to given callback.
// If the callback returns an error, the file is not written.
func AtomicWriter(p string, w func(io.Writer) error, opts ...AtomicWriteOpt) error {
writer, err := NewAtomicWriter(p, opts...)
if err != nil {
return err
}
err = w(writer)
if err != nil {
return writer.Fail(err)
} else {
return writer.Close()
}
}
func syncDir(dirname string) error {
dir, err := os.Open(dirname)
if err != nil {
return err
}
if err := dir.Sync(); err != nil {
// Some filesystems (vboxfs) return EINVAL when trying
// to call sync on a directory. We ignore the error in
// this case since the directory sync doesn't impact
// atomicity (only durability).
if !errors.Is(err, syscall.EINVAL) {
return err
}
}
return dir.Close()
}