fix: build list of files before modifying the filesystem

In some environments modifiying while iterating do not work well.

Signed-off-by: CaCO3 <caco@ruinelli.ch>
This commit is contained in:
CaCO3 2023-10-27 22:22:07 +02:00 committed by Daniel Kesselberg
parent 086a1ed7be
commit 70953abac2
No known key found for this signature in database
GPG Key ID: 36E3664E099D0614
4 changed files with 52 additions and 32 deletions

View File

@ -461,7 +461,7 @@ class Updater {
if (!file_exists($backupFolderLocation . '/' . dirname($fileName))) {
$state = mkdir($backupFolderLocation . '/' . dirname($fileName), 0750, true);
if ($state === false) {
throw new \Exception('Could not create folder: '.$backupFolderLocation.'/'.dirname($fileName));
throw new \Exception('Could not create folder: ' . $backupFolderLocation . '/' . dirname($fileName));
}
}
@ -544,7 +544,7 @@ class Updater {
/** @var false|string $response */
$response = curl_exec($curl);
if ($response === false) {
throw new \Exception('Could not do request to updater server: '.curl_error($curl));
throw new \Exception('Could not do request to updater server: ' . curl_error($curl));
}
curl_close($curl);
@ -737,7 +737,7 @@ EOF;
return implode('.', $OC_Version);
}
throw new \Exception("OC_Version not found in $versionFile");
throw new \Exception('OC_Version not found in ' . $versionFile);
}
/**
@ -754,15 +754,15 @@ EOF;
if ($zipState === true) {
$extraction = $zip->extractTo(dirname($downloadedFilePath));
if ($extraction === false) {
throw new \Exception('Error during unpacking zipfile: '.($zip->getStatusString()));
throw new \Exception('Error during unpacking zipfile: ' . ($zip->getStatusString()));
}
$zip->close();
$state = unlink($downloadedFilePath);
if ($state === false) {
throw new \Exception("Can't unlink ". $downloadedFilePath);
throw new \Exception("Could not unlink " . $downloadedFilePath);
}
} else {
throw new \Exception("Can't handle ZIP file. Error code is: ".print_r($zipState, true));
throw new \Exception("Can't handle ZIP file. Error code is: " . print_r($zipState, true));
}
// Ensure that the downloaded version is not lower
@ -804,7 +804,7 @@ EOF;
}
$state = file_put_contents($this->baseDir . '/../' . $file, $content);
if ($state === false) {
throw new \Exception('Can\'t replace entry point: '.$file);
throw new \Exception('Can\'t replace entry point: ' . $file);
}
}
@ -841,10 +841,14 @@ EOF;
}
foreach ($files as $file) {
unlink($file);
if (unlink($file) === false) {
throw new \Exception('Could not unlink ' . $file);
}
}
foreach ($directories as $dir) {
rmdir($dir);
if (rmdir($dir) === false) {
throw new \Exception('Could not rmdir ' . $dir);
}
}
$state = rmdir($folder);
@ -933,7 +937,10 @@ EOF;
* @var string $path
* @var \SplFileInfo $fileInfo
*/
foreach ($this->getRecursiveDirectoryIterator() as $path => $fileInfo) {
// Build file list first, so the removals won't mess with it
/** @var array<string, \SplFileInfo> */
$fileList = iterator_to_array($this->getRecursiveDirectoryIterator(), true);
foreach ($fileList as $path => $fileInfo) {
$currentDir = $this->baseDir . '/../';
$fileName = explode($currentDir, $path)[1];
$folderStructure = explode('/', $fileName, -1);
@ -950,7 +957,7 @@ EOF;
if ($fileInfo->isFile() || $fileInfo->isLink()) {
$state = unlink($path);
if ($state === false) {
throw new \Exception('Could not unlink: '.$path);
throw new \Exception('Could not unlink: ' . $path);
}
} elseif ($fileInfo->isDir()) {
$state = rmdir($path);
@ -973,7 +980,10 @@ EOF;
* @var string $path
* @var \SplFileInfo $fileInfo
*/
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
// Build file list first, so the renames won't mess with it
/** @var array<string, \SplFileInfo> */
$fileList = iterator_to_array($this->getRecursiveDirectoryIterator($dataLocation), true);
foreach ($fileList as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);
@ -1054,12 +1064,12 @@ EOF;
$this->moveWithExclusions($storageLocation, []);
$state = rmdir($storageLocation);
if ($state === false) {
throw new \Exception('Could not rmdir $storagelocation');
throw new \Exception('Could not rmdir ' . $storageLocation);
}
$state = unlink($this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOptionMandatoryString('instanceid') . '/.step');
if ($state === false) {
throw new \Exception('Could not rmdir .step');
throw new \Exception('Could not unlink .step');
}
if (function_exists('opcache_reset')) {
@ -1079,7 +1089,7 @@ EOF;
if (!file_exists($updaterDir)) {
$result = mkdir($updaterDir);
if ($result === false) {
throw new \Exception('Could not create $updaterDir');
throw new \Exception('Could not create ' . $updaterDir);
}
}
$result = touch($updaterDir . '/.step');

View File

@ -423,7 +423,7 @@ class Updater {
if (!file_exists($backupFolderLocation . '/' . dirname($fileName))) {
$state = mkdir($backupFolderLocation . '/' . dirname($fileName), 0750, true);
if ($state === false) {
throw new \Exception('Could not create folder: '.$backupFolderLocation.'/'.dirname($fileName));
throw new \Exception('Could not create folder: ' . $backupFolderLocation . '/' . dirname($fileName));
}
}
@ -506,7 +506,7 @@ class Updater {
/** @var false|string $response */
$response = curl_exec($curl);
if ($response === false) {
throw new \Exception('Could not do request to updater server: '.curl_error($curl));
throw new \Exception('Could not do request to updater server: ' . curl_error($curl));
}
curl_close($curl);
@ -699,7 +699,7 @@ EOF;
return implode('.', $OC_Version);
}
throw new \Exception("OC_Version not found in $versionFile");
throw new \Exception('OC_Version not found in ' . $versionFile);
}
/**
@ -716,15 +716,15 @@ EOF;
if ($zipState === true) {
$extraction = $zip->extractTo(dirname($downloadedFilePath));
if ($extraction === false) {
throw new \Exception('Error during unpacking zipfile: '.($zip->getStatusString()));
throw new \Exception('Error during unpacking zipfile: ' . ($zip->getStatusString()));
}
$zip->close();
$state = unlink($downloadedFilePath);
if ($state === false) {
throw new \Exception("Can't unlink ". $downloadedFilePath);
throw new \Exception("Could not unlink " . $downloadedFilePath);
}
} else {
throw new \Exception("Can't handle ZIP file. Error code is: ".print_r($zipState, true));
throw new \Exception("Can't handle ZIP file. Error code is: " . print_r($zipState, true));
}
// Ensure that the downloaded version is not lower
@ -766,7 +766,7 @@ EOF;
}
$state = file_put_contents($this->baseDir . '/../' . $file, $content);
if ($state === false) {
throw new \Exception('Can\'t replace entry point: '.$file);
throw new \Exception('Can\'t replace entry point: ' . $file);
}
}
@ -803,10 +803,14 @@ EOF;
}
foreach ($files as $file) {
unlink($file);
if (unlink($file) === false) {
throw new \Exception('Could not unlink ' . $file);
}
}
foreach ($directories as $dir) {
rmdir($dir);
if (rmdir($dir) === false) {
throw new \Exception('Could not rmdir ' . $dir);
}
}
$state = rmdir($folder);
@ -895,7 +899,10 @@ EOF;
* @var string $path
* @var \SplFileInfo $fileInfo
*/
foreach ($this->getRecursiveDirectoryIterator() as $path => $fileInfo) {
// Build file list first, so the removals won't mess with it
/** @var array<string, \SplFileInfo> */
$fileList = iterator_to_array($this->getRecursiveDirectoryIterator(), true);
foreach ($fileList as $path => $fileInfo) {
$currentDir = $this->baseDir . '/../';
$fileName = explode($currentDir, $path)[1];
$folderStructure = explode('/', $fileName, -1);
@ -912,7 +919,7 @@ EOF;
if ($fileInfo->isFile() || $fileInfo->isLink()) {
$state = unlink($path);
if ($state === false) {
throw new \Exception('Could not unlink: '.$path);
throw new \Exception('Could not unlink: ' . $path);
}
} elseif ($fileInfo->isDir()) {
$state = rmdir($path);
@ -935,7 +942,10 @@ EOF;
* @var string $path
* @var \SplFileInfo $fileInfo
*/
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
// Build file list first, so the renames won't mess with it
/** @var array<string, \SplFileInfo> */
$fileList = iterator_to_array($this->getRecursiveDirectoryIterator($dataLocation), true);
foreach ($fileList as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);
@ -1016,12 +1026,12 @@ EOF;
$this->moveWithExclusions($storageLocation, []);
$state = rmdir($storageLocation);
if ($state === false) {
throw new \Exception('Could not rmdir $storagelocation');
throw new \Exception('Could not rmdir ' . $storageLocation);
}
$state = unlink($this->getUpdateDirectoryLocation() . '/updater-'.$this->getConfigOptionMandatoryString('instanceid') . '/.step');
if ($state === false) {
throw new \Exception('Could not rmdir .step');
throw new \Exception('Could not unlink .step');
}
if (function_exists('opcache_reset')) {
@ -1041,7 +1051,7 @@ EOF;
if (!file_exists($updaterDir)) {
$result = mkdir($updaterDir);
if ($result === false) {
throw new \Exception('Could not create $updaterDir');
throw new \Exception('Could not create ' . $updaterDir);
}
}
$result = touch($updaterDir . '/.step');

Binary file not shown.

View File

@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'aac9e4b1f9edc88a0d28dcbfca50276c9a2c25ba',
'reference' => 'a3dc4e26264a1f9f5b072675744b4c9c6cae5e4f',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'aac9e4b1f9edc88a0d28dcbfca50276c9a2c25ba',
'reference' => 'a3dc4e26264a1f9f5b072675744b4c9c6cae5e4f',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),