This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_file.go
209 lines (181 loc) · 4.14 KB
/
atomic_file.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
205
206
207
208
209
package atomicfile
import (
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
)
// Some references:
// - https://www.slideshare.net/nan1nan1/eat-my-data
// - https://lwn.net/Articles/457667/
var (
// ErrCancelled is returned by calls subsequent to Cancel()
ErrCancelled = errors.New("cancelled")
// ensure we implement desired interface
_ io.WriteCloser = &File{}
)
// File allows writing to a file atomically
// i.e. if the while file is not written successfully, we make sure
// to clean things up
type File struct {
dstPath string
dir string
tmpFile *os.File
err error
tmpPath string // for debugging
}
// New creates new File
func New(path string) (*File, error) {
dir, fName := filepath.Split(path)
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
if fName == "" {
return nil, &os.PathError{Op: "open", Path: path, Err: os.ErrInvalid}
}
tmpFile, err := ioutil.TempFile(dir, fName)
if err != nil {
return nil, err
}
return &File{
dstPath: path,
dir: dir,
tmpFile: tmpFile,
tmpPath: tmpFile.Name(),
}, nil
}
func (f *File) handleError(err error) error {
if err == nil {
return nil
}
// remember the first errro
if f.err == nil {
f.err = err
}
// cleanup i.e. delete temporary file
_ = f.Close()
return err
}
// Write writes data to a file
func (f *File) Write(d []byte) (int, error) {
if f.err != nil {
return 0, f.err
}
n, err := f.tmpFile.Write(d)
return n, f.handleError(err)
}
func (f *File) SetWriteDeadline(t time.Time) error {
if f.err != nil {
return f.err
}
err := f.tmpFile.SetWriteDeadline(t)
return f.handleError(err)
}
func (f *File) Sync() error {
if f.err != nil {
return f.err
}
err := f.tmpFile.Sync()
return f.handleError(err)
}
func (f *File) Truncate(size int64) error {
if f.err != nil {
return f.err
}
err := f.tmpFile.Truncate(size)
return f.handleError(err)
}
func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
if f.err != nil {
return 0, f.err
}
ret, err = f.tmpFile.Seek(offset, whence)
return ret, f.handleError(err)
}
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
if f.err != nil {
return 0, f.err
}
n, err = f.tmpFile.WriteAt(b, off)
return n, f.handleError(err)
}
func (f *File) WriteString(s string) (n int, err error) {
if f.err != nil {
return 0, f.err
}
n, err = f.tmpFile.WriteString(s)
return n, f.handleError(err)
}
func (f *File) alreadyClosed() bool {
return f.tmpFile == nil
}
// RemoveIfNotClosed removes the temp file if we didn't Close
// the file yet. Destination file will not be created.
// Use it with defer to ensure cleanup in case of a panic on the
// same goroutine that happens before Close.
// RemoveIfNotClosed after Close is a no-op.
func (f *File) RemoveIfNotClosed() {
if f == nil {
return
}
if f.alreadyClosed() {
// a no-op if already closed
return
}
f.err = ErrCancelled
_ = f.Close()
}
// Close closes the file. Can be called multiple times to make it
// easier to use via defer
func (f *File) Close() error {
if f.alreadyClosed() {
// return the first error we encountered
return f.err
}
tmpFile := f.tmpFile
f.tmpFile = nil
// cleanup things (delete temporary files) if:
// - there was an error in Write()
// - thre was an error in Sync()
// - Close() failed
// - rename to destination failed
// https://www.joeshaw.org/dont-defer-close-on-writable-files/
errSync := tmpFile.Sync()
errClose := tmpFile.Close()
// delete the temporary file in case of errors
didRename := false
defer func() {
if !didRename {
// ignoring error on this one
_ = os.Remove(f.tmpPath)
}
}()
// if there was an error during write, return that error
if f.err != nil {
return f.err
}
err := errSync
if err == nil {
err = errClose
}
if err == nil {
// this will over-write dstPath (if it exists)
err = os.Rename(f.tmpPath, f.dstPath)
didRename = (err == nil)
// for extra protection against crashes elsewhere,
// sync directory after rename
fdir, _ := os.Open(f.dir)
if fdir != nil {
// ignore errors as those are a nice have, not must have
_ = fdir.Sync()
_ = fdir.Close()
}
}
if f.err == nil {
f.err = err
}
return f.err
}