-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
136 lines (115 loc) · 3.56 KB
/
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
package safe
import (
"io/ioutil"
"os"
"time"
)
// AltNamePostfix is the extension appended to the name of the intermediate file which is generated by the write method.
const AltNamePostfix = ".1"
// TimestampFormat is the format of the timestamp which is appended to the name of the temporary files.
const TimestampFormat = ".2006-01-02T15-04-05.000000"
// SleepTime until ReadFile retries to read the files if they don't exist.
const SleepTime = 10 * time.Millisecond
// DefaultPerm are the permissions used to create files with WriteFile.
const DefaultPerm = 0700
// RemoveFile deletes the file with the name or $(name).1
// NotExist errors are ignored.
func RemoveFile(name string) error {
alt := name + AltNamePostfix
if err := remove(name); err != nil {
return err
}
return remove(alt)
}
// remove a file but ignore NotExist errors
func remove(name string) error {
err := os.Remove(name)
if os.IsNotExist(err) {
return nil
}
return err
}
// ReadFile reads the contents of the file with the name or $(name).1
// It automatically retries three times if the files don't exist in case they are replaced concurrently.
func ReadFile(name string) ([]byte, error) {
alt := name + AltNamePostfix
var (
data []byte
err error
)
for i := 0; i < 3; i++ {
data, err = ioutil.ReadFile(name)
if !os.IsNotExist(err) {
return data, err
}
data, err = ioutil.ReadFile(alt)
if !os.IsNotExist(err) {
return data, err
}
time.Sleep(SleepTime)
}
return data, err
}
// WriteFile writes data to a file with the provided name.
// If possible, this method should not be executed concurrently for the same file.
// This method also creates a temporary file which is deleted immediately after the write is complete.
// It also creates a file $(name).1 which is used to make the write/update interrupt safe.
func WriteFile(name string, data []byte) error {
t := time.Now()
tmp := name + t.Format(TimestampFormat)
alt := name + AltNamePostfix
err := write(tmp, data)
defer os.Remove(tmp)
if err != nil {
return err
}
return safelink(tmp, alt, name)
}
// safelink creates hard links from the tmpname to the altname and from the altname to the name.
// In case a previous process was interrupted, the altname is first linked to the name.
// This complicated procedure makes sure that even if a process is interrupted before creating the link to the name,
// the the contents of the file are never lost.
func safelink(tmpname string, altname string, name string) error {
// Attempt final link in case a previous process was interrupted before the final link.
if err := link(altname, name); err != nil {
return err
}
// Do alt link from tmp file.
if err := link(tmpname, altname); err != nil {
return err
}
// Do final link.
if err := link(altname, name); err != nil {
return err
}
return nil
}
// link the oldname to the newname.
func link(oldname string, newname string) error {
err := os.Remove(newname)
// Ignore NotExist errors in case this is the first time the link is created.
if err != nil && !os.IsNotExist(err) {
return err
}
err = os.Link(oldname, newname)
if os.IsNotExist(err) || os.IsExist(err) {
// Link was concurrently created or alt link was concurrently deleted or alt link never existed.
return nil
}
return err
}
// write data to a new file described by the name with the provided mode.
func write(name string, data []byte) error {
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
if err := f.Chmod(DefaultPerm); err != nil {
return err
}
if _, err := f.Write(data); err != nil {
return err
}
return f.Sync()
}