Strip temp paths, add archive name as prefix

This commit is contained in:
Matt Nadareski
2020-10-31 14:14:35 -07:00
parent 81f0400790
commit 45e10a84ae
15 changed files with 178 additions and 55 deletions

View File

@@ -102,6 +102,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -49,6 +49,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -52,6 +52,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -61,6 +61,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -60,6 +60,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -39,6 +39,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -48,6 +48,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -61,6 +61,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -57,6 +57,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -51,6 +51,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
}

View File

@@ -55,6 +55,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -59,6 +59,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
}

View File

@@ -47,6 +47,9 @@ namespace BurnOutSharp.FileType
}
catch { }
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
catch { }

View File

@@ -156,7 +156,8 @@ namespace BurnOutSharp
// Throw on an invalid path
else
{
// throw new FileNotFoundException($"{path} is not a directory or file, skipping...");
Console.WriteLine($"{path} is not a directory or file, skipping...");
//throw new FileNotFoundException($"{path} is not a directory or file, skipping...");
}
}
@@ -459,6 +460,7 @@ namespace BurnOutSharp
if (SevenZip.ShouldScan(magic))
{
var subProtections = SevenZip.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -466,6 +468,7 @@ namespace BurnOutSharp
if (BFPK.ShouldScan(magic))
{
var subProtections = BFPK.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -473,6 +476,7 @@ namespace BurnOutSharp
if (BZip2.ShouldScan(magic))
{
var subProtections = BZip2.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -480,6 +484,7 @@ namespace BurnOutSharp
if (GZIP.ShouldScan(magic))
{
var subProtections = GZIP.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -487,6 +492,7 @@ namespace BurnOutSharp
if (file != null && InstallShieldCAB.ShouldScan(magic))
{
var subProtections = InstallShieldCAB.Scan(this, file);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -494,6 +500,7 @@ namespace BurnOutSharp
if (file != null && MicrosoftCAB.ShouldScan(magic))
{
var subProtections = MicrosoftCAB.Scan(this, file);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -501,6 +508,7 @@ namespace BurnOutSharp
if (file != null && MSI.ShouldScan(magic))
{
var subProtections = MSI.Scan(this, file);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -508,6 +516,7 @@ namespace BurnOutSharp
if (file != null && MPQ.ShouldScan(magic))
{
var subProtections = MPQ.Scan(this, file);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -515,6 +524,7 @@ namespace BurnOutSharp
if (PKZIP.ShouldScan(magic))
{
var subProtections = PKZIP.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -522,6 +532,7 @@ namespace BurnOutSharp
if (RAR.ShouldScan(magic))
{
var subProtections = RAR.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -529,6 +540,7 @@ namespace BurnOutSharp
if (TapeArchive.ShouldScan(magic))
{
var subProtections = TapeArchive.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -536,6 +548,7 @@ namespace BurnOutSharp
if (file != null && Valve.ShouldScan(magic))
{
var subProtections = Valve.Scan(this, file);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
@@ -543,6 +556,7 @@ namespace BurnOutSharp
if (XZ.ShouldScan(magic))
{
var subProtections = XZ.Scan(this, fs);
Utilities.PrependToKeys(protections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
@@ -10,6 +11,127 @@ namespace BurnOutSharp
{
internal static class Utilities
{
#region Dictionary Manipulation
/// <summary>
/// Append one result to a results dictionary
/// </summary>
/// <param name="original">Dictionary to append to</param>
/// <param name="key">Key to add information to</param>
/// <param name="value">String value to add</param>
public static void AppendToDictionary(Dictionary<string, List<string>> original, string key, string value)
{
AppendToDictionary(original, key, new List<string> { value });
}
/// <summary>
/// Append one result to a results dictionary
/// </summary>
/// <param name="original">Dictionary to append to</param>
/// <param name="key">Key to add information to</param>
/// <param name="value">String value to add</param>
public static void AppendToDictionary(Dictionary<string, List<string>> original, string key, List<string> values)
{
// If the dictionary is null, just return
if (original == null)
return;
// Use a placeholder value if the key is null
key = key ?? "NO FILENAME";
// Add the key if needed and then append the lists
if (!original.ContainsKey(key))
original[key] = new List<string>();
original[key].AddRange(values);
}
/// <summary>
/// Append one results dictionary to another
/// </summary>
/// <param name="original">Dictionary to append to</param>
/// <param name="addition">Dictionary to pull from</param>
public static void AppendToDictionary(Dictionary<string, List<string>> original, Dictionary<string, List<string>> addition)
{
// If either dictionary is missing, just return
if (original == null || addition == null)
return;
// Loop through each of the addition keys and add accordingly
foreach (string key in addition.Keys)
{
if (!original.ContainsKey(key))
original[key] = new List<string>();
original[key].AddRange(addition[key]);
}
}
/// <summary>
/// Prepend a parent path from dictionary keys, if possible
/// </summary>
/// <param name="original">Dictionary to strip values from</param>
/// <param name="pathToPrepend">Path to strip from the keys</param>
public static void PrependToKeys(Dictionary<string, List<string>> original, string pathToPrepend)
{
// If the dictionary is missing, we can't do anything
if (original == null)
return;
// Use a placeholder value if the path is null
pathToPrepend = pathToPrepend ?? "ARCHIVE";
// Get a list of all of the keys
var keys = original.Keys.ToList();
// Iterate and reset keys
for (int i = 0; i < keys.Count; i++)
{
// Get the current key
string currentKey = keys[i];
// Otherwise, get the new key name and transfer over
string newKey = Path.Combine(pathToPrepend, currentKey);
original[newKey] = original[currentKey];
original.Remove(currentKey);
}
}
/// <summary>
/// Strip a parent path from dictionary keys, if possible
/// </summary>
/// <param name="original">Dictionary to strip values from</param>
/// <param name="pathToStrip">Path to strip from the keys</param>
public static void StripFromKeys(Dictionary<string, List<string>> original, string pathToStrip)
{
// If either is missing, we can't do anything
if (original == null || string.IsNullOrEmpty(pathToStrip))
return;
// Get a list of all of the keys
var keys = original.Keys.ToList();
// Iterate and reset keys
for (int i = 0; i < keys.Count; i++)
{
// Get the current key
string currentKey = keys[i];
// If the key doesn't start with the path, don't touch it
if (!currentKey.StartsWith(pathToStrip, StringComparison.OrdinalIgnoreCase))
continue;
// Otherwise, get the new key name and transfer over
string newKey = currentKey.Substring(pathToStrip.Length);
original[newKey] = original[currentKey];
original.Remove(currentKey);
}
}
#endregion
#region Byte Arrays
/// <summary>
/// Search for a byte array in another array
/// </summary>
@@ -70,6 +192,8 @@ namespace BurnOutSharp
return true;
}
#endregion
/// <summary>
/// Get the file version as reported by the filesystem
/// </summary>
@@ -110,60 +234,6 @@ namespace BurnOutSharp
return fsName;
}
/// <summary>
/// Append one result to a results dictionary
/// </summary>
/// <param name="original">Dictionary to append to</param>
/// <param name="key">Key to add information to</param>
/// <param name="value">String value to add</param>
public static void AppendToDictionary(Dictionary<string, List<string>> original, string key, string value)
{
AppendToDictionary(original, key, new List<string> { value });
}
/// <summary>
/// Append one result to a results dictionary
/// </summary>
/// <param name="original">Dictionary to append to</param>
/// <param name="key">Key to add information to</param>
/// <param name="value">String value to add</param>
public static void AppendToDictionary(Dictionary<string, List<string>> original, string key, List<string> values)
{
// If the dictionary is null, just return
if (original == null)
return;
// Use a placeholder value if the key is null
key = key ?? "NO FILENAME";
// Add the key if needed and then append the lists
if (!original.ContainsKey(key))
original[key] = new List<string>();
original[key].AddRange(values);
}
/// <summary>
/// Append one results dictionary to another
/// </summary>
/// <param name="original">Dictionary to append to</param>
/// <param name="addition">Dictionary to pull from</param>
public static void AppendToDictionary(Dictionary<string, List<string>> original, Dictionary<string, List<string>> addition)
{
// If either dictionary is missing, just return
if (original == null || addition == null)
return;
// Loop through each of the addition keys and add accordingly
foreach (string key in addition.Keys)
{
if (!original.ContainsKey(key))
original[key] = new List<string>();
original[key].AddRange(addition[key]);
}
}
#region P/Invoke
// https://stackoverflow.com/questions/8819188/c-sharp-classes-to-undelete-files/8820157#8820157