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);
}
}
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");
}
}
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;
}
}
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);
}
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!");
}
}
Aggregations