protected bool DeleteFile(string file, out bool restartRequired) { if (!File.Exists(file)) { Logger.Trace($"'{file}' file is already deleted."); restartRequired = false; return(true); } var deleteSuccess = FileSystemExtensions.DeleteFileWithRetry(file, out restartRequired, true, 2, 500, (ex, attempt) => { Logger?.Trace( $"Error occurred while deleting file '{file}'. Error details: {ex.Message}. Retrying after {0.5f} seconds..."); return(true); }); if (deleteSuccess) { Logger.Info($"{file} file deleted."); } else { _lockedFiles.Add(file); Logger.Info($"{file} file is scheduled for deletion after restarting."); } return(deleteSuccess); }
protected override void SynchronizedInvoke(CancellationToken token) { if (token.IsCancellationRequested) { return; } if (!FileSystemExtensions.DeleteFileWithRetry(File, out _)) { throw new Exception($"Failed to delete file: {File}"); } }
public void RestoreBackup(IComponent component) { if (!_backupLookup.ContainsKey(component)) { return; } var backupFile = _backupLookup[component]; var componentFile = component.GetFilePath(); var remove = true; try { if (string.IsNullOrEmpty(backupFile)) { if (!File.Exists(componentFile)) { return; } var success = FileSystemExtensions.DeleteFileWithRetry(componentFile, out _); if (success) { return; } remove = false; throw new IOException("Unable to restore the backup. Please restart your computer!"); } else { if (!File.Exists(backupFile)) { return; } if (File.Exists(componentFile)) { var backupHash = UpdaterUtilities.GetFileHash(backupFile !, HashType.Sha256); var fileHash = UpdaterUtilities.GetFileHash(backupFile !, HashType.Sha256); if (backupHash.SequenceEqual(fileHash)) { remove = false; return; } } var success = FileSystemExtensions.MoveFile(backupFile, component.GetFilePath(), true); if (!success) { remove = false; throw new IOException($"Unable to restore the backup file '{backupFile}'. Please restart your computer!"); } if (UpdateConfiguration.Instance.DownloadOnlyMode) { ComponentDownloadPathStorage.Instance.Remove(component); } try { FileSystemExtensions.DeleteFileWithRetry(backupFile !, out _); } catch { remove = false; } } } finally { if (remove) { lock (_syncObject) _backupLookup.Remove(component); } } }