From 2c4004dc5a407f203184aded33318698fc5a1e67 Mon Sep 17 00:00:00 2001 From: Berserker Date: Mon, 27 May 2019 01:14:09 +0300 Subject: [PATCH] Improved current directory handle detection using hack of double SetCurrentDirectoryW for different paths --- Vfs.dproj | 18 +----------------- Vfs.res | Bin 5160 -> 5160 bytes VfsControl.pas | 12 +++++++++--- VfsHooks.pas | 6 +++++- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/Vfs.dproj b/Vfs.dproj index 8963cd2..cbaa36b 100644 --- a/Vfs.dproj +++ b/Vfs.dproj @@ -30,23 +30,7 @@ Delphi.Personality VCLApplication -FalseTrueFalseTrueTrue1000FalseFalseFalseFalseFalse10491251Alexander Shostak (aka EtherniDee)Virtual File System1.0.0.01.0.0.0Vfs.dpr - - - - - - - - - - - - - - - - +FalseTrueFalseTrueTrue1010FalseFalseFalseFalseFalse10491251Alexander Shostak (aka EtherniDee)Virtual File System1.0.1.01.0.0.0Vfs.dpr Microsoft Office 2000 Sample Automation Server Wrapper Components diff --git a/Vfs.res b/Vfs.res index 2670d72fd34b4f9221a4ef3a2a98fb58793705d5..9d145b482bf4a5a13d99b4be6d5d1bd1da592c99 100644 GIT binary patch delta 29 hcmZ3Xu|i`*lOQJ}0|NsXZ|)YfWMnkld{iiz5ddcT2AKc= delta 29 hcmZ3Xu|i`*lOQJp5HNy>&E0~QjEn}Gj|wF-0sv;x29^K- diff --git a/VfsControl.pas b/VfsControl.pas index 7335b52..6b83f5d 100644 --- a/VfsControl.pas +++ b/VfsControl.pas @@ -37,6 +37,7 @@ type function RunVfs (DirListingOrder: VfsBase.TDirListingSortType): LONGBOOL; stdcall; var CurrDir: WideString; + SysDir: WideString; begin with VfsBase.VfsCritSection do begin @@ -47,11 +48,16 @@ begin if result then begin VfsHooks.InstallHooks; - // Try to ensure, that current directory handle is tracked by VfsOpenFiles + // Hask: Try to ensure, that current directory handle is tracked by VfsOpenFiles + // Windows SetCurrentDirectoryW is does not reopen directory for the same path, thus + // not triggering NtCreateFile + // Not thread safe CurrDir := WinUtils.GetCurrentDirW; + SysDir := WinUtils.GetSysDirW; - if CurrDir <> '' then begin - WinUtils.SetCurrentDirW(CurrDir); + if (CurrDir <> '') and (SysDir <> '') then begin + WinUtils.SetCurrentDirW(SysDir); + {!} Assert(WinUtils.SetCurrentDirW(CurrDir), 'Failed to restore current directory from system directory during VFS initialization'); end; end; diff --git a/VfsHooks.pas b/VfsHooks.pas index 46b3c61..44a8a66 100644 --- a/VfsHooks.pas +++ b/VfsHooks.pas @@ -9,7 +9,7 @@ unit VfsHooks; uses Windows, SysUtils, Math, Utils, WinNative, Concur, - StrLib, Alg, + StrLib, Alg, WinUtils, VfsBase, VfsUtils, VfsPatching, VfsDebug, VfsApiDigger, VfsOpenFiles; @@ -48,6 +48,10 @@ var function GetFilePathByHandle (hFile: THandle): WideString; begin result := VfsOpenFiles.GetOpenedFilePath(hFile); + + if (result = '') and VfsDebug.LoggingEnabled then begin + WriteLog('GetFilePathByHandle', Format('Failed to get path for handle %x. Current directory is: %s', [integer(hFile), WinUtils.GetCurrentDirW])); + end; end; (* Returns single absolute path, not dependant on RootDirectory member. '\??\' prefix is always removed, \\.\ and \\?\ paths remain not touched. *)