-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement repair step to delete previous setup files
Will be necessary run the follow occ command first: files:scan-app-data libresign Signed-off-by: Vitor Mattos <[email protected]>
- Loading branch information
1 parent
fa0c03b
commit eb72b75
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 LibreCode coop and contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Libresign\Migration; | ||
|
||
use OC\DB\Connection; | ||
use OCP\Files\AppData\IAppDataFactory; | ||
use OCP\Files\IAppData; | ||
use OCP\Files\SimpleFS\ISimpleFolder; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\IRepairStep; | ||
|
||
class DeleteOldBinaries implements IRepairStep { | ||
protected IAppData $appData; | ||
protected IOutput $output; | ||
protected array $allowedFiles = [ | ||
'x86_64', | ||
'aarch64', | ||
'openssl_config', | ||
'cfssl_config', | ||
'unauthenticated', | ||
]; | ||
public function __construct( | ||
protected IAppDataFactory $appDataFactory, | ||
) { | ||
$this->appData = $appDataFactory->get('libresign'); | ||
} | ||
|
||
public function getName(): string { | ||
return 'Delete old binaries.'; | ||
} | ||
|
||
public function run(IOutput $output): void { | ||
$output->warning('Run the follow command first: files:scan-app-data libresign'); | ||
$this->output = $output; | ||
$folder = $this->appData->getFolder('/'); | ||
|
||
$list = $this->getDirectoryListing($folder); | ||
foreach ($list as $file) { | ||
if (!in_array($file->getName(), $this->allowedFiles)) { | ||
$file->delete(); | ||
} | ||
} | ||
} | ||
|
||
protected function reconnectToDatabase(): Connection { | ||
/** @var Connection $connection*/ | ||
$connection = \OC::$server->get(Connection::class); | ||
try { | ||
$connection->close(); | ||
} catch (\Exception $ex) { | ||
$this->output->info("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); | ||
} | ||
while (!$connection->isConnected()) { | ||
try { | ||
$connection->connect(); | ||
} catch (\Exception $ex) { | ||
$this->output->info("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); | ||
sleep(60); | ||
} | ||
} | ||
return $connection; | ||
} | ||
|
||
private function getDirectoryListing(ISimpleFolder $node): array { | ||
$reflection = new \ReflectionClass($node); | ||
$reflectionProperty = $reflection->getProperty('folder'); | ||
$reflectionProperty->setAccessible(true); | ||
$folder = $reflectionProperty->getValue($node); | ||
$list = $folder->getDirectoryListing(); | ||
return $list; | ||
} | ||
} |