Skip to content

Commit

Permalink
Merge pull request #13 from Al12rs/file-in-use
Browse files Browse the repository at this point in the history
Fix for WB file in use and xEdit renaming fail.
  • Loading branch information
Al12rs authored Jun 29, 2019
2 parents 911d3f4 + 3b2a004 commit 91a8de6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/shared/directory_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,14 @@ class DirectoryTree
if (auto par = parent()) {
spdlog::get("usvfs")->info("remove from tree {}", m_Name.c_str());
auto self = par->m_Nodes.find(m_Name.c_str());
par->erase(self);
if (self != par->m_Nodes.end()) {
par->erase(self);
}
else {
//trying to remove a node that des not exist, most likely because it was already removed in a lower level call.
//this is known to happen when MoveFile has the MOVEFILE_COPY_ALLOWED flag and moving a mapped file.
spdlog::get("usvfs")->warn("Failed to remove inexisting node from tree: {}", m_Name.c_str());
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/usvfs_dll/hooks/kernel32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ HMODULE WINAPI usvfs::hook_LoadLibraryExW(LPCWSTR lpFileName, HANDLE hFile,
HMODULE res = nullptr;

HOOK_START_GROUP(MutExHookGroup::LOAD_LIBRARY)
// Why is the usual if (!callContext.active()... check missing?

RerouteW reroute = RerouteW::create(READ_CONTEXT(), callContext, lpFileName);
PRE_REALCALL
Expand Down Expand Up @@ -488,6 +489,7 @@ DWORD WINAPI usvfs::hook_SetFileAttributesW(
DWORD res = 0UL;

HOOK_START_GROUP(MutExHookGroup::FILE_ATTRIBUTES)
// Why is the usual if (!callContext.active()... check missing?

RerouteW reroute = RerouteW::create(READ_CONTEXT(), callContext, lpFileName);
PRE_REALCALL
Expand All @@ -508,6 +510,7 @@ BOOL WINAPI usvfs::hook_DeleteFileW(LPCWSTR lpFileName)
BOOL res = FALSE;

HOOK_START_GROUP(MutExHookGroup::DELETE_FILE)
// Why is the usual if (!callContext.active()... check missing?

RerouteW reroute = RerouteW::create(READ_CONTEXT(), callContext, lpFileName);

Expand Down Expand Up @@ -800,6 +803,9 @@ BOOL WINAPI usvfs::hook_MoveFileWithProgressA(LPCSTR lpExistingFileName, LPCSTR

BOOL WINAPI usvfs::hook_MoveFileWithProgressW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, LPPROGRESS_ROUTINE lpProgressRoutine, LPVOID lpData, DWORD dwFlags)
{

//TODO: Remove all redundant hooks to moveFile alternatives.
//it would appear that all other moveFile functions end up calling this one with no additional code.
BOOL res = FALSE;

HOOK_START_GROUP(MutExHookGroup::SHELL_FILEOP)
Expand Down Expand Up @@ -861,7 +867,9 @@ BOOL WINAPI usvfs::hook_MoveFileWithProgressW(LPCWSTR lpExistingFileName, LPCWST
writeReroute.updateResult(callContext, res);

if (res) {
readReroute.removeMapping(READ_CONTEXT(), isDirectory); // Updating the rerouteCreate to check deleted file entries should make this okay
//TODO: this call causes the node to be removed twice in case of MOVEFILE_COPY_ALLOWED as the deleteFile hook lower level already takes care of it,
//but deleteFile can't be disabled since we are relying on it in case of MOVEFILE_REPLACE_EXISTING for the destination file.
readReroute.removeMapping(READ_CONTEXT(), isDirectory); // Updating the rerouteCreate to check deleted file entries should make this okay (not related to comments above)

if (writeReroute.newReroute()) {
if (isDirectory)
Expand Down Expand Up @@ -1094,6 +1102,7 @@ DLLEXPORT BOOL WINAPI usvfs::hook_RemoveDirectoryW(
BOOL res = FALSE;

HOOK_START_GROUP(MutExHookGroup::DELETE_FILE)
// Why is the usual if (!callContext.active()... check missing?

RerouteW reroute = RerouteW::create(READ_CONTEXT(), callContext, lpPathName);

Expand Down
6 changes: 6 additions & 0 deletions src/usvfs_dll/hooks/ntdll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@ NTSTATUS ntdll_mess_NtOpenFile(PHANDLE FileHandle,
NTSTATUS res = STATUS_NO_SUCH_FILE;

HOOK_START_GROUP(MutExHookGroup::OPEN_FILE)
// Why is the usual if (!callContext.active()... check missing?

bool storePath = false;
if (((OpenOptions & FILE_DIRECTORY_FILE) != 0UL)
Expand Down Expand Up @@ -1299,6 +1300,10 @@ NTSTATUS WINAPI usvfs::hook_NtClose(HANDLE Handle)
// std::lock_guard<std::recursive_mutex> lock(activeSearches.queryMutex);
auto iter = activeSearches.info.find(Handle);
if (iter != activeSearches.info.end()) {
if (iter->second.currentSearchHandle != INVALID_HANDLE_VALUE) {
::CloseHandle(iter->second.currentSearchHandle);
}

activeSearches.info.erase(iter);
log = true;
}
Expand Down Expand Up @@ -1340,6 +1345,7 @@ NTSTATUS WINAPI usvfs::hook_NtQueryAttributesFile(
NTSTATUS res = STATUS_SUCCESS;

HOOK_START_GROUP(MutExHookGroup::FILE_ATTRIBUTES)
// Why is the usual if (!callContext.active()... check missing?

UnicodeString inPath = CreateUnicodeString(ObjectAttributes);

Expand Down

0 comments on commit 91a8de6

Please sign in to comment.