-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfile.go
167 lines (156 loc) · 3.47 KB
/
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
package atomic
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/klauspost/compress/zstd"
gzip "github.com/klauspost/pgzip"
)
// File behaves like os.File, but does an atomic rename operation at Close.
type File struct {
*os.File
path string
}
// New creates a new temporary file that will replace the file at the given
// path when Closed.
func New(path string, mode os.FileMode) (*File, error) {
f, err := ioutil.TempFile(filepath.Dir(path), fmt.Sprintf("%s-tmp-", filepath.Base(path)))
if err != nil {
return nil, err
}
if err := os.Chmod(f.Name(), mode); err != nil {
f.Close()
os.Remove(f.Name())
return nil, err
}
return &File{File: f, path: path}, nil
}
// Close the file replacing the configured file.
func (f *File) Close() error {
if err := f.File.Close(); err != nil {
os.Remove(f.File.Name())
return err
}
if err := os.Rename(f.Name(), f.path); err != nil {
return err
}
return nil
}
// Abort closes the file and removes it instead of replacing the configured
// file. This is useful if after starting to write to the file you decide you
// don't want it anymore.
func (f *File) Abort() error {
if err := f.File.Close(); err != nil {
os.Remove(f.Name())
return err
}
if err := os.Remove(f.Name()); err != nil {
return err
}
return nil
}
func CompressType(filename string, ty string) (string, error) {
tf, err := ioutil.TempFile("", "span-atomic-")
if err != nil {
return "", err
}
defer tf.Close()
var w io.WriteCloser
switch {
case ty == "zstd":
w, err = zstd.NewWriter(tf)
if err != nil {
return "", err
}
default:
w = gzip.NewWriter(tf)
}
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
if _, err := io.Copy(w, f); err != nil {
return "", err
}
if err := w.Close(); err != nil {
return "", err
}
return tf.Name(), nil
}
// Compress and return path to compressed file.
func Compress(filename string) (string, error) {
return CompressType(filename, "gzip")
}
// WriteFile writes the data to a temp file and atomically move if everything else succeeds.
func WriteFile(filename string, data []byte, perm os.FileMode) error {
dir, name := path.Split(filename)
f, err := ioutil.TempFile(dir, name)
if err != nil {
return err
}
_, err = f.Write(data)
if err == nil {
err = f.Sync()
}
if closeErr := f.Close(); err == nil {
err = closeErr
}
if permErr := os.Chmod(f.Name(), perm); err == nil {
err = permErr
}
if err == nil {
err = os.Rename(f.Name(), filename)
}
// Any err should result in full cleanup.
if err != nil {
os.Remove(f.Name())
}
return err
}
// Move moves a file from a source path to a destination path
// This must be used across the codebase for compatibility with Docker volumes
// and Golang (fixes Invalid cross-device link when using os.Rename)
func Move(src, dst string) error {
sabs, err := filepath.Abs(src)
if err != nil {
return err
}
dabs, err := filepath.Abs(dst)
if err != nil {
return err
}
if sabs == dabs {
return nil
}
inf, err := os.Open(src)
if err != nil {
return err
}
defer inf.Close()
dstDir := filepath.Dir(dst)
if !exists(dstDir) {
err = os.MkdirAll(dstDir, 0775)
if err != nil {
return err
}
}
of, err := os.Create(dst)
if err != nil {
return err
}
defer of.Close()
_, err = io.Copy(of, inf)
if err != nil {
return err
}
return os.Remove(src)
}
// exists returns whether or not a file or path exists
func exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}