Fixed the home dir redaction code to take 8.3 paths into account

This commit is contained in:
Emma Alyx Wunder 2024-06-13 04:36:25 +02:00
parent 3e3eb365f8
commit db68879351
2 changed files with 41 additions and 0 deletions

View file

@ -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}";

View file

@ -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();
}
}
}