Skip to content

Commit

Permalink
+ Better Exception handling
Browse files Browse the repository at this point in the history
+ Obfuscate file name and dates
  • Loading branch information
mhmd-azeez committed Nov 16, 2016
1 parent 6d3d151 commit dda07ee
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 27 deletions.
28 changes: 28 additions & 0 deletions RudeFox.FrontEnd/Models/FileSystemType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RudeFox.Models
{
public enum FileSystemType
{
/// <summary>
/// New Technology File System.
/// </summary>
NTFS,
/// <summary>
/// File Allocation Table.
/// </summary>
FAT32,
/// <summary>
/// Unknown File System.
/// </summary>
Unknown,
/// <summary>
/// Used with ExpanDrive.
/// </summary>
EXFS
}
}
1 change: 1 addition & 0 deletions RudeFox.FrontEnd/RudeFox.FrontEnd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Helpers\Constants.cs" />
<Compile Include="Models\FileSystemType.cs" />
<Compile Include="Models\ItemType.cs" />
<Compile Include="Services\DialogService.cs" />
<Compile Include="Services\ShredderService.cs" />
Expand Down
74 changes: 61 additions & 13 deletions RudeFox.FrontEnd/Services/ShredderService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RudeFox.Helpers;
using RudeFox.Models;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -19,7 +20,8 @@ private ShredderService()
#endregion

#region Fields
private const int MAX_BUFFER_SIZE = 1 * Constants.MEGABYTE;
private const int MAX_BUFFER_SIZE = Constants.MEGABYTE;
private const int OBFUSCATE_ROUNDS = 5;

Random _random = new Random();
#endregion
Expand Down Expand Up @@ -95,8 +97,7 @@ public async Task<bool> ShredFileAsync(FileInfo file, CancellationToken cancella
};

if (!file.Exists) return false;

if (cancellationToken != null) cancellationToken.ThrowIfCancellationRequested();
file.IsReadOnly = false;

var result = await OverWriteFileAsync(file, cancellationToken, writeProgress).ConfigureAwait(false);
if (!result) return result;
Expand All @@ -110,6 +111,8 @@ await Task.Run(() =>
file.Delete();
}).ConfigureAwait(false);

await DestroyEntityMetaData(file);

if (progress != null) progress.Report(1.0);

return true;
Expand Down Expand Up @@ -160,15 +163,41 @@ public async Task<bool> ShredFolderAsync(DirectoryInfo folder, CancellationToken
if (percent == 1.0)
bytesComplete += length;
};

var result = await ShredFolderAsync(dir, cancellationToken, itemProgress).ConfigureAwait(false);
if (!result) return result;
}
}

// BUG: Don't know why this causes issues.
// await DestroyEntityMetaData(folder);
folder.Delete();

return true;
}

public bool IsFileLocked(FileInfo file)
{
FileStream stream = null;

try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null) stream.Close();
}

return false;
}
#endregion

#region Private Methods
private async Task<bool> OverWriteFileAsync(FileInfo file, CancellationToken cancellationToken, IProgress<double> progress)
{
if (!file.Exists) return false;
Expand Down Expand Up @@ -208,25 +237,44 @@ await Task.Run(() =>

return true;
}

public bool IsFileLocked(FileInfo file)
private async Task<bool> DestroyEntityMetaData(FileSystemInfo entity, FileSystemType fileSystemType = FileSystemType.Unknown)
{
FileStream stream = null;
if (!entity.Exists)
return false;

try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
// prevent the indexing service from locking the file
entity.Attributes = FileAttributes.NotContentIndexed;
}
catch (IOException)
catch (ArgumentException e)
{
return true;
throw new UnauthorizedAccessException(e.Message, e);
}
finally

await Task.Run(() =>
{
if (stream != null) stream.Close();
}
// rename the file a few times to remove it from the file system table.
for (var round = 0; round < OBFUSCATE_ROUNDS; round++)
{
var newPath = Path.Combine(Path.GetDirectoryName(entity.FullName), Path.GetRandomFileName());

return false;
var file = entity as FileInfo;
var folder = entity as DirectoryInfo;

if (file != null)
file.MoveTo(newPath);
else
folder.MoveTo(newPath);
}

var newTime = new DateTime(1981, 1, 1, 0, 0, 1);
entity.LastAccessTime = newTime;
entity.LastWriteTime = newTime;
entity.CreationTime = newTime;
});

return true;
}
#endregion
}
Expand Down
43 changes: 33 additions & 10 deletions RudeFox.FrontEnd/ViewModels/MainWindowVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public ObservableCollection<WorkItemVM> WorkItems
#endregion

#region Drag and drop

void IDropTarget.DragOver(IDropInfo dropInfo)
{
var data = dropInfo.Data as IDataObject;
Expand Down Expand Up @@ -90,6 +89,7 @@ private async void DeleteItems(List<string> paths)
join p in paths
on i.Path equals p
select p;

paths.RemoveAll(p => duplicates.Contains(p));

foreach (var path in paths)
Expand Down Expand Up @@ -126,8 +126,9 @@ on i.Path equals p

var tasks = newItems.Select(item =>
{
return ShredderService.Instance.ShredItemAsync(item.Path, item.CancellationTokenSource.Token, item.TaskProgress);
});
item.Task = ShredderService.Instance.ShredItemAsync(item.Path, item.CancellationTokenSource.Token, item.TaskProgress);
return item.Task;
}).ToList();

try
{
Expand All @@ -139,18 +140,40 @@ on i.Path equals p
}
catch (AggregateException exc)
{
var msg = $"{exc.InnerExceptions.Count()} error(s) occured: ";
foreach(var ex in exc.InnerExceptions)
var failedTasks = tasks.Where(t => t.IsFaulted);
tasks.RemoveAll(t => failedTasks.Contains(t));

var failedItems = WorkItems.Where(item => item.Task.IsFaulted || item.Task.IsCanceled);

for (int i = 0; i < WorkItems.Count; i++)
{
msg += "\n---------------------------";
msg += Environment.NewLine;
msg += ex.ToString();
if (failedItems.Contains(WorkItems[i]))
{
WorkItems.RemoveAt(i);
i--;
}
}
MessageBox.Show(msg);

var exception = exc.Flatten();
MessageBox.Show(exception.ToString());

}
catch( Exception exc)
{
var failed = tasks.Where(t => t.IsFaulted);
var failedTasks = tasks.Where(t => t.IsFaulted);
tasks.RemoveAll(t => failedTasks.Contains(t));

var failedItems = WorkItems.Where(item => item.Task.IsFaulted || item.Task.IsCanceled);

for (int i = 0; i < WorkItems.Count; i++)
{
if (failedItems.Contains(WorkItems[i]))
{
WorkItems.RemoveAt(i);
i--;
}
}

MessageBox.Show(exc.ToString());
}
}
Expand Down
17 changes: 13 additions & 4 deletions RudeFox.FrontEnd/ViewModels/WorkItemVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public double Progress
}
}

private long _length = -1;
private long _bytes = -1;
public long Bytes
{
get { return _length; }
get { return _bytes; }
set
{
if (SetProperty(ref _length, value))
if (SetProperty(ref _bytes, value))
RaisePropertyChanged(nameof(Size));
}
}
Expand Down Expand Up @@ -116,6 +116,13 @@ public string Size
}
}

private Task _task;
public Task Task
{
get { return _task; }
set { SetProperty(ref _task, value); }
}

public CancellationTokenSource CancellationTokenSource { get; set; }
public Progress<double> TaskProgress { get; set; }

Expand All @@ -134,8 +141,10 @@ private async void CalculateBytes()
{
if (File.Exists(Path))
Bytes = new FileInfo(Path).Length;
else
else if (Directory.Exists(Path))
Bytes = await ShredderService.Instance.GetFolderSize(new DirectoryInfo(Path));
else
Bytes = -1;
}
private void OnDeleteRequested(bool canceled = false)
{
Expand Down

0 comments on commit dda07ee

Please sign in to comment.