Skip to content

Commit

Permalink
Merge pull request #518 from nextcloud/revert-515-prevent-directory-m…
Browse files Browse the repository at this point in the history
…odifications-when-iterating2

Revert "Prevent directory modifications when iterating"
  • Loading branch information
blizzz committed Nov 14, 2023
2 parents 44beafb + 991897b commit 4d660d8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 52 deletions.
40 changes: 15 additions & 25 deletions index.php
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("Could not unlink " . $downloadedFilePath);
throw new \Exception("Can't 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,14 +841,10 @@ private function recursiveDelete(string $folder): void {
}

foreach ($files as $file) {
if (unlink($file) === false) {
throw new \Exception('Could not unlink ' . $file);
}
unlink($file);
}
foreach ($directories as $dir) {
if (rmdir($dir) === false) {
throw new \Exception('Could not rmdir ' . $dir);
}
rmdir($dir);
}

$state = rmdir($folder);
Expand Down Expand Up @@ -937,10 +933,7 @@ public function deleteOldFiles(): void {
* @var string $path
* @var \SplFileInfo $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) {
foreach ($this->getRecursiveDirectoryIterator() as $path => $fileInfo) {
$currentDir = $this->baseDir . '/../';
$fileName = explode($currentDir, $path)[1];
$folderStructure = explode('/', $fileName, -1);
Expand All @@ -957,7 +950,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 @@ -980,10 +973,7 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
* @var string $path
* @var \SplFileInfo $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) {
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);

Expand Down Expand Up @@ -1064,12 +1054,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 unlink .step');
throw new \Exception('Could not rmdir .step');
}

if (function_exists('opcache_reset')) {
Expand All @@ -1089,7 +1079,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: 15 additions & 25 deletions lib/Updater.php
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("Could not unlink " . $downloadedFilePath);
throw new \Exception("Can't 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,14 +803,10 @@ private function recursiveDelete(string $folder): void {
}

foreach ($files as $file) {
if (unlink($file) === false) {
throw new \Exception('Could not unlink ' . $file);
}
unlink($file);
}
foreach ($directories as $dir) {
if (rmdir($dir) === false) {
throw new \Exception('Could not rmdir ' . $dir);
}
rmdir($dir);
}

$state = rmdir($folder);
Expand Down Expand Up @@ -899,10 +895,7 @@ public function deleteOldFiles(): void {
* @var string $path
* @var \SplFileInfo $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) {
foreach ($this->getRecursiveDirectoryIterator() as $path => $fileInfo) {
$currentDir = $this->baseDir . '/../';
$fileName = explode($currentDir, $path)[1];
$folderStructure = explode('/', $fileName, -1);
Expand All @@ -919,7 +912,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 @@ -942,10 +935,7 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
* @var string $path
* @var \SplFileInfo $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) {
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);

Expand Down Expand Up @@ -1026,12 +1016,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 unlink .step');
throw new \Exception('Could not rmdir .step');
}

if (function_exists('opcache_reset')) {
Expand All @@ -1051,7 +1041,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
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '14ccc22088938656fb221e3d7f7e8928a58332ab',
'reference' => 'aac9e4b1f9edc88a0d28dcbfca50276c9a2c25ba',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '14ccc22088938656fb221e3d7f7e8928a58332ab',
'reference' => 'aac9e4b1f9edc88a0d28dcbfca50276c9a2c25ba',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit 4d660d8

Please sign in to comment.