Skip to content

Commit

Permalink
fix: make deleteOldFiles / moveNewVersionInPlace more robust
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Richards <[email protected]>
  • Loading branch information
joshtrichards committed Dec 19, 2024
1 parent e73aa39 commit ef4c5c1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 52 deletions.
82 changes: 56 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
81 changes: 55 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
Binary file modified updater.phar
Binary file not shown.

0 comments on commit ef4c5c1

Please sign in to comment.