Skip to content

Commit e61751a

Browse files
committed
stream: use error code non backed enums with c headers
1 parent 8526c33 commit e61751a

21 files changed

+851
-568
lines changed

build/gen_stub.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,6 +3260,7 @@ public function __construct(
32603260
private /* readonly */ array $propertyInfos,
32613261
public array $funcInfos,
32623262
private readonly array $enumCaseInfos,
3263+
private readonly bool $generateCNameTable,
32633264
public readonly ?string $cond,
32643265
public ?int $phpVersionIdMinimumCompatibility,
32653266
public readonly bool $isUndocumentable,
@@ -3454,30 +3455,49 @@ public function getCDeclarations(): string
34543455
if ($this->type !== "enum") {
34553456
return '';
34563457
}
3457-
3458+
34583459
$code = '';
3459-
3460+
34603461
if ($this->cond) {
34613462
$code .= "#if {$this->cond}\n";
34623463
}
3463-
3464+
34643465
$cEnumName = 'zend_enum_' . str_replace('\\', '_', $this->name->toString());
3465-
3466+
34663467
$code .= "typedef enum {$cEnumName} {\n";
3467-
3468+
34683469
$i = 1;
34693470
foreach ($this->enumCaseInfos as $case) {
34703471
$cName = 'ZEND_ENUM_' . str_replace('\\', '_', $this->name->toString()) . '_' . $case->name;
34713472
$code .= "\t{$cName} = {$i},\n";
34723473
$i++;
34733474
}
3474-
3475+
34753476
$code .= "} {$cEnumName};\n";
3477+
3478+
$caseCount = count($this->enumCaseInfos);
3479+
3480+
if ($this->generateCNameTable && $caseCount > 0) {
3481+
$cPrefix = 'ZEND_ENUM_' . str_replace('\\', '_', $this->name->toString());
3482+
$useGuard = $cPrefix . '_USE_NAME_TABLE';
34763483

3484+
$code .= "\n#define {$cPrefix}_CASE_COUNT {$caseCount}\n";
3485+
$code .= "\n#ifdef {$useGuard}\n";
3486+
$code .= "static const char *{$cEnumName}_case_names[{$cPrefix}_CASE_COUNT + 1] = {\n";
3487+
3488+
foreach ($this->enumCaseInfos as $case) {
3489+
$cName = $cPrefix . '_' . $case->name;
3490+
$code .= "\t[{$cName}] = \"{$case->name}\",\n";
3491+
}
3492+
3493+
$code .= "};\n";
3494+
$code .= "#endif\n";
3495+
}
3496+
34773497
if ($this->cond) {
34783498
$code .= "#endif\n";
34793499
}
3480-
3500+
34813501
return $code;
34823502
}
34833503

