-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcopygo122_test.go
62 lines (53 loc) · 1.4 KB
/
copygo122_test.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
//go:build go1.22 && !go1.23
// Copyright 2016 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 tagliatelle_test
import (
"io"
"io/fs"
"os"
"path/filepath"
)
// CopyFS IMPORTANT:
// This is a modified copy of os.CopyFS.
// os.CopyFS is available in go1.23.
// TODO(ldez) remove this file when bump to go1.23.
//
//nolint:all // code from Go.
func CopyFS(dir string, fsys fs.FS) error {
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
fpath := path
newPath := filepath.Join(dir, fpath)
if d.IsDir() {
return os.MkdirAll(newPath, 0777)
}
// TODO(panjf2000): handle symlinks with the help of fs.ReadLinkFS
// once https://go.dev/issue/49580 is done.
// we also need filepathlite.IsLocal from https://go.dev/cl/564295.
if !d.Type().IsRegular() {
return &os.PathError{Op: "CopyFS", Path: path, Err: os.ErrInvalid}
}
r, err := fsys.Open(path)
if err != nil {
return err
}
defer r.Close()
info, err := r.Stat()
if err != nil {
return err
}
w, err := os.OpenFile(newPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0666|info.Mode()&0777)
if err != nil {
return err
}
if _, err := io.Copy(w, r); err != nil {
w.Close()
return &os.PathError{Op: "Copy", Path: newPath, Err: err}
}
return w.Close()
})
}