blob: 104a545c3abe7ba99e0fd34e69fc2d0aca135ed1 [file] [log] [blame]
package org.eclipse.epp.installer.internal.core;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.epp.installer.core.model.Context;
import org.eclipse.epp.installer.core.model.IRollbackAction;
/**
* RemoveFileAction used to remove files and directories.
*
* @author andrey
* @version $Revision: 1.1 $
*/
public class RestoreFileAction implements IRollbackAction {
public static File getOriginalFile(InstallLogEntry e) {
return new File(e.getArgument().split(InstallLogEntry.BACKUP_ENTRY_TOKEN)[0]);
}
public static File getStoredFile(InstallLogEntry e) {
return new File(e.getArgument().split(InstallLogEntry.BACKUP_ENTRY_TOKEN)[1]);
}
public static boolean isStoredTemporary(InstallLogEntry e) {
return Boolean.valueOf(e.getArgument().split(InstallLogEntry.BACKUP_ENTRY_TOKEN)[2]).booleanValue();
}
/**
* Execute action.
*
* @param entry
* InstallLogEntry to get data from.
* @return IStatus <code>Status.OK_STATUS</code> failures are saved.
* @see org.eclipse.epp.installer.core.model.IRollbackAction#rollback(InstallLogEntry)
*/
public IStatus rollback(InstallLogEntry entry) {
File storedFile = getStoredFile(entry);
File originalFile = getOriginalFile(entry);
// do not restore temporary backuped files if not necessary
if (isStoredTemporary(entry) && !BackupManager.getBackupManager().isRestoreTemporary())
return Status.OK_STATUS;
try {
if (storedFile.exists()) {
if (originalFile.exists()) {
originalFile.delete();
}
if (!storedFile.isDirectory()) {
originalFile.createNewFile();
Util.copyFiles(storedFile, originalFile);
} else {
originalFile.mkdir();
}
}
} catch (IOException e) {
return new Status(IStatus.ERROR, Context.PLUGIN_ID, 0,
"can't restore file: " + originalFile, null);
}
return Status.OK_STATUS;
}
/**
* Return action description.
*
* @param entry
* InstallLogEntry entry to get data from.
* @return String action description.
* @see org.eclipse.epp.installer.core.model.IRollbackAction#getDescription(InstallLogEntry)
*/
public String getDescription(InstallLogEntry entry) {
return MessageFormat.format("Restoring file {0}.",
new Object[] { getOriginalFile(entry) });
}
}