@@ -4996,6 +5016,7 @@ function parseClass(
49965016
$isStrictProperties = array_key_exists('strict-properties', $tagMap);
49975017
$isNotSerializable = array_key_exists('not-serializable', $tagMap);
49985018
$isUndocumentable = $isUndocumentable || array_key_exists('undocumentable', $tagMap);
5019+
$generateCNameTable = array_key_exists('c-name-table', $tagMap);
49995020
foreach ($tags as $tag) {
50005021
if ($tag->name === 'alias') {
50015022
$alias = $tag->getValue();
@@ -5057,6 +5078,7 @@ function parseClass(
50575078
$properties,
50585079
$methods,
50595080
$enumCases,
5081+
$generateCNameTable,
50605082
$cond,
50615083
$minimumPhpVersionIdCompatibility,
50625084
$isUndocumentable

ext/phar/dirstream.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -254,29 +254,29 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,
254254
phar_archive_data *phar;
255255

256256
if ((resource = phar_parse_url(wrapper, context, path, mode, options)) == NULL) {
257-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_INVALID_URL,
257+
php_stream_wrapper_log_warn(wrapper, context, options, InvalidUrl,
258258
"phar url \"%s\" is unknown", path);
259259
return NULL;
260260
}
261261

262262
/* we must have at the very least phar://alias.phar/ */
263263
if (!resource->scheme || !resource->host || !resource->path) {
264264
if (resource->host && !resource->path) {
265-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_INVALID_PATH,
265+
php_stream_wrapper_log_warn(wrapper, context, options, InvalidPath,
266266
"phar error: no directory in \"%s\", must have at least phar://%s/ for root directory (always use full path to a new phar)",
267267
path, ZSTR_VAL(resource->host));
268268
php_url_free(resource);
269269
return NULL;
270270
}
271271
php_url_free(resource);
272-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_INVALID_URL,
272+
php_stream_wrapper_log_warn(wrapper, context, options, InvalidUrl,
273273
"phar error: invalid url \"%s\", must have at least phar://%s/", path, path);
274274
return NULL;
275275
}
276276

277277
if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
278278
php_url_free(resource);
279-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_PROTOCOL_UNSUPPORTED,
279+
php_stream_wrapper_log_warn(wrapper, context, options, ProtocolUnsupported,
280280
"phar error: not a phar url \"%s\"", path);
281281
return NULL;
282282
}
@@ -285,10 +285,10 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,
285285

286286
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
287287
if (error) {
288-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_NOT_FOUND, "%s", error);
288+
php_stream_wrapper_log_warn(wrapper, context, options, NotFound, "%s", error);
289289
efree(error);
290290
} else {
291-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_NOT_FOUND,
291+
php_stream_wrapper_log_warn(wrapper, context, options, NotFound,
292292
"phar file \"%s\" is unknown", ZSTR_VAL(resource->host));
293293
}
294294
php_url_free(resource);
@@ -359,7 +359,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
359359

360360
/* pre-readonly check, we need to know if this is a data phar */
361361
if (FAILURE == phar_split_fname(url_from, strlen(url_from), &arch, &arch_len, NULL, NULL, 2, 2)) {
362-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_NOT_FOUND,
362+
php_stream_wrapper_log_warn(wrapper, context, options, NotFound,
363363
"phar error: cannot create directory \"%s\", no phar archive specified", url_from);
364364
return 0;
365365
}
@@ -371,7 +371,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
371371
efree(arch);
372372

373373
if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
374-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_READONLY,
374+
php_stream_wrapper_log_warn(wrapper, context, options, Readonly,
375375
"phar error: cannot create directory \"%s\", write operations disabled", url_from);
376376
return 0;
377377
}
@@ -383,20 +383,20 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
383383
/* we must have at the very least phar://alias.phar/internalfile.php */
384384
if (!resource->scheme || !resource->host || !resource->path) {
385385
php_url_free(resource);
386-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_INVALID_URL,
386+
php_stream_wrapper_log_warn(wrapper, context, options, InvalidUrl,
387387
"phar error: invalid url \"%s\"", url_from);
388388
return 0;
389389
}
390390

391391
if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
392392
php_url_free(resource);
393-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_PROTOCOL_UNSUPPORTED,
393+
php_stream_wrapper_log_warn(wrapper, context, options, ProtocolUnsupported,
394394
"phar error: not a phar stream url \"%s\"", url_from);
395395
return 0;
396396
}
397397

398398
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
399-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_MKDIR_FAILED,
399+
php_stream_wrapper_log_warn(wrapper, context, options, MkdirFailed,
400400
"phar error: cannot create directory \"%s\" in phar \"%s\", error retrieving phar information: %s",
401401
ZSTR_VAL(resource->path) + 1, ZSTR_VAL(resource->host), error);
402402
efree(error);
@@ -410,15 +410,15 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
410410
zend_string_efree(e->filename);
411411
efree(e);
412412
}
413-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_ALREADY_EXISTS,
413+
php_stream_wrapper_log_warn(wrapper, context, options, AlreadyExists,
414414
"phar error: cannot create directory \"%s\" in phar \"%s\", directory already exists",
415415
ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host));
416416
php_url_free(resource);
417417
return 0;
418418
}
419419

420420
if (error) {
421-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_MKDIR_FAILED,
421+
php_stream_wrapper_log_warn(wrapper, context, options, MkdirFailed,
422422
"phar error: cannot create directory \"%s\" in phar \"%s\", %s",
423423
ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
424424
efree(error);
@@ -428,15 +428,15 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
428428

429429
if (phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, ZSTR_LEN(resource->path) - 1, 0, &error, true)) {
430430
/* entry exists as a file */
431-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_ALREADY_EXISTS,
431+
php_stream_wrapper_log_warn(wrapper, context, options, AlreadyExists,
432432
"phar error: cannot create directory \"%s\" in phar \"%s\", file already exists",
433433
ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host));
434434
php_url_free(resource);
435435
return 0;
436436
}
437437

