Skip to content

Commit 1f617a5

Browse files
Quetzacoalt91ga-devfront
authored andcommitted
Prevent issues to happen while unlinking a non-existing but cached file
1 parent 4e620c8 commit 1f617a5

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,6 @@ protected function cleanXmlFiles(): void
703703
{
704704
$files = [
705705
$this->container->getProperty(UpgradeContainer::PS_ADMIN_PATH) . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'header.tpl',
706-
_PS_ROOT_DIR_ . '/app/cache/dev/class_index.php',
707-
_PS_ROOT_DIR_ . '/app/cache/prod/class_index.php',
708-
_PS_ROOT_DIR_ . '/cache/class_index.php',
709706
_PS_ROOT_DIR_ . '/config/xml/blog-fr.xml',
710707
_PS_ROOT_DIR_ . '/config/xml/default_country_modules_list.xml',
711708
_PS_ROOT_DIR_ . '/config/xml/modules_list.xml',
@@ -714,8 +711,6 @@ protected function cleanXmlFiles(): void
714711
_PS_ROOT_DIR_ . '/config/xml/tab_modules_list.xml',
715712
_PS_ROOT_DIR_ . '/config/xml/trusted_modules_list.xml',
716713
_PS_ROOT_DIR_ . '/config/xml/untrusted_modules_list.xml',
717-
_PS_ROOT_DIR_ . '/var/cache/dev/class_index.php',
718-
_PS_ROOT_DIR_ . '/var/cache/prod/class_index.php',
719714
];
720715
foreach ($files as $path) {
721716
if ($this->fileSystem->exists($path)) {

classes/UpgradeTools/FilesystemAdapter.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
namespace PrestaShop\Module\AutoUpgrade\UpgradeTools;
2323

24+
use CallbackFilterIterator;
2425
use FilesystemIterator;
26+
use IteratorIterator;
2527
use RecursiveCallbackFilterIterator;
2628
use RecursiveDirectoryIterator;
2729
use RecursiveIteratorIterator;
@@ -242,17 +244,24 @@ public function clearDirectory(string $folderToClear): bool
242244
{
243245
$hasDeletedItems = false;
244246

245-
if ($this->filesystem->exists($folderToClear)) {
246-
foreach (scandir($folderToClear) as $item) {
247-
if ($item !== '.' && $item !== '..' && $item !== 'index.php') {
248-
$path = $folderToClear . DIRECTORY_SEPARATOR . $item;
249-
$this->filesystem->remove($path);
247+
if (!$this->filesystem->exists($folderToClear)) {
248+
return $hasDeletedItems;
249+
}
250250

251-
$hasDeletedItems = true;
252-
}
253-
}
251+
$directory = new FilesystemIterator(
252+
$folderToClear, FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_FILEINFO
253+
);
254+
$filter = new CallbackFilterIterator($directory, function ($current) {
255+
return $current->getFilename() !== 'index.php';
256+
});
257+
$iterator = new IteratorIterator($filter);
258+
259+
foreach ($iterator as $file) {
260+
$this->filesystem->remove($file);
254261
}
255262

256-
return $hasDeletedItems;
263+
clearstatcache();
264+
265+
return (bool) iterator_count($iterator);
257266
}
258267
}

0 commit comments

Comments
 (0)