protected File getAbsolutePathFile(String relativePath) { return new File(config.getLocalDir(), relativePath); // TODO [medium] This does not work for 'some\file' on windows! }
private void startRecursiveWatcher() { logger.log(Level.INFO, "Starting recursive watcher for " + config.getLocalDir() + " ..."); Path localDir = Paths.get(config.getLocalDir().getAbsolutePath()); List<Path> ignorePaths = new ArrayList<Path>(); ignorePaths.add(Paths.get(config.getAppDir().getAbsolutePath())); ignorePaths.add(Paths.get(config.getCacheDir().getAbsolutePath())); ignorePaths.add(Paths.get(config.getDatabaseDir().getAbsolutePath())); ignorePaths.add(Paths.get(config.getLogDir().getAbsolutePath())); recursiveWatcher = RecursiveWatcher.createRecursiveWatcher(localDir, ignorePaths, options.getSettleDelay(), this); try { recursiveWatcher.start(); } catch (Exception e) { logger.log(Level.WARNING, "Cannot initiate file watcher. Relying on regular tree walks.", e); } }
private void startIndexerThread(BlockingQueue<DatabaseVersion> databaseVersionQueue) { // Get a list of files that have been updated ChangeSet localChanges = result.getStatusResult().getChangeSet(); List<File> locallyUpdatedFiles = extractLocallyUpdatedFiles(localChanges); List<File> locallyDeletedFiles = extractLocallyDeletedFiles(localChanges); // Iterate over the changes, deduplicate, and feed DatabaseVersions into an iterator Deduper deduper = new Deduper(config.getChunker(), config.getMultiChunker(), config.getTransformer(), options.getTransactionSizeLimit(), options.getTransactionFileLimit()); AsyncIndexer asyncIndexer = new AsyncIndexer(config, deduper, locallyUpdatedFiles, locallyDeletedFiles, databaseVersionQueue); new Thread(asyncIndexer, "AsyncI/" + config.getLocalDir().getName()).start(); }
/** * Creates the transfer manager factory builder from the {@link Config} * using the configured {@link TransferPlugin}. Using this builder, the * feature-wrapped transfer manager can be built. * * @see TransferManagerBuilder * @param config Local folder configuration with transfer plugin settings * @return Transfer manager builder */ public static TransferManagerBuilder build(Config config) throws StorageException { TransferManager transferManager = config.getTransferPlugin().createTransferManager(config.getConnection(), config); logger.log(Level.INFO, "Building " + transferManager.getClass().getSimpleName() + " from config '" + config.getLocalDir().getName() + "' ..."); return new TransferManagerBuilder(config, transferManager); }
public FileSystemAction(Config config, MemoryDatabase winningDatabase, FileVersion file1, FileVersion file2) { this.config = config; this.winningDatabase = winningDatabase; this.fileVersion1 = file1; this.fileVersion2 = file2; this.fileVersionHelper = new FileVersionComparator(config.getLocalDir(), config.getChunker().getChecksumAlgorithm()); }
@Test public void testConfigHelperLoadConfig() throws Exception { // Setup Config testConfig = TestConfigUtil.createTestLocalConfig(); // Run Config loadedConfig = ConfigHelper.loadConfig(testConfig.getLocalDir()); // Test assertNotNull(loadedConfig); assertEquals(testConfig.getAppDir(), loadedConfig.getAppDir()); assertEquals(testConfig.getCacheDir(), loadedConfig.getCacheDir()); assertNotNull(loadedConfig.getChunker()); assertEquals(testConfig.getChunker().toString(), loadedConfig.getChunker().toString()); assertNotNull(loadedConfig.getCache()); assertNotNull(loadedConfig.getConnection()); assertEquals(testConfig.getDatabaseDir(), loadedConfig.getDatabaseDir()); assertEquals(testConfig.getDatabaseFile(), loadedConfig.getDatabaseFile()); assertEquals(testConfig.getDisplayName(), loadedConfig.getDisplayName()); assertEquals(testConfig.getLocalDir(), loadedConfig.getLocalDir()); assertEquals(testConfig.getLogDir(), loadedConfig.getLogDir()); assertEquals(testConfig.getMachineName(), loadedConfig.getMachineName()); assertEquals(testConfig.getMasterKey(), loadedConfig.getMasterKey()); assertNotNull(loadedConfig.getMultiChunker()); assertNotNull(loadedConfig.getRepoId()); assertNotNull(loadedConfig.getTransformer()); // Tear down TestConfigUtil.deleteTestLocalConfigAndData(testConfig); }
@Test public void testConfigValid() throws Exception { // Setup File localDir = new File("/some/folder"); ConfigTO configTO = new ConfigTO(); RepoTO repoTO = new RepoTO(); configTO.setMachineName("somevalidmachinename"); // <<< valid repoTO.setChunkerTO(TestConfigUtil.createFixedChunkerTO()); // <<< valid repoTO.setMultiChunker(TestConfigUtil.createZipMultiChunkerTO()); // <<< valid repoTO.setRepoId(new byte[] { 0x01, 0x02 }); // <<< valid repoTO.setTransformers(null); // <<< valid // Run! Config config = new Config(localDir, configTO, repoTO); // Test assertEquals("/some/folder/.syncany", config.getAppDir().getAbsolutePath()); assertEquals("/some/folder/.syncany/cache", config.getCacheDir().getAbsolutePath()); assertEquals("/some/folder/.syncany/db", config.getDatabaseDir().getAbsolutePath()); assertEquals("/some/folder/.syncany/db/local.db", config.getDatabaseFile().getAbsolutePath()); assertNotNull(config.getChunker()); assertEquals("FixedChunker", config.getChunker().getClass().getSimpleName()); assertEquals("SHA1", config.getChunker().getChecksumAlgorithm()); assertNotNull(config.getMultiChunker()); assertEquals("ZipMultiChunker", config.getMultiChunker().getClass().getSimpleName()); assertNotNull(config.getTransformer()); assertEquals("NoTransformer", config.getTransformer().getClass().getSimpleName()); assertNotNull(config.getCache()); }
if (actualLocalFile.toFile().equals(config.getLocalDir())) { return FileVisitResult.CONTINUE; boolean isAppRelatedDir = actualLocalFile.toFile().equals(config.getAppDir()) || actualLocalFile.toFile().equals(config.getCache()) || actualLocalFile.toFile().equals(config.getDatabaseDir()) || actualLocalFile.toFile().equals(config.getLogDir()); if (!config.getIgnoredFiles().isFileIgnored(relativeFilePath, actualLocalFile.toFile().getName())) { changeSet.getNewFiles().add(relativeFilePath); logger.log(Level.FINEST, "- New file: " + relativeFilePath);
public static Config createTestLocalConfig(String machineName, TransferSettings connection) throws Exception { File tempLocalDir = TestFileUtil.createTempDirectoryInSystemTemp(createUniqueName("client-" + machineName, connection)); tempLocalDir.mkdirs(); RepoTO repoTO = createRepoTO(); // Create config TO ConfigTO configTO = new ConfigTO(); configTO.setMachineName(machineName + CipherUtil.createRandomAlphabeticString(20)); // Get Masterkey SaltedSecretKey masterKey = getMasterKey(); configTO.setMasterKey(masterKey); LocalTransferSettings localConnection = (LocalTransferSettings) connection; // Create connection TO Map<String, String> localConnectionSettings = new HashMap<String, String>(); localConnectionSettings.put("path", localConnection.getPath().getAbsolutePath()); configTO.setTransferSettings(connection); // Create Config config = new Config(tempLocalDir, configTO, repoTO); config.setConnection(connection); config.getAppDir().mkdirs(); config.getCacheDir().mkdirs(); config.getDatabaseDir().mkdirs(); config.getLogDir().mkdirs(); config.getStateDir().mkdirs(); // Write to config folder (required for some tests) new Persister().write(configTO, new File(config.getAppDir() + "/" + Config.FILE_CONFIG)); new Persister().write(repoTO, new File(config.getAppDir() + "/" + Config.FILE_REPO)); return config; }
@Test public void testConfigHelperLoadConfigTO() throws Exception { // Setup Config testConfig = TestConfigUtil.createTestLocalConfig(); // Run ConfigTO loadedConfigTO = ConfigHelper.loadConfigTO(testConfig.getLocalDir()); // Test assertNotNull(loadedConfigTO); assertEquals(testConfig.getDisplayName(), loadedConfigTO.getDisplayName()); assertEquals(testConfig.getMachineName(), loadedConfigTO.getMachineName()); assertEquals(testConfig.getMasterKey(), loadedConfigTO.getMasterKey()); // Tear down TestConfigUtil.deleteTestLocalConfigAndData(testConfig); }
File localEncryptedMultiChunkFile = config.getCache().getEncryptedMultiChunkFile(multiChunkId); File localDecryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkId); MultichunkRemoteFile remoteMultiChunkFile = new MultichunkRemoteFile(multiChunkId); eventBus.post(new DownDownloadFileSyncExternalEvent(config.getLocalDir().getAbsolutePath(), "multichunk", multiChunkNumber, unknownMultiChunkIds.size())); InputStream multiChunkInputStream = config.getTransformer().createInputStream(new FileInputStream(localEncryptedMultiChunkFile)); OutputStream decryptedMultiChunkOutputStream = new FileOutputStream(localDecryptedMultiChunkFile);
Config config = new Config(localDir, configTO, repoTO); assertNotNull(config.getChunker()); assertNotNull(config.getMultiChunker()); assertNotNull(config.getTransformer()); assertEquals("CipherTransformer", config.getTransformer().getClass().getSimpleName());
/** * This method serializes the transaction to a local temporary file. */ private File writeLocalTransactionFile() throws StorageException { try { File localTransactionFile = config.getCache().createTempFile("transaction"); writeToFile(config.getTransformer(), localTransactionFile); return localTransactionFile; } catch (Exception e) { throw new StorageException("Could not create temporary file for transaction", e); } }
@Override public LsRemoteOperationResult execute() throws Exception { logger.log(Level.INFO, ""); logger.log(Level.INFO, "Running 'Remote Status' at client " + config.getMachineName() + " ..."); logger.log(Level.INFO, "--------------------------------------------"); eventBus.post(new LsRemoteStartSyncExternalEvent(config.getLocalDir().getAbsolutePath())); TransferManager transferManager = createTransferManager(loadedTransferManager); List<DatabaseRemoteFile> knownDatabases = localDatabase.getKnownDatabases(); List<DatabaseRemoteFile> unknownRemoteDatabases = listUnknownRemoteDatabases(transferManager, knownDatabases); transferManager.disconnect(); boolean hasChanges = unknownRemoteDatabases.size() > 0; eventBus.post(new LsRemoteEndSyncExternalEvent(config.getLocalDir().getAbsolutePath(), hasChanges)); return new LsRemoteOperationResult(new ArrayList<>(unknownRemoteDatabases)); }
File reconstructedFileInCache = config.getCache().createTempFile("reconstructedFileVersion"); logger.log(Level.INFO, " - Creating file " + fileVersion.getPath() + " to " + reconstructedFileInCache + " ..."); Chunker chunker = config.getChunker(); MultiChunker multiChunker = config.getMultiChunker(); File decryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkIdForChunk);
@Override public void run() { try { logger.log(Level.INFO, "STARTING watch at" + config.getLocalDir()); watchOperationResult = null; // Write port to portFile File portFile = config.getPortFile(); portFile.createNewFile(); portFile.deleteOnExit(); new Persister().write(portTO, portFile); // Start operation (blocks!) watchOperationResult = watchOperation.execute(); logger.log(Level.INFO, "STOPPED watch at " + config.getLocalDir()); } catch (Exception e) { logger.log(Level.SEVERE, "ERROR while running watch at " + config.getLocalDir(), e); } } }, "WR/" + config.getLocalDir().getName());
@Test(expected = ConfigException.class) public void testConfigInitRepoTONull() throws Exception { File localDir = new File("/some/folder"); ConfigTO configTO = new ConfigTO(); RepoTO repoTO = null; new Config(localDir, configTO, repoTO); }
private void runDebugCommand(List<?> nonOptionArgs) throws Exception { logger.log(Level.INFO, "Running 'decrypt' command with arguments: "+nonOptionArgs); if (nonOptionArgs.size() != 1) { throw new Exception("Invalid syntax for 'debug' command. Argument expected."); } if (!isInitializedScope()) { throw new Exception("Command 'debug' can only be run in initialized local dir."); } File decryptFile = new File(nonOptionArgs.get(0).toString()); if (!decryptFile.exists()) { throw new Exception("Given file does not exist: "+decryptFile); } InputStream fileInputStream = config.getTransformer().createInputStream(new FileInputStream(decryptFile)); IOUtils.copy(fileInputStream, System.out); System.exit(0); }
/** * Downloads the previously identified new/unknown remote databases to the local cache * and returns a map with the local cache files mapped to the given remote database * files. The method additionally fires events for every database it downloads. */ private SortedMap<File, DatabaseRemoteFile> downloadUnknownRemoteDatabases(List<DatabaseRemoteFile> unknownRemoteDatabases) throws StorageException { logger.log(Level.INFO, "Downloading unknown databases."); SortedMap<File, DatabaseRemoteFile> unknownRemoteDatabasesInCache = new TreeMap<File, DatabaseRemoteFile>(); int downloadFileIndex = 0; for (DatabaseRemoteFile remoteFile : unknownRemoteDatabases) { File unknownRemoteDatabaseFileInCache = config.getCache().getDatabaseFile(remoteFile.getName()); DatabaseRemoteFile unknownDatabaseRemoteFile = new DatabaseRemoteFile(remoteFile.getName()); logger.log(Level.INFO, "- Downloading {0} to local cache at {1}", new Object[] { remoteFile.getName(), unknownRemoteDatabaseFileInCache }); eventBus.post(new DownDownloadFileSyncExternalEvent(config.getLocalDir().getAbsolutePath(), "database", ++downloadFileIndex, unknownRemoteDatabases.size())); transferManager.download(unknownDatabaseRemoteFile, unknownRemoteDatabaseFileInCache); unknownRemoteDatabasesInCache.put(unknownRemoteDatabaseFileInCache, unknownDatabaseRemoteFile); result.getDownloadedUnknownDatabases().add(remoteFile.getName()); } return unknownRemoteDatabasesInCache; }
@Test public void testConfigHelperFindLocalDirInPathTwoLevelsDown() throws Exception { // Setup Config testConfig = TestConfigUtil.createTestLocalConfig(); new File(testConfig.getAppDir()+"/some/folder").mkdirs(); File startingPath = new File(testConfig.getAppDir()+"/some/folder"); // Run File actualLocalDir = ConfigHelper.findLocalDirInPath(startingPath); // Test assertNotNull(actualLocalDir); assertEquals(testConfig.getLocalDir(), actualLocalDir.getCanonicalFile()); // Tear down TestConfigUtil.deleteTestLocalConfigAndData(testConfig); }