forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
198 lines (161 loc) · 4.28 KB
/
fs.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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vfs
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sync"
"github.com/golang/glog"
"k8s.io/kops/pkg/try"
"k8s.io/kops/util/pkg/hashing"
)
type FSPath struct {
location string
}
var _ Path = &FSPath{}
var _ HasHash = &FSPath{}
func NewFSPath(location string) *FSPath {
return &FSPath{location: location}
}
func (p *FSPath) Join(relativePath ...string) Path {
args := []string{p.location}
args = append(args, relativePath...)
joined := path.Join(args...)
return &FSPath{location: joined}
}
func (p *FSPath) WriteFile(data io.ReadSeeker, acl ACL) error {
dir := path.Dir(p.location)
err := os.MkdirAll(dir, 0755)
if err != nil {
return fmt.Errorf("error creating directories %q: %v", dir, err)
}
f, err := ioutil.TempFile(dir, "tmp")
if err != nil {
return fmt.Errorf("error creating temp file in %q: %v", dir, err)
}
// Note from here on in we have to close f and delete or rename the temp file
tempfile := f.Name()
_, err = io.Copy(f, data)
if closeErr := f.Close(); err == nil {
err = closeErr
}
if err == nil {
err = os.Rename(tempfile, p.location)
if err != nil {
err = fmt.Errorf("error during file write of %q: rename failed: %v", p.location, err)
}
}
if err == nil {
return nil
}
// Something went wrong; try to remove the temp file
if removeErr := os.Remove(tempfile); removeErr != nil {
glog.Warningf("unable to remove temp file %q: %v", tempfile, removeErr)
}
return err
}
// To prevent concurrent creates on the same file while maintaining atomicity of writes,
// we take a process-wide lock during the operation.
// Not a great approach, but fine for a single process (with low concurrency)
// TODO: should we take a file lock or equivalent here? Can we use RENAME_NOREPLACE ?
var createFileLock sync.Mutex
func (p *FSPath) CreateFile(data io.ReadSeeker, acl ACL) error {
createFileLock.Lock()
defer createFileLock.Unlock()
// Check if exists
_, err := os.Stat(p.location)
if err == nil {
return os.ErrExist
}
if !os.IsNotExist(err) {
return err
}
return p.WriteFile(data, acl)
}
// ReadFile implements Path::ReadFile
func (p *FSPath) ReadFile() ([]byte, error) {
return ioutil.ReadFile(p.location)
}
// WriteTo implements io.WriterTo
func (p *FSPath) WriteTo(out io.Writer) (int64, error) {
f, err := os.Open(p.location)
if err != nil {
return 0, err
}
defer try.CloseFile(f)
return io.Copy(out, f)
}
func (p *FSPath) ReadDir() ([]Path, error) {
files, err := ioutil.ReadDir(p.location)
if err != nil {
if os.IsNotExist(err) {
return nil, err
}
return nil, err
}
var paths []Path
for _, f := range files {
paths = append(paths, NewFSPath(path.Join(p.location, f.Name())))
}
return paths, nil
}
func (p *FSPath) ReadTree() ([]Path, error) {
var paths []Path
err := readTree(p.location, &paths)
if err != nil {
return nil, err
}
return paths, nil
}
// readTree recursively finds files and adds them to dest
// It excludes directories.
func readTree(base string, dest *[]Path) error {
files, err := ioutil.ReadDir(base)
if err != nil {
return err
}
for _, f := range files {
p := path.Join(base, f.Name())
if f.IsDir() {
err = readTree(p, dest)
if err != nil {
return err
}
} else {
*dest = append(*dest, NewFSPath(p))
}
}
return nil
}
func (p *FSPath) Base() string {
return path.Base(p.location)
}
func (p *FSPath) Path() string {
return p.location
}
func (p *FSPath) String() string {
return p.Path()
}
func (p *FSPath) Remove() error {
return os.Remove(p.location)
}
func (p *FSPath) PreferredHash() (*hashing.Hash, error) {
return p.Hash(hashing.HashAlgorithmSHA256)
}
func (p *FSPath) Hash(a hashing.HashAlgorithm) (*hashing.Hash, error) {
glog.V(2).Infof("hashing file %q", p.location)
return a.HashFile(p.location)
}