ProxyCacheManagerのStartProxyGenerationにおいてContainsKeyとTryAddの間に存在したTOCTOU競合を除去し、TryAddの戻り値のみで重複判断するよう修正しました。 RunGenerationAsyncで3箇所に重複していたActiveGenerations.Remove呼び出しをループ後の単一パスに集約し、delayMs変数で各ケースの遅延時間を管理するよう統一しました。 FallbackStreamのWriteメソッドで_inner.Writeの呼び出しを_switchLockの内側に移動し、SwitchToDiskCoreをロック内から呼ぶ内部メソッドとして分離することでWriteとSwitchToDiskの競合を解消しました。 ProxyCacheEntryのSetMemoryDataおよびSetDiskPathにおいて旧フィールドをクリアしてから新フィールドを設定する順序に変更し、IsValidがどちらもnullという一貫性のない状態を観測しないよう保証しました。 MemoryBudgetのTryAllocateにおいてGetAvailablePhysicalMemoryの呼び出しをCASループ外からループ内に移動し、ループを複数回回る間に物理メモリの実態が変化しても常に最新の値で判断するよう修正しました。 ProxyCacheManagerのMakeCacheKeyを文字列補間からstring.Create+stackallocベースに書き換え、中間文字列アロケーションを排除しました。 SettingsViewModelとCacheEntryViewModelに重複していたFormatBytesをUtilities/ByteFormatterとして一元化し、stackallocとstring.Createを組み合わせることでアロケーションをゼロに抑えました。 IDialogServiceインターフェースとWpfDialogService実装を新規追加し、SettingsViewModelが直接呼び出していたSystem.Windows.MessageBox.Showをインターフェース経由に変更しました。 SettingsViewModelのコンストラクタをProxyCacheManagerとMemoryBudgetを直接受け取る形に変更し、PluginHost.Instanceへの直接依存を除去しました。 PluginHost.Instanceの解決はZeroDiskProxySettingsView.xaml.csのコンポジションルートで行うよう責務を整理しました。 GenerationPopupViewのOnItemsChangedからShow・Hideの呼び出しを削除し、ウィンドウ可視性の管理をPluginHost.OnGenerationsChangedに一元化しました。 MfProxyEncoderにValidateInputPathとValidateTempPathを追加し、パストラバーサル攻撃を防止しました。 ValidateTempPathはstackallocとSpan<char>を用いてゼロアロケーションで_fallbackDirectory配下であることを検証します。 ExportDetectorのウィンドウタイトルバッファを256文字から512文字に拡大し、長いウィンドウタイトルによる切り捨てを防止しました。 MfProxyEncoder.AlignTo16をprivate staticからinternal staticに変更しテスト可能にしました。 WinRT APIへの移行により不要になったMfGuids内の全GUIDを削除し、実際に参照されているMF_MT_MAX_KEYFRAME_SPACINGのみを残しました。
139 行
4.3 KiB
C#
139 行
4.3 KiB
C#
using System.Diagnostics;
|
|
using System.IO;
|
|
|
|
namespace ZeroDiskProxy.Core;
|
|
|
|
internal sealed class ProxyCacheEntry : IDisposable
|
|
{
|
|
private byte[]? _memoryData;
|
|
private string? _diskPath;
|
|
private long _cachedSize;
|
|
private long _lastAccessedTicks = DateTime.UtcNow.Ticks;
|
|
private readonly object _diskWriteLock = new();
|
|
private int _disposed;
|
|
|
|
internal string OriginalPath { get; }
|
|
internal float Scale { get; }
|
|
internal uint ProxyWidth { get; init; }
|
|
internal uint ProxyHeight { get; init; }
|
|
internal bool IsInMemory => Volatile.Read(ref _memoryData) is not null;
|
|
internal bool IsValid => Volatile.Read(ref _disposed) == 0 && (Volatile.Read(ref _memoryData) is not null || Volatile.Read(ref _diskPath) is not null);
|
|
internal long DataSize => Interlocked.Read(ref _cachedSize);
|
|
internal DateTime CreatedAt { get; } = DateTime.UtcNow;
|
|
internal DateTime LastAccessedAt => new(Interlocked.Read(ref _lastAccessedTicks));
|
|
|
|
internal ProxyCacheEntry(string originalPath, float scale)
|
|
{
|
|
OriginalPath = originalPath;
|
|
Scale = scale;
|
|
}
|
|
|
|
internal void SetMemoryData(byte[] data)
|
|
{
|
|
Interlocked.Exchange(ref _cachedSize, data.LongLength);
|
|
Volatile.Write(ref _diskPath, null);
|
|
Volatile.Write(ref _memoryData, data);
|
|
UpdateLastAccess();
|
|
}
|
|
|
|
internal void SetDiskPath(string path, long size)
|
|
{
|
|
Interlocked.Exchange(ref _cachedSize, size);
|
|
Volatile.Write(ref _memoryData, null);
|
|
Volatile.Write(ref _diskPath, path);
|
|
UpdateLastAccess();
|
|
}
|
|
|
|
internal Stream OpenReadStream()
|
|
{
|
|
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
|
UpdateLastAccess();
|
|
|
|
var mem = Volatile.Read(ref _memoryData);
|
|
if (mem is not null)
|
|
return new MemoryStream(mem, writable: false);
|
|
|
|
var disk = Volatile.Read(ref _diskPath);
|
|
if (disk is not null && File.Exists(disk))
|
|
return new FileStream(disk, FileMode.Open, FileAccess.Read, FileShare.Read, 65536, FileOptions.SequentialScan);
|
|
|
|
throw new InvalidOperationException("Proxy data unavailable");
|
|
}
|
|
|
|
internal string GetOrCreateTempFilePath(string tempDirectory)
|
|
{
|
|
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
|
UpdateLastAccess();
|
|
|
|
var disk = Volatile.Read(ref _diskPath);
|
|
if (disk is not null && File.Exists(disk))
|
|
return disk;
|
|
|
|
lock (_diskWriteLock)
|
|
{
|
|
disk = Volatile.Read(ref _diskPath);
|
|
if (disk is not null && File.Exists(disk))
|
|
return disk;
|
|
|
|
var mem = Volatile.Read(ref _memoryData);
|
|
if (mem is null)
|
|
throw new InvalidOperationException("Proxy data unavailable");
|
|
|
|
Directory.CreateDirectory(tempDirectory);
|
|
var tempPath = Path.Combine(tempDirectory, string.Concat("zdp_read_", Guid.NewGuid().ToString("N"), ".mp4"));
|
|
File.WriteAllBytes(tempPath, mem);
|
|
Volatile.Write(ref _diskPath, tempPath);
|
|
return tempPath;
|
|
}
|
|
}
|
|
|
|
internal CacheEntrySnapshot CreateSnapshot()
|
|
{
|
|
return new CacheEntrySnapshot(
|
|
OriginalPath,
|
|
Path.GetFileName(OriginalPath),
|
|
Scale,
|
|
ProxyWidth,
|
|
ProxyHeight,
|
|
IsInMemory,
|
|
DataSize,
|
|
CreatedAt,
|
|
LastAccessedAt);
|
|
}
|
|
|
|
private void UpdateLastAccess() =>
|
|
Interlocked.Exchange(ref _lastAccessedTicks, DateTime.UtcNow.Ticks);
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
|
return;
|
|
|
|
Volatile.Write(ref _memoryData, null);
|
|
var disk = Interlocked.Exchange(ref _diskPath, null);
|
|
if (disk is null)
|
|
return;
|
|
|
|
try
|
|
{
|
|
if (File.Exists(disk))
|
|
File.Delete(disk);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(string.Concat("[ProxyCacheEntry] Delete failed: ", ex.Message));
|
|
}
|
|
}
|
|
}
|
|
|
|
internal readonly record struct CacheEntrySnapshot(
|
|
string OriginalPath,
|
|
string FileName,
|
|
float Scale,
|
|
uint ProxyWidth,
|
|
uint ProxyHeight,
|
|
bool IsInMemory,
|
|
long DataSize,
|
|
DateTime CreatedAt,
|
|
DateTime LastAccessedAt
|
|
); |