forked from podman-container-tools/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.go
More file actions
120 lines (103 loc) · 2.92 KB
/
Copy pathextract.go
File metadata and controls
120 lines (103 loc) · 2.92 KB
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
package artifacts
import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"github.com/containers/podman/v5/pkg/bindings"
)
func Extract(ctx context.Context, artifactName string, target string, options *ExtractOptions) error {
conn, err := bindings.GetClient(ctx)
if err != nil {
return fmt.Errorf("getting client: %w", err)
}
if options == nil {
options = new(ExtractOptions)
}
// Check if target is a directory to know if we can copy more than one blob
targetIsDirectory := false
stat, err := os.Stat(target)
if err == nil {
targetIsDirectory = stat.IsDir()
} else if !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("stat target %q failed: %w", target, err)
}
// If the target is not a directory, request API to return the blob without title.
// If a blob has a malicious title it will be returned from the API without it
// as the file will be written to the provided target
if !targetIsDirectory {
options.WithExcludeTitle(true)
}
params, err := options.ToParams()
if err != nil {
return fmt.Errorf("converting options to params: %w", err)
}
response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/artifacts/%s/extract", params, nil, artifactName)
if err != nil {
return err
}
defer response.Body.Close()
if !response.IsSuccess() {
return response.Process(nil)
}
multipleBlobs := false
tr := tar.NewReader(response.Body)
for {
header, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
return err
}
if !targetIsDirectory && multipleBlobs {
return fmt.Errorf("the artifact consists of several blobs and the target %q is not a directory and neither digest or title was specified to only copy a single blob", target)
}
// If destination isn't a file, extract to target/filename
fileTarget := target
if targetIsDirectory {
filename := header.Name
// This matches the logic from generateArtifactBlobName().
for i := range len(filename) {
if os.IsPathSeparator(filename[i]) {
return fmt.Errorf("invalid filename: %q cannot contain %c", filename, filename[i])
}
}
fileTarget = filepath.Join(target, filename)
}
if header.Typeflag == tar.TypeReg {
err = extractFile(tr, fileTarget)
if err != nil {
return err
}
}
// Signal that the first blob has been extracted so we can return an error if more
// than one blob are being extracted when target is not a directory.
multipleBlobs = true
}
return nil
}
func extractFile(tr *tar.Reader, fileTarget string) (retErr error) {
outFile, err := os.Create(fileTarget)
if err != nil {
return err
}
// Use an anonymous function to enable capturing the error from
// outFile.Close() upon returning.
defer func() {
cErr := outFile.Close()
if retErr == nil {
retErr = cErr
}
}()
_, err = io.Copy(outFile, tr)
if err != nil {
return fmt.Errorf("failed to extract blob to %s: %w", fileTarget, err)
}
return nil
}