forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-odb-writepack.cpp
387 lines (319 loc) · 10 KB
/
php-odb-writepack.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
/*
* php-odb-writepack.cpp
*
* Copyright (C) Roger P. Gee
*
* This unit provides the out-of-line implementation for the GitODBWritepack
* class.
*/
#include "php-object.h"
using namespace php_git2;
// Custom class handlers
static zval* odb_writepack_read_property(
zval* object,
zval* member,
int type,
void** cache_slot,
zval* rv);
#if PHP_API_VERSION >= 20190902
static zval* odb_writepack_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot);
#else
static void odb_writepack_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot);
#endif
static int odb_writepack_has_property(
zval* object,
zval* member,
int has_set_exists,
void** cache_slot);
// Class method entries
zend_function_entry php_git2::odb_writepack_methods[] = {
PHP_ABSTRACT_ME(GitODBWritepack,append,NULL)
PHP_ABSTRACT_ME(GitODBWritepack,commit,NULL)
PHP_FE_END
};
// php_zend_object init function
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_writepack_object>::handlers;
template<>
void php_zend_object<php_odb_writepack_object>::init(zend_class_entry* ce)
{
handlers.read_property = odb_writepack_read_property;
handlers.write_property = odb_writepack_write_property;
handlers.has_property = odb_writepack_has_property;
handlers.offset = offset();
UNUSED(ce);
}
// Implementation of php_odb_writepack_object
php_odb_writepack_object::php_odb_writepack_object():
writepack(nullptr), owner(nullptr)
{
}
php_odb_writepack_object::~php_odb_writepack_object()
{
// Attempt to free the owner resource. This only really frees the owner if
// its refcount reaches zero.
if (owner != nullptr) {
git2_resource_base::free_recursive(owner);
}
}
void php_odb_writepack_object::create_custom_writepack(zval* zobj,zval* zbackend)
{
// NOTE: this member function should be called under the php-git2 standard
// exception handler.
// Make sure the writepack does not already exist.
if (writepack != nullptr) {
throw php_git2_error_exception(
"cannot create custom ODB writepack - object already in use"
);
}
// Unset any previous 'zbackend' property.
zend_unset_property(Z_OBJCE_P(zobj),zobj,"backend",sizeof("backend")-1);
// Assign 'zbackend' property. This will prevent the parent backend from
// freeing while the writepack is in use.
Z_ADDREF_P(zbackend);
zend_hash_str_add(Z_OBJPROP_P(zobj),"backend",sizeof("backend")-1,zbackend);
// Create new custom writepack.
writepack = new (emalloc(sizeof(git_odb_writepack_php)))
git_odb_writepack_php(Z_OBJ_P(zobj));
// Reset owner (in case it is set). We do not need to maintain a direct
// owner reference since we are maintaining a reference to the backend in
// the object's properties hashtable.
assign_owner(nullptr);
}
void php_odb_writepack_object::assign_owner(php_git_odb* newOwner)
{
// Free previous owner if any.
if (owner != nullptr) {
git2_resource_base::free_recursive(owner);
}
owner = newOwner;
// Increment reference count (if set). This keeps the ODB alive while the
// writepack is in use.
if (owner != nullptr) {
owner->up_ref();
}
}
/*static*/ int php_odb_writepack_object::append(
git_odb_writepack* writepack,
const void* data,
size_t length,
git_transfer_progress* prog)
{
int result;
zval_array<2> params;
method_wrapper method("append",writepack);
ZVAL_STRINGL(params[0],reinterpret_cast<const char*>(data),length);
params.make_ref(1);
convert_transfer_progress(Z_REFVAL_P(params[1]),prog);
result = method.call(params);
if (result == GIT_OK) {
// Write back transfer progress to library structure.
convert_transfer_progress(*prog,Z_REFVAL_P(params[1]));
}
return result;
}
/*static*/ int php_odb_writepack_object::commit(
git_odb_writepack* writepack,
git_transfer_progress* prog)
{
int result;
zval_array<1> params;
method_wrapper method("commit",writepack);
params.make_ref(0);
convert_transfer_progress(Z_REFVAL_P(params[0]),prog);
result = method.call(params);
if (result == GIT_OK) {
// Write back transfer progress to library structure.
convert_transfer_progress(*prog,Z_REFVAL_P(params[0]));
}
return result;
}
/*static*/ void php_odb_writepack_object::free(git_odb_writepack* writepack)
{
method_wrapper::object_wrapper object(writepack);
// Explicitly call the destructor on the custom writepack. Then free the
// block of memory that holds the custom writepack.
object.object()->~git_odb_writepack_php();
efree(writepack);
}
// Implementation of custom git_odb_writepack
php_odb_writepack_object::
git_odb_writepack_php::git_odb_writepack_php(zend_object* obj)
{
// Blank out the base class (which is the structure defined in the git2
// headers).
memset(this,0,sizeof(struct git_odb_writepack));
// Assign zend_object to local zval so it is kept alive for duration of
// custom backend.
ZVAL_OBJ(&thisobj,obj);
Z_ADDREF(thisobj);
// Assign functions that will invoke the PHP methods in the userspace
// class. Note that the methods are necessarily overridden since they are
// abstract in the base class.
append = php_odb_writepack_object::append;
commit = php_odb_writepack_object::commit;
free = php_odb_writepack_object::free;
}
php_odb_writepack_object::
git_odb_writepack_php::~git_odb_writepack_php()
{
// Free object zval.
zval_ptr_dtor(&thisobj);
}
// Implementation of custom object handlers
zval* odb_writepack_read_property(
zval* object,
zval* member,
int type,
void** cache_slot,
zval* rv)
{
zval* retval = rv;
zval tmp_member;
php_odb_writepack_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.
storage = php_zend_object<php_odb_writepack_object>::get_storage(Z_OBJ_P(object));
if (strcmp(Z_STRVAL_P(member),"backend") == 0) {
zval* lookup;
lookup = zend_hash_find(Z_OBJPROP_P(object),Z_STR_P(member));
if (lookup == nullptr) {
// NOTE: We can only safely create a backend object if an owner is
// set on the writepack.
if (storage->writepack != nullptr && storage->owner != nullptr) {
// Create GitODBBackend object instance to represent the
// git_odb_backend attached to the writepack. The object does
// not own the underlying backend object since we pass an owner.
php_git2_make_odb_backend(
retval,
storage->writepack->backend,
storage->owner);
Z_ADDREF_P(retval);
zend_hash_add(Z_OBJPROP_P(object),Z_STR_P(member),retval);
}
}
else {
retval = lookup;
}
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
retval = std->read_property(object,member,type,cache_slot,rv);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return retval;
}
#if PHP_API_VERSION >= 20190902
zval* odb_writepack_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),"backend") == 0) {
zend_throw_error(
nullptr,
"Property '%s' of GitODBWritepack cannot be updated",
Z_STRVAL_P(member));
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
result = std->write_property(object,member,value,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return result;
}
#else
void odb_writepack_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),"backend") == 0) {
zend_throw_error(
nullptr,
"Property '%s' of GitODBWritepack cannot be updated",
Z_STRVAL_P(member));
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
std->write_property(object,member,value,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
}
#endif
int odb_writepack_has_property(
zval* object,
zval* member,
int has_set_exists,
void** cache_slot)
{
int result;
zval tmp_member;
git_odb_writepack* writepack;
// 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;
}
using zend_object_t = php_zend_object<php_odb_writepack_object>;
writepack = zend_object_t::get_storage(Z_OBJ_P(object))->writepack;
if (strcmp(Z_STRVAL_P(member),"backend") == 0) {
if (has_set_exists == 2) {
result = (writepack != nullptr);
}
else {
result = (writepack != nullptr && writepack->backend != nullptr);
}
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
result = std->has_property(object,member,has_set_exists,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return result;
}
/*
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* End:
*/