Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make deleteOldFiles / moveNewVersionInPlace more robust #594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 61 additions & 26 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/


class UpdateException extends \Exception {

/** @param list<string> $data */
Expand Down Expand Up @@ -927,10 +928,13 @@ public function deleteOldFiles(): void {
* @var string $path
* @var \SplFileInfo $fileInfo
*/
$files = [];
$directories = [];
foreach ($this->getRecursiveDirectoryIterator() as $path => $fileInfo) {
$currentDir = $this->baseDir . '/../';
$fileName = explode($currentDir, $path)[1];
$folderStructure = explode('/', $fileName, -1);

// Exclude the exclusions
if (isset($folderStructure[0])) {
if (array_search($folderStructure[0], $excludedElements) !== false) {
Expand All @@ -942,18 +946,30 @@ public function deleteOldFiles(): void {
}
}
if ($fileInfo->isFile() || $fileInfo->isLink()) {
$state = unlink($path);
if ($state === false) {
throw new \Exception('Could not unlink: '.$path);
}
$files[] = $path;
} elseif ($fileInfo->isDir()) {
$state = rmdir($path);
if ($state === false) {
throw new \Exception('Could not rmdir: '.$path);
}
$directories[] = $path;
}
}

//
// Do the actual writes (outside the RDI to avoid problems on FreeBSD/etc)
//

foreach ($files as $file) {
$state = unlink($file);
if ($state === false) {
throw new \Exception('Could not unlink: '.$path);
}
}

foreach ($directories as $dir) {
$state = rmdir($dir);
if ($state === false) {
throw new \Exception('Could not rmdir: '.$path);
}
}

$this->silentLog('[info] end of deleteOldFiles()');
}

Expand All @@ -967,6 +983,8 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
* @var string $path
* @var \SplFileInfo $fileInfo
*/
$files = [];
$directories = [];
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);
Expand All @@ -983,29 +1001,41 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
}

if ($fileInfo->isFile()) {
if (!file_exists($this->baseDir . '/../' . dirname($fileName))) {
$state = mkdir($this->baseDir . '/../' . dirname($fileName), 0755, true);
if ($state === false) {
throw new \Exception('Could not mkdir ' . $this->baseDir . '/../' . dirname($fileName));
}
}
$state = rename($path, $this->baseDir . '/../' . $fileName);
if ($state === false) {
throw new \Exception(
sprintf(
'Could not rename %s to %s',
$path,
$this->baseDir . '/../' . $fileName
)
);
}
$files[$path] = $fileName;
}
if ($fileInfo->isDir()) {
$state = rmdir($path);
$directories[] = $path;
}
}

//
// Do the actual writes (outside the RDI to avoid problems on FreeBSD/etc)
//

foreach ($files as $file => $fileName) {
if (!file_exists($this->baseDir . '/../' . dirname($fileName))) {
$state = mkdir($this->baseDir . '/../' . dirname($fileName), 0755, true);
if ($state === false) {
throw new \Exception('Could not rmdir ' . $path);
throw new \Exception('Could not mkdir ' . $this->baseDir . '/../' . dirname($fileName));
}
}
$state = rename($file, $this->baseDir . '/../' . $fileName);
if ($state === false) {
throw new \Exception(
sprintf(
'Could not rename %s to %s',
$file,
$this->baseDir . '/../' . $fileName
)
);
}
}

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

Expand Down Expand Up @@ -1061,6 +1091,11 @@ public function finalize(): void {
opcache_reset();
}

if (function_exists('memory_get_peak_usage')) {
$memUsage = round(memory_get_peak_usage() / 1024 / 1024, 2);
$this->silentLog('[info] Peak memory usage: ' . $memUsage . 'MiB');
}

$this->silentLog('[info] end of finalize()');
}

Expand Down
86 changes: 60 additions & 26 deletions lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,13 @@ public function deleteOldFiles(): void {
* @var string $path
* @var \SplFileInfo $fileInfo
*/
$files = [];
$directories = [];
foreach ($this->getRecursiveDirectoryIterator() as $path => $fileInfo) {
$currentDir = $this->baseDir . '/../';
$fileName = explode($currentDir, $path)[1];
$folderStructure = explode('/', $fileName, -1);

// Exclude the exclusions
if (isset($folderStructure[0])) {
if (array_search($folderStructure[0], $excludedElements) !== false) {
Expand All @@ -905,18 +908,30 @@ public function deleteOldFiles(): void {
}
}
if ($fileInfo->isFile() || $fileInfo->isLink()) {
$state = unlink($path);
if ($state === false) {
throw new \Exception('Could not unlink: '.$path);
}
$files[] = $path;
} elseif ($fileInfo->isDir()) {
$state = rmdir($path);
if ($state === false) {
throw new \Exception('Could not rmdir: '.$path);
}
$directories[] = $path;
}
}

//
// Do the actual writes (outside the RDI to avoid problems on FreeBSD/etc)
//

foreach ($files as $file) {
$state = unlink($file);
if ($state === false) {
throw new \Exception('Could not unlink: '.$path);
}
}

foreach ($directories as $dir) {
$state = rmdir($dir);
if ($state === false) {
throw new \Exception('Could not rmdir: '.$path);
}
}

$this->silentLog('[info] end of deleteOldFiles()');
}

Expand All @@ -930,6 +945,8 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
* @var string $path
* @var \SplFileInfo $fileInfo
*/
$files = [];
$directories = [];
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);
Expand All @@ -946,29 +963,41 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
}

if ($fileInfo->isFile()) {
if (!file_exists($this->baseDir . '/../' . dirname($fileName))) {
$state = mkdir($this->baseDir . '/../' . dirname($fileName), 0755, true);
if ($state === false) {
throw new \Exception('Could not mkdir ' . $this->baseDir . '/../' . dirname($fileName));
}
}
$state = rename($path, $this->baseDir . '/../' . $fileName);
if ($state === false) {
throw new \Exception(
sprintf(
'Could not rename %s to %s',
$path,
$this->baseDir . '/../' . $fileName
)
);
}
$files[$path] = $fileName;
}
if ($fileInfo->isDir()) {
$state = rmdir($path);
$directories[] = $path;
}
}

//
// Do the actual writes (outside the RDI to avoid problems on FreeBSD/etc)
//

foreach ($files as $file => $fileName) {
if (!file_exists($this->baseDir . '/../' . dirname($fileName))) {
$state = mkdir($this->baseDir . '/../' . dirname($fileName), 0755, true);
if ($state === false) {
throw new \Exception('Could not rmdir ' . $path);
throw new \Exception('Could not mkdir ' . $this->baseDir . '/../' . dirname($fileName));
}
}
$state = rename($file, $this->baseDir . '/../' . $fileName);
if ($state === false) {
throw new \Exception(
sprintf(
'Could not rename %s to %s',
$file,
$this->baseDir . '/../' . $fileName
)
);
}
}

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

Expand Down Expand Up @@ -1024,6 +1053,11 @@ public function finalize(): void {
opcache_reset();
}

if (function_exists('memory_get_peak_usage')) {
$memUsage = round(memory_get_peak_usage() / 1024 / 1024, 2);
$this->silentLog('[info] Peak memory usage: ' . $memUsage . 'MiB');
}

$this->silentLog('[info] end of finalize()');
}

Expand Down
Binary file modified updater.phar
Binary file not shown.
Loading