[DATFromDir] Add delayed delete for locked files that have been moved

This commit is contained in:
Matt Nadareski
2016-09-21 22:13:58 -07:00
parent dcbae99696
commit 7f44558083

View File

@@ -31,6 +31,7 @@ namespace SabreTools
// Other required variables // Other required variables
private Logger _logger; private Logger _logger;
private List<string> _clean;
// Public variables // Public variables
public DatFile DatData public DatFile DatData
@@ -72,6 +73,7 @@ namespace SabreTools
_copyFiles = copyFiles; _copyFiles = copyFiles;
_maxDegreeOfParallelism = maxDegreeOfParallelism; _maxDegreeOfParallelism = maxDegreeOfParallelism;
_logger = logger; _logger = logger;
_clean = new List<string>();
} }
/// <suaxmary> /// <suaxmary>
@@ -167,6 +169,7 @@ namespace SabreTools
} }
// Now that we're done, delete the temp folder (if it's not the default) // Now that we're done, delete the temp folder (if it's not the default)
_logger.User("Cleaning temp folder");
try try
{ {
if (_tempDir != Path.GetTempPath()) if (_tempDir != Path.GetTempPath())
@@ -179,6 +182,28 @@ namespace SabreTools
// Just absorb the error for now // Just absorb the error for now
} }
// Now if we have other files to delete, do so
if (_copyFiles)
{
_logger.User("Cleaning copied files");
Parallel.ForEach(_clean,
new ParallelOptions { MaxDegreeOfParallelism = _maxDegreeOfParallelism },
file =>
{
while (File.Exists(file))
{
try
{
Directory.Delete(Path.GetDirectoryName(file), true);
}
catch
{
// Just absorb the error for now
}
}
});
}
return true; return true;
} }
@@ -195,7 +220,8 @@ namespace SabreTools
string newitem = item; string newitem = item;
if (_copyFiles) if (_copyFiles)
{ {
newitem = Path.Combine(tempSubDir, Path.GetFileName(item)); newitem = Path.Combine(_tempDir, Path.GetRandomFileName(), Path.GetFileName(item));
Directory.CreateDirectory(Path.GetDirectoryName(newitem));
File.Copy(item, newitem, true); File.Copy(item, newitem, true);
} }
@@ -218,13 +244,13 @@ namespace SabreTools
} }
_datdata.Files[key].Add(rom); _datdata.Files[key].Add(rom);
_logger.User("File added: " + Path.GetFileNameWithoutExtension(newitem) + Environment.NewLine); _logger.User("File added: " + Path.GetFileNameWithoutExtension(item) + Environment.NewLine);
} }
} }
else else
{ {
_logger.User("File not added: " + Path.GetFileNameWithoutExtension(newitem) + Environment.NewLine); _logger.User("File not added: " + Path.GetFileNameWithoutExtension(item) + Environment.NewLine);
return; return;
} }
@@ -268,7 +294,7 @@ namespace SabreTools
// If the file was an archive and was extracted successfully, check it // If the file was an archive and was extracted successfully, check it
if (!encounteredErrors) if (!encounteredErrors)
{ {
_logger.Log(Path.GetFileName(newitem) + " treated like an archive"); _logger.Log(Path.GetFileName(item) + " treated like an archive");
List<string> extracted = Directory.EnumerateFiles(tempSubDir, "*", SearchOption.AllDirectories).ToList(); List<string> extracted = Directory.EnumerateFiles(tempSubDir, "*", SearchOption.AllDirectories).ToList();
Parallel.ForEach(extracted, Parallel.ForEach(extracted,
new ParallelOptions { MaxDegreeOfParallelism = _maxDegreeOfParallelism }, new ParallelOptions { MaxDegreeOfParallelism = _maxDegreeOfParallelism },
@@ -279,7 +305,7 @@ namespace SabreTools
Path.Combine((_datdata.Type == "SuperDAT" Path.Combine((_datdata.Type == "SuperDAT"
? (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, _basePath.Length) ? (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, _basePath.Length)
: ""), : ""),
Path.GetFileNameWithoutExtension(newitem))); Path.GetFileNameWithoutExtension(item)));
}); });
} }
// Otherwise, just get the info on the file itself // Otherwise, just get the info on the file itself
@@ -288,6 +314,19 @@ namespace SabreTools
ProcessFile(newitem, _basePath, ""); ProcessFile(newitem, _basePath, "");
} }
// Cue to delete the file if it's a copy
if (_copyFiles)
{
try
{
Directory.Delete(Path.GetDirectoryName(newitem), true);
}
catch
{
_clean.Add(newitem);
}
}
// Delete the sub temp directory // Delete the sub temp directory
if (Directory.Exists(tempSubDir)) if (Directory.Exists(tempSubDir))
{ {