-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy patharchive.go
More file actions
186 lines (166 loc) · 4.35 KB
/
Copy patharchive.go
File metadata and controls
186 lines (166 loc) · 4.35 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
package boxlite
/*
#include "bridge.h"
#include <stdlib.h>
*/
import "C"
import (
"context"
"runtime/cgo"
"strings"
"unsafe"
)
func validateArchiveArgument(value, parameter string, allowEmpty bool) error {
if value == "" && !allowEmpty {
return &Error{
Code: ErrInvalidArgument,
Message: parameter + " must not be empty",
}
}
if strings.IndexByte(value, 0) >= 0 {
return &Error{
Code: ErrInvalidArgument,
Message: parameter + " must not contain NUL bytes",
}
}
return nil
}
func archiveNameCString(name string) (*C.char, func()) {
if name == "" {
return nil, func() {}
}
cName := toCString(name)
return cName, func() {
C.free(unsafe.Pointer(cName))
}
}
func abandonOwnedResult[T any](result <-chan handleResult[T], handle cgo.Handle, dispose func(T)) {
if claimHandleForDispatch(handle) {
handle.Delete()
return
}
go func() {
completed := <-result
dispose(completed.value)
}()
}
// Export exports the box to a portable .boxlite archive.
//
// If dest is a directory, Export creates the archive inside it and returns the
// actual archive file path selected by the backend.
//
// Export never deletes the archive. The caller owns the returned file and is
// responsible for retaining, moving, or deleting it.
func (b *Box) Export(ctx context.Context, dest string) (string, error) {
if err := validateArchiveArgument(dest, "export destination", false); err != nil {
return "", err
}
if err := ctx.Err(); err != nil {
return "", err
}
b.runtime.ensureDrainRunning()
cDest := toCString(dest)
defer C.free(unsafe.Pointer(cDest))
result := make(chan handleResult[*C.char], 1)
handle := registerHandleForDispatch(cgo.NewHandle(result))
if err := ctx.Err(); err != nil {
deleteHandleForDispatch(handle)
return "", err
}
var cError C.CBoxliteError
code := C.boxlite_box_export(
b.handle,
cDest,
C.cbBoxExport(),
handleToPtr(handle),
&cError,
)
if code != C.Ok {
deleteHandleForDispatch(handle)
return "", freeError(&cError)
}
dispose := func(path *C.char) {
if path != nil {
freeBoxliteString(path)
}
}
select {
case completed := <-result:
defer dispose(completed.value)
if completed.err != nil {
return "", completed.err
}
return cString(completed.value), nil
case <-ctx.Done():
abandonOwnedResult(result, handle, dispose)
return "", ctx.Err()
case <-b.runtime.closing:
abandonOwnedResult(result, handle, dispose)
return "", ErrRuntimeClosed
}
}
// Import imports a .boxlite archive and returns a new, stopped box.
//
// Local runtimes treat caller-provided archives as trusted and preserve their
// complete configuration. REST runtimes upload the archive, and the server
// applies its untrusted-upload policy.
//
// An empty name leaves the imported box unnamed, matching Create without
// WithName. Import assigns a new box ID.
//
// Import never consumes or deletes archivePath. The caller owns the archive
// and is responsible for retaining, moving, or deleting it.
func (r *Runtime) Import(ctx context.Context, archivePath, name string) (*Box, error) {
if err := validateArchiveArgument(archivePath, "archive path", false); err != nil {
return nil, err
}
if err := validateArchiveArgument(name, "import name", true); err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
r.ensureDrainRunning()
cArchivePath := toCString(archivePath)
defer C.free(unsafe.Pointer(cArchivePath))
cName, freeName := archiveNameCString(name)
defer freeName()
result := make(chan handleResult[*C.CBoxHandle], 1)
handle := registerHandleForDispatch(cgo.NewHandle(result))
if err := ctx.Err(); err != nil {
deleteHandleForDispatch(handle)
return nil, err
}
var cError C.CBoxliteError
code := C.boxlite_runtime_import(
r.handle,
cArchivePath,
cName,
C.cbRuntimeImport(),
handleToPtr(handle),
&cError,
)
if code != C.Ok {
deleteHandleForDispatch(handle)
return nil, freeError(&cError)
}
dispose := func(handle *C.CBoxHandle) {
if handle != nil {
C.boxlite_box_free(handle)
}
}
select {
case completed := <-result:
if completed.err != nil {
dispose(completed.value)
return nil, completed.err
}
return newBoxFromHandle(r, completed.value, name), nil
case <-ctx.Done():
abandonOwnedResult(result, handle, dispose)
return nil, ctx.Err()
case <-r.closing:
abandonOwnedResult(result, handle, dispose)
return nil, ErrRuntimeClosed
}
}