/** * Create a new default sync instance. * * @param InputContract $inputHandler * @param OutputContract $outputHandler * @param SyncOutputContract $outputHelper * @param SyncSettingsContract $settings * * @return SyncContract */ public static function createSync(InputContract $inputHandler, OutputContract $outputHandler, SyncSettingsContract $settings = null, SyncOutputContract &$outputHelper = null) { if (is_null($outputHelper)) { $outputHelper = new SyncNullOutput(); } if (!$settings) { $settings = new SyncSettings(); } $inputHandler->applySettings($settings); $outputHandler->applySettings($settings); return new Sync(new SyncManager($inputHandler, $outputHandler, $settings), new SyncWorker($inputHandler, $outputHandler, $settings), $outputHelper, $settings); }
/** * Is another process alive and working on this folder? * * @return bool */ public function isOtherProcessAlive() { if (!$this->output->has($this->aliveFile)) { return false; } if ($this->output->read($this->aliveFile) < time() - $this->settings->timeout()) { return false; } return true; }
/** * Get a list of all files that exists on both sides but differ on the output side. * * @param bool $checkSize * @param bool $checkHash * * @return array */ public function getFilesToUpdate($checkSize = true, $checkHash = false) { $filesToUpdate = []; foreach ($this->inputFiles as $file) { // File exists or it is excluded? if (!$this->output->has($file['path']) || $this->exclude($file['path'], $this->settings->excludeOutput())) { continue; } // Compare timestamps $timeStamp = $this->output->getTimestamp($file['path']) >= $file['timestamp']; // Compare filesizes $fileSize = !isset($file['size']) || !$checkSize || $this->output->getSize($file['path']) === $file['size']; // Compare hashes $hash = !$checkHash && true; // TODO: integrate checking by hashing the file contents if ($fileSize && $timeStamp && $hash) { continue; } $filesToUpdate[] = $file; } return $filesToUpdate; }
/** * Decrypt the given output. * * @param OutputContract $input * @param OutputContract $output * @param SyncOutputContract $console */ public function decrypt(OutputContract $input, OutputContract $output, SyncOutputContract $console = null) { $files = $input->listContents('', true); if ($console) { $console->setTotal(count($files)); } foreach ($files as $file) { if ($file['type'] == 'dir') { if ($console) { $console->out(SyncOutput::ENC_ENCRYPT_FILE, ['file' => $file['path']]); } continue; } try { $fileContents = $this->crypto->decrypt($input->read($file['path']), $this->key); if ($output->has($file['path'])) { $output->update($file['path'], $fileContents); } else { $output->write($file['path'], $fileContents); } unset($fileContents); if ($console) { $console->out(SyncOutput::ENC_DECRYPT_FILE, ['file' => $file['path']]); } } catch (GeneralSecurityException $e) { if ($console) { $console->out(SyncOutput::ENC_DECRYPT_FILE, ['file' => $file['path'], 'output' => $e]); } } } }