forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp-odb-backend-internal.cpp
742 lines (624 loc) · 20.7 KB
/
php-odb-backend-internal.cpp
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
* php-odb-backend-internal.cpp
*
* Copyright (C) Roger P. Gee
*/
#include "php-object.h"
using namespace php_git2;
// Class method entries
// string GitODBBackend_Internal::read(&$type,$oid);
static PHP_METHOD(GitODBBackend_Internal,read);
ZEND_BEGIN_ARG_INFO_EX(GitODBBackend_Internal_read_arginfo,0,0,2)
ZEND_ARG_PASS_INFO(1)
ZEND_END_ARG_INFO()
static PHP_METHOD(GitODBBackend_Internal,read_prefix);
ZEND_BEGIN_ARG_INFO_EX(GitODBBackend_Internal_read_prefix_arginfo,0,0,3)
ZEND_ARG_PASS_INFO(1)
ZEND_ARG_PASS_INFO(1)
ZEND_END_ARG_INFO()
static PHP_METHOD(GitODBBackend_Internal,read_header);
ZEND_BEGIN_ARG_INFO_EX(GitODBBackend_Internal_read_header_arginfo,0,0,3)
ZEND_ARG_PASS_INFO(1)
ZEND_ARG_PASS_INFO(1)
ZEND_END_ARG_INFO()
static PHP_METHOD(GitODBBackend_Internal,write);
static PHP_METHOD(GitODBBackend_Internal,writestream);
static PHP_METHOD(GitODBBackend_Internal,readstream);
static PHP_METHOD(GitODBBackend_Internal,exists);
static PHP_METHOD(GitODBBackend_Internal,exists_prefix);
ZEND_BEGIN_ARG_INFO_EX(GitODBBackend_Internal_exists_prefix_arginfo,0,0,2)
ZEND_ARG_PASS_INFO(1)
ZEND_END_ARG_INFO()
static PHP_METHOD(GitODBBackend_Internal,refresh);
static PHP_METHOD(GitODBBackend_Internal,for_each);
static PHP_METHOD(GitODBBackend_Internal,writepack);
zend_function_entry php_git2::odb_backend_internal_methods[] = {
PHP_ME(GitODBBackend_Internal,read,GitODBBackend_Internal_read_arginfo,ZEND_ACC_PUBLIC)
PHP_ME(
GitODBBackend_Internal,
read_prefix,
GitODBBackend_Internal_read_prefix_arginfo,
ZEND_ACC_PUBLIC
)
PHP_ME(
GitODBBackend_Internal,
read_header,
GitODBBackend_Internal_read_header_arginfo,
ZEND_ACC_PUBLIC
)
PHP_ME(
GitODBBackend_Internal,
write,
NULL,
ZEND_ACC_PUBLIC
)
PHP_ME(GitODBBackend_Internal,writestream,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend_Internal,readstream,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend_Internal,exists,NULL,ZEND_ACC_PUBLIC)
PHP_ME(
GitODBBackend_Internal,
exists_prefix,
GitODBBackend_Internal_exists_prefix_arginfo,
ZEND_ACC_PUBLIC
)
PHP_ME(GitODBBackend_Internal,refresh,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend_Internal,for_each,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend_Internal,writepack,NULL,ZEND_ACC_PUBLIC)
PHP_FE_END
};
// Make function implementation
void php_git2::php_git2_make_odb_backend(
zval* zp,
git_odb_backend* backend,
php_git_odb* owner)
{
php_odb_backend_object* storage;
// Create object zval.
object_init_ex(zp,php_git2::class_entry[php_git2_odb_backend_internal_obj]);
storage = php_zend_object<php_odb_backend_internal_object>::get_storage(Z_OBJ_P(zp));
// Assign the git2 odb_backend object.
storage->backend = backend;
// Assign owner. (If set, this increments owner refcount to prevent the ODB
// from freeing while the backend object is in use.)
storage->assign_owner(owner);
// Assign kind based on if owner set.
if (owner != nullptr) {
storage->kind = php_odb_backend_object::conventional;
}
else {
storage->kind = php_odb_backend_object::user;
}
}
// php_zend_object init function
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_backend_object>::handlers;
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_backend_internal_object>::handlers;
template<>
void php_zend_object<php_odb_backend_internal_object>::init(zend_class_entry* ce)
{
auto& parentHandlers = php_zend_object<php_odb_backend_object>::handlers;
handlers.read_property = parentHandlers.read_property;
handlers.write_property = parentHandlers.write_property;
handlers.has_property = parentHandlers.has_property;
handlers.get_constructor = php_git2::not_allowed_get_constructor;
handlers.offset = offset();
UNUSED(ce);
}
// Implementation of php_odb_backend_internal_object
php_odb_backend_internal_object::php_odb_backend_internal_object()
{
}
// Implementation of class methods
PHP_METHOD(GitODBBackend_Internal,read)
{
php_bailer bailer;
zval* ztype;
char* strOid;
size_t strOidLen;
php_odb_backend_object* object;
void* data = NULL;
size_t size = 0;
git_object_t type = GIT_OBJ_ANY;
git_oid oid;
// Grab object backing. Verify that the backend exists and the function is
// implemented.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->read == nullptr) {
zend_throw_error(nullptr,"GitODBBackend_Internal::read(): method is not available");
return;
}
// Grab parameters. The first parameter is passed by reference so we have to
// extract its zval.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"z/s",&ztype,&strOid,&strOidLen) == FAILURE) {
return;
}
// Interpret the string parameter as a human-readable OID. Convert it and
// then call read().
try {
int retval;
convert_oid_fromstr(&oid,strOid,strOidLen);
retval = object->backend->read(&data,&size,&type,object->backend,&oid);
if (retval < 0) {
php_git2::git_error(retval);
}
else {
// Copy the result into the return zval. Then set the out
// parameters. Finally we have to free the buffer allocated by the
// call to read().
RETVAL_STRINGL((const char*)data,size);
ZVAL_LONG(ztype,type);
free(data);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBBackend_Internal,read_prefix)
{
php_bailer bailer;
zval* zoid;
zval* ztype;
char* strOid;
size_t strOidLen;
php_odb_backend_object* object;
// Grab object backing. Verify that the backend exists and the function is
// implemented.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->read_prefix == nullptr) {
zend_throw_error(nullptr,"GitODBBackend_Internal::read_prefix(): method is not available");
return;
}
// Grab parameters. The first three parameters are passed by reference so we
// have to extract their zvals.
if (zend_parse_parameters(
ZEND_NUM_ARGS(),
"z/z/s",
&zoid,
&ztype,
&strOid,
&strOidLen
) == FAILURE)
{
return;
}
void* data = NULL;
size_t size = 0;
git_object_t type = GIT_OBJ_ANY;
git_oid prefix;
git_oid full;
// Interpret the string parameter as a human-readable OID. Convert it and
// then call read_prefix().
try {
int retval;
convert_oid_fromstr(&prefix,strOid,strOidLen);
retval = object->backend->read_prefix(
&full,
&data,
&size,
&type,
object->backend,
&prefix,
strOidLen);
if (retval < 0) {
php_git2::git_error(retval);
}
else {
// Copy the result into the return zval. Then set the out
// parameters. Finally we have to free the buffer allocated by the
// call to read_prefix().
RETVAL_STRINGL((const char*)data,size);
convert_oid(zoid,&full);
ZVAL_LONG(ztype,type);
free(data);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBBackend_Internal,read_header)
{
php_bailer bailer;
zval* zsize;
zval* ztype;
char* strOid;
size_t strOidLen;
php_odb_backend_object* object;
size_t size = 0;
git_object_t type = GIT_OBJ_ANY;
git_oid oid;
// Grab object backing. Verify that the backend exists and the function is
// implemented.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->read_header == nullptr) {
zend_throw_error(
nullptr,
"GitODBBackend_Internal::read_header(): method is not available"
);
return;
}
// Grab parameters. The first two parameters are passed by reference so we
// have to extract their zvals.
if (zend_parse_parameters(ZEND_NUM_ARGS(),
"z/z/s",
&zsize,
&ztype,
&strOid,
&strOidLen
) == FAILURE)
{
return;
}
// Interpret the string parameter as a human-readable OID. Convert it
// and then call read().
try {
int retval;
convert_oid_fromstr(&oid,strOid,strOidLen);
retval = object->backend->read_header(&size,&type,object->backend,&oid);
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
return;
}
// This varient just returns the header as output parameters and returns
// null.
ZVAL_LONG(zsize,size);
ZVAL_LONG(ztype,type);
}
PHP_METHOD(GitODBBackend_Internal,write)
{
php_bailer bailer;
char* uoid;
size_t uoidSize;
char* data;
size_t dataSize;
zend_long type;
git_oid oid;
php_odb_backend_object* object;
// Backend must be created and write function must be available.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->write == nullptr) {
zend_throw_error(nullptr,"GitODBBackend_Internal::write(): method is not available");
return;
}
// Grab parameters.
if (zend_parse_parameters(ZEND_NUM_ARGS(),
"ssl",
&uoid,
&uoidSize,
&data,
&dataSize,
&type
) == FAILURE)
{
return;
}
// Convert OID hex string to oid structure and call underlying function.
try {
int retval;
convert_oid_fromstr(&oid,uoid,uoidSize);
retval = object->backend->write(object->backend,&oid,data,dataSize,(git_object_t)type);
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBBackend_Internal,writestream)
{
php_bailer bailer;
zend_long length;
zend_long objectType;
git_odb_stream* outstream = nullptr;
php_odb_backend_object* object;
// Backend must be created and write function must be available.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->writestream == nullptr) {
zend_throw_error(
nullptr,
"GitODBBackend_Internal::writestream(): method is not available"
);
return;
}
// Grab parameters.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"ll",&length,&objectType) == FAILURE) {
return;
}
// Call the underlying function.
try {
int retval;
retval = object->backend->writestream(
&outstream,
object->backend,
static_cast<git_object_size_t>(length),
static_cast<git_object_t>(objectType));
if (retval < 0) {
php_git2::git_error(retval);
}
else if (outstream != nullptr) {
// Create GitODBStream object to wrap git_odb_stream. Do NOT set an
// owner since we want the GitODBStream object to free the stream
// using its free() function.
php_git2_make_odb_stream(return_value,outstream,nullptr);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBBackend_Internal,readstream)
{
php_bailer bailer;
zval* zsizeRef;
zval* ztypeRef;
char* oidstr;
size_t oidstr_len;
php_odb_backend_object* object;
// Backend must be created and write function must be available.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->readstream == nullptr) {
zend_throw_error(
nullptr,
"GitODBBackend_Internal::readstream(): method is not available"
);
return;
}
// Grab parameters.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"z/z/s",&zsizeRef,&ztypeRef,&oidstr,&oidstr_len) == FAILURE) {
return;
}
// Call the underlying function.
try {
int retval;
git_oid oid;
git_odb_stream* outstream = nullptr;
size_t outsize = 0;
git_object_t outtype = GIT_OBJ_ANY;
convert_oid_fromstr(&oid,oidstr,oidstr_len);
retval = object->backend->readstream(
&outstream,
&outsize,
&outtype,
object->backend,
&oid);
if (retval < 0) {
php_git2::git_error(retval);
}
else if (outstream != nullptr) {
// Create GitODBStream object to wrap git_odb_stream. Do NOT set an
// owner since we want the GitODBStream object to free the stream
// using its free() function.
php_git2_make_odb_stream(return_value,outstream,nullptr);
}
ZVAL_LONG(zsizeRef,outsize);
ZVAL_LONG(ztypeRef,outtype);
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBBackend_Internal,exists)
{
php_bailer bailer;
int retval;
char* uoid;
size_t uoidSize;
git_oid oid;
php_odb_backend_object* object;
// Backend must be created.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->exists == nullptr) {
zend_throw_error(nullptr,"GitODBBackend_Internal::exists(): method is not available");
return;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(),"s",&uoid,&uoidSize) == FAILURE) {
return;
}
// Convert OID hex string to oid structure.
convert_oid_fromstr(&oid,uoid,uoidSize);
// Call underlying function.
try {
retval = object->backend->exists(object->backend,&oid);
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
return;
}
// The exists() function should return 1 if found.
if (retval == 1) {
RETURN_TRUE;
}
RETURN_FALSE;
}
PHP_METHOD(GitODBBackend_Internal,exists_prefix)
{
php_bailer bailer;
int retval;
zval* zoid;
char* uoid;
size_t uoidSize;
git_oid oid;
git_oid fullOid;
php_odb_backend_object* object;
// Backend must be created.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->exists_prefix == nullptr) {
zend_throw_error(
nullptr,
"GitODBBackend_Internal::exists_prefix(): method is not available"
);
return;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(),"z/s",&zoid,&uoid,&uoidSize) == FAILURE) {
return;
}
// Convert OID hex string to oid structure.
convert_oid_fromstr(&oid,uoid,uoidSize);
// Call underlying function.
try {
retval = object->backend->exists_prefix(&fullOid,object->backend,&oid,uoidSize);
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
return;
}
// Confusingly, the exists_prefix() function returns 0 if found.
if (retval == 0) {
// Set the out variable to the full OID string.
convert_oid(zoid,&fullOid);
RETURN_TRUE;
}
// No need to set the out variable. PHP already did this if it was a new
// variable. Otherwise it will just keep its old value.
RETURN_FALSE;
}
PHP_METHOD(GitODBBackend_Internal,refresh)
{
php_bailer bailer;
php_odb_backend_object* object;
// Backend must be created and the function must be implemented.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->refresh == nullptr) {
zend_throw_error(nullptr,"GitODBBackend_Internal::refresh(): method is not available");
return;
}
// Perform function call.
try {
int retval;
retval = object->backend->refresh(object->backend);
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBBackend_Internal,for_each)
{
int retval;
php_bailer bailer;
php_odb_backend_object* object;
php_callback_sync callback;
zval* zcallback;
zval* zpayload;
// Backend must be created and the function must be implemented.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(getThis());
if (object->backend == nullptr || object->backend->foreach == nullptr) {
zend_throw_error(
nullptr,
"GitODBBackend_Internal::for_each(): method is not available"
);
return;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(),"zz",&zcallback,&zpayload) == FAILURE) {
return;
}
callback.set_members(zcallback,zpayload);
// Call the underlying function.
try {
php_callback_handler<odb_foreach_callback> handler;
retval = object->backend->foreach(object->backend,
handler.byval_git2(),callback.byval_git2());
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBBackend_Internal,writepack)
{
php_bailer bailer;
zval* thisobj = getThis();
php_odb_backend_object* object;
zval* zcallback;
zval* zpayload;
git_odb_writepack* wp;
php_callback_sync_nullable* callback;
// Backend must be created and the function must be implemented.
object = php_zend_object<php_odb_backend_internal_object>::get_storage(thisobj);
if (object->backend == nullptr || object->backend->writepack == nullptr) {
zend_throw_error(
nullptr,
"GitODBBackend_Internal::writepack(): method is not available"
);
return;
}
// Create callback instance.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"zz",&zcallback,&zpayload) == FAILURE) {
return;
}
callback = new (emalloc(sizeof(php_callback_sync_nullable)))
php_callback_sync_nullable;
callback->set_members(zcallback,zpayload);
try {
using handler_t = php_callback_handler_nullable_connector<
transfer_progress_callback
>;
int retval;
handler_t handler(*callback);
// Call the underlying function.
retval = object->backend->writepack(
&wp,
object->backend,
object->backend->odb,
handler.byval_git2(),
callback->byval_git2());
if (retval < 0) {
php_git2::git_error(retval);
}
else {
// Create return value out of the writepack and callback. The
// callback will be managed by the GitODBWritepack_Internal object.
php_git2_make_odb_writepack(return_value,wp,callback,thisobj,object->owner);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
callback->~php_callback_sync_nullable();
efree(callback);
}
}
/*
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* End:
*/