Skip to content

Commit 268e492

Browse files
author
Phil E. Taylor
authored
[4] Ensure opcache file invalidate on file copy & delete (#32915)
1 parent a0e2f45 commit 268e492

File tree

10 files changed

+163
-76
lines changed

10 files changed

+163
-76
lines changed

administrator/components/com_admin/script.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8160,8 +8160,8 @@ protected function fixFilenameCasing()
81608160
if ($newBasename !== $expectedBasename)
81618161
{
81628162
// Rename the file.
8163-
rename(JPATH_ROOT . $old, JPATH_ROOT . $old . '.tmp');
8164-
rename(JPATH_ROOT . $old . '.tmp', JPATH_ROOT . $expected);
8163+
File::move(JPATH_ROOT . $old, JPATH_ROOT . $old . '.tmp');
8164+
File::move(JPATH_ROOT . $old . '.tmp', JPATH_ROOT . $expected);
81658165

81668166
continue;
81678167
}
@@ -8176,14 +8176,14 @@ protected function fixFilenameCasing()
81768176
if (!in_array($expectedBasename, scandir(dirname($newRealpath))))
81778177
{
81788178
// Rename the file.
8179-
rename(JPATH_ROOT . $old, JPATH_ROOT . $old . '.tmp');
8180-
rename(JPATH_ROOT . $old . '.tmp', JPATH_ROOT . $expected);
8179+
File::move(JPATH_ROOT . $old, JPATH_ROOT . $old . '.tmp');
8180+
File::move(JPATH_ROOT . $old . '.tmp', JPATH_ROOT . $expected);
81818181
}
81828182
}
81838183
else
81848184
{
81858185
// On Unix with both files: Delete the incorrectly cased file.
8186-
unlink(JPATH_ROOT . $old);
8186+
File::delete(JPATH_ROOT . $old);
81878187
}
81888188
}
81898189
}

administrator/components/com_config/src/Model/ApplicationModel.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -956,12 +956,6 @@ private function writeConfigFile(Registry $config)
956956
throw new \RuntimeException(Text::_('COM_CONFIG_ERROR_WRITE_FAILED'));
957957
}
958958

959-
// Invalidates the cached configuration file
960-
if (function_exists('opcache_invalidate'))
961-
{
962-
\opcache_invalidate($file);
963-
}
964-
965959
// Attempt to make the file unwriteable.
966960
if (Path::isOwner($file) && !Path::setPermissions($file, '0444'))
967961
{

administrator/components/com_joomlaupdate/restore.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ public function createDirRecursive($dirName, $perms)
14941494
// Is this a file instead of a directory?
14951495
if (is_file($root . $path))
14961496
{
1497+
$this->clearFileInOPCache($root . $path);
14971498
@unlink($root . $path);
14981499
$ret = @mkdir($root . $path);
14991500
}
@@ -1523,6 +1524,8 @@ public function chmod($file, $perms)
15231524

15241525
public function unlink($file)
15251526
{
1527+
$this->clearFileInOPCache($file);
1528+
15261529
return @unlink($file);
15271530
}
15281531

@@ -1533,7 +1536,20 @@ public function rmdir($directory)
15331536

15341537
public function rename($from, $to)
15351538
{
1536-
return @rename($from, $to);
1539+
$this->clearFileInOPCache($from);
1540+
$ret = @rename($from, $to);
1541+
$this->clearFileInOPCache($to);
1542+
1543+
return $ret;
1544+
}
1545+
1546+
public function clearFileInOPCache($file){
1547+
if (ini_get('opcache.enable')
1548+
&& function_exists('opcache_invalidate')
1549+
&& (!ini_get('opcache.restrict_api') || stripos(realpath($_SERVER['SCRIPT_FILENAME']), ini_get('opcache.restrict_api')) === 0))
1550+
{
1551+
\opcache_invalidate($file, true);
1552+
}
15371553
}
15381554

15391555
}
@@ -5105,10 +5121,13 @@ public function __toString()
51055121
$filename = dirname(__FILE__) . '/restore_finalisation.php';
51065122
if (file_exists($filename))
51075123
{
5108-
// opcode cache busting before including the filename
5109-
if (function_exists('opcache_invalidate'))
5124+
// We cannot use the Filesystem API here.
5125+
if (ini_get('opcache.enable')
5126+
&& function_exists('opcache_invalidate')
5127+
&& (!ini_get('opcache.restrict_api') || stripos(realpath($_SERVER['SCRIPT_FILENAME']), ini_get('opcache.restrict_api')) === 0)
5128+
)
51105129
{
5111-
\opcache_invalidate($filename);
5130+
\opcache_invalidate($filename, true);
51125131
}
51135132
if (function_exists('apc_compile_file'))
51145133
{

administrator/components/com_joomlaupdate/restore_finalisation.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ function finalizeRestore($siteRoot, $restorePath)
7474
{
7575
(new JoomlaInstallerScript)->deleteUnexistingFiles();
7676
}
77-
78-
// Clear OPcache
79-
if (function_exists('opcache_reset'))
80-
{
81-
\opcache_reset();
82-
}
8377
}
8478
}
8579
}
@@ -97,7 +91,7 @@ function finalizeRestore($siteRoot, $restorePath)
9791
abstract class File
9892
{
9993
/**
100-
* Proxies checking a folder exists to the native php version
94+
* Proxies checking a file exists to the native php version
10195
*
10296
* @param string $fileName The path to the file to be checked
10397
*
@@ -121,9 +115,26 @@ public static function exists($fileName)
121115
*/
122116
public static function delete($fileName)
123117
{
118+
/** @var \AKPostprocDirect $postproc */
124119
$postproc = \AKFactory::getPostProc();
125120
$postproc->unlink($fileName);
126121
}
122+
/**
123+
* Proxies moving a file to the restore.php version
124+
*
125+
* @param string $src The path to the source file
126+
* @param string $dest The path to the destination file
127+
*
128+
* @return boolean True on success
129+
*
130+
* @since __DEPLOY_VERSION__
131+
*/
132+
public static function move($src, $dest)
133+
{
134+
/** @var \AKPostprocDirect $postproc */
135+
$postproc = \AKFactory::getPostProc();
136+
$postproc->rename($src, $dest);
137+
}
127138
}
128139
}
129140

