mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[FileTools] Create and use safe file/directory delete
This commit is contained in:
@@ -149,16 +149,9 @@ namespace SabreTools.Helper.Dats
|
|||||||
|
|
||||||
// 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)
|
||||||
Globals.Logger.User("Cleaning temp folder");
|
Globals.Logger.User("Cleaning temp folder");
|
||||||
try
|
if (tempDir != Path.GetTempPath())
|
||||||
{
|
{
|
||||||
if (tempDir != Path.GetTempPath())
|
FileTools.SafeTryDeleteDirectory(tempDir);
|
||||||
{
|
|
||||||
Directory.Delete(tempDir, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Just absorb the error for now
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -333,18 +326,11 @@ namespace SabreTools.Helper.Dats
|
|||||||
// Cue to delete the file if it's a copy
|
// Cue to delete the file if it's a copy
|
||||||
if (copyFiles && item != newItem)
|
if (copyFiles && item != newItem)
|
||||||
{
|
{
|
||||||
try
|
FileTools.SafeTryDeleteDirectory(newBasePath);
|
||||||
{
|
|
||||||
Directory.Delete(newBasePath, true);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the sub temp directory
|
// Delete the sub temp directory
|
||||||
if (Directory.Exists(tempSubDir))
|
FileTools.SafeTryDeleteDirectory(tempSubDir);
|
||||||
{
|
|
||||||
Directory.Delete(tempSubDir, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -447,7 +447,7 @@ namespace SabreTools.Helper.Dats
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Globals.Logger.Verbose("Attempting to delete input file '" + file + "'");
|
Globals.Logger.Verbose("Attempting to delete input file '" + file + "'");
|
||||||
File.Delete(file);
|
FileTools.SafeTryDeleteFile(file, true);
|
||||||
Globals.Logger.Verbose("File '" + file + "' deleted");
|
Globals.Logger.Verbose("File '" + file + "' deleted");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -457,11 +457,7 @@ namespace SabreTools.Helper.Dats
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now delete the temp directory
|
// Now delete the temp directory
|
||||||
try
|
FileTools.SafeTryDeleteDirectory(tempSubDir);
|
||||||
{
|
|
||||||
Directory.Delete(tempSubDir, true);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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
|
// And now clear the temp folder to get rid of any transient files if we unzipped
|
||||||
if (isZip)
|
if (isZip)
|
||||||
{
|
{
|
||||||
try
|
FileTools.SafeTryDeleteDirectory(tempDir);
|
||||||
{
|
|
||||||
Directory.Delete(tempDir, true);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rebuilt;
|
return rebuilt;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
using SabreTools.Helper.Data;
|
using SabreTools.Helper.Data;
|
||||||
|
using SabreTools.Helper.Tools;
|
||||||
|
|
||||||
#if MONO
|
#if MONO
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@@ -758,7 +759,7 @@ namespace ROMVault2.SupportedFiles.Zip
|
|||||||
_zipstream.Dispose();
|
_zipstream.Dispose();
|
||||||
|
|
||||||
// Delete the failed file
|
// Delete the failed file
|
||||||
File.Delete(_zipFileInfo.FullName);
|
FileTools.SafeTryDeleteFile(_zipFileInfo.FullName);
|
||||||
_zipFileInfo = null;
|
_zipFileInfo = null;
|
||||||
_zipOpen = ZipOpenType.Closed;
|
_zipOpen = ZipOpenType.Closed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using SabreTools.Helper.Data;
|
using SabreTools.Helper.Data;
|
||||||
|
using SabreTools.Helper.Tools;
|
||||||
|
|
||||||
#if MONO
|
#if MONO
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@@ -58,15 +59,8 @@ namespace SabreTools.Helper.Skippers
|
|||||||
// If the output file has size 0, delete it
|
// If the output file has size 0, delete it
|
||||||
if (new FileInfo(output).Length == 0)
|
if (new FileInfo(output).Length == 0)
|
||||||
{
|
{
|
||||||
try
|
FileTools.SafeTryDeleteFile(output);
|
||||||
{
|
success = false;
|
||||||
File.Delete(output);
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Don't log this file deletion error
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
|
|||||||
@@ -1290,7 +1290,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
// If the old file exists, delete it and replace
|
// If the old file exists, delete it and replace
|
||||||
if (File.Exists(archiveFileName))
|
if (File.Exists(archiveFileName))
|
||||||
{
|
{
|
||||||
File.Delete(archiveFileName);
|
FileTools.SafeTryDeleteFile(archiveFileName);
|
||||||
}
|
}
|
||||||
File.Move(tempFile, archiveFileName);
|
File.Move(tempFile, archiveFileName);
|
||||||
|
|
||||||
@@ -1402,11 +1402,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
FileTools.CleanDirectory(tempPath);
|
FileTools.CleanDirectory(tempPath);
|
||||||
try
|
FileTools.SafeTryDeleteDirectory(tempPath);
|
||||||
{
|
|
||||||
Directory.Delete(tempPath);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, sort the input files and write out in the correct order
|
// 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]);
|
zipFile.CompressFiles(zipFileStream, inputFiles[index]);
|
||||||
|
|
||||||
oldZipFileEntryStream.Dispose();
|
oldZipFileEntryStream.Dispose();
|
||||||
try
|
FileTools.SafeTryDeleteFile(inputFiles[index]);
|
||||||
{
|
|
||||||
File.Delete(inputFiles[index]);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1497,7 +1489,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
// If the old file exists, delete it and replace
|
// If the old file exists, delete it and replace
|
||||||
if (File.Exists(archiveFileName))
|
if (File.Exists(archiveFileName))
|
||||||
{
|
{
|
||||||
File.Delete(archiveFileName);
|
FileTools.SafeTryDeleteFile(archiveFileName);
|
||||||
}
|
}
|
||||||
File.Move(tempFile, archiveFileName);
|
File.Move(tempFile, archiveFileName);
|
||||||
|
|
||||||
@@ -1785,11 +1777,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
FileTools.CleanDirectory(tempPath);
|
FileTools.CleanDirectory(tempPath);
|
||||||
try
|
FileTools.SafeTryDeleteDirectory(tempPath);
|
||||||
{
|
|
||||||
Directory.Delete(tempPath);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, sort the input files and write out in the correct order
|
// 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]);
|
zipFile.CompressFiles(zipFileStream, inputFiles[index]);
|
||||||
|
|
||||||
oldZipFileEntryStream.Dispose();
|
oldZipFileEntryStream.Dispose();
|
||||||
try
|
FileTools.SafeTryDeleteFile(inputFiles[index]);
|
||||||
{
|
|
||||||
File.Delete(inputFiles[index]);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1880,7 +1864,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
// If the old file exists, delete it and replace
|
// If the old file exists, delete it and replace
|
||||||
if (File.Exists(archiveFileName))
|
if (File.Exists(archiveFileName))
|
||||||
{
|
{
|
||||||
File.Delete(archiveFileName);
|
FileTools.SafeTryDeleteFile(archiveFileName);
|
||||||
}
|
}
|
||||||
File.Move(tempFile, archiveFileName);
|
File.Move(tempFile, archiveFileName);
|
||||||
|
|
||||||
@@ -2146,7 +2130,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
// If the old file exists, delete it and replace
|
// If the old file exists, delete it and replace
|
||||||
if (File.Exists(archiveFileName))
|
if (File.Exists(archiveFileName))
|
||||||
{
|
{
|
||||||
File.Delete(archiveFileName);
|
FileTools.SafeTryDeleteFile(archiveFileName);
|
||||||
}
|
}
|
||||||
File.Move(tempFile, archiveFileName);
|
File.Move(tempFile, archiveFileName);
|
||||||
|
|
||||||
|
|||||||
@@ -336,19 +336,11 @@ namespace SabreTools.Helper.Tools
|
|||||||
{
|
{
|
||||||
foreach (string file in Directory.EnumerateFiles(dirname, "*", SearchOption.TopDirectoryOnly))
|
foreach (string file in Directory.EnumerateFiles(dirname, "*", SearchOption.TopDirectoryOnly))
|
||||||
{
|
{
|
||||||
try
|
SafeTryDeleteFile(file);
|
||||||
{
|
|
||||||
File.Delete(file);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
foreach (string dir in Directory.EnumerateDirectories(dirname, "*", SearchOption.TopDirectoryOnly))
|
foreach (string dir in Directory.EnumerateDirectories(dirname, "*", SearchOption.TopDirectoryOnly))
|
||||||
{
|
{
|
||||||
try
|
SafeTryDeleteDirectory(dir);
|
||||||
{
|
|
||||||
Directory.Delete(dir, true);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -528,6 +520,72 @@ namespace SabreTools.Helper.Tools
|
|||||||
return true;
|
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
|
#endregion
|
||||||
|
|
||||||
#region Stream Information
|
#region Stream Information
|
||||||
|
|||||||
Reference in New Issue
Block a user