438438
if (error) {
439-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_MKDIR_FAILED,
439+
php_stream_wrapper_log_warn(wrapper, context, options, MkdirFailed,
440440
"phar error: cannot create directory \"%s\" in phar \"%s\", %s",
441441
ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
442442
efree(error);
@@ -467,7 +467,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
467467
entry.old_flags = PHAR_ENT_PERM_DEF_DIR;
468468

469469
if (NULL == zend_hash_add_mem(&phar->manifest, entry.filename, &entry, sizeof(phar_entry_info))) {
470-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_MKDIR_FAILED,
470+
php_stream_wrapper_log_warn(wrapper, context, options, MkdirFailed,
471471
"phar error: cannot create directory \"%s\" in phar \"%s\", adding to manifest failed",
472472
ZSTR_VAL(entry.filename), phar->fname);
473473
zend_string_efree(entry.filename);
@@ -477,7 +477,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
477477
phar_flush(phar, &error);
478478

479479
if (error) {
480-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_MKDIR_FAILED,
480+
php_stream_wrapper_log_warn(wrapper, context, options, MkdirFailed,
481481
"phar error: cannot create directory \"%s\" in phar \"%s\", %s",
482482
ZSTR_VAL(entry.filename), phar->fname, error);
483483
zend_hash_del(&phar->manifest, entry.filename);
@@ -503,7 +503,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
503503

504504
/* pre-readonly check, we need to know if this is a data phar */
505505
if (FAILURE == phar_split_fname(url, strlen(url), &arch, &arch_len, NULL, NULL, 2, 2)) {
506-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_NOT_FOUND,
506+
php_stream_wrapper_log_warn(wrapper, context, options, NotFound,
507507
"phar error: cannot remove directory \"%s\", no phar archive specified, or phar archive does not exist", url);
508508
return 0;
509509
}
@@ -515,7 +515,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
515515
efree(arch);
516516

517517
if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
518-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_READONLY,
518+
php_stream_wrapper_log_warn(wrapper, context, options, Readonly,
519519
"phar error: cannot rmdir directory \"%s\", write operations disabled", url);
520520
return 0;
521521
}
@@ -527,20 +527,20 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
527527
/* we must have at the very least phar://alias.phar/internalfile.php */
528528
if (!resource->scheme || !resource->host || !resource->path) {
529529
php_url_free(resource);
530-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_INVALID_URL,
530+
php_stream_wrapper_log_warn(wrapper, context, options, InvalidUrl,
531531
"phar error: invalid url \"%s\"", url);
532532
return 0;
533533
}
534534

535535
if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
536536
php_url_free(resource);
537-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_PROTOCOL_UNSUPPORTED,
537+
php_stream_wrapper_log_warn(wrapper, context, options, ProtocolUnsupported,
538538
"phar error: not a phar stream url \"%s\"", url);
539539
return 0;
540540
}
541541

542542
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
543-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_RMDIR_FAILED,
543+
php_stream_wrapper_log_warn(wrapper, context, options, RmdirFailed,
544544
"phar error: cannot remove directory \"%s\" in phar \"%s\", error retrieving phar information: %s",
545545
ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
546546
efree(error);
@@ -552,12 +552,12 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
552552

553553
if (!(entry = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, path_len, 2, &error, true))) {
554554
if (error) {
555-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_RMDIR_FAILED,
555+
php_stream_wrapper_log_warn(wrapper, context, options, RmdirFailed,
556556
"phar error: cannot remove directory \"%s\" in phar \"%s\", %s",
557557
ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
558558
efree(error);
559559
} else {
560-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_NOT_FOUND,
560+
php_stream_wrapper_log_warn(wrapper, context, options, NotFound,
561561
"phar error: cannot remove directory \"%s\" in phar \"%s\", directory does not exist",
562562
ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host));
563563
}
@@ -573,7 +573,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
573573
zend_string_starts_with_cstr(str_key, ZSTR_VAL(resource->path)+1, path_len)
574574
&& IS_SLASH(ZSTR_VAL(str_key)[path_len])
575575
) {
576-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_RMDIR_FAILED,
576+
php_stream_wrapper_log_warn(wrapper, context, options, RmdirFailed,
577577
"phar error: Directory not empty");
578578
if (entry->is_temp_dir) {
579579
zend_string_efree(entry->filename);
@@ -590,7 +590,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
590590
zend_string_starts_with_cstr(str_key, ZSTR_VAL(resource->path)+1, path_len)
591591
&& IS_SLASH(ZSTR_VAL(str_key)[path_len])
592592
) {
593-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_RMDIR_FAILED,
593+
php_stream_wrapper_log_warn(wrapper, context, options, RmdirFailed,
594594
"phar error: Directory not empty");
595595
if (entry->is_temp_dir) {
596596
zend_string_efree(entry->filename);
@@ -612,7 +612,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
612612
phar_flush(phar, &error);
613613

614614
if (error) {
615-
php_stream_wrapper_log_warn(wrapper, context, options, STREAM_ERROR_CODE_RMDIR_FAILED,
615+
php_stream_wrapper_log_warn(wrapper, context, options, RmdirFailed,
616616
"phar error: cannot remove directory \"%s\" in phar \"%s\", %s",
617617
ZSTR_VAL(entry->filename), phar->fname, error);
618618
php_url_free(resource);

0 commit comments

Comments
 (0)