-
Notifications
You must be signed in to change notification settings - Fork 9
/
copy.go
77 lines (64 loc) · 1.6 KB
/
copy.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fnfs
import (
"context"
"io"
"io/fs"
"path/filepath"
"namespacelabs.dev/foundation/internal/bytestream"
)
func CopyTo(ctx context.Context, dst WriteFS, dstBasePath string, src fs.FS) error {
return VisitFiles(ctx, src, func(path string, contents bytestream.ByteStream, dirent fs.DirEntry) error {
st, err := dirent.Info()
if err != nil {
return err
}
target := filepath.Join(dstBasePath, path)
if mkdir, has := dst.(MkdirFS); has {
dir := filepath.Dir(target)
// Not using as we may not have write permissions then: addExecToRead(st.Mode())
if err := mkdir.MkdirAll(dir, 0700); err != nil {
return err
}
}
return WriteByteStream(ctx, dst, target, contents, st.Mode().Perm())
})
}
func CopyFile(dst WriteFS, dstFile string, src fs.FS, srcFile string) error {
r, err := src.Open(srcFile)
if err != nil {
return err
}
defer r.Close()
st, err := r.Stat()
if err != nil {
return err
}
if mkdir, has := dst.(MkdirFS); has {
dir := filepath.Dir(dstFile)
if err := mkdir.MkdirAll(dir, addExecToRead(st.Mode())); err != nil {
return err
}
}
w, err := dst.OpenWrite(dstFile, st.Mode())
if err != nil {
return err
}
defer w.Close()
_, err = io.Copy(w, r)
return err
}
func addExecToRead(mode fs.FileMode) fs.FileMode {
if (mode & 0004) != 0 {
mode |= 0001
}
if (mode & 0040) != 0 {
mode |= 0010
}
if (mode & 0400) != 0 {
mode |= 0100
}
return mode
}