refactory

This commit is contained in:
DavidXanatos 2024-04-28 16:36:37 +02:00
parent bda087c1a6
commit 4a8650ca9d
5 changed files with 103 additions and 71 deletions

View File

@ -118,6 +118,9 @@ SBIEDLL_EXPORT NTSTATUS File_GetName(
HANDLE RootDirectory, UNICODE_STRING *ObjectName,
WCHAR **OutTruePath, WCHAR **OutCopyPath, ULONG *OutFlags);
static WCHAR *File_TranslateDosToNtPath2(
const WCHAR *DosPath, ULONG DosPathLen);
static WCHAR *File_GetName_TranslateSymlinks(
THREAD_DATA *TlsData, const WCHAR *objname_buf, ULONG objname_len,
BOOLEAN *translated);
@ -262,6 +265,9 @@ static NTSTATUS File_NtDeleteFile(OBJECT_ATTRIBUTES *ObjectAttributes);
static NTSTATUS File_NtDeleteFileImpl(OBJECT_ATTRIBUTES *ObjectAttributes);
static WCHAR* File_CanonizePath(
const wchar_t* absolute_path, ULONG abs_path_len, const wchar_t* relative_path, ULONG rel_path_len);
static NTSTATUS File_OpenForRenameFile(
HANDLE* pSourceHandle, const WCHAR *TruePath);

View File

