forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-odb-writepack-internal.cpp
339 lines (277 loc) · 8.51 KB
/
php-odb-writepack-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
/**
* php-odb-writepack-internal.cpp
*
* Copyright (C) Roger P. Gee
*/
#include "php-object.h"
using namespace php_git2;
// Class method entries
static PHP_METHOD(GitODBWritepack_Internal,append);
static PHP_METHOD(GitODBWritepack_Internal,commit);
zend_function_entry php_git2::odb_writepack_internal_methods[] = {
PHP_ME(GitODBWritepack_Internal,append,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBWritepack_Internal,commit,NULL,ZEND_ACC_PUBLIC)
PHP_FE_END
};
// Custom class handlers.
static zval* odb_writepack_internal_read_property(
zval* object,
zval* member,
int type,
void** cache_slot,
zval* rv);
#if PHP_API_VERSION >= 20190902
static zval* odb_writepack_internal_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot);
#else
static void odb_writepack_internal_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot);
#endif
static int odb_writepack_internal_has_property(
zval* object,
zval* member,
int has_set_exists,
void** cache_slot);
// Make function implementation
void php_git2::php_git2_make_odb_writepack(
zval* zp,
git_odb_writepack* writepack,
php_callback_sync* cb,
zval* backend,
php_git_odb* owner)
{
php_odb_writepack_internal_object* obj;
object_init_ex(zp,php_git2::class_entry[php_git2_odb_writepack_internal_obj]);
obj = php_zend_object<php_odb_writepack_internal_object>::get_storage(Z_OBJ_P(zp));
// Assign the writepack and callback. The callback is merely along for the
// ride so it can be destroyed at the proper time.
obj->writepack = writepack;
obj->cb = cb;
// Assign owner. If set, this will prevent the parent ODB from freeing
// during the lifetime of the writepack.
obj->assign_owner(owner);
// Assign backend if we have a backend specified. This will prevent the
// parent backend object from freeing while the writepack is in use. We keep
// this reference so we can refer to the same backend used to create the
// writepack instead of creating a new one via the ODB.
if (backend != nullptr) {
ZVAL_COPY(&obj->backend,backend);
}
}
// php_zend_object init function
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_writepack_object>::handlers;
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_writepack_internal_object>::handlers;
template<>
void php_zend_object<php_odb_writepack_internal_object>::init(zend_class_entry* ce)
{
handlers.read_property = odb_writepack_internal_read_property;
handlers.write_property = odb_writepack_internal_write_property;
handlers.has_property = odb_writepack_internal_has_property;
handlers.get_constructor = php_git2::not_allowed_get_constructor;
handlers.offset = offset();
UNUSED(ce);
}
php_odb_writepack_internal_object::php_odb_writepack_internal_object():
php_odb_writepack_object(), cb(nullptr)
{
memset(&prog,0,sizeof(git_transfer_progress));
ZVAL_UNDEF(&backend);
}
php_odb_writepack_internal_object::~php_odb_writepack_internal_object()
{
// Free the writepack. Note: we always are responsible for freeing the
// writepack from an internal object.
if (writepack != nullptr) {
writepack->free(writepack);
}
// Destroy any associated callback.
if (cb != nullptr) {
cb->~php_callback_sync();
efree(cb);
}
zval_ptr_dtor(&backend);
}
// Implementation of custom class handlers.
zval* odb_writepack_internal_read_property(
zval* object,
zval* member,
int type,
void** cache_slot,
zval* rv)
{
zval* retval = rv;
zval tmp_member;
php_odb_writepack_internal_object* storage;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
// Handle special properties of the git_odb_writepack.
using zend_object_t = php_zend_object<php_odb_writepack_internal_object>;
storage = zend_object_t::get_storage(Z_OBJ_P(object));
if (strcmp(Z_STRVAL_P(member),"progress") == 0) {
php_git2::convert_transfer_progress(retval,&storage->prog);
}
else if (strcmp(Z_STRVAL_P(member),"backend") == 0
&& Z_TYPE(storage->backend) != IS_UNDEF)
{
ZVAL_COPY_VALUE(retval,&storage->backend);
}
else {
// Invoke base class handler.
auto handler = php_zend_object<php_odb_writepack_object>::handlers.read_property;
retval = (*handler)(object,member,type,cache_slot,rv);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return retval;
}
#if PHP_API_VERSION >= 20190902
zval* odb_writepack_internal_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot)
{
zval* result = value;
zval tmp_member;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
if (strcmp(Z_STRVAL_P(member),"progress") == 0) {
zend_throw_error(
nullptr,
"Property '%s' of GitODBWritepack_Internal cannot be updated",
Z_STRVAL_P(member));
}
else {
// Invoke base class handler.
auto handler = php_zend_object<php_odb_writepack_object>::handlers.write_property;
result = (*handler)(object,member,value,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return result;
}
#else
void odb_writepack_internal_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot)
{
zval tmp_member;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
if (strcmp(Z_STRVAL_P(member),"progress") == 0) {
zend_throw_error(
nullptr,
"Property '%s' of GitODBWritepack_Internal cannot be updated",
Z_STRVAL_P(member));
}
else {
// Invoke base class handler.
auto handler = php_zend_object<php_odb_writepack_object>::handlers.write_property;
(*handler)(object,member,value,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
}
#endif
int odb_writepack_internal_has_property(
zval* object,
zval* member,
int has_set_exists,
void** cache_slot)
{
int result;
zval tmp_member;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
if (strcmp(Z_STRVAL_P(member),"progress") == 0) {
result = true;
}
else {
// Invoke base class handler.
auto handler = php_zend_object<php_odb_writepack_object>::handlers.has_property;
result = (*handler)(object,member,has_set_exists,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return result;
}
// Implementation of object methods
PHP_METHOD(GitODBWritepack_Internal,append)
{
php_bailer bailer;
int result;
char* buf;
size_t amt;
php_odb_writepack_internal_object* object;
object = php_zend_object<php_odb_writepack_internal_object>::get_storage(getThis());
assert(object->writepack != nullptr);
if (zend_parse_parameters(ZEND_NUM_ARGS(),"s",&buf,&amt) != SUCCESS) {
return;
}
try {
result = object->writepack->append(object->writepack,buf,amt,&object->prog);
if (result < 0) {
php_git2::git_error(result);
}
} catch (php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBWritepack_Internal,commit)
{
php_bailer bailer;
int result;
php_odb_writepack_internal_object* object;
object = php_zend_object<php_odb_writepack_internal_object>::get_storage(getThis());
assert(object->writepack != nullptr);
try {
result = object->writepack->commit(object->writepack,&object->prog);
if (result < 0) {
php_git2::git_error(result);
}
} catch (php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
/*
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* End:
*/