Search in sources :

Example 1 with TempRemoteFile

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

the class RemoteTransaction method moveToFinalLocation.

/**
 * This method constitutes the second step in the committing process. All files have been uploaded, and they are
 * now moved to their final location.
 */
private void moveToFinalLocation() throws StorageException {
    for (ActionTO action : transactionTO.getActions()) {
        if (action.getType().equals(ActionType.UPLOAD)) {
            RemoteFile tempRemoteFile = action.getTempRemoteFile();
            RemoteFile finalRemoteFile = action.getRemoteFile();
            logger.log(Level.INFO, "- Moving temp. file {0} to final location {1} ...", new Object[] { tempRemoteFile, finalRemoteFile });
            transferManager.move(tempRemoteFile, finalRemoteFile);
            action.setStatus(ActionStatus.DONE);
        }
    }
}
Also used : ActionTO(org.syncany.plugins.transfer.to.ActionTO) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile)

Example 2 with TempRemoteFile

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

the class RemoteTransaction method uploadAndMoveToTempLocation.

/**
 * This method performs the first step for all files in the committing process.
 * For UPLOADs, this is uploading the file to the temporary remote location.
 * For DELETEs, this is moving the file from the original remote location to a temporary remote location.
 * If this is a transaction that is being resumed, the {@link ActionStatus} will show that this part has
 * already been done. In this case, we do not repeat it.
 *
 * This is the expensive part of the committing process, when we are talking about I/O. Hence this is also
 * the most likely part to be interrupted on weak connections.
 */
private void uploadAndMoveToTempLocation() throws StorageException {
    TransactionStats stats = gatherTransactionStats();
    int uploadFileIndex = 0;
    for (ActionTO action : transactionTO.getActions()) {
        if (action.getStatus().equals(ActionStatus.UNSTARTED)) {
            // If we are resuming, this has not been started yet.
            RemoteFile tempRemoteFile = action.getTempRemoteFile();
            if (action.getType().equals(ActionType.UPLOAD)) {
                // The action is an UPLOAD, upload file to temporary remote location
                File localFile = action.getLocalTempLocation();
                long localFileSize = localFile.length();
                eventBus.post(new UpUploadFileInTransactionSyncExternalEvent(config.getLocalDir().getAbsolutePath(), ++uploadFileIndex, stats.totalUploadFileCount, localFileSize, stats.totalUploadSize));
                logger.log(Level.INFO, "- Uploading {0} to temp. file {1} ...", new Object[] { localFile, tempRemoteFile });
                transferManager.upload(localFile, tempRemoteFile);
                action.setStatus(ActionStatus.STARTED);
            } else if (action.getType().equals(ActionType.DELETE)) {
                // The action is a DELETE, move file to temporary remote location.
                RemoteFile remoteFile = action.getRemoteFile();
                try {
                    logger.log(Level.INFO, "- Moving {0} to temp. file {1} ...", new Object[] { remoteFile, tempRemoteFile });
                    transferManager.move(remoteFile, tempRemoteFile);
                } catch (StorageMoveException e) {
                    logger.log(Level.INFO, "  -> FAILED (don't care!), because the remoteFile does not exist: " + remoteFile);
                }
                action.setStatus(ActionStatus.STARTED);
            }
        }
    }
}
Also used : ActionTO(org.syncany.plugins.transfer.to.ActionTO) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) File(java.io.File) TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) UpUploadFileInTransactionSyncExternalEvent(org.syncany.operations.daemon.messages.UpUploadFileInTransactionSyncExternalEvent) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile)

Example 3 with TempRemoteFile

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

the class TransactionAwareFeatureTransferManager method removeUnreferencedTemporaryFiles.

/**
 * Removes temporary files on the offsite storage that are not listed in any
 * of the {@link TransactionRemoteFile}s available remotely.
 *
 * <p>Temporary files might be left over from unfinished transactions.
 */
