/// <summary> /// A more efficient function for deleting all files off the server. This will delete all /// files on the B2 server, including those that aren't on the file database manifest and /// the file database manifest itself! /// </summary> /// <param name="authorizationSessionGenerator">A generator function for the authorization session</param> public void DeleteAllFiles(Func <BackblazeB2AuthorizationSession> authorizationSessionGenerator) { this.Debug("Begin deleting all files"); RemoveAllFiles(); IEnumerable <FileResult> rawB2FileList = GetRawB2FileNames(authorizationSessionGenerator()); object lockObject = new object(); Parallel.ForEach(rawB2FileList, rawB2File => { CancellationEventRouter.GlobalCancellationToken.ThrowIfCancellationRequested(); DeleteFileAction deleteFileAction = new DeleteFileAction(authorizationSessionGenerator(), rawB2File.FileID, rawB2File.FileName); BackblazeB2ActionResult <BackblazeB2DeleteFileResult> deletionResult = deleteFileAction.Execute(); if (deletionResult.HasErrors) { lock (lockObject) { this.Critical($"Failed to delete file {rawB2File.FileName}. Reason: {deletionResult}"); } } else { lock (lockObject) { this.Info($"Deleted file: {rawB2File.FileName}"); } } }); }
public void DeleteFileActionConstructorTest() { DeleteFileAction target = new DeleteFileAction(); Assert.AreEqual(55, target.Height, "The property 'Height' is not properly initialized"); Assert.IsTrue(target.IsTemplate, "The property 'IsTemplate' is not properly initialized"); Assert.IsFalse(target.IsSelected, "The property 'IsSelected' is not properly initialized"); Assert.AreEqual(string.Empty, target.FullPath, "The property 'FullPath' is not properly initialized"); }
private static System.Action GetAction(int taskID, ActionModel action) { IAction actionInstance = null; switch (action.Action) { case ActionType.WriteFile: actionInstance = new WriteFileAction(); break; case ActionType.IF: actionInstance = new IFAction(); break; case ActionType.HttpRequest: actionInstance = new HttpRequestAction(); break; case ActionType.Shutdown: actionInstance = new ShutdownAction(); break; case ActionType.StartProcess: actionInstance = new StartProcessAction(); break; case ActionType.OpenURL: actionInstance = new OpenURLAction(); break; case ActionType.Snipping: actionInstance = new SnippingAction(); break; case ActionType.DeleteFile: actionInstance = new DeleteFileAction(); break; case ActionType.SoundPlay: actionInstance = new SoundPlayAction(); break; case ActionType.GetIPAddress: actionInstance = new GetIPAddressAction(); break; case ActionType.Keyboard: actionInstance = new KeyboardAction(); break; } if (actionInstance != null) { return(actionInstance.GenerateAction(taskID, action)); } return(null); }
public void ValidateDataTest() { DeleteFileAction target = new DeleteFileAction(); Assert.IsTrue(target.ConfigurationState == GenericAction.ConfigurationStates.NotConfigured, "The property 'ConfigurationState' is not properly initialized"); target.FullPath = @"C:\Windows\System32\file.txt"; Assert.IsTrue(target.ConfigurationState == GenericAction.ConfigurationStates.Configured, "The property 'ConfigurationState' is not properly updated"); target.FullPath = string.Empty; Assert.IsTrue(target.ConfigurationState == GenericAction.ConfigurationStates.Misconfigured, "The property 'ConfigurationState' doesn't revert back to Misconfigured"); }
public void GetXMLActionTest() { DeleteFileAction target = new DeleteFileAction(); target.FullPath = @"C:\Windows\System32\Config.ini"; string expected = "<Action>\r\n<ElementType>CustomActions.DeleteFileAction</ElementType>\r\n<FullPath>" + target.FullPath + "</FullPath>\r\n</Action>"; string actual; actual = target.GetXMLAction(); Assert.AreEqual(expected, actual, "The 'GetXMLAction' method doesn't return the good string"); target.FullPath = @" C:\Windows\System32\Config.ini "; actual = target.GetXMLAction(); Assert.AreEqual(expected, actual, "The 'GetXMLAction' method doesn't return the good string"); }
private void initControls() { FileTree = new TreeView(); FileTree.Size = new Size(50, 50); FileTree.ImageList = AssetManager.getImageList(); //fileTree.ImageList.ImageSize = new Size(32, 32); //fileTree.ImageList.ColorDepth = ColorDepth.Depth32Bit; //fileTree.ImageList.ImageSize = new Size(16, 16); //fileTree.ImageList = new ImageList(); //fileTree.ImageList.Images.Add(AssetManager.ICON_FLDR); //fileTree.ImageList.Images.Add(AssetManager.ICON_FILE); newLevelAction = new NewLevelAction(); newTypeAction = new NewTypeAction(); newScriptAction = new NewScriptAction(); newArtAction = new NewArtAction(); newSoundAction = new NewSoundAction(); newFolderAction = new NewFolderAction(); newShaderAction = new NewShaderAction(); folderNodeMenu = new ContextMenuStrip(); folderNodeMenu.Width = 200; ToolStripLabel label = new ToolStripLabel("Add File..."); folderNodeMenu.Text = "Add File"; // folderNodeMenu.Items.Add(label); folderNodeMenu.Items.Add(newTypeAction.generateControl <ToolStripButton>()); folderNodeMenu.Items.Add(newScriptAction.generateControl <ToolStripButton>()); folderNodeMenu.Items.Add(newLevelAction.generateControl <ToolStripButton>()); folderNodeMenu.Items.Add(newArtAction.generateControl <ToolStripButton>()); folderNodeMenu.Items.Add(newSoundAction.generateControl <ToolStripButton>()); folderNodeMenu.Items.Add(newShaderAction.generateControl <ToolStripButton>()); //folderNodeMenu.Items.Add(newFolderAction.generateControl<ToolStripButton>()); //TODO add folder support deleteAction = new DeleteFileAction(this); fileNodeMenu = new ContextMenuStrip(); ToolStripLabel fileNodeLabel = new ToolStripLabel("Options..."); fileNodeMenu.Text = "Options..."; //fileNodeMenu.Items.Add(fileNodeLabel); fileNodeMenu.Items.Add(deleteAction.generateControl <ToolStripButton>()); }
/// <summary> /// Prunes any shards on the server that are not accounted for in the database manifest /// </summary> /// <param name="authorizationSessionGenerator">The generator for an authorization session</param> public void PruneShards(Func <BackblazeB2AuthorizationSession> authorizationSessionGenerator) { this.Debug("Pruning shards"); // Get just the file names on the server ListFilesAction listFilesAction = ListFilesAction.CreateListFileActionForFileNames(authorizationSessionGenerator(), Config.BucketID, true); BackblazeB2ActionResult <BackblazeB2ListFilesResult> listFilesActionResult = listFilesAction.Execute(); if (listFilesActionResult.HasErrors) { throw new FailedToGetListOfFilesOnB2Exception { BackblazeErrorDetails = listFilesActionResult.Errors, }; } object lockObject = new object(); IDictionary <string, FileResult> fileNameToFileResultMap = listFilesActionResult.Result.Files.ToDictionary(k => k.FileName, v => v); ISet <string> allDatabaseFileShardIds = FileDatabaseManifestFiles.SelectMany(t => t.FileShardIDs).ToHashSet(); ISet <string> allRawFileNamesOnServer = fileNameToFileResultMap.Keys.ToHashSet(); ISet <string> allFilesNotAccountedFor = allRawFileNamesOnServer.Except(allDatabaseFileShardIds).Where(t => t.Equals(RemoteFileDatabaseManifestName, StringComparison.OrdinalIgnoreCase) == false).ToHashSet(); Parallel.ForEach(allFilesNotAccountedFor, fileNameNotAccountedFor => { CancellationEventRouter.GlobalCancellationToken.ThrowIfCancellationRequested(); FileResult fileNotAccountedFor = fileNameToFileResultMap[fileNameNotAccountedFor]; DeleteFileAction deleteFileAction = new DeleteFileAction(authorizationSessionGenerator(), fileNotAccountedFor.FileID, fileNotAccountedFor.FileName); BackblazeB2ActionResult <BackblazeB2DeleteFileResult> deletionResult = deleteFileAction.Execute(); if (deletionResult.HasErrors) { lock (lockObject) { this.Critical($"Failed to prune file. Reason: {deletionResult}"); } } else { lock (lockObject) { this.Info($"Pruned shard: {fileNameNotAccountedFor}"); } } }); }
public void UserProfileNotificationTest() { DeleteFileAction target = new DeleteFileAction(); Assert.IsFalse(target.RefersToUserProfile, "The property 'RefersToUserProfile' is not properly initialized"); foreach (string folder in CommonData.userProfileRelatedFolders) { target.FullPath = folder; Assert.IsTrue(target.RefersToUserProfile, folder + " should set 'RefersToUserProfile' to true."); } target.FullPath = @"C:\Temp"; Assert.IsFalse(target.RefersToUserProfile); foreach (string folder in CommonData.otherFolders) { target.FullPath = folder; Assert.IsFalse(target.RefersToUserProfile, folder + " should not set 'RefersToUserProfile' to true."); } }
public void FullPathTest() { DeleteFileAction target = new DeleteFileAction(); Assert.IsTrue(target.ConfigurationState == GenericAction.ConfigurationStates.NotConfigured, "The property 'ConfigurationState' is not properly initialized"); string expected = @"C:\Windows\System32\Config.ini"; string actual; target.FullPath = expected; actual = target.FullPath; Assert.AreEqual(expected, actual, "The property 'FullPath' is not properly initialized."); Assert.IsTrue(target.ConfigurationState == GenericAction.ConfigurationStates.Configured, "The property 'ConfigurationState' is not properly updated"); target.FullPath = string.Empty; actual = target.FullPath; Assert.AreEqual(string.Empty, actual, "The property 'FullPath' cannot be set to string.Empty."); Assert.IsTrue(target.ConfigurationState == GenericAction.ConfigurationStates.Misconfigured, "The property 'ConfigurationState' is not properly updated"); target.FullPath = @" C:\Windows\System32\Config.ini "; actual = target.FullPath; Assert.AreEqual(expected, actual, "The property 'FullPath' is not not properly trimed."); Assert.IsTrue(target.ConfigurationState == GenericAction.ConfigurationStates.Configured, "The property 'ConfigurationState' is not properly updated"); }
public void FullPathToNullText() { DeleteFileAction target = new DeleteFileAction(); target.FullPath = null; }
private static IAction GetAction(ActionModel action) { IAction actionInstance = null; switch (action.Action) { case ActionType.WriteFile: actionInstance = new WriteFileAction(); break; case ActionType.IF: actionInstance = new IFAction(); break; case ActionType.HttpRequest: actionInstance = new HttpRequestAction(); break; case ActionType.Shutdown: actionInstance = new ShutdownAction(); break; case ActionType.StartProcess: actionInstance = new StartProcessAction(); break; case ActionType.OpenURL: actionInstance = new OpenURLAction(); break; case ActionType.Snipping: actionInstance = new SnippingAction(); break; case ActionType.DeleteFile: actionInstance = new DeleteFileAction(); break; case ActionType.SoundPlay: actionInstance = new SoundPlayAction(); break; case ActionType.GetIPAddress: actionInstance = new GetIPAddressAction(); break; case ActionType.Keyboard: actionInstance = new KeyboardAction(); break; case ActionType.SystemNotification: actionInstance = new SystemNotificationAction(); break; case ActionType.DownloadFile: actionInstance = new DownloadFileAction(); break; case ActionType.Dialog: actionInstance = new DialogAction(); break; case ActionType.Delay: actionInstance = new DelayAction(); break; case ActionType.Loops: actionInstance = new LoopsAction(); break; case ActionType.KillProcess: actionInstance = new KillProcessAction(); break; case ActionType.SetDeviceVolume: actionInstance = new SetDeviceVolumeAction(); break; case ActionType.Regex: actionInstance = new RegexAction(); break; case ActionType.ReadFile: actionInstance = new ReadFileAction(); break; case ActionType.JsonDeserialize: actionInstance = new JsonDeserializeAction(); break; } if (actionInstance != null) { return(actionInstance); } return(null); }
private GenericAction GetElementFromXML(XmlReader reader) { GenericAction element = null; string elementType = reader.ReadElementContentAsString(); switch (elementType) { case "CustomActions.AddRegKeyAction": element = new AddRegKeyAction(); break; case "CustomActions.AddRegValueAction": element = new AddRegValueAction(); break; case "CustomActions.ChangeRegDataAction": element = new ChangeRegDataAction(); break; case "CustomActions.ChangeServiceAction": element = new ChangeServiceAction(); break; case "CustomActions.CopyFileAction": element = new CopyFileAction(); break; case "CustomActions.CreateFolderAction": element = new CreateFolderAction(); break; case "CustomActions.CreateShortcutAction": element = new CreateShortcutAction(); break; case "CustomActions.CreateTextFileAction": element = new CreateTextFileAction(); break; case "CustomActions.DeleteFileAction": element = new DeleteFileAction(); break; case "CustomActions.DeleteFolderAction": element = new DeleteFolderAction(); break; case "CustomActions.DeleteRegKeyAction": element = new DeleteRegKeyAction(); break; case "CustomActions.DeleteRegValueAction": element = new DeleteRegValueAction(); break; case "CustomActions.DeleteTaskAction": element = new DeleteTaskAction(); break; case "CustomActions.ExecutableAction": element = new ExecutableAction(); break; case "CustomActions.ImportRegFileAction": element = new ImportRegFileAction(); break; case "CustomActions.KillProcessAction": element = new KillProcessAction(); break; case "CustomActions.RebootAction": element = new RebootAction(); break; case "CustomActions.RegisterDLLAction": element = new RegisterDLLAction(); break; case "CustomActions.RenameFileAction": element = new RenameFileAction(); break; case "CustomActions.RenameFolderAction": element = new RenameFolderAction(); break; case "CustomActions.RenameRegKeyAction": element = new RenameRegKeyAction(); break; case "CustomActions.RenameRegValueAction": element = new RenameRegValueAction(); break; case "CustomActions.RunPowershellScriptAction": element = new RunPowershellScriptAction(); break; case "CustomActions.RunVbScriptAction": element = new RunVbScriptAction(); break; case "CustomActions.ShutdownAction": element = new ShutdownAction(); break; case "CustomActions.StartServiceAction": element = new StartServiceAction(); break; case "CustomActions.StopServiceAction": element = new StopServiceAction(); break; case "CustomActions.UninstallMsiProductByGuidAction": element = new UninstallMsiProductByGuidAction(); break; case "CustomActions.UninstallMsiProductByNameAction": element = new UninstallMsiProductByNameAction(); break; case "CustomActions.UnregisterDLLAction": element = new UnregisterDLLAction(); break; case "CustomActions.UnregisterServiceAction": element = new UnregisterServiceAction(); break; case "CustomActions.WaitAction": element = new WaitAction(); break; case "CustomActions.InstallMsiAction": element = new InstallMsiAction(); break; case "CustomActions.ReturnCode": try { this.SetReturnCodeFromXml(reader); } catch (Exception ex) { if (this.IsUIEnable) { System.Windows.Forms.MessageBox.Show(Localizator.Getinstance().GetLocalizedString("UnableToSetReturnCodeFromXmlFile") + "\r\n" + ex.Message); } else { throw new Exception(Localizator.Getinstance().GetLocalizedString("UnableToSetReturnCodeFromXmlFile") + "\r\n" + ex.Message); } } break; default: if (this.IsUIEnable) { System.Windows.Forms.MessageBox.Show(Localizator.Getinstance().GetLocalizedString("ThisActionHasNotBeenRecognized") + elementType); } else { throw new Exception(Localizator.Getinstance().GetLocalizedString("ThisActionHasNotBeenRecognized") + elementType); } break; } return(element); }
/// <summary> /// Deletes a file off the remote file system /// </summary> /// <param name="authorizationSessionGenerator">The generator function that returns an authorization session</param> /// <param name="file">The file to delete</param> public void DeleteFile( Func <BackblazeB2AuthorizationSession> authorizationSessionGenerator, Database.File file ) { this.Debug($"Begin deleting file: {file.FileName}"); // Remove entry in the File Manifest RemoveFile(file); // Determine if another file shares the same file shards IEnumerable <Database.File> filesThatShareTheSameShards = from currentFile in FileDatabaseManifestFiles where file.FileLength == currentFile.FileLength && file.FileShardIDs.Length == currentFile.FileShardIDs.Length && file.SHA1.Equals(currentFile.SHA1, StringComparison.OrdinalIgnoreCase) && // Even if a single shard ID is shared by another file, that's enough to count it // as being equal (or at least not eligible for hard-deletion) file.FileShardIDs.Any(t => currentFile.FileShardIDs.Contains(t)) select currentFile; // If there are no files that share the same Shard IDs if (filesThatShareTheSameShards.Any() == false) { // Get the raw B2 File List so we can get the B2 file IDs of the file shards ListFilesAction listFilesAction = ListFilesAction.CreateListFileActionForFileNames(authorizationSessionGenerator(), Config.BucketID, true); BackblazeB2ActionResult <BackblazeB2ListFilesResult> listFilesActionResult = listFilesAction.Execute(); if (listFilesActionResult.HasErrors) { throw new FailedToGetListOfFilesOnB2Exception { BackblazeErrorDetails = listFilesActionResult.Errors, }; } IEnumerable <FileResult> rawB2FileList = listFilesActionResult.Result.Files; IDictionary <string, FileResult> fileNameToFileResult = rawB2FileList.ToDictionary(k => k.FileName, v => v); foreach (string shardID in file.FileShardIDs) { if (fileNameToFileResult.TryGetValue(shardID, out FileResult fileShardToDelete)) { DeleteFileAction deleteFileAction = new DeleteFileAction(authorizationSessionGenerator(), fileShardToDelete.FileID, fileShardToDelete.FileName); BackblazeB2ActionResult <BackblazeB2DeleteFileResult> deletionResult = deleteFileAction.Execute(); if (deletionResult.HasErrors) { this.Critical($"Failed to delete file. Reason: {deletionResult}"); } } } } else { this.Info($"File {file.FileName} shares file shares with another file. Will not perform a hard-delete. Removing from file manifest instead"); } while (TryUploadFileDatabaseManifest(authorizationSessionGenerator()) == false) { Thread.Sleep(5); } this.Info($"Deleted file: {file.FileName}"); }