Skip to content

Commit

Permalink
Merge pull request #515 from caco3/prevent-directory-modifications-wh…
Browse files Browse the repository at this point in the history
…en-iterating2

Prevent directory modifications when iterating
  • Loading branch information
Altahrim authored Nov 3, 2023
2 parents 086a1ed + 27447ce commit d2a1167
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
40 changes: 25 additions & 15 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public function createBackup(): void {
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));
}
}

Expand Down Expand Up @@ -544,7 +544,7 @@ private function getUpdateServerResponse(): array {
/** @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);

Expand Down Expand Up @@ -737,7 +737,7 @@ private function getVersionByVersionFile(string $versionFile): string {
return implode('.', $OC_Version);
}

throw new \Exception("OC_Version not found in $versionFile");
throw new \Exception('OC_Version not found in ' . $versionFile);
}

/**
Expand All @@ -754,15 +754,15 @@ public function extractDownload(): void {
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
Expand Down Expand Up @@ -804,7 +804,7 @@ public function replaceEntryPoints(): void {
}
$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);
}
}

Expand Down Expand Up @@ -841,10 +841,14 @@ private function recursiveDelete(string $folder): void {
}

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);
Expand Down Expand Up @@ -933,7 +937,10 @@ public function deleteOldFiles(): void {
* @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);
Expand All @@ -950,7 +957,7 @@ public function deleteOldFiles(): void {
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);
Expand All @@ -973,7 +980,10 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
* @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);

Expand Down Expand Up @@ -1054,12 +1064,12 @@ public function finalize(): void {
$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')) {
Expand All @@ -1079,7 +1089,7 @@ private function writeStep(string $state, int $step): void {
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');
Expand Down
40 changes: 25 additions & 15 deletions lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public function createBackup(): void {
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));
}
}

Expand Down Expand Up @@ -506,7 +506,7 @@ private function getUpdateServerResponse(): array {
/** @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);

Expand Down Expand Up @@ -699,7 +699,7 @@ private function getVersionByVersionFile(string $versionFile): string {
return implode('.', $OC_Version);
}

throw new \Exception("OC_Version not found in $versionFile");
throw new \Exception('OC_Version not found in ' . $versionFile);
}

/**
Expand All @@ -716,15 +716,15 @@ public function extractDownload(): void {
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
Expand Down Expand Up @@ -766,7 +766,7 @@ public function replaceEntryPoints(): void {
}
$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);
}
}

Expand Down Expand Up @@ -803,10 +803,14 @@ private function recursiveDelete(string $folder): void {
}

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);
Expand Down Expand Up @@ -895,7 +899,10 @@ public function deleteOldFiles(): void {
* @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);
Expand All @@ -912,7 +919,7 @@ public function deleteOldFiles(): void {
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);
Expand All @@ -935,7 +942,10 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
* @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);

Expand Down Expand Up @@ -1016,12 +1026,12 @@ public function finalize(): void {
$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')) {
Expand All @@ -1041,7 +1051,7 @@ private function writeStep(string $state, int $step): void {
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');
Expand Down
Binary file modified updater.phar
Binary file not shown.
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'aac9e4b1f9edc88a0d28dcbfca50276c9a2c25ba',
'reference' => '14ccc22088938656fb221e3d7f7e8928a58332ab',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'aac9e4b1f9edc88a0d28dcbfca50276c9a2c25ba',
'reference' => '14ccc22088938656fb221e3d7f7e8928a58332ab',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit d2a1167

Please sign in to comment.