Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
fix(macos): address alias review: single error release, alias check first, Trash targets broken
mendax-iitm23b17d112f4a
diff --git a/src/Filee.Platform.MacOS/NativeMethods.cs b/src/Filee.Platform.MacOS/NativeMethods.csindex e2c00ff8..507ff0f2 100644--- a/src/Filee.Platform.MacOS/NativeMethods.cs+++ b/src/Filee.Platform.MacOS/NativeMethods.cs@@ -16,6 +16,11 @@ internal static class NativeMethodsprivate static readonly nint PidKey = CFStringCreateWithCString(0, "kCGWindowOwnerPID", 0x08000100);private static readonly nint AlphaKey = CFStringCreateWithCString(0, "kCGWindowAlpha", 0x08000100);private static readonly nint LayerKey = CFStringCreateWithCString(0, "kCGWindowLayer", 0x08000100);+ private static readonly nint IsAliasFileKey = CoreFoundationConstant("kCFURLIsAliasFileKey");+ private static readonly nint BooleanTrue = CoreFoundationConstant("kCFBooleanTrue");++ private static nint CoreFoundationConstant(string name) =>+ Marshal.ReadIntPtr(NativeLibrary.GetExport(NativeLibrary.Load(CoreFoundation), name));/// <summary>Process names of the app windows on screen (layer 0), front to back.</summary>internal static IEnumerable<string> WindowOwnerNames()@@ -211,8 +216,8 @@ internal static class NativeMethods/// <summary>/// The existing file or folder the Finder alias at <paramref name="path"/> points to; null for anything that- /// isn't an alias (CFURLCreateBookmarkDataFromFile reads no bookmark from it), a broken alias, or one whose- /// target is on a volume that isn't mounted: resolving never shows UI or mounts a server, since a drag waits on it.+ /// isn't an alias, a broken alias, one whose target is in a Trash, or one whose target is on a volume that isn't+ /// mounted: resolving never shows UI or mounts a server, since a drag waits on it./// </summary>internal static string? AliasTarget(string path){@@ -223,6 +228,9 @@ internal static class NativeMethodsnint data = 0, target = 0;try{+ // Every dropped path comes through here, on the UI thread: only an alias (metadata, no read) is opened.+ if (!IsAliasFile(url))+ return null;data = CFURLCreateBookmarkDataFromFile(0, url, out var error);if (data == 0){@@ -240,7 +248,8 @@ internal static class NativeMethods// composed, and APFS and HFS+ find the file either way, so a "보고서" alias and its target match.var resolved = System.Text.Encoding.UTF8.GetString(buffer, 0, Array.IndexOf(buffer, (byte)0)).Normalize(System.Text.NormalizationForm.FormC);- return File.Exists(resolved) || Directory.Exists(resolved) ? resolved : null;+ // A bookmark follows its file into the Trash, where a converted copy would be lost: that alias is broken.+ return !InTrash(resolved) && (File.Exists(resolved) || Directory.Exists(resolved)) ? resolved : null;}finally{@@ -260,7 +269,6 @@ internal static class NativeMethodstry{data = CFURLCreateBookmarkData(0, targetUrl, suitableForBookmarkFile, 0, 0, out var error);- Release(error);if (data == 0 || !CFURLWriteBookmarkDataToFile(data, aliasUrl, 0, out error)){Release(error);@@ -275,6 +283,24 @@ internal static class NativeMethods}}+ private static bool IsAliasFile(nint url)+ {+ if (!CFURLCopyResourcePropertyForKey(url, IsAliasFileKey, out var value, out var error))+ {+ Release(error);+ return false;+ }+ var isAlias = value == BooleanTrue;+ Release(value);+ return isAlias;+ }++ private static bool InTrash(string path)+ {+ var trash = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".Trash") + "/";+ return (path + "/").StartsWith(trash, StringComparison.Ordinal) || path.Contains("/.Trashes/", StringComparison.Ordinal);+ }+private static nint FileUrl(string path){var bytes = System.Text.Encoding.UTF8.GetBytes(Path.GetFullPath(path));@@ -294,6 +320,10 @@ internal static class NativeMethods[return: MarshalAs(UnmanagedType.I1)]private static extern bool CFURLGetFileSystemRepresentation(nint url, [MarshalAs(UnmanagedType.I1)] bool resolveAgainstBase, byte[] buffer, nint maxLength);+ [DllImport(CoreFoundation)]+ [return: MarshalAs(UnmanagedType.I1)]+ private static extern bool CFURLCopyResourcePropertyForKey(nint url, nint key, out nint value, out nint error);+[DllImport(CoreFoundation)]private static extern nint CFURLCreateBookmarkDataFromFile(nint allocator, nint fileUrl, out nint error);diff --git a/tests/Filee.Platform.Tests/MacOSAliasTests.cs b/tests/Filee.Platform.Tests/MacOSAliasTests.csindex b31cac94..d496c706 100644--- a/tests/Filee.Platform.Tests/MacOSAliasTests.cs+++ b/tests/Filee.Platform.Tests/MacOSAliasTests.cs@@ -59,6 +59,34 @@ public sealed class MacOSAliasTests : IDisposableAssert.Null(platform.ShortcutTarget(Path.Combine(_folder, "missing alias")));}+ [Fact]+ public void An_alias_to_a_file_in_a_trash_is_broken()+ {+ if (!OperatingSystem.IsMacOS())+ {+ Assert.Skip("Finder aliases need macOS.");+ return;+ }+ // The layout of a volume's Trash; the user's ~/.Trash is matched the same way.+ var trash = Directory.CreateDirectory(Path.Combine(_folder, ".Trashes", "501")).FullName;+ var trashed = Path.Combine(trash, "report.docx");+ File.WriteAllText(trashed, "x");+ var toTrashed = Alias("report alias", trashed);++ Assert.Null(new MacOSPlatformServices().ShortcutTarget(toTrashed));+ }++ [Fact]+ public void Writing_an_alias_to_a_missing_file_throws()+ {+ if (!OperatingSystem.IsMacOS())+ {+ Assert.Skip("Finder aliases need macOS.");+ return;+ }+ Assert.Throws<IOException>(() => Alias("missing alias", Path.Combine(_folder, "missing.pdf")));+ }+[Fact]public void An_alias_dropped_with_its_target_gives_the_file_once_and_a_broken_one_stays(){