@ -549,16 +549,16 @@ _FX VOID File_SavePathTree_internal(LIST* Root, const WCHAR* name, WCHAR* (*Tran
//---------------------------------------------------------------------------
// File_TranslateNtToDosPath2
// File_TranslateNtToDosPathForDatFile
//---------------------------------------------------------------------------
_FX WCHAR* File_TranslateNtToDosPath2(const WCHAR *NtPath)
_FX WCHAR* File_TranslateNtToDosPathForDatFile(const WCHAR *NtPath)
{
WCHAR *DosPath = NULL;
ULONG len_nt;
len_nt = wcslen(NtPath) + 16;
len_nt = wcslen(NtPath) + 11;
DosPath = Dll_Alloc(len_nt * sizeof(WCHAR));
wcscpy(DosPath, NtPath);
@ -635,7 +635,7 @@ _FX BOOLEAN File_SavePathTree()
{
EnterCriticalSection(File_PathRoot_CritSec);
File_SavePathTree_internal(&File_PathRoot, FILE_PATH_FILE_NAME, File_TranslateNtToDosPath2);
File_SavePathTree_internal(&File_PathRoot, FILE_PATH_FILE_NAME, File_TranslateNtToDosPathForDatFile);
File_GetAttributes_internal(FILE_PATH_FILE_NAME, &File_PathsFileSize, &File_PathsFileDate, NULL);
@ -764,11 +764,11 @@ _FX BOOLEAN File_LoadPathTree_internal(LIST* Root, const WCHAR* name, WCHAR* (*T
//---------------------------------------------------------------------------
// File_TranslateDosToNtPath2
// File_TranslateDosToNtPathForDatFile
//---------------------------------------------------------------------------
_FX WCHAR *File_TranslateDosToNtPath2(const WCHAR *DosPath)
_FX WCHAR *File_TranslateDosToNtPathForDatFile(const WCHAR *DosPath)
{
WCHAR *NtPath = NULL;
ULONG len_dos;
@ -841,7 +841,7 @@ _FX BOOLEAN File_LoadPathTree()
EnterCriticalSection(File_PathRoot_CritSec);
File_LoadPathTree_internal(&File_PathRoot, FILE_PATH_FILE_NAME, File_TranslateDosToNtPath2);
File_LoadPathTree_internal(&File_PathRoot, FILE_PATH_FILE_NAME, File_TranslateDosToNtPathForDatFile);
LeaveCriticalSection(File_PathRoot_CritSec);
@ -1038,7 +1038,7 @@ _FX NTSTATUS File_MarkDeleted_v2(const WCHAR* TruePath)
HANDLE hPathsFile;
if (File_OpenDataFile(FILE_PATH_FILE_NAME, &hPathsFile, TRUE))
{
File_AppendPathEntry_internal(hPathsFile, Path, FILE_DELETED_FLAG, NULL, File_TranslateNtToDosPath2);
File_AppendPathEntry_internal(hPathsFile, Path, FILE_DELETED_FLAG, NULL, File_TranslateNtToDosPathForDatFile);
NtClose(hPathsFile);

View File

@ -3353,17 +3353,15 @@ _FX NTSTATUS File_SetReparsePoint(
//TargetPath = Dll_Alloc((wcslen(CopyPath) + 4) * sizeof(WCHAR));
//wcscpy(TargetPath, CopyPath);
WCHAR* NewSubstituteNameBuffer = NULL;
WCHAR* NewSubstituteNameBuffer = CopyPath;
WCHAR* OldPrintNameBuffer = PrintNameBuffer; // we don't need to change the display name
if (Data->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
NewSubstituteNameBuffer = File_TranslateNtToDosPath2(CopyPath);
memmove(NewSubstituteNameBuffer + 4, NewSubstituteNameBuffer, (wcslen(NewSubstituteNameBuffer) + 1) * sizeof(wchar_t)); // File_TranslateNtToDosPath2 alocates 16 extry chars for such cases
SbieDll_TranslateNtToDosPath(NewSubstituteNameBuffer);
memmove(NewSubstituteNameBuffer + 4, NewSubstituteNameBuffer, (wcslen(NewSubstituteNameBuffer) + 1) * sizeof(wchar_t));
wcsncpy(NewSubstituteNameBuffer, L"\\??\\", 4);
} else
NewSubstituteNameBuffer = CopyPath;
}
SubstituteNameLength = wcslen(NewSubstituteNameBuffer) * sizeof(WCHAR);
@ -3398,9 +3396,6 @@ _FX NTSTATUS File_SetReparsePoint(
memcpy(SubstituteNameBuffer, NewSubstituteNameBuffer, SubstituteNameLength + sizeof(WCHAR));
memcpy(PrintNameBuffer, OldPrintNameBuffer, PrintNameLength + sizeof(WCHAR));
if (Data->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Dll_Free(NewSubstituteNameBuffer);
} __except (EXCEPTION_EXECUTE_HANDLER) {
status = GetExceptionCode();
}

View File

@ -1665,14 +1665,29 @@ _FX WCHAR *File_AllocAndInitEnvironment_2(
//---------------------------------------------------------------------------
// File_TranslateDosToNtPath
// File_ConcatPath2
//---------------------------------------------------------------------------
_FX WCHAR *File_TranslateDosToNtPath(const WCHAR *DosPath)
_FX WCHAR *File_ConcatPath2(const WCHAR *Path1, ULONG Path1Len, const WCHAR *Path2, ULONG Path2Len)
{
ULONG len_dos = Path1Len + Path2Len;
WCHAR* Path = Dll_Alloc((len_dos + 1) * sizeof(WCHAR));
wmemcpy(Path, Path1, Path1Len);
wmemcpy(Path + Path1Len, Path2, Path2Len);
Path[len_dos] = L'\0';
return Path;
}
//---------------------------------------------------------------------------
// File_TranslateDosToNtPath2
//---------------------------------------------------------------------------
_FX WCHAR *File_TranslateDosToNtPath2(const WCHAR *DosPath, ULONG DosPathLen)
{
WCHAR *NtPath = NULL;
ULONG len_dos;
if (DosPath && DosPath[0] && DosPath[1]) {
@ -1682,11 +1697,7 @@ _FX WCHAR *File_TranslateDosToNtPath(const WCHAR *DosPath)
// network path
//
DosPath += 2;
len_dos = wcslen(DosPath) + 1;
NtPath = Dll_Alloc((File_MupLen + len_dos) * sizeof(WCHAR));
wmemcpy(NtPath, File_Mup, File_MupLen);
wmemcpy(NtPath + File_MupLen, DosPath, len_dos);
NtPath = File_ConcatPath2(File_Mup, File_MupLen, DosPath + 2, DosPathLen - 2);
} else if (DosPath[1] == L':' &&
(DosPath[2] == L'\\' || DosPath[2] == L'\0')) {
@ -1698,11 +1709,7 @@ _FX WCHAR *File_TranslateDosToNtPath(const WCHAR *DosPath)
FILE_DRIVE *drive = File_GetDriveForLetter(DosPath[0]);
if (drive) {
DosPath += 2;
len_dos = wcslen(DosPath) + 1;
NtPath = Dll_Alloc((drive->len + len_dos) * sizeof(WCHAR));
wmemcpy(NtPath, drive->path, drive->len);
wmemcpy(NtPath + drive->len, DosPath, len_dos);
NtPath = File_ConcatPath2(drive->path, drive->len, DosPath + 2, DosPathLen - 2);
LeaveCriticalSection(File_DrivesAndLinks_CritSec);
}
@ -1713,6 +1720,17 @@ _FX WCHAR *File_TranslateDosToNtPath(const WCHAR *DosPath)
}
//---------------------------------------------------------------------------
// File_TranslateDosToNtPath
//---------------------------------------------------------------------------
_FX WCHAR *File_TranslateDosToNtPath(const WCHAR *DosPath)
{
return File_TranslateDosToNtPath2(DosPath, DosPath ? wcslen(DosPath) : 0);
}
//---------------------------------------------------------------------------
// File_GetSetDeviceMap
//---------------------------------------------------------------------------

View File

@ -323,34 +323,45 @@ _FX FILE_GUID *File_GetLinkForGuid(const WCHAR* guid_str)
//---------------------------------------------------------------------------
// File_TranslateGuidToNtPath
// File_TranslateGuidToNtPath2
//---------------------------------------------------------------------------
_FX WCHAR* File_TranslateGuidToNtPath(const WCHAR* input_str)
_FX WCHAR* File_TranslateGuidToNtPath2(const WCHAR* GuidPath, ULONG GuidPathLen)
{
ULONG len;
WCHAR* NtPath;
if (_wcsnicmp(input_str, L"\\??\\Volume{", 11) != 0)
return NULL;
if (GuidPath && GuidPathLen >= 48 && _wcsnicmp(GuidPath, L"\\??\\Volume{", 11) == 0) {
FILE_GUID* guid = File_GetLinkForGuid(&input_str[10]);
if (guid) {
//
// guid path
//
input_str += 48;
len = wcslen(input_str) + 1;
NtPath = Dll_Alloc((guid->len + len) * sizeof(WCHAR));
wmemcpy(NtPath, guid->path, guid->len);
wmemcpy(NtPath + guid->len, input_str, len);
FILE_GUID* guid = File_GetLinkForGuid(&GuidPath[10]);
if (guid) {
LeaveCriticalSection(File_DrivesAndLinks_CritSec);
File_ConcatPath2(guid->path + 48, guid->len - 48, GuidPath, GuidPathLen);
return NtPath;
LeaveCriticalSection(File_DrivesAndLinks_CritSec);
}
}
return NULL;
return NtPath;
}
//---------------------------------------------------------------------------
// File_TranslateGuidToNtPath
//---------------------------------------------------------------------------
_FX WCHAR* File_TranslateGuidToNtPath(const WCHAR* GuidPath)
{
return File_TranslateGuidToNtPath2(GuidPath, GuidPath ? wcslen(GuidPath) : 0);
}
//---------------------------------------------------------------------------
// File_AddLink
//---------------------------------------------------------------------------
@ -1036,47 +1047,49 @@ _FX FILE_LINK *File_AddTempLink(WCHAR *path)
if (NT_SUCCESS(status)) {
WCHAR* SubstituteNameBuffer = NULL;
//WCHAR* PrintNameBuffer = NULL;
ULONG SubstituteNameLength = 0;
BOOL RelativePath = FALSE;
if (reparseDataBuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK)
{
if (reparseDataBuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
SubstituteNameBuffer = &reparseDataBuffer->SymbolicLinkReparseBuffer.PathBuffer[reparseDataBuffer->SymbolicLinkReparseBuffer.SubstituteNameOffset/sizeof(WCHAR)];
if (reparseDataBuffer->SymbolicLinkReparseBuffer.Flags & SYMLINK_FLAG_RELATIVE)
RelativePath = TRUE;
SubstituteNameBuffer[reparseDataBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(WCHAR)] = 0;
}
else if (reparseDataBuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
{
SubstituteNameLength = reparseDataBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength;
} else if (reparseDataBuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
SubstituteNameBuffer = &reparseDataBuffer->MountPointReparseBuffer.PathBuffer[reparseDataBuffer->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR)];
SubstituteNameBuffer[reparseDataBuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR)] = 0;
SubstituteNameLength = reparseDataBuffer->MountPointReparseBuffer.SubstituteNameLength;
}
if (SubstituteNameBuffer)
{
if (RelativePath)
{
// todo RelativePath - for now we fall back to the old method
}
else
{
WCHAR* input_str = SubstituteNameBuffer;
if (SubstituteNameBuffer) {
WCHAR* input_str = NULL;
if (RelativePath) {
WCHAR* LinkName = wcsrchr(path, L'\\');
input_str = File_CanonizePath(path, (ULONG)(LinkName - path), SubstituteNameBuffer, SubstituteNameLength / sizeof(WCHAR));
} else {
input_str = SubstituteNameBuffer;
if (_wcsnicmp(input_str, L"\\??\\Volume{", 11) == 0)
input_str = File_TranslateGuidToNtPath(SubstituteNameBuffer);
input_str = File_TranslateGuidToNtPath2(SubstituteNameBuffer, SubstituteNameLength / sizeof(WCHAR));
else if (_wcsnicmp(input_str, File_BQQB, 4) == 0)
input_str = File_TranslateDosToNtPath(SubstituteNameBuffer + 4);
input_str = File_TranslateDosToNtPath2(SubstituteNameBuffer + 4, SubstituteNameLength / sizeof(WCHAR));
}
if (input_str) {
if (input_str) {
ULONG input_len = wcslen(input_str);
while (input_len > 0 && input_str[input_len - 1] == L'\\')
input_len -= 1; // remove trailing backslash
ULONG input_len = wcslen(input_str);
while (input_len > 0 && input_str[input_len - 1] == L'\\')
input_len -= 1; // remove trailing backslash
newpath = File_TranslateTempLinks_2(input_str, input_len);
newpath = File_TranslateTempLinks_2(input_str, input_len);
if (input_str != SubstituteNameBuffer)
Dll_Free(input_str);
}
if (input_str != SubstituteNameBuffer)
Dll_Free(input_str);
}
}
}