Exemple #1
0
  private void downloadAndDecryptMultiChunks(Set<MultiChunkEntry> unknownMultiChunks)
      throws StorageException, IOException {
    logger.log(Level.INFO, "- Downloading and extracting multichunks ...");
    TransferManager transferManager = config.getConnection().createTransferManager();

    // TODO [medium] Check existing files by checksum and do NOT download them if they exist
    // locally, or copy them

    for (MultiChunkEntry multiChunkEntry : unknownMultiChunks) {
      File localEncryptedMultiChunkFile =
          config.getCache().getEncryptedMultiChunkFile(multiChunkEntry.getId());
      File localDecryptedMultiChunkFile =
          config.getCache().getDecryptedMultiChunkFile(multiChunkEntry.getId());
      MultiChunkRemoteFile remoteMultiChunkFile =
          new MultiChunkRemoteFile(
              localEncryptedMultiChunkFile
                  .getName()); // TODO [low] Make MultiChunkRemoteFile class, or something like that

      logger.log(
          Level.INFO,
          "  + Downloading multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ...");
      transferManager.download(remoteMultiChunkFile, localEncryptedMultiChunkFile);
      result.downloadedMultiChunks.add(multiChunkEntry);

      logger.log(
          Level.INFO,
          "  + Decrypting multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ...");
      InputStream multiChunkInputStream =
          config
              .getTransformer()
              .createInputStream(new FileInputStream(localEncryptedMultiChunkFile));
      OutputStream decryptedMultiChunkOutputStream =
          new FileOutputStream(localDecryptedMultiChunkFile);

      // TODO [medium] Calculate checksum while writing file, to verify correct content
      FileUtil.appendToOutputStream(multiChunkInputStream, decryptedMultiChunkOutputStream);

      decryptedMultiChunkOutputStream.close();
      multiChunkInputStream.close();

      logger.log(
          Level.FINE,
          "  + Locally deleting multichunk " + StringUtil.toHex(multiChunkEntry.getId()) + " ...");
      localEncryptedMultiChunkFile.delete();
    }

    transferManager.disconnect();
  }
Exemple #2
0
  private List<File> downloadUnknownRemoteDatabases(
      TransferManager transferManager, List<RemoteFile> unknownRemoteDatabases)
      throws StorageException {
    logger.log(Level.INFO, "Downloading unknown databases.");
    List<File> unknownRemoteDatabasesInCache = new ArrayList<File>();

    for (RemoteFile remoteFile : unknownRemoteDatabases) {
      File unknownRemoteDatabaseFileInCache =
          config.getCache().getDatabaseFile(remoteFile.getName());

      logger.log(
          Level.INFO,
          "- Downloading {0} to local cache at {1}",
          new Object[] {remoteFile.getName(), unknownRemoteDatabaseFileInCache});
      transferManager.download(
          new DatabaseRemoteFile(remoteFile.getName()), unknownRemoteDatabaseFileInCache);

      unknownRemoteDatabasesInCache.add(unknownRemoteDatabaseFileInCache);
      result.getDownloadedUnknownDatabases().add(remoteFile.getName());
    }

    return unknownRemoteDatabasesInCache;
  }
Exemple #3
0
  private void pruneConflictingLocalBranch(Branch winnersBranch) throws Exception {
    Branch localPruneBranch =
        databaseReconciliator.findLosersPruneBranch(localBranch, winnersBranch);
    logger.log(Level.INFO, "- Database versions to REMOVE locally: " + localPruneBranch);

    if (localPruneBranch.size() == 0) {
      logger.log(Level.INFO, "  + Nothing to prune locally. No conflicts. Only updates. Nice!");
    } else {
      // Load dirty database (if existent)
      logger.log(Level.INFO, "  + Pruning databases locally ...");
      Database dirtyDatabase = new Database();

      for (DatabaseVersionHeader databaseVersionHeader : localPruneBranch.getAll()) {
        // Database version
        DatabaseVersion databaseVersion =
            localDatabase.getDatabaseVersion(databaseVersionHeader.getVectorClock());
        dirtyDatabase.addDatabaseVersion(databaseVersion);

        // Remove database version locally
        logger.log(Level.INFO, "    * Removing " + databaseVersionHeader + " ...");
        localDatabase.removeDatabaseVersion(databaseVersion);

        DatabaseRemoteFile remoteFileToPrune =
            new DatabaseRemoteFile(
                "db-"
                    + config.getMachineName()
                    + "-"
                    + databaseVersionHeader.getVectorClock().get(config.getMachineName()));
        logger.log(Level.INFO, "    * Deleting remote database file " + remoteFileToPrune + " ...");
        transferManager.delete(remoteFileToPrune);
      }

      logger.log(
          Level.INFO, "    * Saving dirty database to " + config.getDirtyDatabaseFile() + " ...");
      saveLocalDatabase(dirtyDatabase, config.getDirtyDatabaseFile());
    }
  }