-
Notifications
You must be signed in to change notification settings - Fork 18
/
artifacts.go
106 lines (86 loc) · 2.19 KB
/
artifacts.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
package rpc
import (
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/internal/executor/pathsafe"
"io"
"os"
"path/filepath"
)
func (r *RPC) UploadArtifacts(stream api.CirrusCIService_UploadArtifactsServer) (err error) {
var currentArtifactName string
artifactWriter := NewArtifactWriter(r.artifactsDir)
defer func() {
if awErr := artifactWriter.Close(); awErr != nil && err == nil {
err = awErr
}
}()
for {
artifactEntry, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
r.logger.Warnf("error while receiving artifacts: %v", err)
return err
}
if r.artifactsDir == "" {
continue
}
switch obj := artifactEntry.Value.(type) {
case *api.ArtifactEntry_ArtifactsUpload_:
currentArtifactName = obj.ArtifactsUpload.Name
case *api.ArtifactEntry_Chunk:
if !pathsafe.IsPathSafe(currentArtifactName) {
continue
}
artifactPath := filepath.Join(currentArtifactName, obj.Chunk.ArtifactPath)
if _, err := artifactWriter.WriteTo(artifactPath, obj.Chunk.Data); err != nil {
return err
}
}
}
if err := stream.SendAndClose(&api.UploadArtifactsResponse{}); err != nil {
r.logger.Warnf("error while closing artifacts stream: %v", err)
return err
}
return nil
}
type ArtifactWriter struct {
baseDir string
currentPath string
currentFile *os.File
}
func NewArtifactWriter(baseDir string) *ArtifactWriter {
return &ArtifactWriter{
baseDir: baseDir,
currentPath: "",
currentFile: nil,
}
}
func (aw *ArtifactWriter) WriteTo(name string, b []byte) (int, error) {
path := filepath.Join(aw.baseDir, name)
//nolint:nestif // doesn't look that complicated
if aw.currentPath != path {
if aw.currentFile != nil {
if err := aw.currentFile.Close(); err != nil {
return 0, err
}
}
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return 0, err
}
var err error
aw.currentFile, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return 0, err
}
aw.currentPath = path
}
return aw.currentFile.Write(b)
}
func (aw *ArtifactWriter) Close() error {
if aw.currentFile == nil {
return nil
}
return aw.currentFile.Close()
}