Allow FS access log to be used without a backing FS server

Reduces the hactoolnet build size by ~260 KB due to fssrv code no longer being used
This commit is contained in:
Alex Barney 2021-06-15 01:28:49 -07:00
parent e500e475eb
commit b3dc972881
6 changed files with 47 additions and 6 deletions

View file

@ -22,6 +22,7 @@ namespace LibHac.Fs
public GlobalAccessLogMode GlobalAccessLogMode;
public AccessLogTarget LocalAccessLogTarget;
public bool IsServerless;
public bool IsAccessLogInitialized;
public SdkMutexType MutexForAccessLogInitialization;
@ -55,6 +56,13 @@ namespace LibHac.Fs
{
public static Result GetGlobalAccessLogMode(this FileSystemClient fs, out GlobalAccessLogMode mode)
{
// Allow the access log to be used without an FS server by storing the mode locally in that situation.
if (fs.Globals.AccessLog.IsServerless)
{
mode = fs.Globals.AccessLog.GlobalAccessLogMode;
return Result.Success;
}
using ReferenceCountedDisposable<IFileSystemProxy> fsProxy = fs.Impl.GetFileSystemProxyServiceObject();
Result rc = fsProxy.Target.GetGlobalAccessLogMode(out mode);
@ -64,6 +72,13 @@ namespace LibHac.Fs
public static Result SetGlobalAccessLogMode(this FileSystemClient fs, GlobalAccessLogMode mode)
{
// Allow the access log to be used without an FS server by storing the mode locally in that situation.
if (fs.Globals.AccessLog.IsServerless)
{
fs.Globals.AccessLog.GlobalAccessLogMode = mode;
return Result.Success;
}
using ReferenceCountedDisposable<IFileSystemProxy> fsProxy = fs.Impl.GetFileSystemProxyServiceObject();
Result rc = fsProxy.Target.SetGlobalAccessLogMode(mode);
@ -93,6 +108,17 @@ namespace LibHac.Fs
}
}
/// <summary>
/// Sets whether the FS access log should call the FS service when getting or setting the
/// global access log mode. This allows the access log to be used when using an FS client without a server.
/// </summary>
/// <param name="fs">The <see cref="FileSystemClient"/> to use.</param>
/// <param name="isServerless">Does this client lack an FS server?</param>
public static void SetServerlessAccessLog(this FileSystemClient fs, bool isServerless)
{
fs.Globals.AccessLog.IsServerless = isServerless;
}
private static void SetLocalAccessLogImpl(FileSystemClient fs, bool enabled)
{
if (enabled)

View file

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using LibHac.Common;
using LibHac.Fs;
@ -181,7 +182,7 @@ namespace LibHac.FsSystem
storage.GetSize(out long storageSize).ThrowIfFailure();
var arr = new T[storageSize / Marshal.SizeOf<T>()];
var arr = new T[storageSize / Unsafe.SizeOf<T>()];
Span<byte> dest = MemoryMarshal.Cast<T, byte>(arr.AsSpan());
storage.Read(0, dest);

View file

@ -181,7 +181,7 @@ namespace LibHac.Os.Impl
{
using ScopedLock<InternalCriticalSection> lk = ScopedLock.Lock(ref GetLockCount(ref rwLock).Cs);
Assert.SdkEqual(GetWriteLockCount(in rwLock), 0u);
Assert.SdkRequiresGreater(GetWriteLockCount(in rwLock), 0u);
Assert.SdkNotEqual(GetWriteLocked(in GetLockCount(ref rwLock)), 0u);
Assert.SdkEqual(rwLock.OwnerThread, Environment.CurrentManagedThreadId);

View file

@ -53,6 +53,9 @@ namespace hactoolnet
fs.Register(mountName.ToU8Span(), OpenFileSystem(i));
fs.Register("output".ToU8Span(), new LocalFileSystem(ctx.Options.SectionOutDir[i]));
fs.Impl.EnableFileSystemAccessorAccessLog(mountName.ToU8Span());
fs.Impl.EnableFileSystemAccessorAccessLog("output".ToU8Span());
FsUtils.CopyDirectoryWithProgress(fs, (mountName + ":/").ToU8Span(), "output:/".ToU8Span(), logger: ctx.Logger).ThrowIfFailure();
fs.Unmount(mountName.ToU8Span());
@ -162,6 +165,9 @@ namespace hactoolnet
fs.Register("code".ToU8Span(), OpenFileSystemByType(NcaSectionType.Code));
fs.Register("output".ToU8Span(), new LocalFileSystem(ctx.Options.ExefsOutDir));
fs.Impl.EnableFileSystemAccessorAccessLog("code".ToU8Span());
fs.Impl.EnableFileSystemAccessorAccessLog("output".ToU8Span());
FsUtils.CopyDirectoryWithProgress(fs, "code:/".ToU8Span(), "output:/".ToU8Span(), logger: ctx.Logger).ThrowIfFailure();
fs.Unmount("code".ToU8Span());

View file

@ -8,6 +8,7 @@ using LibHac.Common;
using LibHac.Common.Keys;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.Fs.Impl;
using LibHac.FsSystem;
using LibHac.FsSystem.Save;
using static hactoolnet.Print;
@ -34,6 +35,7 @@ namespace hactoolnet
FileSystemClient fs = ctx.Horizon.Fs;
fs.Register("save".ToU8Span(), save);
fs.Impl.EnableFileSystemAccessorAccessLog("save".ToU8Span());
if (ctx.Options.Validate)
{
@ -43,6 +45,7 @@ namespace hactoolnet
if (ctx.Options.OutDir != null)
{
fs.Register("output".ToU8Span(), new LocalFileSystem(ctx.Options.OutDir));
fs.Impl.EnableFileSystemAccessorAccessLog("output".ToU8Span());
FsUtils.CopyDirectoryWithProgress(fs, "save:/".ToU8Span(), "output:/".ToU8Span(), logger: ctx.Logger).ThrowIfFailure();
@ -89,6 +92,7 @@ namespace hactoolnet
if (ctx.Options.RepackSource != null)
{
fs.Register("input".ToU8Span(), new LocalFileSystem(ctx.Options.RepackSource));
fs.Impl.EnableFileSystemAccessorAccessLog("input".ToU8Span());
fs.CleanDirectoryRecursively("save:/".ToU8Span());
fs.Commit("save".ToU8Span());

View file

@ -70,10 +70,7 @@ namespace hactoolnet
{
ctx.Logger = logger;
OpenKeySet(ctx);
Horizon horizon = HorizonFactory.CreateWithDefaultFsConfig(new HorizonConfiguration(),
new InMemoryFileSystem(), ctx.KeySet);
ctx.Horizon = horizon.CreatePrivilegedHorizonClient();
InitializeHorizon(ctx);
if (ctx.Options.AccessLog != null)
{
@ -247,6 +244,13 @@ namespace hactoolnet
ctx.KeySet = keySet;
}
private static void InitializeHorizon(Context ctx)
{
var horizon = new Horizon(new HorizonConfiguration());
ctx.Horizon = horizon.CreatePrivilegedHorizonClient();
ctx.Horizon.Fs.SetServerlessAccessLog(true);
}
private static void ProcessKeygen(Context ctx)
{
Console.WriteLine(ExternalKeyWriter.PrintAllKeys(ctx.KeySet));