/** * Creates a temporary file, either using the config (if initialized) or * using the global temporary directory. */ protected File createTempFile(String name) throws IOException { if (config == null) { return File.createTempFile(String.format("temp-%s-", name), ".tmp"); } else { return config.getCache().createTempFile(name); } }
@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()); }
@Override public File getMultiChunkFile(MultiChunkId multiChunkId) { return config.getCache().getEncryptedMultiChunkFile(multiChunkId); }
@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); }
private void clearCache() { config.getCache().clear(); } }
/** * Creates a temporary file, either using the config (if initialized) or * using the global temporary directory. */ protected File createTempFile(String name) throws IOException { // TODO [low] duplicate code with AbstractTransferManager if (config == null) { return File.createTempFile(String.format("temp-%s-", name), ".tmp"); } else { return config.getCache().createTempFile(name); } } }
/** * 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); } }
|| actualLocalFile.toFile().equals(config.getCache()) || actualLocalFile.toFile().equals(config.getDatabaseDir()) || actualLocalFile.toFile().equals(config.getLogDir());
/** * 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; }
/** * This methods adds the multichunks that are not yet present in the remote repo to the {@link RemoteTransaction} for * uploading. Multichunks are not uploaded if they are dirty. * * @param multiChunkEntries Collection of multiChunkEntries that are included in the new {@link DatabaseVersion} */ private void addMultiChunksToTransaction(RemoteTransaction remoteTransaction, Collection<MultiChunkEntry> multiChunksEntries) throws InterruptedException, StorageException { List<MultiChunkId> dirtyMultiChunkIds = localDatabase.getDirtyMultiChunkIds(); for (MultiChunkEntry multiChunkEntry : multiChunksEntries) { if (dirtyMultiChunkIds.contains(multiChunkEntry.getId())) { logger.log(Level.INFO, "- Ignoring multichunk (from dirty database, already uploaded), " + multiChunkEntry.getId() + " ..."); } else { File localMultiChunkFile = config.getCache().getEncryptedMultiChunkFile(multiChunkEntry.getId()); MultichunkRemoteFile remoteMultiChunkFile = new MultichunkRemoteFile(multiChunkEntry.getId()); logger.log(Level.INFO, "- Uploading multichunk {0} from {1} to {2} ...", new Object[] { multiChunkEntry.getId(), localMultiChunkFile, remoteMultiChunkFile }); remoteTransaction.upload(localMultiChunkFile, remoteMultiChunkFile); } } }
/** * This method writes the file with merged databases for a single client and adds it to a Map containing all merged * database files. This is done by querying the local database for all {@link DatabaseVersion}s by this client and * serializing them. * * @param clientName for which we want to write the merged dataabse file. * @param allMergedDatabaseFiles Map where we add the merged file once it is written. */ private void writeMergeFile(String clientName, Map<File, DatabaseRemoteFile> allMergedDatabaseFiles) throws StorageException, IOException { // Increment the version by 1, to signal cleanup has occurred long lastClientVersion = getNewestDatabaseFileVersion(clientName, localDatabase.getKnownDatabases()); DatabaseRemoteFile newRemoteMergeDatabaseFile = new DatabaseRemoteFile(clientName, lastClientVersion + 1); File newLocalMergeDatabaseFile = config.getCache().getDatabaseFile(newRemoteMergeDatabaseFile.getName()); logger.log(Level.INFO, " + Writing new merge file (all files up to {0}) to {1} ...", new Object[] { lastClientVersion, newLocalMergeDatabaseFile }); Iterator<DatabaseVersion> lastNDatabaseVersions = localDatabase.getDatabaseVersionsTo(clientName, lastClientVersion); DatabaseXmlSerializer databaseDAO = new DatabaseXmlSerializer(config.getTransformer()); databaseDAO.save(lastNDatabaseVersions, newLocalMergeDatabaseFile); allMergedDatabaseFiles.put(newLocalMergeDatabaseFile, newRemoteMergeDatabaseFile); }
File reconstructedFileInCache = config.getCache().createTempFile("reconstructedFileVersion"); logger.log(Level.INFO, " - Creating file " + fileVersion.getPath() + " to " + reconstructedFileInCache + " ..."); File decryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkIdForChunk);
/** * This method checks what the current cleanup number is, increments it by one and adds * a new cleanup file to the transaction, to signify to other clients that Cleanup has occurred. */ private void updateCleanupFileInTransaction() throws StorageException, IOException { if (remoteTransaction.isEmpty()) { // No need to bump numbers return; } // Find all existing cleanup files Map<String, CleanupRemoteFile> cleanupFiles = transferManager.list(CleanupRemoteFile.class); long lastRemoteCleanupNumber = getLastRemoteCleanupNumber(cleanupFiles); // Schedule any existing cleanup files for deletion for (CleanupRemoteFile cleanupRemoteFile : cleanupFiles.values()) { remoteTransaction.delete(cleanupRemoteFile); } // Upload a new cleanup file that indicates changes File newCleanupFile = config.getCache().createTempFile("cleanup"); long newCleanupNumber = lastRemoteCleanupNumber + 1; remoteTransaction.upload(newCleanupFile, new CleanupRemoteFile(newCleanupNumber)); localDatabase.writeCleanupNumber(newCleanupNumber); }
File localDeltaDatabaseFile = config.getCache().getDatabaseFile(remoteDeltaDatabaseFile.getName());