public void removeUnreferencedTemporaryFiles() throws StorageException {
    // Retrieve all transactions
    Map<TransactionTO, TransactionRemoteFile> transactions = retrieveRemoteTransactions();
    Collection<TempRemoteFile> tempRemoteFiles = list(TempRemoteFile.class).values();
    // Find all remoteFiles that are referenced in a transaction
    Set<TempRemoteFile> tempRemoteFilesInTransactions = new HashSet<TempRemoteFile>();
    for (TransactionTO transaction : transactions.keySet()) {
        for (ActionTO action : transaction.getActions()) {
            tempRemoteFilesInTransactions.add(action.getTempRemoteFile());
        }
    }
    // Consider just those files that are not referenced and delete them.
    tempRemoteFiles.removeAll(tempRemoteFilesInTransactions);
    for (TempRemoteFile unreferencedTempRemoteFile : tempRemoteFiles) {
        logger.log(Level.INFO, "Unreferenced temporary file found. Deleting {0}", unreferencedTempRemoteFile);
        underlyingTransferManager.delete(unreferencedTempRemoteFile);
    }
}
Also used : TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) ActionTO(org.syncany.plugins.transfer.to.ActionTO) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) TransactionTO(org.syncany.plugins.transfer.to.TransactionTO) HashSet(java.util.HashSet)

Example 4 with TempRemoteFile

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

the class TransactionAwareFeatureTransferManager method downloadDeletedTempFileInTransaction.

/**
 * Downloads all transaction files and looks for the corresponding temporary file
 * for the given remote file. If there is a temporary file, the file is downloaded
 * instead of the original file.
 *
 * <p>This method is <b>expensive</b>, but it is only called by {@link #download(RemoteFile, File) download()}
 * if a file does not exist.
 */
private void downloadDeletedTempFileInTransaction(RemoteFile remoteFile, File localFile) throws StorageException {
    logger.log(Level.INFO, "File {0} not found, checking if it is being deleted ...", remoteFile.getName());
    Set<TransactionTO> transactions = retrieveRemoteTransactions().keySet();
    TempRemoteFile tempRemoteFile = null;
    // Find file: If the file is being deleted and the name matches, download temporary file instead.
    for (TransactionTO transaction : transactions) {
        for (ActionTO action : transaction.getActions()) {
            if (action.getType().equals(ActionType.DELETE) && action.getRemoteFile().equals(remoteFile)) {
                tempRemoteFile = action.getTempRemoteFile();
                break;
            }
        }
    }
    // Download file, or throw exception
    if (tempRemoteFile != null) {
        logger.log(Level.INFO, "-> File {0} in process of being deleted; downloading corresponding temp. file {1} ...", new Object[] { remoteFile.getName(), tempRemoteFile.getName() });
        underlyingTransferManager.download(tempRemoteFile, localFile);
    } else {
        logger.log(Level.WARNING, "-> File {0} does not exist and is not in any transaction. Throwing exception.", remoteFile.getName());
        throw new StorageFileNotFoundException("File " + remoteFile.getName() + " does not exist and is not in any transaction");
    }
}
Also used : TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) StorageFileNotFoundException(org.syncany.plugins.transfer.StorageFileNotFoundException) ActionTO(org.syncany.plugins.transfer.to.ActionTO) TransactionTO(org.syncany.plugins.transfer.to.TransactionTO)

Example 5 with TempRemoteFile

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

the class RemoteTransaction method delete.

/**
 * Adds the deletion of a file to this transaction. Generates a temporary file
 * to store it while the transaction is being finalized.
 */
public void delete(RemoteFile remoteFile) throws StorageException {
    TempRemoteFile temporaryRemoteFile = new TempRemoteFile(remoteFile);
    logger.log(Level.INFO, "- Adding file to TX for DELETE: " + remoteFile + "-> Temp. remote file: " + temporaryRemoteFile);
    ActionTO action = new ActionTO();
    action.setType(ActionType.DELETE);
    action.setRemoteLocation(remoteFile);
    action.setRemoteTempLocation(temporaryRemoteFile);
    transactionTO.addAction(action);
}
Also used : TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) ActionTO(org.syncany.plugins.transfer.to.ActionTO)

Aggregations

TempRemoteFile (org.syncany.plugins.transfer.files.TempRemoteFile)7 ActionTO (org.syncany.plugins.transfer.to.ActionTO)7 TransactionRemoteFile (org.syncany.plugins.transfer.files.TransactionRemoteFile)4 RemoteFile (org.syncany.plugins.transfer.files.RemoteFile)3 TransactionTO (org.syncany.plugins.transfer.to.TransactionTO)2 File (java.io.File)1 HashSet (java.util.HashSet)1 UpUploadFileInTransactionSyncExternalEvent (org.syncany.operations.daemon.messages.UpUploadFileInTransactionSyncExternalEvent)1 StorageFileNotFoundException (org.syncany.plugins.transfer.StorageFileNotFoundException)1