[FileTools] Create and use safe file/directory delete

This commit is contained in:
Matt Nadareski
2017-03-15 14:44:44 -07:00
parent 0995718106
commit 26d49d9e19
6 changed files with 88 additions and 73 deletions

View File

@@ -149,16 +149,9 @@ namespace SabreTools.Helper.Dats
// Now that we're done, delete the temp folder (if it's not the default)
Globals.Logger.User("Cleaning temp folder");
try
{
if (tempDir != Path.GetTempPath())
{
Directory.Delete(tempDir, true);
}
}
catch
{
// Just absorb the error for now
FileTools.SafeTryDeleteDirectory(tempDir);
}
return true;
@@ -333,18 +326,11 @@ namespace SabreTools.Helper.Dats
// Cue to delete the file if it's a copy
if (copyFiles && item != newItem)
{
try
{
Directory.Delete(newBasePath, true);
}
catch { }
FileTools.SafeTryDeleteDirectory(newBasePath);
}
// Delete the sub temp directory
if (Directory.Exists(tempSubDir))
{
Directory.Delete(tempSubDir, true);
}
FileTools.SafeTryDeleteDirectory(tempSubDir);
}
/// <summary>

View File

@@ -447,7 +447,7 @@ namespace SabreTools.Helper.Dats
try
{
Globals.Logger.Verbose("Attempting to delete input file '" + file + "'");
File.Delete(file);
FileTools.SafeTryDeleteFile(file, true);
Globals.Logger.Verbose("File '" + file + "' deleted");
}
catch (Exception ex)
@@ -457,11 +457,7 @@ namespace SabreTools.Helper.Dats
}
// Now delete the temp directory
try
{
Directory.Delete(tempSubDir, true);
}
catch { }
FileTools.SafeTryDeleteDirectory(tempSubDir);
}
/// <summary>
@@ -768,11 +764,7 @@ namespace SabreTools.Helper.Dats
// And now clear the temp folder to get rid of any transient files if we unzipped
if (isZip)
{
try
{
Directory.Delete(tempDir, true);
}
catch { }
FileTools.SafeTryDeleteDirectory(tempDir);
}
return rebuilt;

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
#if MONO
using System.IO;
@@ -758,7 +759,7 @@ namespace ROMVault2.SupportedFiles.Zip
_zipstream.Dispose();
// Delete the failed file
File.Delete(_zipFileInfo.FullName);
FileTools.SafeTryDeleteFile(_zipFileInfo.FullName);
_zipFileInfo = null;
_zipOpen = ZipOpenType.Closed;
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
#if MONO
using System.IO;
@@ -58,16 +59,9 @@ namespace SabreTools.Helper.Skippers
// If the output file has size 0, delete it
if (new FileInfo(output).Length == 0)
{
try
{
File.Delete(output);
FileTools.SafeTryDeleteFile(output);
success = false;
}
catch
{
// Don't log this file deletion error
}
}
return success;
}

View File

@@ -1290,7 +1290,7 @@ namespace SabreTools.Helper.Tools
// If the old file exists, delete it and replace
if (File.Exists(archiveFileName))
{
File.Delete(archiveFileName);
FileTools.SafeTryDeleteFile(archiveFileName);
}
File.Move(tempFile, archiveFileName);
@@ -1402,11 +1402,7 @@ namespace SabreTools.Helper.Tools
}
FileTools.CleanDirectory(tempPath);
try
{
Directory.Delete(tempPath);
}
catch { }
FileTools.SafeTryDeleteDirectory(tempPath);
}
// Otherwise, sort the input files and write out in the correct order
@@ -1474,11 +1470,7 @@ namespace SabreTools.Helper.Tools
zipFile.CompressFiles(zipFileStream, inputFiles[index]);
oldZipFileEntryStream.Dispose();
try
{
File.Delete(inputFiles[index]);
}
catch { }
FileTools.SafeTryDeleteFile(inputFiles[index]);
}
}
@@ -1497,7 +1489,7 @@ namespace SabreTools.Helper.Tools
// If the old file exists, delete it and replace
if (File.Exists(archiveFileName))
{
File.Delete(archiveFileName);
FileTools.SafeTryDeleteFile(archiveFileName);
}
File.Move(tempFile, archiveFileName);
@@ -1785,11 +1777,7 @@ namespace SabreTools.Helper.Tools
}
FileTools.CleanDirectory(tempPath);
try
{
Directory.Delete(tempPath);
}
catch { }
FileTools.SafeTryDeleteDirectory(tempPath);
}
// Otherwise, sort the input files and write out in the correct order
@@ -1857,11 +1845,7 @@ namespace SabreTools.Helper.Tools
zipFile.CompressFiles(zipFileStream, inputFiles[index]);
oldZipFileEntryStream.Dispose();
try
{
File.Delete(inputFiles[index]);
}
catch { }
FileTools.SafeTryDeleteFile(inputFiles[index]);
}
}
@@ -1880,7 +1864,7 @@ namespace SabreTools.Helper.Tools
// If the old file exists, delete it and replace
if (File.Exists(archiveFileName))
{
File.Delete(archiveFileName);
FileTools.SafeTryDeleteFile(archiveFileName);
}
File.Move(tempFile, archiveFileName);
@@ -2146,7 +2130,7 @@ namespace SabreTools.Helper.Tools
// If the old file exists, delete it and replace
if (File.Exists(archiveFileName))
{
File.Delete(archiveFileName);
FileTools.SafeTryDeleteFile(archiveFileName);
}
File.Move(tempFile, archiveFileName);

View File

@@ -336,19 +336,11 @@ namespace SabreTools.Helper.Tools
{
foreach (string file in Directory.EnumerateFiles(dirname, "*", SearchOption.TopDirectoryOnly))
{
try
{
File.Delete(file);
}
catch { }
SafeTryDeleteFile(file);
}
foreach (string dir in Directory.EnumerateDirectories(dirname, "*", SearchOption.TopDirectoryOnly))
{
try
{
Directory.Delete(dir, true);
}
catch { }
SafeTryDeleteDirectory(dir);
}
}
@@ -528,6 +520,72 @@ namespace SabreTools.Helper.Tools
return true;
}
/// <summary>
/// Try to safely delete a directory, optionally throwing the error
/// </summary>
/// <param name="file">Name of the directory to delete</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the file didn't exist or could be deleted, false otherwise</returns>
public static bool SafeTryDeleteDirectory(string file, bool throwOnError = false)
{
// Check if the file exists first
if (!Directory.Exists(file))
{
return true;
}
// Now wrap deleting the file
try
{
Directory.Delete(file, true);
return true;
}
catch (Exception ex)
{
if (throwOnError)
{
throw ex;
}
else
{
return false;
}
}
}
/// <summary>
/// Try to safely delete a file, optionally throwing the error
/// </summary>
/// <param name="file">Name of the file to delete</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the file didn't exist or could be deleted, false otherwise</returns>
public static bool SafeTryDeleteFile(string file, bool throwOnError = false)
{
// Check if the file exists first
if (!File.Exists(file))
{
return true;
}
// Now wrap deleting the file
try
{
File.Delete(file);
return true;
}
catch (Exception ex)
{
if (throwOnError)
{
throw ex;
}
else
{
return false;
}
}
}
#endregion
#region Stream Information