Skip to content

Commit

Permalink
Optimized some code parts
Browse files Browse the repository at this point in the history
  • Loading branch information
hxseven committed Mar 11, 2012
1 parent 148eb06 commit 8e1dbf7
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 102 deletions.
5 changes: 2 additions & 3 deletions RED2/Lib/FindEmptyDirectoryWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,20 @@ private bool ScanFolders(DirectoryInfo _StartFolder, int _depth)
{
if (Folder.FullName.ToLower().Contains(f.ToLower()))
ignoreFolder = true;


}
}

// Scan sub folder:
bool isSubFolderEmpty = false;

if (!ignoreFolder)
isSubFolderEmpty = this.ScanFolders(Folder, _depth+1);

// is empty?
if (isSubFolderEmpty && !ignoreFolder)
{
this.emptyFolderCount++;

// Folder is empty, report that to the gui:
this.ReportProgress(-1, Folder);
}
Expand Down
57 changes: 28 additions & 29 deletions RED2/Lib/REDCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ public class REDCore
{
private REDWorkflowSteps currentProcessStep = REDWorkflowSteps.Init;

public bool SimulateDeletion { get; set; }
private List<DirectoryInfo> emptyFolderList = null;
private bool stopDeleteProcessTrigger = false;
private Dictionary<String, String> protectedFolderList = new Dictionary<string, string>();

private CalculateDirectoryCountWorker calcDirCountWorker = null;
private FindEmptyDirectoryWorker searchEmptyFoldersWorker = null;

// Events
public event EventHandler<REDCoreWorkflowStepChangedEventArgs> OnWorkflowStepChanged;
public event EventHandler<REDCoreErrorEventArgs> OnError;
public event EventHandler<REDCoreCalcDirWorkerFinishedEventArgs> OnCalcDirWorkerFinished;
Expand All @@ -25,21 +31,13 @@ public class REDCore
public event EventHandler<REDCoreDeleteProcessUpdateEventArgs> OnDeleteProcessChanged;
public event EventHandler<REDCoreDeleteProcessFinishedEventArgs> OnDeleteProcessFinished;

public bool SimulateDeletion { get; set; }

private List<DirectoryInfo> emptyFolderList = null;

private bool stopDeleteProcessTrigger = false;
private Dictionary<String, String> protectedFolderList = new Dictionary<string, string>();

public REDCore()
{
this.SimulateDeletion = false;
}

public void init()
{

protectedFolderList = new Dictionary<string, string>();
this.set_step(REDWorkflowSteps.Init);
}
Expand Down Expand Up @@ -98,7 +96,6 @@ private void showErrorMsg(string errorMessage)
this.OnError(this, new REDCoreErrorEventArgs(errorMessage));
}


/// <summary>
/// Start searching empty folders
/// </summary>
Expand Down Expand Up @@ -137,7 +134,6 @@ public void SearchingForEmptyDirectories(DirectoryInfo StartFolder, string tbIgn
searchEmptyFoldersWorker.RunWorkerAsync(StartFolder);

#endregion

}

