mirror of
https://github.com/reactos/reactos.git
synced 2024-10-01 23:14:53 +00:00
[KERNEL32]
Simplify overcomplicated function CreateHardlinkW(), and SEHify it. svn path=/trunk/; revision=53823
This commit is contained in:
parent
2c440bd22d
commit
acee43d271
|
@ -2,11 +2,10 @@
|
||||||
*
|
*
|
||||||
* COPYRIGHT: See COPYING in the top level directory
|
* COPYRIGHT: See COPYING in the top level directory
|
||||||
* PROJECT: ReactOS system libraries
|
* PROJECT: ReactOS system libraries
|
||||||
* FILE: lib/kernel32/file/hardlink.c
|
* FILE: dll/win32/kernel32/client/file/hardlink.c
|
||||||
* PURPOSE: Hardlink functions
|
* PURPOSE: Hardlink functions
|
||||||
* PROGRAMMER: Thomas Weidenmueller (w3seek@users.sourceforge.net)
|
* PROGRAMMER: Thomas Weidenmueller (w3seek@users.sourceforge.net)
|
||||||
* UPDATE HISTORY:
|
* Pierre Schweitzer (pierre.schweitzer@reactos.org)
|
||||||
* Created 13/03/2004
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* INCLUDES *****************************************************************/
|
/* INCLUDES *****************************************************************/
|
||||||
|
@ -27,170 +26,111 @@ CreateHardLinkW(LPCWSTR lpFileName,
|
||||||
LPCWSTR lpExistingFileName,
|
LPCWSTR lpExistingFileName,
|
||||||
LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
||||||
{
|
{
|
||||||
UNICODE_STRING LinkTarget, LinkName;
|
|
||||||
LPVOID lpSecurityDescriptor;
|
|
||||||
PFILE_LINK_INFORMATION LinkInformation;
|
|
||||||
IO_STATUS_BLOCK IoStatus;
|
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
BOOL Ret = FALSE;
|
BOOL Ret = FALSE;
|
||||||
|
ULONG NeededSize;
|
||||||
|
IO_STATUS_BLOCK IoStatusBlock;
|
||||||
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
||||||
|
HANDLE hTarget = INVALID_HANDLE_VALUE;
|
||||||
|
PFILE_LINK_INFORMATION LinkInformation = NULL;
|
||||||
|
UNICODE_STRING LinkTarget = {0, 0, NULL}, LinkName = {0, 0, NULL};
|
||||||
|
|
||||||
|
TRACE("CreateHardLinkW: %S, %S, %p\n", lpFileName, lpExistingFileName, lpSecurityAttributes);
|
||||||
|
|
||||||
|
/* Validate parameters */
|
||||||
if(!lpFileName || !lpExistingFileName)
|
if(!lpFileName || !lpExistingFileName)
|
||||||
{
|
{
|
||||||
SetLastError(ERROR_INVALID_PARAMETER);
|
SetLastError(ERROR_INVALID_PARAMETER);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
lpSecurityDescriptor = (lpSecurityAttributes ? lpSecurityAttributes->lpSecurityDescriptor : NULL);
|
_SEH2_TRY
|
||||||
|
|
||||||
if(RtlDetermineDosPathNameType_U((LPWSTR)lpFileName) == 1 ||
|
|
||||||
RtlDetermineDosPathNameType_U((LPWSTR)lpExistingFileName) == 1)
|
|
||||||
{
|
{
|
||||||
WARN("CreateHardLinkW() cannot handle UNC Paths!\n");
|
/* Get target UNC path */
|
||||||
SetLastError(ERROR_INVALID_NAME);
|
if (!RtlDosPathNameToNtPathName_U(lpExistingFileName, &LinkTarget, NULL, NULL))
|
||||||
return FALSE;
|
{
|
||||||
|
SetLastError(ERROR_PATH_NOT_FOUND);
|
||||||
|
_SEH2_LEAVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(RtlDosPathNameToNtPathName_U(lpExistingFileName, &LinkTarget, NULL, NULL))
|
/* Open target */
|
||||||
{
|
|
||||||
ULONG NeededSize = RtlGetFullPathName_U((LPWSTR)lpExistingFileName, 0, NULL, NULL);
|
|
||||||
if(NeededSize > 0)
|
|
||||||
{
|
|
||||||
LPWSTR lpNtLinkTarget = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, NeededSize * sizeof(WCHAR));
|
|
||||||
if(lpNtLinkTarget != NULL)
|
|
||||||
{
|
|
||||||
LPWSTR lpFilePart;
|
|
||||||
|
|
||||||
if(RtlGetFullPathName_U((LPWSTR)lpExistingFileName, NeededSize, lpNtLinkTarget, &lpFilePart) &&
|
|
||||||
(*lpNtLinkTarget) != L'\0')
|
|
||||||
{
|
|
||||||
UNICODE_STRING CheckDrive, LinkDrive;
|
|
||||||
WCHAR wCheckDrive[10];
|
|
||||||
|
|
||||||
swprintf(wCheckDrive, L"\\??\\%c:", (WCHAR)(*lpNtLinkTarget));
|
|
||||||
RtlInitUnicodeString(&CheckDrive, wCheckDrive);
|
|
||||||
|
|
||||||
RtlZeroMemory(&LinkDrive, sizeof(UNICODE_STRING));
|
|
||||||
|
|
||||||
LinkDrive.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, (MAX_PATH + 1) * sizeof(WCHAR));
|
|
||||||
if(LinkDrive.Buffer != NULL)
|
|
||||||
{
|
|
||||||
HANDLE hFile, hTarget;
|
|
||||||
OBJECT_ATTRIBUTES ObjectAttributes;
|
|
||||||
|
|
||||||
InitializeObjectAttributes(&ObjectAttributes,
|
|
||||||
&CheckDrive,
|
|
||||||
OBJ_CASE_INSENSITIVE,
|
|
||||||
NULL,
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
Status = NtOpenSymbolicLinkObject(&hFile, 1, &ObjectAttributes);
|
|
||||||
if(NT_SUCCESS(Status))
|
|
||||||
{
|
|
||||||
UNICODE_STRING LanManager;
|
|
||||||
|
|
||||||
RtlInitUnicodeString(&LanManager, L"\\Device\\LanmanRedirector\\");
|
|
||||||
|
|
||||||
NtQuerySymbolicLinkObject(hFile, &LinkDrive, NULL);
|
|
||||||
|
|
||||||
if(!RtlPrefixUnicodeString(&LanManager, &LinkDrive, TRUE))
|
|
||||||
{
|
|
||||||
InitializeObjectAttributes(&ObjectAttributes,
|
InitializeObjectAttributes(&ObjectAttributes,
|
||||||
&LinkTarget,
|
&LinkTarget,
|
||||||
OBJ_CASE_INSENSITIVE,
|
OBJ_CASE_INSENSITIVE,
|
||||||
NULL,
|
NULL,
|
||||||
lpSecurityDescriptor);
|
(lpSecurityAttributes ? lpSecurityAttributes->lpSecurityDescriptor : NULL));
|
||||||
|
|
||||||
Status = NtOpenFile(&hTarget,
|
Status = NtOpenFile(&hTarget,
|
||||||
SYNCHRONIZE | DELETE,
|
SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
|
||||||
&ObjectAttributes,
|
&ObjectAttributes,
|
||||||
&IoStatus,
|
&IoStatusBlock,
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||||
FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_REPARSE_POINT);
|
FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT);
|
||||||
if(NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
if(RtlDosPathNameToNtPathName_U(lpFileName, &LinkName, NULL, NULL))
|
BaseSetLastNTError(Status);
|
||||||
|
_SEH2_LEAVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get UNC path name for link */
|
||||||
|
if (!RtlDosPathNameToNtPathName_U(lpFileName, &LinkName, NULL, NULL))
|
||||||
{
|
{
|
||||||
NeededSize = sizeof(FILE_LINK_INFORMATION) + LinkName.Length + sizeof(WCHAR);
|
SetLastError(ERROR_PATH_NOT_FOUND);
|
||||||
LinkInformation = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, NeededSize);
|
_SEH2_LEAVE;
|
||||||
if(LinkInformation != NULL)
|
}
|
||||||
|
|
||||||
|
/* Prepare data for link */
|
||||||
|
NeededSize = sizeof(FILE_LINK_INFORMATION) + LinkName.Length;
|
||||||
|
LinkInformation = RtlAllocateHeap(RtlGetProcessHeap(), 0, NeededSize);
|
||||||
|
if (!LinkInformation)
|
||||||
{
|
{
|
||||||
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
||||||
|
_SEH2_LEAVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
RtlMoveMemory(LinkInformation->FileName, LinkName.Buffer, LinkName.Length);
|
||||||
LinkInformation->ReplaceIfExists = FALSE;
|
LinkInformation->ReplaceIfExists = FALSE;
|
||||||
LinkInformation->RootDirectory = 0;
|
LinkInformation->RootDirectory = 0;
|
||||||
LinkInformation->FileNameLength = LinkName.Length;
|
LinkInformation->FileNameLength = LinkName.Length;
|
||||||
RtlCopyMemory(LinkInformation->FileName, LinkName.Buffer, LinkName.Length);
|
|
||||||
|
|
||||||
Status = NtSetInformationFile(hTarget, &IoStatus, LinkInformation, NeededSize, FileLinkInformation);
|
/* Create hard link */
|
||||||
|
Status = NtSetInformationFile(hTarget,
|
||||||
|
&IoStatusBlock,
|
||||||
|
LinkInformation,
|
||||||
|
NeededSize,
|
||||||
|
FileLinkInformation);
|
||||||
if (NT_SUCCESS(Status))
|
if (NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
Ret = TRUE;
|
Ret = TRUE;
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
_SEH2_FINALLY
|
||||||
{
|
{
|
||||||
BaseSetLastNTError(Status);
|
if (LinkTarget.Buffer)
|
||||||
}
|
|
||||||
|
|
||||||
RtlFreeHeap(RtlGetProcessHeap(), 0, LinkInformation);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetLastError(ERROR_PATH_NOT_FOUND);
|
|
||||||
}
|
|
||||||
NtClose(hTarget);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WARN("Unable to open link destination \"%wZ\"!\n", &LinkTarget);
|
|
||||||
BaseSetLastNTError(Status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WARN("Path \"%wZ\" must not be a mapped drive!\n", &LinkDrive);
|
|
||||||
SetLastError(ERROR_INVALID_NAME);
|
|
||||||
}
|
|
||||||
|
|
||||||
NtClose(hFile);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
BaseSetLastNTError(Status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetLastError(ERROR_INVALID_NAME);
|
|
||||||
}
|
|
||||||
RtlFreeHeap(RtlGetProcessHeap(), 0, lpNtLinkTarget);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetLastError(ERROR_INVALID_NAME);
|
|
||||||
}
|
|
||||||
RtlFreeHeap(RtlGetProcessHeap(), 0, LinkTarget.Buffer);
|
RtlFreeHeap(RtlGetProcessHeap(), 0, LinkTarget.Buffer);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (hTarget != INVALID_HANDLE_VALUE)
|
||||||
{
|
{
|
||||||
SetLastError(ERROR_PATH_NOT_FOUND);
|
NtClose(hTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (LinkName.Buffer)
|
||||||
|
{
|
||||||
|
RtlFreeHeap(RtlGetProcessHeap(), 0, LinkName.Buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (LinkInformation)
|
||||||
|
{
|
||||||
|
RtlFreeHeap(RtlGetProcessHeap(), 0, LinkInformation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_SEH2_END;
|
||||||
|
|
||||||
return Ret;
|
return Ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @implemented
|
* @implemented
|
||||||
*/
|
*/
|
||||||
|
@ -199,24 +139,30 @@ CreateHardLinkA(LPCSTR lpFileName,
|
||||||
LPCSTR lpExistingFileName,
|
LPCSTR lpExistingFileName,
|
||||||
LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
||||||
{
|
{
|
||||||
PWCHAR FileNameW, ExistingFileNameW;
|
|
||||||
BOOL Ret;
|
BOOL Ret;
|
||||||
|
PUNICODE_STRING lpFileNameW;
|
||||||
|
UNICODE_STRING ExistingFileNameW;
|
||||||
|
|
||||||
if(!lpFileName || !lpExistingFileName)
|
lpFileNameW = Basep8BitStringToStaticUnicodeString(lpFileName);
|
||||||
|
if (!lpFileNameW)
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!lpExistingFileName)
|
||||||
{
|
{
|
||||||
SetLastError(ERROR_INVALID_PARAMETER);
|
SetLastError(ERROR_INVALID_PARAMETER);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(FileNameW = FilenameA2W(lpFileName, FALSE)))
|
if (!Basep8BitStringToDynamicUnicodeString(&ExistingFileNameW, lpExistingFileName))
|
||||||
|
{
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
if (!(ExistingFileNameW = FilenameA2W(lpExistingFileName, TRUE)))
|
Ret = CreateHardLinkW(lpFileNameW->Buffer, ExistingFileNameW.Buffer, lpSecurityAttributes);
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
Ret = CreateHardLinkW(FileNameW , ExistingFileNameW , lpSecurityAttributes);
|
RtlFreeHeap(RtlGetProcessHeap(), 0, ExistingFileNameW.Buffer);
|
||||||
|
|
||||||
RtlFreeHeap(RtlGetProcessHeap(), 0, ExistingFileNameW);
|
|
||||||
|
|
||||||
return Ret;
|
return Ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue