From b627ea87c9f7ac32070968009d3c58bad00a0b6e Mon Sep 17 00:00:00 2001 From: Carl Johnsen Date: Thu, 5 Mar 2026 18:11:53 +0100 Subject: [PATCH] Changed from PInvoke call to get error messages to the Marshal library call that does the same --- proprietary/DiskImage/Disk/Mac.cs | 36 +++++-------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/proprietary/DiskImage/Disk/Mac.cs b/proprietary/DiskImage/Disk/Mac.cs index 9250087bb..19e418220 100644 --- a/proprietary/DiskImage/Disk/Mac.cs +++ b/proprietary/DiskImage/Disk/Mac.cs @@ -153,7 +153,7 @@ namespace Duplicati.Proprietary.DiskImage.Disk if (m_fileDescriptor < 0) { int errorCode = Marshal.GetLastWin32Error(); - string errorMessage = GetErrnoMessage(errorCode); + string errorMessage = System.Runtime.InteropServices.Marshal.GetPInvokeErrorMessage(errorCode); Duplicati.Library.Logging.Log.WriteErrorMessage(LOGTAG, "initialize", null, $"Failed to open device {m_devicePath}: {errorMessage} (errno: {errorCode})"); return Task.FromResult(false); } @@ -212,7 +212,7 @@ namespace Duplicati.Proprietary.DiskImage.Disk if (ioctl_no_arg(m_fileDescriptor, DKIOCSYNCHRONIZECACHE) < 0) { int errorCode = Marshal.GetLastWin32Error(); - string errorMessage = GetErrnoMessage(errorCode); + string errorMessage = System.Runtime.InteropServices.Marshal.GetPInvokeErrorMessage(errorCode); Duplicati.Library.Logging.Log.WriteWarningMessage(LOGTAG, "dispose", null, $"Failed to flush data: {errorMessage} (errno: {errorCode})"); } } @@ -299,7 +299,7 @@ namespace Duplicati.Proprietary.DiskImage.Disk if (bytesRead.ToInt64() < 0) { int errorCode = Marshal.GetLastWin32Error(); - string errorMessage = GetErrnoMessage(errorCode); + string errorMessage = System.Runtime.InteropServices.Marshal.GetPInvokeErrorMessage(errorCode); throw new IOException($"Failed to read from disk at offset {offset + totalBytesRead}: {errorMessage} (errno: {errorCode})"); } if (bytesRead.ToInt64() == 0) @@ -379,7 +379,7 @@ namespace Duplicati.Proprietary.DiskImage.Disk if (bytesRead.ToInt64() < 0) { int errorCode = Marshal.GetLastWin32Error(); - string errorMessage = GetErrnoMessage(errorCode); + string errorMessage = System.Runtime.InteropServices.Marshal.GetPInvokeErrorMessage(errorCode); throw new IOException($"Failed to read existing data for padding at offset {offset}: {errorMessage} (errno: {errorCode})"); } } @@ -406,7 +406,7 @@ namespace Duplicati.Proprietary.DiskImage.Disk if (bytesWritten.ToInt64() < 0) { int errorCode = Marshal.GetLastWin32Error(); - string errorMessage = GetErrnoMessage(errorCode); + string errorMessage = System.Runtime.InteropServices.Marshal.GetPInvokeErrorMessage(errorCode); string hint = errorCode == 13 // EACCES ? "The disk may be mounted or you don't have sufficient permissions. Try unmounting the disk before writing." : errorCode == 30 // EROFS @@ -459,34 +459,8 @@ namespace Duplicati.Proprietary.DiskImage.Disk [LibraryImport("libSystem", SetLastError = true)] private static unsafe partial IntPtr pwrite(int fd, byte* buf, IntPtr count, long offset); - [LibraryImport("libSystem", SetLastError = true)] - private static partial IntPtr strerror(int errnum); - #endregion - /// - /// Gets the error message corresponding to the specified errno value. - /// - /// The error number. - /// A string describing the error. - private static string GetErrnoMessage(int errno) - { - try - { - IntPtr msgPtr = strerror(errno); - var result = msgPtr != IntPtr.Zero - ? Marshal.PtrToStringUTF8(msgPtr) ?? $"Unknown error (errno: {errno})" - : $"Unknown error (errno: {errno})"; - - Console.WriteLine($"strerror({errno}) returned: {result}"); - return result; - } - catch - { - return $"Unknown error (errno: {errno})"; - } - } - /// public static async IAsyncEnumerable ListPhysicalDrivesAsync([EnumeratorCancellation] CancellationToken cancellationToken) {