Skip to content

Commit

Permalink
feat: implement repair step to delete previous setup files
Browse files Browse the repository at this point in the history
Will be necessary run the follow occ command first:

files:scan-app-data libresign

Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Jun 5, 2024
1 parent fa0c03b commit eb72b75
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ Developed with ❤️ by [LibreCode](https://librecode.coop). Help us transform
<architecture>x86_64</architecture>
<architecture>aarch64</architecture>
</dependencies>
<repair-steps>
<post-migration>
<step>OCA\Libresign\Migration\DeleteOldBinaries</step>
</post-migration>
</repair-steps>
<commands>
<command>OCA\Libresign\Command\Configure\Check</command>
<command>OCA\Libresign\Command\Configure\Cfssl</command>
Expand Down
78 changes: 78 additions & 0 deletions lib/Migration/DeleteOldBinaries.php
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;
}
}

0 comments on commit eb72b75

Please sign in to comment.