Search in sources :

Example 6 with ActionTO

use of org.syncany.plugins.transfer.to.ActionTO 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 7 with ActionTO

use of org.syncany.plugins.transfer.to.ActionTO 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 8 with ActionTO

use of org.syncany.plugins.transfer.to.ActionTO in project syncany by syncany.

the class UpOperation method attemptResumeTransactions.

private Collection<RemoteTransaction> attemptResumeTransactions(Collection<Long> versions) {
    try {
        Collection<RemoteTransaction> remoteTransactions = new ArrayList<>();
        for (Long version : versions) {
            File transactionFile = config.getTransactionFile(version);
            // If a single transaction file is missing, we should restart
            if (!transactionFile.exists()) {
                return null;
            }
            TransactionTO transactionTO = TransactionTO.load(null, transactionFile);
            // Verify if all files needed are in cache.
            for (ActionTO action : transactionTO.getActions()) {
                if (action.getType() == ActionType.UPLOAD) {
                    if (action.getStatus() == ActionStatus.UNSTARTED) {
                        if (!action.getLocalTempLocation().exists()) {
                            // Unstarted upload has no cached local copy, abort
                            return null;
                        }
                    }
                }
            }
            remoteTransactions.add(new RemoteTransaction(config, transferManager, transactionTO));
        }
        return remoteTransactions;
    } catch (Exception e) {
        logger.log(Level.WARNING, "Invalid transaction file. Cannot resume!");
        return null;
    }
}
Also used : ActionTO(org.syncany.plugins.transfer.to.ActionTO) RemoteTransaction(org.syncany.plugins.transfer.RemoteTransaction) ArrayList(java.util.ArrayList) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) StorageException(org.syncany.plugins.transfer.StorageException) SQLException(java.sql.SQLException) IOException(java.io.IOException) TransactionTO(org.syncany.plugins.transfer.to.TransactionTO)

Example 9 with ActionTO

use of org.syncany.plugins.transfer.to.ActionTO 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)

Example 10 with ActionTO

use of org.syncany.plugins.transfer.to.ActionTO in project syncany by syncany.

the class RemoteTransaction method deleteTempRemoteFiles.

/**
 * This method deletes the temporary remote files that were the result of deleted files.
 *
 * Actually deleting remote files is done after finishing the transaction, because
 * it cannot be rolled back! If this fails, the temporary files will eventually
 * be cleaned up by Cleanup and download will not download these, because
 * they are not in any transaction file.
 */
private void deleteTempRemoteFiles() throws StorageException {
    boolean success = true;
    for (ActionTO action : transactionTO.getActions()) {
        if (action.getStatus().equals(ActionStatus.STARTED)) {
            // If we are resuming, this action has not been comopleted.
            if (action.getType().equals(ActionType.DELETE)) {
                RemoteFile tempRemoteFile = action.getTempRemoteFile();
                logger.log(Level.INFO, "- Deleting temp. file {0}  ...", new Object[] { tempRemoteFile });
                try {
                    transferManager.delete(tempRemoteFile);
                } catch (Exception e) {
                    logger.log(Level.INFO, "Failed to delete: " + tempRemoteFile, " because of: " + e);
                    success = false;
                }
                action.setStatus(ActionStatus.DONE);
            }
        }
    }
    if (success) {
        logger.log(Level.INFO, "END of TX.delTemp(): Sucessfully deleted final files.");
    } else {
        logger.log(Level.INFO, "END of TX.delTemp(): Did not succesfully delete all files!");
    }
}
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)

Aggregations

ActionTO (org.syncany.plugins.transfer.to.ActionTO)12 TempRemoteFile (org.syncany.plugins.transfer.files.TempRemoteFile)7 TransactionRemoteFile (org.syncany.plugins.transfer.files.TransactionRemoteFile)7 TransactionTO (org.syncany.plugins.transfer.to.TransactionTO)5 RemoteFile (org.syncany.plugins.transfer.files.RemoteFile)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 UpUploadFileInTransactionSyncExternalEvent (org.syncany.operations.daemon.messages.UpUploadFileInTransactionSyncExternalEvent)1 BlockingTransfersException (org.syncany.operations.up.BlockingTransfersException)1 RemoteTransaction (org.syncany.plugins.transfer.RemoteTransaction)1 StorageException (org.syncany.plugins.transfer.StorageException)1 StorageFileNotFoundException (org.syncany.plugins.transfer.StorageFileNotFoundException)1 StorageMoveException (org.syncany.plugins.transfer.StorageMoveException)1 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)1 MultichunkRemoteFile (org.syncany.plugins.transfer.files.MultichunkRemoteFile)1