This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
copy_op.go
174 lines (156 loc) · 4.95 KB
/
copy_op.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// 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 snapshot
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/uber/makisu/lib/fileio"
"github.com/uber/makisu/lib/pathutils"
"github.com/uber/makisu/lib/utils"
)
// CopyOperation defines a copy operation that occurred to generate a layer from.
type CopyOperation struct {
srcRoot string
srcs []string
dst string
uid int
gid int
chown bool
preserveOwner bool
blacklist []string
// Indicates if the copy op is used for copying from previous stages.
internal bool
}
// NewCopyOperation initializes and validates a CopyOperation. Use "internal" to
// specify if the copy op is used for copying from previous stages.
func NewCopyOperation(
srcs []string, srcRoot, workDir, dst, chownStr string,
blacklist []string, internal, preserveOwner bool) (*CopyOperation, error) {
if err := checkCopyParams(srcs, workDir, dst); err != nil {
return nil, fmt.Errorf("check copy param: %s", err)
}
chown := chownStr != ""
if chown && preserveOwner {
return nil, fmt.Errorf("both chown and archive are true")
}
uid, gid, err := utils.ResolveChown(chownStr)
if err != nil {
return nil, fmt.Errorf("resolve chown str: %s", err)
}
relSources := make([]string, len(srcs))
for k, src := range srcs {
relSources[k] = pathutils.RelPath(src)
}
dst = resolveDestination(workDir, dst)
return &CopyOperation{
srcRoot: srcRoot,
srcs: relSources,
dst: dst,
uid: uid,
gid: gid,
chown: chown,
preserveOwner: preserveOwner,
blacklist: blacklist,
internal: internal,
}, nil
}
// Execute performs the actual copying of files specified by the CopyOperation.
func (c *CopyOperation) Execute() error {
var err error
for _, src := range c.srcs {
src, err = evalSymlinks(src, c.srcRoot)
if err != nil {
return fmt.Errorf("eval symlinks for %s: %s", src, err)
}
src = filepath.Join(c.srcRoot, src)
fi, err := os.Lstat(src)
if err != nil {
return fmt.Errorf("lstat %s: %s", src, err)
}
blacklist := c.blacklist
if c.internal {
// Copying checkpointed files from sandbox dir, and there is no need to
// blacklist any path, since they would have been filtered out by checkpoint.
blacklist = []string{}
}
var copier *fileio.Copier
if c.chown {
// COPY --chown.
// Owner decided by --chown.
copier = fileio.NewCopier(blacklist,
fileio.WithDstDirOwner(c.uid, c.gid, false),
fileio.WithDstFileAndChildrenOwner(c.uid, c.gid, true),
)
} else if !c.internal {
// Copying from context, owner should be root if no --chown.
// Whether --archive is provided doesn't matter in this case.
copier = fileio.NewCopier(blacklist,
fileio.WithDstDirOwner(0, 0, false),
fileio.WithDstFileAndChildrenOwner(0, 0, true),
)
} else if c.preserveOwner {
// COPY --from --archive.
stat := utils.FileInfoStat(fi)
copier = fileio.NewCopier(blacklist,
fileio.WithDstDirOwner(int(stat.Uid), int(stat.Gid), false))
} else {
// COPY --from.
copier = fileio.NewCopier(blacklist)
}
if fi.IsDir() {
// Dir to dir
if err := copier.CopyDir(src, c.dst); err != nil {
return fmt.Errorf("copy dir %s to dir %s: %s", src, c.dst, err)
}
} else if isDirFormat(c.dst) {
// File to dir
targetFilePath := filepath.Join(c.dst, filepath.Base(src))
if err := copier.CopyFile(src, targetFilePath); err != nil {
return fmt.Errorf("copy file %s to dir %s: %s", src, targetFilePath, err)
}
} else {
// File to file
if err := copier.CopyFile(src, c.dst); err != nil {
return fmt.Errorf("copy file %s to file %s: %s", src, c.dst, err)
}
}
}
return nil
}
func resolveDestination(workDir, dst string) string {
if filepath.IsAbs(dst) {
return dst
}
absDst := filepath.Join(workDir, dst)
// Preserve trailing "/".
if isDirFormat(dst) {
absDst += "/"
}
return absDst
}
func checkCopyParams(srcs []string, workDir, dst string) error {
if len(srcs) == 0 {
return fmt.Errorf("srcs cannot be empty")
} else if len(srcs) > 1 && !isDirFormat(dst) {
return fmt.Errorf("tarring multiple sources, destination must end with \"/\"")
} else if !filepath.IsAbs(dst) && !filepath.IsAbs(workDir) {
return fmt.Errorf("dst is not absolute path, must specify absolute working directory")
}
return nil
}
func isDirFormat(dst string) bool {
return strings.HasSuffix(dst, "/") || dst == "." || dst == ".."
}