forked from libgit2/git2go
/
transport.go
449 lines (369 loc) · 12.8 KB
/
transport.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package git
/*
#include <string.h>
#include <git2.h>
#include <git2/sys/transport.h>
typedef struct {
git_smart_subtransport parent;
void *ptr;
} _go_managed_smart_subtransport;
typedef struct {
git_smart_subtransport_stream parent;
void *ptr;
} _go_managed_smart_subtransport_stream;
int _go_git_smart_transport_register(const char *scheme, void *payload);
int _go_git_transport_smart(git_transport **out, git_remote *owner, int stateless, void *subtransport_payload);
void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stream *stream);
*/
import "C"
import (
"errors"
"io"
"reflect"
"runtime"
"unsafe"
)
// SmartServiceAction is an action that the smart transport can ask a
// subtransport to perform.
type SmartServiceAction int
const (
// SmartServiceActionUploadpackLs is used upon connecting to a remote, and is
// used to perform reference discovery prior to performing a pull operation.
SmartServiceActionUploadpackLs SmartServiceAction = C.GIT_SERVICE_UPLOADPACK_LS
// SmartServiceActionUploadpack is used when performing a pull operation.
SmartServiceActionUploadpack SmartServiceAction = C.GIT_SERVICE_UPLOADPACK
// SmartServiceActionReceivepackLs is used upon connecting to a remote, and is
// used to perform reference discovery prior to performing a push operation.
SmartServiceActionReceivepackLs SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK_LS
// SmartServiceActionReceivepack is used when performing a push operation.
SmartServiceActionReceivepack SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK
)
// RegisteredSmartTransport represents a transport that has been registered.
type RegisteredSmartTransport struct {
name string
stateless bool
callback SmartSubtransportCallback
handle unsafe.Pointer
}
// Transport encapsulates a way to communicate with a Remote.
type Transport struct {
ptr *C.git_transport
}
// SmartProxyOptions gets a copy of the proxy options for this transport.
func (t *Transport) SmartProxyOptions() (*ProxyOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var cpopts C.git_proxy_options
if ret := C.git_transport_smart_proxy_options(&cpopts, t.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return proxyOptionsFromC(&cpopts), nil
}
// SmartCredentials calls the credentials callback for this transport.
func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, error) {
cred := newCred()
var cstr *C.char
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if user != "" {
cstr = C.CString(user)
defer C.free(unsafe.Pointer(cstr))
}
ret := C.git_transport_smart_credentials(&cred.ptr, t.ptr, cstr, C.int(methods))
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// SmartCertificateCheck calls the certificate check for this transport.
func (t *Transport) SmartCertificateCheck(cert *Certificate, valid bool, hostname string) error {
var ccert *C.git_cert
switch cert.Kind {
case CertificateHostkey:
chostkeyCert := C.git_cert_hostkey{
parent: C.git_cert{
cert_type: C.GIT_CERT_HOSTKEY_LIBSSH2,
},
_type: C.git_cert_ssh_t(cert.Kind),
}
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_md5[0]), unsafe.Pointer(&cert.Hostkey.HashMD5[0]), C.size_t(len(cert.Hostkey.HashMD5)))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha1[0]), unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), C.size_t(len(cert.Hostkey.HashSHA1)))
ccert = (*C.git_cert)(unsafe.Pointer(&chostkeyCert))
case CertificateX509:
cx509Cert := C.git_cert_x509{
parent: C.git_cert{
cert_type: C.GIT_CERT_X509,
},
len: C.size_t(len(cert.X509.Raw)),
data: C.CBytes(cert.X509.Raw),
}
defer C.free(cx509Cert.data)
ccert = (*C.git_cert)(unsafe.Pointer(&cx509Cert))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
chostname := C.CString(hostname)
defer C.free(unsafe.Pointer(chostname))
cvalid := C.int(0)
if valid {
cvalid = C.int(1)
}
ret := C.git_transport_smart_certificate_check(t.ptr, ccert, cvalid, chostname)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
// SmartSubtransport is the interface for custom subtransports which carry data
// for the smart transport.
type SmartSubtransport interface {
// Action creates a SmartSubtransportStream for the provided url and
// requested action.
Action(url string, action SmartServiceAction) (SmartSubtransportStream, error)
// Close closes the SmartSubtransport.
//
// Subtransports are guaranteed a call to Close between
// calls to Action, except for the following two "natural" progressions
// of actions against a constant URL.
//
// 1. UPLOADPACK_LS -> UPLOADPACK
// 2. RECEIVEPACK_LS -> RECEIVEPACK
Close() error
// Free releases the resources of the SmartSubtransport.
Free()
}
// SmartSubtransportStream is the interface for streams used by the smart
// transport to read and write data from a subtransport.
type SmartSubtransportStream interface {
io.Reader
io.Writer
// Free releases the resources of the SmartSubtransportStream.
Free()
}
// SmartSubtransportCallback is a function which creates a new subtransport for
// the smart transport.
type SmartSubtransportCallback func(remote *Remote, transport *Transport) (SmartSubtransport, error)
// NewRegisteredSmartTransport adds a custom transport definition, to be used
// in addition to the built-in set of transports that come with libgit2.
func NewRegisteredSmartTransport(
name string,
stateless bool,
callback SmartSubtransportCallback,
) (*RegisteredSmartTransport, error) {
cstr := C.CString(name)
defer C.free(unsafe.Pointer(cstr))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
registeredSmartTransport := new(RegisteredSmartTransport)
registeredSmartTransport.name = name
registeredSmartTransport.stateless = stateless
registeredSmartTransport.callback = callback
registeredSmartTransport.handle = pointerHandles.Track(registeredSmartTransport)
ret := C._go_git_smart_transport_register(cstr, registeredSmartTransport.handle)
runtime.KeepAlive(callback)
runtime.KeepAlive(cstr)
if ret != 0 {
pointerHandles.Untrack(registeredSmartTransport.handle)
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(registeredSmartTransport, (*RegisteredSmartTransport).Free)
return registeredSmartTransport, nil
}
// Free releases all resources used by the RegisteredSmartTransport and
// unregisters the custom transport definition referenced by it.
func (t *RegisteredSmartTransport) Free() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(t.name)
defer C.free(unsafe.Pointer(cstr))
if ret := C.git_transport_unregister(cstr); ret < 0 {
return MakeGitError(ret)
}
pointerHandles.Untrack(t.handle)
runtime.SetFinalizer(t, nil)
t.handle = nil
return nil
}
//export smartTransportCb
func smartTransportCb(out **C.git_transport, owner *C.git_remote, param unsafe.Pointer) C.int {
if out == nil {
return -1
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
registeredSmartTransport := pointerHandles.Get(param).(*RegisteredSmartTransport)
remote, ok := remotePointers.Get(owner)
if !ok {
remote = nil
}
subtransport := C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport{})))
managed := &wrappedSmartSubtransport{
remote: remote,
callback: registeredSmartTransport.callback,
subtransportPtr: subtransport,
}
managedPtr := pointerHandles.Track(managed)
managed.ptr = managedPtr
(*C._go_managed_smart_subtransport)(subtransport).ptr = managedPtr
stateless := C.int(0)
if registeredSmartTransport.stateless {
stateless = C.int(1)
}
ret := C._go_git_transport_smart(out, owner, stateless, subtransport)
if ret != 0 {
pointerHandles.Untrack(managedPtr)
}
return ret
}
//export smartTransportSubtransportCb
func smartTransportSubtransportCb(wrapperPtr *C._go_managed_smart_subtransport, owner *C.git_transport) C.int {
subtransport, ok := pointerHandles.Get(wrapperPtr.ptr).(*wrappedSmartSubtransport)
if !ok {
return setLibgit2Error(ErrClassNet, errNoWrappedSmartSubtransport)
}
subtransport.transport = &Transport{
ptr: owner,
}
underlyingSmartSubtransport, err := subtransport.callback(subtransport.remote, subtransport.transport)
if err != nil {
return setLibgit2Error(ErrClassNet, err)
}
subtransport.underlying = underlyingSmartSubtransport
return 0
}
type wrappedSmartSubtransport struct {
owner *C.git_transport
callback SmartSubtransportCallback
ptr unsafe.Pointer
remote *Remote
transport *Transport
subtransportPtr unsafe.Pointer
underlying SmartSubtransport
}
var errNoWrappedSmartSubtransport = errors.New("passed object is not a wrappedSmartSubtransport")
func getSmartSubtransportInterface(_t *C.git_smart_subtransport) (*wrappedSmartSubtransport, error) {
wrapperPtr := (*C._go_managed_smart_subtransport)(unsafe.Pointer(_t))
subtransport, ok := pointerHandles.Get(wrapperPtr.ptr).(*wrappedSmartSubtransport)
if !ok {
return nil, errNoWrappedSmartSubtransport
}
return subtransport, nil
}
//export smartSubtransportActionCb
func smartSubtransportActionCb(out **C.git_smart_subtransport_stream, t *C.git_smart_subtransport, url *C.char, action C.git_smart_service_t) C.int {
subtransport, err := getSmartSubtransportInterface(t)
if err != nil {
return setLibgit2Error(ErrClassNet, err)
}
underlyingStream, err := subtransport.underlying.Action(C.GoString(url), SmartServiceAction(action))
if err != nil {
return setLibgit2Error(ErrClassNet, err)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
stream := C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport_stream{})))
managed := &wrappedSmartSubtransportStream{
underlying: underlyingStream,
streamPtr: stream,
}
managedPtr := pointerHandles.Track(managed)
managed.ptr = managedPtr
(*C._go_managed_smart_subtransport_stream)(stream).ptr = managedPtr
C._go_git_setup_smart_subtransport_stream((*C._go_managed_smart_subtransport_stream)(stream))
*out = (*C.git_smart_subtransport_stream)(stream)
return 0
}
//export smartSubtransportCloseCb
func smartSubtransportCloseCb(t *C.git_smart_subtransport) C.int {
subtransport, err := getSmartSubtransportInterface(t)
if err != nil {
return setLibgit2Error(ErrClassNet, err)
}
if subtransport.underlying != nil {
err = subtransport.underlying.Close()
if err != nil {
return setLibgit2Error(ErrClassNet, err)
}
}
return 0
}
//export smartSubtransportFreeCb
func smartSubtransportFreeCb(t *C.git_smart_subtransport) {
subtransport, err := getSmartSubtransportInterface(t)
if err != nil {
panic(err)
}
if subtransport.underlying != nil {
subtransport.underlying.Free()
subtransport.underlying = nil
}
pointerHandles.Untrack(subtransport.ptr)
C.free(subtransport.subtransportPtr)
subtransport.ptr = nil
subtransport.subtransportPtr = nil
}
type wrappedSmartSubtransportStream struct {
owner *C.git_smart_subtransport_stream
ptr unsafe.Pointer
streamPtr unsafe.Pointer
underlying SmartSubtransportStream
}
var errNoSmartSubtransportStream = errors.New("passed object is not a wrappedSmartSubtransportStream")
func getSmartSubtransportStreamInterface(_s *C.git_smart_subtransport_stream) (*wrappedSmartSubtransportStream, error) {
wrapperPtr := (*C._go_managed_smart_subtransport_stream)(unsafe.Pointer(_s))
stream, ok := pointerHandles.Get(wrapperPtr.ptr).(*wrappedSmartSubtransportStream)
if !ok {
return nil, errNoSmartSubtransportStream
}
return stream, nil
}
//export smartSubtransportStreamWriteCb
func smartSubtransportStreamWriteCb(s *C.git_smart_subtransport_stream, buffer *C.char, bufLen C.size_t) C.int {
stream, err := getSmartSubtransportStreamInterface(s)
if err != nil {
return setLibgit2Error(ErrClassNet, err)
}
var p []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
header.Cap = int(bufLen)
header.Len = int(bufLen)
header.Data = uintptr(unsafe.Pointer(buffer))
if _, err := stream.underlying.Write(p); err != nil {
return setLibgit2Error(ErrClassNet, err)
}
return 0
}
//export smartSubtransportStreamReadCb
func smartSubtransportStreamReadCb(s *C.git_smart_subtransport_stream, buffer *C.char, bufSize C.size_t, bytesRead *C.size_t) C.int {
stream, err := getSmartSubtransportStreamInterface(s)
if err != nil {
return setLibgit2Error(ErrClassNet, err)
}
var p []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
header.Cap = int(bufSize)
header.Len = int(bufSize)
header.Data = uintptr(unsafe.Pointer(buffer))
n, err := stream.underlying.Read(p)
*bytesRead = C.size_t(n)
if n == 0 && err != nil {
if err == io.EOF {
return 0
}
return setLibgit2Error(ErrClassNet, err)
}
return 0
}
//export smartSubtransportStreamFreeCb
func smartSubtransportStreamFreeCb(s *C.git_smart_subtransport_stream) {
stream, err := getSmartSubtransportStreamInterface(s)
if err != nil {
panic(err)
}
stream.underlying.Free()
pointerHandles.Untrack(stream.ptr)
C.free(stream.streamPtr)
stream.ptr = nil
stream.streamPtr = nil
}