Search in sources :

Example 1 with SyncanyRemoteFile

use of org.syncany.plugins.transfer.files.SyncanyRemoteFile in project syncany by syncany.

the class ConnectOperation method execute.

@Override
public ConnectOperationResult execute() throws Exception {
    logger.log(Level.INFO, "");
    logger.log(Level.INFO, "Running 'Connect'");
    logger.log(Level.INFO, "--------------------------------------------");
    // Decrypt and init configTO
    ConfigTO configTO = null;
    try {
        configTO = createConfigTO();
    } catch (CipherException e) {
        logger.log(Level.FINE, "Could not create config", e);
        return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
    }
    // Init plugin and transfer manager
    transferManager = createTransferManagerFromNullConfig(options.getConfigTO());
    // Test the repo
    if (!performRepoTest(transferManager)) {
        logger.log(Level.INFO, "- Connecting to the repo failed, repo already exists or cannot be created: " + result.getResultCode());
        return result;
    }
    logger.log(Level.INFO, "- Connecting to the repo was successful; now downloading repo file ...");
    // Create local .syncany directory
    File tmpRepoFile = downloadFile(transferManager, new SyncanyRemoteFile());
    if (CipherUtil.isEncrypted(tmpRepoFile)) {
        logger.log(Level.INFO, "- Repo is ENCRYPTED. Decryption necessary.");
        if (configTO.getMasterKey() == null) {
            logger.log(Level.INFO, "- No master key present; Asking for password ...");
            boolean retryPassword = true;
            while (retryPassword) {
                SaltedSecretKey possibleMasterKey = askPasswordAndCreateMasterKey();
                logger.log(Level.INFO, "- Master key created. Now verifying by decrypting repo file...");
                if (decryptAndVerifyRepoFile(tmpRepoFile, possibleMasterKey)) {
                    logger.log(Level.INFO, "- SUCCESS: Repo file decrypted successfully.");
                    configTO.setMasterKey(possibleMasterKey);
                    retryPassword = false;
                } else {
                    logger.log(Level.INFO, "- FAILURE: Repo file decryption failed. Asking for retry.");
                    retryPassword = askRetryPassword();
                    if (!retryPassword) {
                        logger.log(Level.INFO, "- No retry possible/desired. Returning NOK_DECRYPT_ERROR.");
                        return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
                    }
                }
            }
        } else {
            logger.log(Level.INFO, "- Master key present; Now verifying by decrypting repo file...");
            if (!decryptAndVerifyRepoFile(tmpRepoFile, configTO.getMasterKey())) {
                logger.log(Level.INFO, "- FAILURE: Repo file decryption failed. Returning NOK_DECRYPT_ERROR.");
                return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
            }
        }
    } else {
        String repoFileStr = FileUtils.readFileToString(tmpRepoFile);
        verifyRepoFile(repoFileStr);
    }
    // Success, now do the work!
    File appDir = createAppDirs(options.getLocalDir());
    // Write file 'config.xml'
    File configFile = new File(appDir, Config.FILE_CONFIG);
    configTO.save(configFile);
    // Write file 'syncany'
    File repoFile = new File(appDir, Config.FILE_REPO);
    FileUtils.copyFile(tmpRepoFile, repoFile);
    tmpRepoFile.delete();
    // Write file 'master'
    if (configTO.getMasterKey() != null) {
        File masterFile = new File(appDir, Config.FILE_MASTER);
        new MasterTO(configTO.getMasterKey().getSalt()).save(masterFile);
    }
    // Shutdown plugin
    transferManager.disconnect();
    // Add to daemon (if requested)
    if (options.isDaemon()) {
        try {
            boolean addedToDaemonConfig = DaemonConfigHelper.addFolder(options.getLocalDir());
            result.setAddedToDaemon(addedToDaemonConfig);
        } catch (Exception e) {
            logger.log(Level.WARNING, "Cannot add folder to daemon config.", e);
            result.setAddedToDaemon(false);
        }
    }
    result.setResultCode(ConnectResultCode.OK);
    return result;
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) CipherException(org.syncany.crypto.CipherException) ConfigTO(org.syncany.config.to.ConfigTO) MasterTO(org.syncany.config.to.MasterTO) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) MasterRemoteFile(org.syncany.plugins.transfer.files.MasterRemoteFile) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) File(java.io.File) StorageException(org.syncany.plugins.transfer.StorageException) CipherException(org.syncany.crypto.CipherException)

