-
Notifications
You must be signed in to change notification settings - Fork 122
/
copy.go
158 lines (138 loc) · 5.57 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
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
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2019-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package files
import (
"bytes"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/apptainer/apptainer/internal/pkg/util/bin"
"github.com/apptainer/apptainer/internal/pkg/util/fs"
"github.com/apptainer/apptainer/pkg/util/archive"
)
// makeParentDir ensures existence of the expected destination directory for the cp command
// based on the supplied path.
func makeParentDir(path string) error {
_, err := os.Stat(path)
if !os.IsNotExist(err) {
return nil
}
// if path ends with a trailing '/' always ensure the full path exists as a directory
// because 'cp' is expecting a dir in these cases
if strings.HasSuffix(path, "/") {
if err := os.MkdirAll(filepath.Clean(path), 0o755); err != nil {
return fmt.Errorf("while creating full path: %s", err)
}
return nil
}
// only make parent directory
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("while creating parent of path: %s", err)
}
return nil
}
// CopyFromHost should be used to copy files into the rootfs from the host fs.
// src is a path relative to CWD on the host, or an absolute path on the host.
// dstRel is a destination path inside dstRootfs.
// An empty dstRel "" means copy the src file to the same path in the rootfs.
// All symlinks encountered in the copy will be dereferenced (cp -L behavior).
func CopyFromHost(src, dstRel, dstRootfs string) error {
// resolve any bash globbing in filepath
paths, err := expandPath(src)
if err != nil {
return fmt.Errorf("while expanding source path with bash: %s: %s", src, err)
}
for _, srcGlobbed := range paths {
// If the dstRel is "" then we are copying to the full source path, appended to the rootfs prefix
dstRelGlobbed := dstRel
if dstRel == "" {
dstRelGlobbed = srcGlobbed
}
// Resolve our destination within the container rootfs
dstResolved, err := secureJoinKeepSlash(dstRootfs, dstRelGlobbed)
if err != nil {
return fmt.Errorf("while resolving destination: %s: %s", dstRelGlobbed, err)
}
// Create any parent dirs for dst that don't already exist
if err := makeParentDir(dstResolved); err != nil {
return fmt.Errorf("while creating parent dir: %v", err)
}
args := []string{"-fLr", srcGlobbed, dstResolved}
var output, stderr bytes.Buffer
// copy each file into bundle rootfs
cp, err := bin.FindBin("cp")
if err != nil {
return err
}
copyCmd := exec.Command(cp, args...)
copyCmd.Stdout = &output
copyCmd.Stderr = &stderr
if err := copyCmd.Run(); err != nil {
return fmt.Errorf("while copying %s to %s: %v: %s", paths, dstResolved, args, stderr.String())
}
}
return nil
}
// CopyFromStage should be used to copy files into the rootfs from a previous stage.
// The src and dst are paths relative to the srcRootfs and dstRootfs.
// An empty dst "" means copy the src file to the same path in the dst rootfs.
// Symlinks are only dereferenced for the specified source or files that resolve
// directly from a specified glob pattern. Any additional links inside a directory
// being copied are not dereferenced.
func CopyFromStage(src, dst, srcRootfs, dstRootfs string) error {
// An absolute path is required for globbing... but with no symlink resolution or
// path cleaning yet.
srcAbs := joinKeepSlash(srcRootfs, src)
// resolve any bash globbing in filepath
paths, err := expandPath(srcAbs)
if err != nil {
return fmt.Errorf("while expanding source path with bash: %s: %s", srcAbs, err)
}
// We manually dereference first-level src symlinks only.
for _, srcGlobbed := range paths {
// Now re-resolve the source files after globbing by using securejoin,
// so that absolute symlinks are dereferenced relative to the source rootfs,
// and the source is enforced to be inside the rootfs.
srcGlobbedRel := strings.TrimPrefix(srcGlobbed, srcRootfs)
srcResolved, err := secureJoinKeepSlash(srcRootfs, srcGlobbedRel)
if err != nil {
return fmt.Errorf("while resolving source: %s: %s", srcGlobbedRel, err)
}
// If the dst is "" then we are copying to the same path in dstRootfs, as src is in srcRootfs.
dstGlobbed := dst
if dst == "" {
dstGlobbed = srcGlobbedRel
}
// Resolve the destination path, keeping any final slash
dstResolved, err := secureJoinKeepSlash(dstRootfs, dstGlobbed)
if err != nil {
return fmt.Errorf("while resolving destination: %s: %s", dstGlobbed, err)
}
// Create any parent dirs for dstResolved that don't already exist.
if err := makeParentDir(dstResolved); err != nil {
return fmt.Errorf("while creating parent dir: %v", err)
}
// If we are copying into a directory then we must use the original source filename,
// for the destination filename, not the one that was resolved out.
// I.E. if copying `/opt/view` to `/opt/` where `/opt/view links-> /opt/.view/abc123`
// we want to create `/opt/view` in the dest, not `/opt/abc123`.
if fs.IsDir(dstResolved) {
_, srcName := path.Split(srcGlobbedRel)
dstResolved = path.Join(dstResolved, srcName)
}
err = archive.CopyWithTar(srcResolved, dstResolved)
if err != nil {
return fmt.Errorf("while copying %s to %s: %s", paths, dstResolved, err)
}
}
return nil
}