-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathwriter.go
More file actions
93 lines (82 loc) · 2.44 KB
/
writer.go
File metadata and controls
93 lines (82 loc) · 2.44 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
package main
import (
"context"
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
"github.com/google/osv.dev/go/logger"
"github.com/google/osv.dev/go/osv/clients"
)
// writeMsg holds the data for a file to be written.
type writeMsg struct {
path string
mimeType string
data []byte
}
// writer is a worker that receives writeMsgs and writes them to either a GCS
// bucket or a local directory.
func writer(ctx context.Context, cancel context.CancelFunc, inCh <-chan writeMsg, client clients.CloudStorage, pathPrefix string, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case msg, ok := <-inCh:
if !ok {
// Channel closed.
return
}
path := filepath.Join(pathPrefix, msg.path)
if client != nil {
// Write to the bucket.
err := client.WriteObject(ctx, path, msg.data, &clients.WriteOptions{
ContentType: msg.mimeType,
})
if err != nil {
logger.Error("failed to write file", slog.String("path", path), slog.Any("err", err))
cancel()
break
}
} else {
cleanPath := filepath.Clean(msg.path)
if filepath.IsAbs(cleanPath) || cleanPath == ".." || strings.HasPrefix(cleanPath, ".."+string(filepath.Separator)) {
logger.Error("invalid file path", slog.String("path", msg.path))
cancel()
break
}
basePath, err := filepath.Abs(pathPrefix)
if err != nil {
logger.Error("failed to get absolute path", slog.String("path", pathPrefix), slog.Any("err", err))
cancel()
break
}
localPath, err := filepath.Abs(filepath.Join(basePath, cleanPath))
if err != nil {
logger.Error("failed to get absolute path", slog.String("path", cleanPath), slog.Any("err", err))
cancel()
break
}
relPath, err := filepath.Rel(basePath, localPath)
if err != nil || relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) {
logger.Error("invalid file path", slog.String("path", msg.path), slog.Any("err", err))
cancel()
break
}
// Write locally.
dir := filepath.Dir(localPath)
if err := os.MkdirAll(dir, 0755); err != nil {
logger.Error("failed to create directories", slog.String("dir", dir), slog.Any("err", err))
cancel()
break
}
if err := os.WriteFile(localPath, msg.data, 0600); err != nil {
logger.Error("failed to write file", slog.String("path", localPath), slog.Any("err", err))
cancel()
break
}
}
case <-ctx.Done():
return
}
}
}