Example 2 with SyncanyRemoteFile

use of org.syncany.plugins.transfer.files.SyncanyRemoteFile in project syncany by syncany.

the class AbstractTransferManagerTest method testUploadListDownloadAndDelete.

@Test
public void testUploadListDownloadAndDelete() throws Exception {
    // Setup
    File tempFromDir = TestFileUtil.createTempDirectoryInSystemTemp();
    File tempToDir = TestFileUtil.createTempDirectoryInSystemTemp();
    // Create connection, upload, list, download
    TransferManager transferManager = loadPluginAndCreateTransferManager();
    transferManager.init(true);
    transferManager.connect();
    // Clear up previous test (if test location is reused)
    cleanTestLocation(transferManager);
    // Run!
    uploadDownloadListDelete(transferManager, tempFromDir, tempToDir, SyncanyRemoteFile.class, new SyncanyRemoteFile[] { new SyncanyRemoteFile() });
    uploadDownloadListDelete(transferManager, tempFromDir, tempToDir, MasterRemoteFile.class, new MasterRemoteFile[] { new MasterRemoteFile() });
    uploadDownloadListDelete(transferManager, tempFromDir, tempToDir, DatabaseRemoteFile.class, new DatabaseRemoteFile[] { new DatabaseRemoteFile("database-A-0001"), new DatabaseRemoteFile("database-B-0002") });
    uploadDownloadListDelete(transferManager, tempFromDir, tempToDir, MultichunkRemoteFile.class, new MultichunkRemoteFile[] { new MultichunkRemoteFile("multichunk-84f7e2b31440aaef9b73de3cadcf4e449aeb55a1"), new MultichunkRemoteFile("multichunk-beefbeefbeefbeefbeefbeefbeefbeefbeefbeef"), new MultichunkRemoteFile("multichunk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") });
    // Clear up previous test (if test location is reused)
    cleanTestLocation(transferManager);
    // Clean local location
    TestFileUtil.deleteDirectory(tempFromDir);
    TestFileUtil.deleteDirectory(tempToDir);
}
Also used : TransferManager(org.syncany.plugins.transfer.TransferManager) TransactionAwareFeatureTransferManager(org.syncany.plugins.transfer.features.TransactionAwareFeatureTransferManager) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) MasterRemoteFile(org.syncany.plugins.transfer.files.MasterRemoteFile) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) MasterRemoteFile(org.syncany.plugins.transfer.files.MasterRemoteFile) File(java.io.File) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) Test(org.junit.Test)

Aggregations

File (java.io.File)2 MasterRemoteFile (org.syncany.plugins.transfer.files.MasterRemoteFile)2 RemoteFile (org.syncany.plugins.transfer.files.RemoteFile)2 SyncanyRemoteFile (org.syncany.plugins.transfer.files.SyncanyRemoteFile)2 Test (org.junit.Test)1 ConfigTO (org.syncany.config.to.ConfigTO)1 MasterTO (org.syncany.config.to.MasterTO)1 CipherException (org.syncany.crypto.CipherException)1 SaltedSecretKey (org.syncany.crypto.SaltedSecretKey)1 StorageException (org.syncany.plugins.transfer.StorageException)1 TransferManager (org.syncany.plugins.transfer.TransferManager)1 TransactionAwareFeatureTransferManager (org.syncany.plugins.transfer.features.TransactionAwareFeatureTransferManager)1 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)1 MultichunkRemoteFile (org.syncany.plugins.transfer.files.MultichunkRemoteFile)1