public override TaskExecutionStatus Execute(bool coldRun)
{
if (string.IsNullOrEmpty(LocalPath))
{
UpdateManager.Instance.Logger.Log(Logger.SeverityLevel.Warning, "FileUpdateTask: LocalPath is empty, task is a noop");
return TaskExecutionStatus.Successful; // Errorneous case, but there's nothing to prepare to, and by default we prefer a noop over an error
}
var dirName = Path.GetDirectoryName(_destinationFile);
if (!Directory.Exists(dirName))
{
Utils.FileSystem.CreateDirectoryStructure(dirName, false);
}
// Create a backup copy if target exists
if (_backupFile == null && File.Exists(_destinationFile))
{
if (!Directory.Exists(Path.GetDirectoryName(Path.Combine(UpdateManager.Instance.Config.BackupFolder, LocalPath))))
{
string backupPath = Path.GetDirectoryName(Path.Combine(UpdateManager.Instance.Config.BackupFolder, LocalPath));
Utils.FileSystem.CreateDirectoryStructure(backupPath, false);
}
_backupFile = Path.Combine(UpdateManager.Instance.Config.BackupFolder, LocalPath);
File.Copy(_destinationFile, _backupFile, true);
}
// Only allow execution if the apply attribute was set to hot-swap, or if this is a cold run
if (CanHotSwap || coldRun)
{
if (File.Exists(_destinationFile))
{
FileLockWait();
if (!PermissionsCheck.HaveWritePermissionsForFileOrFolder(_destinationFile))
{
if (coldRun)
{
UpdateManager.Instance.Logger.Log(Logger.SeverityLevel.Warning, "Don't have permissions to touch {0}", _destinationFile);
File.Delete(_destinationFile); // get the original exception from the system
}
CanHotSwap = false;
}
}
try
{
if (File.Exists(_destinationFile))
{
FileSystem.CopyAccessControl(new FileInfo(_destinationFile), new FileInfo(_tempFile));
File.Delete(_destinationFile);
}
File.Move(_tempFile, _destinationFile);
_tempFile = null;
}
catch (Exception ex)
{
if (coldRun)
{
ExecutionStatus = TaskExecutionStatus.Failed;
throw new UpdateProcessFailedException("Could not replace the file", ex);
}
// Failed hot swap file tasks should now downgrade to cold tasks automatically
CanHotSwap = false;
}
}
if (coldRun || CanHotSwap)
// If we got thus far, we have completed execution
return TaskExecutionStatus.Successful;
// Otherwise, figure out what restart method to use
if (File.Exists(_destinationFile) && !Utils.PermissionsCheck.HaveWritePermissionsForFileOrFolder(_destinationFile))
{
return TaskExecutionStatus.RequiresPrivilegedAppRestart;
}
return TaskExecutionStatus.RequiresAppRestart;
}