forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
220 lines (187 loc) · 4.93 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dep
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"syscall"
"github.com/golang/dep/internal"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
func IsRegular(name string) (bool, error) {
// TODO: lstat?
fi, err := os.Stat(name)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if fi.IsDir() {
return false, errors.Errorf("%q is a directory, should be a file", name)
}
return true, nil
}
func IsDir(name string) (bool, error) {
return internal.IsDir(name)
}
func IsNonEmptyDir(name string) (bool, error) {
isDir, err := IsDir(name)
if !isDir || err != nil {
return isDir, err
}
// Get file descriptor
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
// Query only 1 child. EOF if no children.
_, err = f.Readdirnames(1)
switch err {
case io.EOF:
return false, nil
case nil:
return true, nil
default:
return false, err
}
}
func writeFile(path string, in toml.Marshaler) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
s, err := in.MarshalTOML()
if err != nil {
return err
}
_, err = f.Write(s)
return err
}
// modifyWithString modifies a given file with a new string input.
// This is used to write arbitrary string data to a file, such as
// updating the `Gopkg.toml` file with example data if no deps found
// on init.
func modifyWithString(path, data string) error {
return ioutil.WriteFile(path, []byte(data), 0644)
}
// renameWithFallback attempts to rename a file or directory, but falls back to
// copying in the event of a cross-link device error. If the fallback copy
// succeeds, src is still removed, emulating normal rename behavior.
func renameWithFallback(src, dest string) error {
fi, err := os.Lstat(src)
if err != nil {
return errors.Wrapf(err, "cannot stat %s", src)
}
// Windows cannot use syscall.Rename to rename a directory
if runtime.GOOS == "windows" && fi.IsDir() {
if err := CopyDir(src, dest); err != nil {
return err
}
return errors.Wrapf(os.RemoveAll(src), "cannot delete %s", src)
}
err = os.Rename(src, dest)
if err == nil {
return nil
}
terr, ok := err.(*os.LinkError)
if !ok {
return errors.Wrapf(err, "cannot rename %s to %s", src, dest)
}
// Rename may fail if src and dest are on different devices; fall back to
// copy if we detect that case. syscall.EXDEV is the common name for the
// cross device link error which has varying output text across different
// operating systems.
var cerr error
if terr.Err == syscall.EXDEV {
if fi.IsDir() {
cerr = CopyDir(src, dest)
} else {
cerr = CopyFile(src, dest)
}
} else if runtime.GOOS == "windows" {
// In windows it can drop down to an operating system call that
// returns an operating system error with a different number and
// message. Checking for that as a fall back.
noerr, ok := terr.Err.(syscall.Errno)
// 0x11 (ERROR_NOT_SAME_DEVICE) is the windows error.
// See https://msdn.microsoft.com/en-us/library/cc231199.aspx
if ok && noerr == 0x11 {
cerr = CopyFile(src, dest)
}
} else {
return errors.Wrapf(terr, "link error: cannot rename %s to %s", src, dest)
}
if cerr != nil {
return errors.Wrapf(cerr, "second attemp failed: cannot rename %s to %s", src, dest)
}
return errors.Wrapf(os.RemoveAll(src), "cannot delete %s", src)
}
// CopyDir takes in a directory and copies its contents to the destination.
// It preserves the file mode on files as well.
func CopyDir(src string, dest string) error {
fi, err := os.Lstat(src)
if err != nil {
return errors.Wrapf(err, "cannot stat %s", src)
}
err = os.MkdirAll(dest, fi.Mode())
if err != nil {
return errors.Wrapf(err, "cannot mkdir %s", dest)
}
dir, err := os.Open(src)
if err != nil {
return errors.Wrapf(err, "cannot open %s", src)
}
defer dir.Close()
objects, err := dir.Readdir(-1)
if err != nil {
return errors.Wrapf(err, "cannot read directory %s", dir.Name())
}
for _, obj := range objects {
if obj.Mode()&os.ModeSymlink != 0 {
continue
}
srcfile := filepath.Join(src, obj.Name())
destfile := filepath.Join(dest, obj.Name())
if obj.IsDir() {
err = CopyDir(srcfile, destfile)
if err != nil {
return err
}
continue
}
if err := CopyFile(srcfile, destfile); err != nil {
return err
}
}
return nil
}
// CopyFile copies a file from one place to another with the permission bits
// preserved as well.
func CopyFile(src string, dest string) error {
srcfile, err := os.Open(src)
if err != nil {
return err
}
defer srcfile.Close()
destfile, err := os.Create(dest)
if err != nil {
return err
}
defer destfile.Close()
if _, err := io.Copy(destfile, srcfile); err != nil {
return err
}
srcinfo, err := os.Stat(src)
if err != nil {
return err
}
return os.Chmod(dest, srcinfo.Mode())
}