Make file opens safer and more consistent

This commit is contained in:
Matt Nadareski
2025-01-04 23:52:16 -05:00
parent 58a7558dd8
commit 281375f8e9
12 changed files with 96 additions and 49 deletions

View File

@@ -378,8 +378,18 @@ namespace SabreTools.FileTypes.Archives
/// <inheritdoc/>
public override bool Write(string inputFile, string outDir, BaseFile? baseFile)
{
// Check that the input file exists
if (!File.Exists(inputFile))
{
_logger.Warning($"File '{inputFile}' does not exist!");
return false;
}
inputFile = Path.GetFullPath(inputFile);
// Get the file stream for the file and write out
return Write(File.OpenRead(inputFile), outDir, baseFile);
using Stream inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
return Write(inputStream, outDir, baseFile);
}
/// <inheritdoc/>
@@ -638,7 +648,7 @@ namespace SabreTools.FileTypes.Archives
int index = inputIndexMap[key];
// Open the input file for reading
Stream freadStream = File.OpenRead(inputFiles[index]);
Stream freadStream = File.Open(inputFiles[index], FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
ulong istreamSize = (ulong)new FileInfo(inputFiles[index]).Length;
DateTime dt = DateTime.Now;
@@ -720,7 +730,7 @@ namespace SabreTools.FileTypes.Archives
if (index < 0)
{
// Open the input file for reading
Stream freadStream = File.OpenRead(inputFiles[-index - 1]);
Stream freadStream = File.Open(inputFiles[-index - 1], FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
ulong istreamSize = (ulong)(new FileInfo(inputFiles[-index - 1]).Length);
DateTime dt = DateTime.Now;