administrator/components/com_joomlaupdate/src/Model/UpdateModel.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -863,28 +863,13 @@ public function cleanUp()
863863
$tempdir = $app->get('tmp_path');
864864

865865
$file = $app->getUserState('com_joomlaupdate.file', null);
866-
$target = $tempdir . '/' . $file;
867-
868-
if (!@unlink($target))
869-
{
870-
File::delete($target);
871-
}
866+
File::delete($tempdir . '/' . $file);
872867

873868
// Remove the restoration.php file.
874-
$target = JPATH_COMPONENT_ADMINISTRATOR . '/restoration.php';
875-
876-
if (!@unlink($target))
877-
{
878-
File::delete($target);
879-
}
869+
File::delete(JPATH_COMPONENT_ADMINISTRATOR . '/restoration.php');
880870

881871
// Remove joomla.xml from the site's root.
882-
$target = JPATH_ROOT . '/joomla.xml';
883-
884-
if (!@unlink($target))
885-
{
886-
File::delete($target);
887-
}
872+
File::delete(JPATH_ROOT . '/joomla.xml');
888873

889874
// Unset the update filename from the session.
890875
$app = Factory::getApplication();
@@ -1044,10 +1029,7 @@ public function removePackageFiles()
10441029
{
10451030
if (File::exists($file))
10461031
{
1047-
if (!@unlink($file))
1048-
{
1049-
File::delete($file);
1050-
}
1032+
File::delete($file);
10511033
}
10521034
}
10531035
}

installation/src/Model/CleanupModel.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,6 @@ class CleanupModel extends BaseInstallationModel
3030
*/
3131
public function deleteInstallationFolder()
3232
{
33-
// TODO: Move this to the generic library method once #32915 is merged
34-
if (ini_get('opcache.enable')
35-
&& function_exists('opcache_invalidate')
36-
&& (!ini_get('opcache.restrict_api') || stripos(realpath($_SERVER['SCRIPT_FILENAME']), ini_get('opcache.restrict_api')) === 0))
37-
{
38-
try
39-
{
40-
\opcache_invalidate(JPATH_INSTALLATION . '/index.php', true);
41-
}
42-
catch (\Exception $e)
43-
{
44-
// Silently accept invalidation error
45-
}
46-
}
47-
4833
$return = Folder::delete(JPATH_INSTALLATION) && (!file_exists(JPATH_ROOT . '/joomla.xml') || File::delete(JPATH_ROOT . '/joomla.xml'));
4934

5035
// Rename the robots.txt.dist file if robots.txt doesn't exist

installation/src/Model/ConfigurationModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ private function deleteConfiguration()
665665

666666
if (file_exists($path))
667667
{
668-
unlink($path);
668+
File::delete($path);
669669
}
670670
}
671671
}

libraries/src/Cache/Storage/FileStorage.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
\defined('JPATH_PLATFORM') or die;
1212

1313
use Joomla\CMS\Cache\CacheStorage;
14+
use Joomla\CMS\Filesystem\File;
1415
use Joomla\CMS\Language\Text;
1516
use Joomla\CMS\Log\Log;
1617

@@ -68,6 +69,7 @@ public function __construct($options = array())
6869
// Delete only the existing file if it is empty.
6970
if (@filesize($path) === 0)
7071
{
72+
File::invalidateFileCache($path);
7173
@unlink($path);
7274
}
7375

@@ -240,6 +242,7 @@ public function remove($id, $group)
240242
{
241243
$path = $this->_getFilePath($id, $group);
242244

245+
File::invalidateFileCache($path);
243246
if (!@unlink($path))
244247
{
245248
return false;
@@ -319,6 +322,7 @@ public function gc()
319322

320323
if (($time + $this->_lifetime) < $this->_now || empty($time))
321324
{
325+
File::invalidateFileCache($file);
322326
$result |= @unlink($file);
323327
}
324328
}
@@ -437,6 +441,7 @@ protected function _checkExpire($id, $group)
437441

438442
if (($time + $this->_lifetime) < $this->_now || empty($time))
439443
{
444+
File::invalidateFileCache($path);
440445
@unlink($path);
441446

442447
return false;
@@ -523,6 +528,7 @@ protected function _deleteFolder($path)
523528

524529
if (!empty($files) && !\is_array($files))
525530
{
531+
File::invalidateFileCache($files);
526532
if (@unlink($files) !== true)
527533
{
528534
return false;
@@ -534,7 +540,9 @@ protected function _deleteFolder($path)
534540
{
535541
$file = $this->_cleanPath($file);
536542

537-
// In case of restricted permissions we zap it one way or the other as long as the owner is either the webserver or the ftp
543+
// In case of restricted permissions we delete it one way or the other as long as the owner is either the webserver or the ftp
544+
File::invalidateFileCache($file);
545+
538546
if (@unlink($file) !== true)
539547
{
540548
Log::add(__METHOD__ . ' ' . Text::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', basename($file)), Log::WARNING, 'jerror');

0 commit comments

Comments
 (0)