Skip to content

Commit 6f1501a

Browse files
iamacarpetiluuu1994
authored andcommitted
Add opcache_is_script_cached_in_file_cache() function
Closes GH-16979
1 parent b9844b5 commit 6f1501a

8 files changed

+121
-1
lines changed

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ PHP 8.5 UPGRADE NOTES
430430
. Added grapheme_levenshtein() function.
431431
RFC: https://wiki.php.net/rfc/grapheme_levenshtein
432432

433+
- Opcache:
434+
. Added opcache_is_script_cached_in_file_cache().
435+
433436
- Pdo\Sqlite:
434437
. Added support for Pdo\Sqlite::setAuthorizer(), which is the equivalent of
435438
SQLite3::setAuthorizer(). The only interface difference is that the

ext/opcache/opcache.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ function opcache_jit_blacklist(Closure $closure): void {}
2323
function opcache_get_configuration(): array|false {}
2424

2525
function opcache_is_script_cached(string $filename): bool {}
26+
27+
function opcache_is_script_cached_in_file_cache(string $filename): bool {}

ext/opcache/opcache_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
function test() {}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
GH-16979: Test opcache_is_script_cached_in_file_cache function
3+
--SKIPIF--
4+
<?php
5+
@mkdir(__DIR__ . '/gh16979_cache', 0777, true);
6+
?>
7+
--INI--
8+
opcache.enable=1
9+
opcache.enable_cli=1
10+
opcache.jit=disable
11+
opcache.file_cache="{PWD}/gh16979_cache"
12+
opcache.file_update_protection=0
13+
--EXTENSIONS--
14+
opcache
15+
--FILE--
16+
<?php
17+
18+
$file = __DIR__ . '/gh16979_check_file_cache_function.inc';
19+
var_dump(opcache_is_script_cached_in_file_cache($file));
20+
opcache_compile_file($file);
21+
var_dump(opcache_is_script_cached_in_file_cache($file));
22+
opcache_invalidate($file, force: true);
23+
var_dump(opcache_is_script_cached_in_file_cache($file));
24+
25+
?>
26+
--CLEAN--
27+
<?php
28+
function removeDirRecursive($dir) {
29+
if (!is_dir($dir)) return;
30+
$iterator = new RecursiveIteratorIterator(
31+
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
32+
RecursiveIteratorIterator::CHILD_FIRST
33+
);
34+
foreach ($iterator as $fileinfo) {
35+
if ($fileinfo->isDir()) {
36+
@rmdir($fileinfo->getRealPath());
37+
} else {
38+
@unlink($fileinfo->getRealPath());
39+
}
40+
}
41+
@rmdir($dir);
42+
}
43+
removeDirRecursive(__DIR__ . '/gh16979_cache');
44+
?>
45+
--EXPECT--
46+
bool(false)
47+
bool(true)
48+
bool(false)

ext/opcache/zend_accelerator_module.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "zend_closures.h"
2828
#include "zend_shared_alloc.h"
2929
#include "zend_accelerator_blacklist.h"
30+
#include "zend_file_cache.h"
3031
#include "php_ini.h"
3132
#include "SAPI.h"
3233
#include "zend_virtual_cwd.h"
@@ -364,6 +365,23 @@ static int filename_is_in_cache(zend_string *filename)
364365
return 0;
365366
}
366367

368+
static int filename_is_in_file_cache(zend_string *filename)
369+
{
370+
zend_string *realpath = zend_resolve_path(filename);
371+
if (!realpath) {
372+
return 0;
373+
}
374+
375+
zend_file_handle handle;
376+
zend_stream_init_filename_ex(&handle, filename);
377+
handle.opened_path = realpath;
378+
379+
zend_persistent_script *result = zend_file_cache_script_load_ex(&handle, true);
380+
zend_destroy_file_handle(&handle);
381+
382+
return result != NULL;
383+
}
384+
367385
static int accel_file_in_cache(INTERNAL_FUNCTION_PARAMETERS)
368386
{
369387
if (ZEND_NUM_ARGS() == 1) {
@@ -999,3 +1017,27 @@ ZEND_FUNCTION(opcache_is_script_cached)
9991017

10001018
RETURN_BOOL(filename_is_in_cache(script_name));
10011019
}
1020+
1021+
/* {{{ Return true if the script is cached in OPCache file cache, false if it is not cached or if OPCache is not running. */
1022+
ZEND_FUNCTION(opcache_is_script_cached_in_file_cache)
1023+
{
1024+
zend_string *script_name;
1025+
1026+
ZEND_PARSE_PARAMETERS_START(1, 1)
1027+
Z_PARAM_STR(script_name)
1028+
ZEND_PARSE_PARAMETERS_END();
1029+
1030+
if (!validate_api_restriction()) {
1031+
RETURN_FALSE;
1032+
}
1033+
1034+
if (!(ZCG(accelerator_enabled) || ZCG(accel_directives).file_cache_only)) {
1035+
RETURN_FALSE;
1036+
}
1037+
1038+
if (!ZCG(accel_directives).file_cache) {
1039+
RETURN_FALSE;
1040+
}
1041+
1042+
RETURN_BOOL(filename_is_in_file_cache(script_name));
1043+
}

ext/opcache/zend_file_cache.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,14 @@ static void zend_file_cache_unserialize(zend_persistent_script *script,
18711871
zend_file_cache_unserialize_early_bindings(script, buf);
18721872
}
18731873

1874+
static zend_persistent_script file_cache_validate_success_script;
1875+
18741876
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle)
1877+
{
1878+
return zend_file_cache_script_load_ex(file_handle, false);
1879+
}
1880+
1881+
zend_persistent_script *zend_file_cache_script_load_ex(zend_file_handle *file_handle, bool validate_only)
18751882
{
18761883
zend_string *full_path = file_handle->opened_path;
18771884
int fd;
@@ -1948,6 +1955,16 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
19481955
return NULL;
19491956
}
19501957

1958+
/* return here if validating */
1959+
if (validate_only) {
1960+
if (zend_file_cache_flock(fd, LOCK_UN) != 0) {
1961+
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
1962+
}
1963+
close(fd);
1964+
efree(filename);
1965+
return &file_cache_validate_success_script;
1966+
}
1967+
19511968
checkpoint = zend_arena_checkpoint(CG(arena));
19521969
#if defined(__AVX__) || defined(__SSE2__)
19531970
/* Align to 64-byte boundary */

ext/opcache/zend_file_cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm);
2323
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle);
24+
zend_persistent_script *zend_file_cache_script_load_ex(zend_file_handle *file_handle, bool validate_only);
2425
void zend_file_cache_invalidate(zend_string *full_path);
2526

2627
#endif /* ZEND_FILE_CACHE_H */

0 commit comments

Comments
 (0)