[DatFile] Add first version of "better delete" to sort

This commit is contained in:
Matt Nadareski
2017-01-26 15:49:46 -08:00
parent 12559a0e26
commit 434bec9a9c

View File

@@ -212,6 +212,10 @@ namespace SabreTools.Helper.Dats
// Define the temporary directory // Define the temporary directory
string tempSubDir = Path.GetFullPath(Path.Combine(tempDir, Path.GetRandomFileName())) + Path.DirectorySeparatorChar; string tempSubDir = Path.GetFullPath(Path.Combine(tempDir, Path.GetRandomFileName())) + Path.DirectorySeparatorChar;
// Set the deletion variables
bool usedExternally = false;
bool usedInternally = false;
// Get the required scanning level for the file // Get the required scanning level for the file
bool shouldExternalProcess = false; bool shouldExternalProcess = false;
bool shouldInternalProcess = false; bool shouldInternalProcess = false;
@@ -221,7 +225,7 @@ namespace SabreTools.Helper.Dats
if (shouldExternalProcess) if (shouldExternalProcess)
{ {
Rom rom = FileTools.GetFileInfo(file, logger, noMD5: quickScan, noSHA1: quickScan, header: headerToCheckAgainst); Rom rom = FileTools.GetFileInfo(file, logger, noMD5: quickScan, noSHA1: quickScan, header: headerToCheckAgainst);
RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat, usedExternally = RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat,
romba, updateDat, false /* isZip */, headerToCheckAgainst, logger); romba, updateDat, false /* isZip */, headerToCheckAgainst, logger);
} }
@@ -232,10 +236,11 @@ namespace SabreTools.Helper.Dats
if (quickScan) if (quickScan)
{ {
List<Rom> extracted = ArchiveTools.GetArchiveFileInfo(file, logger); List<Rom> extracted = ArchiveTools.GetArchiveFileInfo(file, logger);
usedInternally = true;
foreach (Rom rom in extracted) foreach (Rom rom in extracted)
{ {
RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat, usedInternally &= RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat,
romba, updateDat, true /* isZip */, headerToCheckAgainst, logger); romba, updateDat, true /* isZip */, headerToCheckAgainst, logger);
} }
} }
@@ -254,18 +259,41 @@ namespace SabreTools.Helper.Dats
Rom rom = FileTools.GetFileInfo(entry, logger, noMD5: quickScan, noSHA1: quickScan, header: headerToCheckAgainst); Rom rom = FileTools.GetFileInfo(entry, logger, noMD5: quickScan, noSHA1: quickScan, header: headerToCheckAgainst);
RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat, RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat,
romba, updateDat, false /* isZip */, headerToCheckAgainst, logger); romba, updateDat, false /* isZip */, headerToCheckAgainst, logger);
// Now we want to remove the file from the temp directory
try
{
File.Delete(entry);
}
catch { }
}
// If the temp directory is empty, we assume that everything inside was used
if (Directory.EnumerateFiles(tempSubDir, "*", SearchOption.AllDirectories).Count() == 0)
{
usedInternally = true;
} }
} }
// Otherwise, just get the info on the file itself // Otherwise, just get the info on the file itself
else if (File.Exists(file)) else if (File.Exists(file))
{ {
Rom rom = FileTools.GetFileInfo(file, logger, noMD5: quickScan, noSHA1: quickScan, header: headerToCheckAgainst); Rom rom = FileTools.GetFileInfo(file, logger, noMD5: quickScan, noSHA1: quickScan, header: headerToCheckAgainst);
RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat, usedExternally = RebuildToOutputWithoutSetsIndividual(rom, file, outDir, tempSubDir, date, inverse, outputFormat,
romba, updateDat, false /* isZip */, headerToCheckAgainst, logger); romba, updateDat, false /* isZip */, headerToCheckAgainst, logger);
} }
} }
} }
// If we are supposed to delete the file, do so
if (delete && (usedExternally || usedInternally))
{
try
{
File.Delete(file);
}
catch { }
}
// Now delete the temp directory // Now delete the temp directory
try try
{ {
@@ -289,9 +317,13 @@ namespace SabreTools.Helper.Dats
/// <param name="isZip">True if the input file is an archive, false otherwise</param> /// <param name="isZip">True if the input file is an archive, false otherwise</param>
/// <param name="headerToCheckAgainst">Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise</param> /// <param name="headerToCheckAgainst">Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise</param>
/// <param name="logger">Logger object for file and console output</param> /// <param name="logger">Logger object for file and console output</param>
private void RebuildToOutputWithoutSetsIndividual(Rom rom, string file, string outDir, string tempDir, bool date, /// <returns>True if the file was able to be rebuilt, false otherwise</returns>
private bool RebuildToOutputWithoutSetsIndividual(Rom rom, string file, string outDir, string tempDir, bool date,
bool inverse, OutputFormat outputFormat, bool romba, bool updateDat, bool isZip, string headerToCheckAgainst, Logger logger) bool inverse, OutputFormat outputFormat, bool romba, bool updateDat, bool isZip, string headerToCheckAgainst, Logger logger)
{ {
// Set the output value
bool rebuilt = false;
// Find if the file has duplicates in the DAT // Find if the file has duplicates in the DAT
bool hasDuplicates = rom.HasDuplicates(this, logger); bool hasDuplicates = rom.HasDuplicates(this, logger);
@@ -304,7 +336,7 @@ namespace SabreTools.Helper.Dats
// If we don't have any duplicates, continue // If we don't have any duplicates, continue
if (dupes.Count == 0) if (dupes.Count == 0)
{ {
return; return rebuilt;
} }
// If we have an archive input, get the real name of the file to use // If we have an archive input, get the real name of the file to use
@@ -317,12 +349,14 @@ namespace SabreTools.Helper.Dats
// If we couldn't extract the file, then continue, // If we couldn't extract the file, then continue,
if (String.IsNullOrEmpty(file)) if (String.IsNullOrEmpty(file))
{ {
return; return rebuilt;
} }
// Now loop through the list and rebuild accordingly // Now loop through the list and rebuild accordingly
foreach (Rom item in dupes) foreach (Rom item in dupes)
{ {
rebuilt = true;
switch (outputFormat) switch (outputFormat)
{ {
case OutputFormat.Folder: case OutputFormat.Folder:
@@ -339,17 +373,22 @@ namespace SabreTools.Helper.Dats
{ {
File.SetCreationTime(outfile, DateTime.Parse(item.Date)); File.SetCreationTime(outfile, DateTime.Parse(item.Date));
} }
rebuilt &= true;
}
catch
{
rebuilt &= false;
} }
catch { }
break; break;
case OutputFormat.TapeArchive: case OutputFormat.TapeArchive:
ArchiveTools.WriteTAR(file, outDir, item, logger, date: date); rebuilt &= ArchiveTools.WriteTAR(file, outDir, item, logger, date: date);
break; break;
case OutputFormat.Torrent7Zip: case OutputFormat.Torrent7Zip:
break; break;
case OutputFormat.TorrentGzip: case OutputFormat.TorrentGzip:
ArchiveTools.WriteTorrentGZ(file, outDir, romba, logger); rebuilt &= ArchiveTools.WriteTorrentGZ(file, outDir, romba, logger);
break; break;
case OutputFormat.TorrentLrzip: case OutputFormat.TorrentLrzip:
break; break;
@@ -358,7 +397,7 @@ namespace SabreTools.Helper.Dats
case OutputFormat.TorrentXZ: case OutputFormat.TorrentXZ:
break; break;
case OutputFormat.TorrentZip: case OutputFormat.TorrentZip:
ArchiveTools.WriteTorrentZip(file, outDir, item, logger, date: date); rebuilt &= ArchiveTools.WriteTorrentZip(file, outDir, item, logger, date: date);
break; break;
} }
@@ -370,6 +409,8 @@ namespace SabreTools.Helper.Dats
catch { } catch { }
} }
} }
return rebuilt;
} }
/// <summary> /// <summary>