Commit 6f107750 authored by Roman Ondráček's avatar Roman Ondráček

Core: try to fix permissions after file manipulation failure (fix #214)

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent a9be7c14
Pipeline #2090 passed with stages
in 13 minutes and 7 seconds
......@@ -21,6 +21,7 @@ declare(strict_types = 1);
namespace App\CloudModule\Presenters;
use App\CloudModule\Forms\AwsFormFactory;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use App\CoreModule\Presenters\ProtectedPresenter;
use Nette\Application\UI\Form;
......@@ -44,9 +45,10 @@ class AwsPresenter extends ProtectedPresenter {
/**
* Constructor
* @param CommandManager $commandManager Command manager
*/
public function __construct() {
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/');
public function __construct(CommandManager $commandManager) {
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/', $commandManager);
parent::__construct();
}
......
......@@ -21,6 +21,7 @@ declare(strict_types = 1);
namespace App\CloudModule\Presenters;
use App\CloudModule\Forms\AzureFormFactory;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use App\CoreModule\Presenters\ProtectedPresenter;
use Nette\Application\UI\Form;
......@@ -44,9 +45,10 @@ class AzurePresenter extends ProtectedPresenter {
/**
* Constructor
* @param CommandManager $commandManager Command manager
*/
public function __construct() {
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/');
public function __construct(CommandManager $commandManager) {
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/', $commandManager);
parent::__construct();
}
......
......@@ -21,6 +21,7 @@ declare(strict_types = 1);
namespace App\CloudModule\Presenters;
use App\CloudModule\Forms\IbmCloudFormFactory;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use App\CoreModule\Presenters\ProtectedPresenter;
use Nette\Application\UI\Form;
......@@ -44,9 +45,10 @@ class IbmCloudPresenter extends ProtectedPresenter {
/**
* Constructor
* @param CommandManager $commandManager Command manager
*/
public function __construct() {
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/');
public function __construct(CommandManager $commandManager) {
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/', $commandManager);
parent::__construct();
}
......
......@@ -78,10 +78,8 @@ class MonitorFormFactory {
$form->addSubmit('save', 'save');
$form->addProtection('core.errors.form-timeout');
$id = $presenter->getParameter('id');
if (isset($id)) {
if (array_key_exists($id, $this->manager->list())) {
$form->setDefaults($this->manager->load((int) $id));
}
if (isset($id) && array_key_exists($id, $this->manager->list())) {
$form->setDefaults($this->manager->load((int) $id));
}
$form->onSuccess[] = [$this, 'save'];
return $form;
......
......@@ -20,6 +20,7 @@ declare(strict_types = 1);
namespace App\ConfigModule\Models;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use App\ServiceModule\Exceptions\NotSupportedInitSystemException;
use App\ServiceModule\Models\ServiceManager;
......@@ -39,6 +40,11 @@ class SchedulerManager {
use SmartObject;
/**
* @var CommandManager Command manager
*/
private $commandManager;
/**
* @var GenericManager Generic config manager
*/
......@@ -70,8 +76,9 @@ class SchedulerManager {
* @param GenericManager $genericManager Generic configuration manager
* @param TaskTimeManager $timeManager Scheduler's task time specification manager
* @param ServiceManager $serviceManager IQRF Gateway Daemon's service manager
* @param CommandManager $commandManager Command manager
*/
public function __construct(MainManager $mainManager, GenericManager $genericManager, TaskTimeManager $timeManager, ServiceManager $serviceManager) {
public function __construct(MainManager $mainManager, GenericManager $genericManager, TaskTimeManager $timeManager, ServiceManager $serviceManager, CommandManager $commandManager) {
$this->genericConfigManager = $genericManager;
$this->serviceManager = $serviceManager;
$this->timeManager = $timeManager;
......@@ -80,7 +87,8 @@ class SchedulerManager {
} catch (IOException | JsonException $e) {
$path = '/var/cache/iqrf-gateway-daemon/scheduler/';
}
$this->fileManager = new JsonFileManager($path);
$this->fileManager = new JsonFileManager($path, $commandManager);
$this->commandManager = $commandManager;
}
/**
......@@ -235,7 +243,7 @@ class SchedulerManager {
* @throws JsonException
*/
public function loadType(string $type): ?array {
$taskManager = new JsonFileManager(__DIR__ . '/../json');
$taskManager = new JsonFileManager(__DIR__ . '/../json', $this->commandManager);
$tasks = $taskManager->read('Scheduler');
if (array_key_exists($type, $tasks)) {
$task = $tasks[$type];
......
......@@ -23,6 +23,7 @@ namespace App\ConfigModule\Presenters;
use App\ConfigModule\Forms\IqrfSpiFormFactory;
use App\ConfigModule\Models\GenericManager;
use App\ConfigModule\Models\IqrfManager;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use Nette\Application\UI\Form;
use Nette\Utils\JsonException;
......@@ -52,10 +53,11 @@ class IqrfSpiPresenter extends GenericPresenter {
* Constructor
* @param IqrfManager $iqrfManager IQRF interface manager
* @param GenericManager $genericManager Generic configuration manager
* @param CommandManager $commandManager Command manager
*/
public function __construct(IqrfManager $iqrfManager, GenericManager $genericManager) {
public function __construct(IqrfManager $iqrfManager, GenericManager $genericManager, CommandManager $commandManager) {
$this->iqrfManager = $iqrfManager;
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/');
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/', $commandManager);
$components = ['iqrf::IqrfSpi'];
parent::__construct($components, $genericManager);
}
......
......@@ -23,6 +23,7 @@ namespace App\ConfigModule\Presenters;
use App\ConfigModule\Forms\IqrfUartFormFactory;
use App\ConfigModule\Models\GenericManager;
use App\ConfigModule\Models\IqrfManager;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use Nette\Application\UI\Form;
use Nette\Utils\JsonException;
......@@ -52,10 +53,11 @@ class IqrfUartPresenter extends GenericPresenter {
* Constructor
* @param IqrfManager $iqrfManager IQRF interface manager
* @param GenericManager $genericManager Generic configuration manager
* @param CommandManager $commandManager Command manager
*/
public function __construct(IqrfManager $iqrfManager, GenericManager $genericManager) {
public function __construct(IqrfManager $iqrfManager, GenericManager $genericManager, CommandManager $commandManager) {
$this->iqrfManager = $iqrfManager;
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/');
$this->fileManager = new JsonFileManager(__DIR__ . '/../json/', $commandManager);
$components = ['iqrf::IqrfUart'];
parent::__construct($components, $genericManager);
}
......
......@@ -36,12 +36,19 @@ class FileManager {
*/
private $directory;
/**
* @var CommandManager Command manager
*/
private $commandManager;
/**
* Constructor
* @param string $directory Directory with files
* @param CommandManager $commandManager Command managers
*/
public function __construct(string $directory) {
public function __construct(string $directory, CommandManager $commandManager) {
$this->directory = $directory;
$this->commandManager = $commandManager;
}
/**
......@@ -58,7 +65,12 @@ class FileManager {
* @throws IOException
*/
public function delete(string $fileName): void {
FileSystem::delete($this->directory . '/' . $fileName);
try {
FileSystem::delete($this->directory . '/' . $fileName);
} catch (IOException $e) {
$this->fixPermissions($fileName);
FileSystem::delete($this->directory . '/' . $fileName);
}
}
/**
......@@ -77,7 +89,12 @@ class FileManager {
* @throws IOException
*/
public function read(string $fileName) {
return FileSystem::read($this->directory . '/' . $fileName);
try {
return FileSystem::read($this->directory . '/' . $fileName);
} catch (IOException $e) {
$this->fixPermissions($fileName);
return FileSystem::read($this->directory . '/' . $fileName);
}
}
/**
......@@ -88,7 +105,21 @@ class FileManager {
*/
public function write(string $fileName, $content): void {
$fileName = 'nette.safe://' . $this->directory . '/' . $fileName;
FileSystem::write($fileName, $content, null);
try {
FileSystem::write($fileName, $content, null);
} catch (IOException $e) {
$this->fixPermissions($fileName);
FileSystem::write($fileName, $content, null);
}
}
/**
* Fixes the permissions
* @param string $fileName File name
*/
private function fixPermissions(string $fileName): void {
$this->commandManager->run('chmod 777 ' . $this->directory);
$this->commandManager->run('chmod 666 ' . $this->directory . '/' . $fileName);
}
}
......@@ -35,9 +35,10 @@ class JsonFileManager extends FileManager {
/**
* Constructor
* @param string $configDir Directory with configuration files
* @param CommandManager $commandManager Command manager
*/
public function __construct(string $configDir) {
parent::__construct($configDir);
public function __construct(string $configDir, CommandManager $commandManager) {
parent::__construct($configDir, $commandManager);
}
/**
......
......@@ -43,9 +43,10 @@ class JsonSchemaManager extends JsonFileManager {
/**
* Constructor
* @param string $configDir Directory with JSON schemas
* @param CommandManager $commandManager Command manager
*/
public function __construct(string $configDir) {
parent::__construct($configDir);
public function __construct(string $configDir, CommandManager $commandManager) {
parent::__construct($configDir, $commandManager);
}
/**
......
......@@ -65,7 +65,7 @@ class VersionManager {
$this->cache = new Cache($storage, 'version_manager');
$this->client = $client;
$this->commandManager = $commandManager;
$this->jsonFileManager = new JsonFileManager(__DIR__ . '/../../../');
$this->jsonFileManager = new JsonFileManager(__DIR__ . '/../../../', $commandManager);
}
/**
......
......@@ -193,9 +193,11 @@ class MigrationManagerTest extends TestCase {
protected function setUp(): void {
Environment::lock('migration', __DIR__ . '/../../temp/');
$this->copyFiles();
$this->fileManager = new FileManager($this->configPath);
$schemaManager = new ComponentSchemaManager($this->schemaPath);
$schemaManagerCorrupted = new ComponentSchemaManager($this->schemaCorruptedPath);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new FileManager($this->configPath, $commandManager);
$schemaManager = new ComponentSchemaManager($this->schemaPath, $commandManager);
$schemaManagerCorrupted = new ComponentSchemaManager($this->schemaCorruptedPath, $commandManager);
$commandStack = new CommandStack();
$this->commandManager = Mockery::mock(CommandManager::class, [false, $commandStack])->makePartial();
$serviceManager = Mockery::mock(ServiceManager::class);
......
......@@ -15,6 +15,8 @@ use App\ConfigModule\Models\GenericManager;
use App\ConfigModule\Models\MainManager;
use App\ConfigModule\Models\SchedulerManager;
use App\ConfigModule\Models\TaskTimeManager;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use App\ServiceModule\Models\ServiceManager;
use Mockery;
......@@ -183,9 +185,11 @@ class SchedulerManagerTest extends TestCase {
$configPath = __DIR__ . '/../../data/';
$configTempPath = __DIR__ . '/../../temp/configuration/';
$schemaPath = __DIR__ . '/../../data/cfgSchemas/';
$this->fileManagerTemp = new JsonFileManager($configTempPath . 'scheduler/');
$fileManager = new JsonFileManager($configPath);
$schemaManager = new ComponentSchemaManager($schemaPath);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManagerTemp = new JsonFileManager($configTempPath . 'scheduler/', $commandManager);
$fileManager = new JsonFileManager($configPath, $commandManager);
$schemaManager = new ComponentSchemaManager($schemaPath, $commandManager);
$genericConfigManager = new GenericManager($fileManager, $schemaManager);
$mainConfigManager = Mockery::mock(MainManager::class);
$mainConfigManager->shouldReceive('load')->andReturn(['cacheDir' => $configPath]);
......@@ -193,8 +197,8 @@ class SchedulerManagerTest extends TestCase {
$mainConfigManagerTemp->shouldReceive('load')->andReturn(['cacheDir' => $configTempPath]);
$this->timeManager = new TaskTimeManager();
$this->serviceManager = Mockery::mock(ServiceManager::class);
$this->manager = new SchedulerManager($mainConfigManager, $genericConfigManager, $this->timeManager, $this->serviceManager);
$this->managerTemp = new SchedulerManager($mainConfigManagerTemp, $genericConfigManager, $this->timeManager, $this->serviceManager);
$this->manager = new SchedulerManager($mainConfigManager, $genericConfigManager, $this->timeManager, $this->serviceManager, $commandManager);
$this->managerTemp = new SchedulerManager($mainConfigManagerTemp, $genericConfigManager, $this->timeManager, $this->serviceManager, $commandManager);
}
/**
......
......@@ -12,6 +12,8 @@ namespace Tests\ConfigModule\Models;
use App\ConfigModule\Models\MainManager;
use App\ConfigModule\Models\SchedulerMigrationManager;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\FileManager;
use App\CoreModule\Models\ZipArchiveManager;
use App\ServiceModule\Models\ServiceManager;
......@@ -130,7 +132,9 @@ class SchedulerMigrationManagerTest extends TestCase {
protected function setUp(): void {
Environment::lock('migration', __DIR__ . '/../../temp/');
$this->copyFiles();
$this->fileManager = new FileManager($this->configPath);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new FileManager($this->configPath, $commandManager);
$mainConfigManager = Mockery::mock(MainManager::class);
$mainConfigManager->shouldReceive('load')->andReturn(['cacheDir' => $this->configTempPath . '/..']);
$serviceManager = Mockery::mock(ServiceManager::class);
......
......@@ -11,8 +11,10 @@ declare(strict_types = 1);
namespace Tests\Integration\ConfigModule\Models;
use App\ConfigModule\Models\ComponentSchemaManager;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Exceptions\InvalidJsonException;
use App\CoreModule\Exceptions\NonExistingJsonSchemaException;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use Tester\Assert;
use Tester\TestCase;
......@@ -91,8 +93,10 @@ class ComponentSchemaManagerTest extends TestCase {
* Sets up the test environment
*/
protected function setUp(): void {
$this->fileManager = new JsonFileManager(self::FILE_PATH);
$this->manager = new ComponentSchemaManager(self::SCHEMA_PATH);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new JsonFileManager(self::FILE_PATH, $commandManager);
$this->manager = new ComponentSchemaManager(self::SCHEMA_PATH, $commandManager);
}
}
......
......@@ -10,6 +10,8 @@ declare(strict_types = 1);
namespace Tests\Integration\CoreModule\Models;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\FileManager;
use Nette\Utils\FileSystem;
use Tester\Assert;
......@@ -101,8 +103,10 @@ class FileManagerTest extends TestCase {
* Sets up the test environment
*/
protected function setUp(): void {
$this->manager = new FileManager($this->path);
$this->managerTest = new FileManager($this->pathTest);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->manager = new FileManager($this->path, $commandManager);
$this->managerTest = new FileManager($this->pathTest, $commandManager);
}
}
......
......@@ -10,6 +10,8 @@ declare(strict_types = 1);
namespace Tests\Integration\CoreModule\Models;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
......@@ -103,8 +105,10 @@ class JsonFileManagerTest extends TestCase {
* Sets up the test environment
*/
protected function setUp(): void {
$this->manager = new JsonFileManager($this->path);
$this->managerTest = new JsonFileManager($this->pathTest);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->manager = new JsonFileManager($this->path, $commandManager);
$this->managerTest = new JsonFileManager($this->pathTest, $commandManager);
}
}
......
......@@ -10,8 +10,10 @@ declare(strict_types = 1);
namespace Tests\Integration\CoreModule\Models;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Exceptions\InvalidJsonException;
use App\CoreModule\Exceptions\NonExistingJsonSchemaException;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use App\CoreModule\Models\JsonSchemaManager;
use Tester\Assert;
......@@ -91,8 +93,10 @@ class JsonSchemaManagerTest extends TestCase {
* Sets up the test environment
*/
protected function setUp(): void {
$this->fileManager = new JsonFileManager(self::FILE_PATH);
$this->manager = new JsonSchemaManager(self::SCHEMA_PATH);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new JsonFileManager(self::FILE_PATH, $commandManager);
$this->manager = new JsonSchemaManager(self::SCHEMA_PATH, $commandManager);
}
}
......
......@@ -10,6 +10,8 @@ declare(strict_types = 1);
namespace Tests\Integration\GatewayModule\Models;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\FileManager;
use App\CoreModule\Models\ZipArchiveManager;
use App\GatewayModule\Models\LogManager;
......@@ -76,7 +78,9 @@ class LogManagerTest extends TestCase {
*/
protected function setUp(): void {
$this->logDir = realpath(__DIR__ . '/../../../data/logs/');
$this->fileManager = new FileManager($this->logDir);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new FileManager($this->logDir, $commandManager);
$this->manager = new LogManager($this->logDir);
}
......
......@@ -22,6 +22,8 @@ namespace Tests\Toolkit\TestCases;
use App\ConfigModule\Models\ComponentSchemaManager;
use App\ConfigModule\Models\GenericManager;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
......@@ -57,8 +59,10 @@ abstract class CloudIntegrationTestCase extends TestCase {
$this->certPath = realpath(__DIR__ . '/../../temp/certificates/') . '/';
$configPath = __DIR__ . '/../../temp/configuration/';
$schemaPath = __DIR__ . '/../../data/cfgSchemas/';
$this->fileManager = new JsonFileManager($configPath);
$schemaManager = new ComponentSchemaManager($schemaPath);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new JsonFileManager($configPath, $commandManager);
$schemaManager = new ComponentSchemaManager($schemaPath, $commandManager);
$this->configManager = new GenericManager($this->fileManager, $schemaManager);
}
......
......@@ -20,6 +20,8 @@ declare(strict_types = 1);
namespace Tests\Toolkit\TestCases;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use Mockery;
use Nette\Utils\JsonException;
......@@ -55,7 +57,9 @@ abstract class DpaParserTestCase extends TestCase {
*/
protected function setUp(): void {
$data = __DIR__ . '/../../data/iqrf/';
$this->fileManager = new JsonFileManager($data);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new JsonFileManager($data, $commandManager);
}
/**
......
......@@ -21,6 +21,8 @@ declare(strict_types = 1);
namespace Tests\Toolkit\TestCases;
use App\ConfigModule\Models\ComponentSchemaManager;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use Mockery;
use Nette\Utils\JsonException;
......@@ -91,9 +93,11 @@ abstract class JsonConfigTestCase extends TestCase {
$configPath = __DIR__ . '/../../data/configuration/';
$configTempPath = __DIR__ . '/../../temp/configuration/';
$schemaPath = __DIR__ . '/../../data/cfgSchemas/';
$this->fileManager = new JsonFileManager($configPath);
$this->fileManagerTemp = new JsonFileManager($configTempPath);
$this->schemaManager = new ComponentSchemaManager($schemaPath);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new JsonFileManager($configPath, $commandManager);
$this->fileManagerTemp = new JsonFileManager($configTempPath, $commandManager);
$this->schemaManager = new ComponentSchemaManager($schemaPath, $commandManager);
}
/**
......
......@@ -10,6 +10,8 @@ declare(strict_types = 1);
namespace Tests\Unit\IqrfNetModule\Models;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\FileManager;
use App\CoreModule\Models\JsonFileManager;
use App\IqrfNetModule\Models\DpaRawManager;
......@@ -46,8 +48,10 @@ class DpaRawManagerTest extends WebSocketTestCase {
protected function setUp(): void {
parent::setUp();
$path = __DIR__ . '/../../../data/iqrf/';
$this->fileManager = new FileManager($path);
$this->jsonFileManager = new JsonFileManager($path);
$commandStack = new CommandStack();
$commandManager = new CommandManager(false, $commandStack);
$this->fileManager = new FileManager($path, $commandManager);
$this->jsonFileManager = new JsonFileManager($path, $commandManager);
$this->request = Mockery::mock(DpaRequest::class);
$this->manager = new DpaRawManager($this->request, $this->wsClient);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment