Skip to content

Commit

Permalink
Make package download more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
KorvinSzanto committed May 21, 2024
1 parent 6aa395f commit 161a9f5
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions concrete/src/Marketplace/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Concrete\Core\Marketplace;

use Concrete\Core\File\Filesystem;
use Concrete\Core\Marketplace\Exception\ErrorSavingRemoteDataException;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Entity\Site\Site;
Expand Down Expand Up @@ -160,14 +161,14 @@ public function download(ConnectionInterface $connection, RemotePackage $package
// Move the files into place
$packageDir = DIR_PACKAGES . '/' . $package->handle;
if ($overwrite && file_exists($packageDir)) {
if (!rename($packageDir, $packageDir . '.old')) {
if (!$this->rename($packageDir, $packageDir . '.old')) {
throw new UnableToPlacePackageException();
}
}

if (!rename($unzipPath . '/' . $package->handle, $packageDir)) {
if (!$this->rename($unzipPath . '/' . $package->handle, $packageDir)) {
if ($overwrite) {
rename($packageDir . '.old', $packageDir);
$this->rename($packageDir . '.old', $packageDir);
}
throw new UnableToPlacePackageException();
}
Expand All @@ -176,6 +177,25 @@ public function download(ConnectionInterface $connection, RemotePackage $package
rmdir($unzipPath);
}

protected function rename(string $old, string $new): bool
{
// First try calling rename, this is the most efficient way to rename things but unfortunately fails if old is a
// directory and on a different filesystem than new.
if (@rename($old, $new)) {
return true;
}

// Fallback to copying to new then deleting old
$this->fileHelper->copyAll($old, $new);
if (!file_exists($new)) {
return false;
}

// Remove the old directory
$this->fileHelper->removeAll($old, true);
return true;
}

protected function rimraf(string $handle)
{
// Make sure we're working with a valid dir
Expand Down

0 comments on commit 161a9f5

Please sign in to comment.