Skip to content

Commit

Permalink
remove file handle closing code from spdEmulatorApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Secre-C committed Dec 23, 2023
1 parent b2de507 commit 1920c8d
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions Emulator/SPD.File.Emulator/SpdEmulatorApi.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using FileEmulationFramework.Interfaces;
using FileEmulationFramework.Interfaces.Reference;
using FileEmulationFramework.Lib.Utilities;
using Microsoft.Win32.SafeHandles;
using SPD.File.Emulator.Interfaces;
using SPD.File.Emulator.Interfaces.Structures.IO;
using SPD.File.Emulator.Utilities;
using System.Runtime.InteropServices;

namespace SPD.File.Emulator;
Expand Down Expand Up @@ -86,30 +86,22 @@ public void InvalidateFile(string spdPath)
public void RegisterSpd(string sourcePath, string destinationPath)
{
nint handle = Native.CreateFileW(sourcePath, FileAccess.Read, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
try

if (handle == new IntPtr(-1))
{
if (handle == new IntPtr(-1))
{
_logger.Error("[SpdEmulatorApi] RegisterSpd: Failed to open spd file with Win32 Error: {0}, Path {1}", Marshal.GetLastWin32Error(), sourcePath);
return;
}
_logger.Error("[SpdEmulatorApi] RegisterSpd: Failed to open spd file with Win32 Error: {0}, Path {1}", Marshal.GetLastWin32Error(), sourcePath);
return;
}

_ = Native.SetFilePointerEx(handle, 0, IntPtr.Zero, 0);
_ = Native.SetFilePointerEx(handle, 0, IntPtr.Zero, 0);

var fileStream = new FileStream(sourcePath, FileMode.Open);
var stream = StreamUtils.CreateMemoryStream(fileStream.Length);
fileStream.CopyTo(stream);
var fileStream = new FileStream(new SafeFileHandle(handle, false), FileAccess.Read);

var emulated = new EmulatedFile<Stream>(stream);
_spdEmulator.RegisterFile(destinationPath, stream);
_framework.RegisterVirtualFile(destinationPath, emulated);
var emulated = new EmulatedFile<Stream>(fileStream);
_spdEmulator.RegisterFile(destinationPath, fileStream);
_framework.RegisterVirtualFile(destinationPath, emulated);

_logger.Info("[SpdEmulatorApi] Registered spd {0} at {1}", sourcePath, destinationPath);
}
finally
{
Native.CloseHandle(handle);
}
_logger.Info("[SpdEmulatorApi] Registered spd {0} at {1}", sourcePath, destinationPath);
}

public bool TryCreateFromSpd(string sourcePath, string route, string destinationPath)
Expand Down

0 comments on commit 1920c8d

Please sign in to comment.