From db688793518f5ff4c7c33327702e6af07f9f8fcf Mon Sep 17 00:00:00 2001 From: Emma Alyx Wunder Date: Thu, 13 Jun 2024 04:36:25 +0200 Subject: [PATCH] Fixed the home dir redaction code to take 8.3 paths into account --- src/Ryujinx.Common/Logging/Logger.cs | 6 ++++ src/Ryujinx.Common/Utilities/WindowsNative.cs | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/Ryujinx.Common/Utilities/WindowsNative.cs diff --git a/src/Ryujinx.Common/Logging/Logger.cs b/src/Ryujinx.Common/Logging/Logger.cs index db46739ac..2f1e842f5 100644 --- a/src/Ryujinx.Common/Logging/Logger.cs +++ b/src/Ryujinx.Common/Logging/Logger.cs @@ -1,5 +1,6 @@ using Ryujinx.Common.Logging.Targets; using Ryujinx.Common.SystemInterop; +using Ryujinx.Common.Utilities; using System; using System.Collections.Generic; using System.Diagnostics; @@ -24,6 +25,7 @@ namespace Ryujinx.Common.Logging public readonly struct Log { private static readonly string _homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + private static readonly string _homeDirShort = WindowsNative.GetShortPathName(_homeDir); // empty string on non-windows platforms private static readonly string _homeDirRedacted = Path.Combine(Directory.GetParent(_homeDir).FullName, "[redacted]"); internal readonly LogLevel Level; @@ -106,6 +108,10 @@ namespace Ryujinx.Common.Logging [MethodImpl(MethodImplOptions.AggressiveInlining)] private static string FormatMessage(LogClass logClass, string caller, string message) { + if (_homeDirShort != "") + { + message = message.Replace(_homeDirShort, _homeDirRedacted); + } message = message.Replace(_homeDir, _homeDirRedacted); return $"{logClass} {caller}: {message}"; diff --git a/src/Ryujinx.Common/Utilities/WindowsNative.cs b/src/Ryujinx.Common/Utilities/WindowsNative.cs new file mode 100644 index 000000000..98e6d0434 --- /dev/null +++ b/src/Ryujinx.Common/Utilities/WindowsNative.cs @@ -0,0 +1,35 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Text; + +namespace Ryujinx.Common.Utilities +{ + public static partial class WindowsNative + { + [SupportedOSPlatform("windows")] + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + static extern int GetShortPathName(string longPath, StringBuilder shortPath, int bufferSize); + + private const int ShortPathBufferLength = 256; + + public static string GetShortPathName(string longPath) + { + if (!OperatingSystem.IsWindows()) + { + return ""; + } + + StringBuilder shortPathBuffer = new StringBuilder(ShortPathBufferLength); + int result = GetShortPathName(longPath, shortPathBuffer, ShortPathBufferLength); + if (result == 0) + { + int errCode = Marshal.GetLastWin32Error(); + Logging.Logger.Debug?.Print(Logging.LogClass.Application, $"GetShortPathName failed for {longPath} (0x{errCode:X08})"); + return ""; + } + + return shortPathBuffer.ToString(); + } + } +}