Repository navigation
Expand file tree
/
Copy patharchive.go
More file actions
190 lines (173 loc) · 3.94 KB
/
Copy patharchive.go
File metadata and controls
190 lines (173 loc) · 3.94 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ramune
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type archiveOpts struct {
Files []string `json:"files"`
Output string `json:"output"`
Gzip bool `json:"gzip"`
Cwd string `json:"cwd"`
}
type extractOpts struct {
Input string `json:"input"`
Output string `json:"output"`
Gzip bool `json:"gzip"`
}
func goBunArchiveTar(args []any) (any, error) {
if len(args) < 1 {
return nil, fmt.Errorf("Archive.tar: options required")
}
optsJSON, _ := args[0].(string)
var opts archiveOpts
if err := json.Unmarshal([]byte(optsJSON), &opts); err != nil {
return nil, fmt.Errorf("Archive.tar: %w", err)
}
if len(opts.Files) == 0 {
return nil, fmt.Errorf("Archive.tar: files required")
}
var buf bytes.Buffer
var w io.WriteCloser = &nopWriteCloser{&buf}
if opts.Gzip {
w = gzip.NewWriter(&buf)
}
tw := tar.NewWriter(w)
defer tw.Close()
defer w.Close()
cwd := opts.Cwd
if cwd == "" {
cwd = "."
}
for _, f := range opts.Files {
path := f
if !filepath.IsAbs(path) {
path = filepath.Join(cwd, path)
}
info, err := os.Stat(path)
if err != nil {
return nil, err
}
hdr, err := tar.FileInfoHeader(info, "")
if err != nil {
return nil, err
}
hdr.Name = f
if err := tw.WriteHeader(hdr); err != nil {
return nil, err
}
if !info.IsDir() {
fh, err := os.Open(path)
if err != nil {
return nil, err
}
_, copyErr := io.Copy(tw, fh)
fh.Close()
if copyErr != nil {
return nil, copyErr
}
}
}
tw.Close()
w.Close()
if opts.Output != "" {
return nil, os.WriteFile(opts.Output, buf.Bytes(), 0644)
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
// safePath checks that target stays within baseDir after cleaning.
func safePath(target, baseDir string) bool {
cleanTarget := filepath.Clean(target)
cleanBase := filepath.Clean(baseDir) + string(filepath.Separator)
return strings.HasPrefix(cleanTarget+string(filepath.Separator), cleanBase)
}
func goBunArchiveUntar(args []any) (any, error) {
if len(args) < 1 {
return nil, fmt.Errorf("Archive.untar: options required")
}
optsJSON, _ := args[0].(string)
var opts extractOpts
if err := json.Unmarshal([]byte(optsJSON), &opts); err != nil {
return nil, fmt.Errorf("Archive.untar: %w", err)
}
var data []byte
var err error
if opts.Input != "" {
data, err = os.ReadFile(opts.Input)
if err != nil {
return nil, err
}
} else if len(args) >= 2 {
b64, _ := args[1].(string)
data, err = base64.StdEncoding.DecodeString(b64)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("Archive.untar: input required")
}
var r io.Reader = bytes.NewReader(data)
if opts.Gzip {
gr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
defer gr.Close()
r = gr
}
outDir := opts.Output
if outDir == "" {
outDir = "."
}
absOutDir, err := filepath.Abs(outDir)
if err != nil {
return nil, err
}
tr := tar.NewReader(r)
var files []string
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
target := filepath.Join(absOutDir, hdr.Name)
if !safePath(target, absOutDir) {
continue
}
if hdr.Typeflag == tar.TypeDir {
os.MkdirAll(target, 0755)
continue
}
os.MkdirAll(filepath.Dir(target), 0755)
fh, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(hdr.Mode))
if err != nil {
return nil, err
}
_, copyErr := io.Copy(fh, tr)
fh.Close()
if copyErr != nil {
return nil, copyErr
}
files = append(files, hdr.Name)
}
out, _ := json.Marshal(files)
return string(out), nil
}
type nopWriteCloser struct{ io.Writer }
func (n *nopWriteCloser) Close() error { return nil }
func (r *Runtime) installArchive() error {
if err := r.registerFuncLocked("__go_bun_archive_tar", goBunArchiveTar); err != nil {
return err
}
return r.registerFuncLocked("__go_bun_archive_untar", goBunArchiveUntar)
}