/// <summary>
Expand Down Expand Up @@ -197,20 +193,21 @@ void FFWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
/// <summary>
/// Finally delete a folder (with security checks before)
/// </summary>
/// <param name="_StartFolder"></param>
/// <param name="startFolder"></param>
/// <returns></returns>
public bool SecureDelete(DirectoryInfo _StartFolder, String[] ignoreFiles, bool cbIgnore0kbFiles, bool deleteToRecycleBin)
public bool SecureDelete(DirectoryInfo startFolder, String[] ignoreFileList, bool ignore_0kb_files, bool deleteToRecycleBin)
{
if (this.SimulateDeletion)
return true;

if (!Directory.Exists(_StartFolder.FullName))
if (!Directory.Exists(startFolder.FullName))
return false;


// Cleanup folder

FileInfo[] Files = _StartFolder.GetFiles();
FileInfo[] Files = startFolder.GetFiles();
Regex RgxPattern = null;

if (Files != null && Files.Length != 0)
{
Expand All @@ -219,46 +216,49 @@ public bool SecureDelete(DirectoryInfo _StartFolder, String[] ignoreFiles, bool
{
FileInfo file = Files[f];

bool matches_a_pattern = false;
bool deleteTrashFile = false;

for (int p = 0; (p < ignoreFiles.Length && !matches_a_pattern); p++)
for (int p = 0; (p < ignoreFileList.Length && !deleteTrashFile); p++)
{
string pattern = ignoreFiles[p];
string pattern = ignoreFileList[p];

if (cbIgnore0kbFiles && file.Length == 0)
matches_a_pattern = true;
if (ignore_0kb_files && file.Length == 0)
{
deleteTrashFile = true;
}
else if (pattern.ToLower() == file.Name.ToLower())
matches_a_pattern = true;
{
deleteTrashFile = true;
}
else if (pattern.Contains("*"))
{
pattern = Regex.Escape(pattern);
pattern = pattern.Replace("\\*", ".*");

Regex RgxPattern = new Regex("^" + pattern + "$");
RgxPattern = new Regex("^" + pattern + "$");

if (RgxPattern.IsMatch(file.Name))
matches_a_pattern = true;
deleteTrashFile = true;
}
else if (pattern.StartsWith("/") && pattern.EndsWith("/"))
{
Regex RgxPattern = new Regex(pattern.Substring(1, pattern.Length - 2));
RgxPattern = new Regex(pattern.Substring(1, pattern.Length - 2));

if (RgxPattern.IsMatch(file.Name))
matches_a_pattern = true;
deleteTrashFile = true;
}

}

// If only one file is good, then stop.
if (matches_a_pattern)
file.Delete();

if (deleteTrashFile)
SystemFunctions.SecureDeleteFile(file, deleteToRecycleBin);
}
}

// End cleanup

return SystemFunctions.SecureDelete(_StartFolder, deleteToRecycleBin);
return SystemFunctions.SecureDeleteDirectory(startFolder, deleteToRecycleBin);
}

internal void CancelCurrentProcess()
Expand Down Expand Up @@ -341,7 +341,6 @@ internal void AddProtectedFolder(string FullName, string Key)
internal void RemoveProtected(string FolderFullName)
{
this.protectedFolderList.Remove(FolderFullName);

}

internal bool ProtectedContainsKey(string FolderFullName)
Expand Down
35 changes: 30 additions & 5 deletions RED2/Lib/SystemFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace RED2
{
public class SystemFunctions
{
public static bool SecureDelete(DirectoryInfo Folder, bool deleteToRecycleBin)
public static bool SecureDeleteDirectory(DirectoryInfo Folder, bool deleteToRecycleBin)
{
// last security check (for files):
if (Folder.GetFiles().Length == 0 && Folder.GetDirectories().Length == 0)
Expand All @@ -20,20 +20,45 @@ public static bool SecureDelete(DirectoryInfo Folder, bool deleteToRecycleBin)
{
if (deleteToRecycleBin)
{
FileSystem.DeleteDirectory(Folder.FullName, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
// FileSystem.DeleteDirectory(Folder.FullName, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
FileSystem.DeleteDirectory(Folder.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
}
else
{
Folder.Delete();
}
return true;
}
catch(Exception ex)
catch (Exception ex)
{
// Do something useful -> convert to event
MessageBox.Show("Error during deletion of directory: " + ex.Message);
}
}
return false;
}

public static bool SecureDeleteFile(FileInfo File, bool deleteToRecycleBin)
{
try
{
if (deleteToRecycleBin)
{
// FileSystem.DeleteDirectory(Folder.FullName, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
FileSystem.DeleteFile(File.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
}
else
{
// Do something useful
MessageBox.Show("Error during deletion: " + ex.Message);
File.Delete();
}
return true;
}
catch (Exception ex)
{
// Do something useful -> convert to event
MessageBox.Show("Error during deletion of file: " + ex.Message);
}

return false;
}

Expand Down
Loading

0 comments on commit 8e1dbf7

Please sign in to comment.