ReactOS 0.4.15-dev-7924-g5949c20
hardlink.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/file/hardlink.c
5 * PURPOSE: Hardlink functions
6 * PROGRAMMER: Thomas Weidenmueller (w3seek@users.sourceforge.net)
7 * Pierre Schweitzer (pierre.schweitzer@reactos.org)
8 */
9
10/* INCLUDES *****************************************************************/
11
12#include <k32.h>
13#define NDEBUG
14#include <debug.h>
15
16/* FUNCTIONS ****************************************************************/
17
18/*
19 * @implemented
20 */
21BOOL
24 IN LPCWSTR lpExistingFileName,
25 IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
26{
28 BOOL Ret = FALSE;
29 ULONG NeededSize;
33 PFILE_LINK_INFORMATION LinkInformation = NULL;
34 UNICODE_STRING LinkTarget, LinkName;
35
36 /* Initialize */
37 LinkTarget.Buffer = LinkName.Buffer = NULL;
38
39 /* Validate parameters */
40 if (!(lpFileName) || !(lpExistingFileName))
41 {
43 return FALSE;
44 }
45
47 {
48 /* Get target UNC path */
49 if (!RtlDosPathNameToNtPathName_U(lpExistingFileName,
51 NULL,
52 NULL))
53 {
54 /* Set the error and fail */
57 }
58
59 /* Open target */
63 NULL,
64 lpSecurityAttributes ?
65 lpSecurityAttributes->lpSecurityDescriptor :
66 NULL);
67 Status = NtOpenFile(&hTarget,
73 if (!NT_SUCCESS(Status))
74 {
75 /* Convert the error and fail */
78 }
79
80 /* Get UNC path name for link */
82 {
83 /* Set the error and fail */
86 }
87
88 /* Allocate data for link */
89 NeededSize = sizeof(FILE_LINK_INFORMATION) + LinkName.Length;
90 LinkInformation = RtlAllocateHeap(RtlGetProcessHeap(), 0, NeededSize);
91 if (!LinkInformation)
92 {
93 /* We're out of memory */
96 }
97
98 /* Setup data for link and create it */
99 RtlMoveMemory(LinkInformation->FileName, LinkName.Buffer, LinkName.Length);
100 LinkInformation->ReplaceIfExists = FALSE;
101 LinkInformation->RootDirectory = 0;
102 LinkInformation->FileNameLength = LinkName.Length;
105 LinkInformation,
106 NeededSize,
108 if (NT_SUCCESS(Status))
109 {
110 /* Set success code */
111 Ret = TRUE;
112 }
113 else
114 {
115 /* Convert error code and return default (FALSE) */
117 }
118 }
120 {
121 /* Cleanup all allocations */
122 if (LinkTarget.Buffer) RtlFreeHeap(RtlGetProcessHeap(), 0, LinkTarget.Buffer);
123 if (hTarget != INVALID_HANDLE_VALUE) NtClose(hTarget);
124 if (LinkName.Buffer) RtlFreeHeap(RtlGetProcessHeap(), 0, LinkName.Buffer);
125 if (LinkInformation) RtlFreeHeap(RtlGetProcessHeap(), 0, LinkInformation);
126 }
127 _SEH2_END;
128 return Ret;
129}
130
131/*
132 * @implemented
133 */
134BOOL
135WINAPI
137 IN LPCSTR lpExistingFileName,
138 IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
139{
140 BOOL Ret;
141 PUNICODE_STRING lpFileNameW;
142 UNICODE_STRING ExistingFileNameW;
143
144 /* Convert the filename to unicode, using MAX_PATH limitations */
146 if (!lpFileNameW) return FALSE;
147
148 /* Check if there's an existing name as well */
149 if (lpExistingFileName)
150 {
151 /* We're already using the static string above, so do this dynamically */
152 if (!Basep8BitStringToDynamicUnicodeString(&ExistingFileNameW,
153 lpExistingFileName))
154 {
155 /* Out of memory -- fail */
156 return FALSE;
157 }
158 }
159 else
160 {
161 /* No existing file name */
162 ExistingFileNameW.Buffer = NULL;
163 }
164
165 /* Call the Wide function, and then free our dynamic string */
166 Ret = CreateHardLinkW(lpFileNameW->Buffer,
167 ExistingFileNameW.Buffer,
168 lpSecurityAttributes);
169 RtlFreeUnicodeString(&ExistingFileNameW);
170 return Ret;
171}
172
173/* EOF */
LONG NTSTATUS
Definition: precomp.h:26
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:590
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:608
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define SetLastError(x)
Definition: compat.h:752
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define FILE_SHARE_READ
Definition: compat.h:136
BOOLEAN WINAPI Basep8BitStringToDynamicUnicodeString(OUT PUNICODE_STRING UnicodeString, IN LPCSTR String)
Definition: utils.c:225
PUNICODE_STRING WINAPI Basep8BitStringToStaticUnicodeString(IN LPCSTR String)
Definition: utils.c:188
#define _SEH2_FINALLY
Definition: filesup.c:21
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
#define _SEH2_LEAVE
Definition: filesup.c:20
unsigned int BOOL
Definition: ntddk_ex.h:94
@ FileLinkInformation
Definition: from_kernel.h:72
#define FILE_OPEN_REPARSE_POINT
Definition: from_kernel.h:46
struct _FILE_LINK_INFORMATION FILE_LINK_INFORMATION
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:25
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(_In_opt_z_ PCWSTR DosPathName, _Out_ PUNICODE_STRING NtPathName, _Out_opt_ PCWSTR *NtFileNamePart, _Out_opt_ PRTL_RELATIVE_NAME_U DirectoryInfo)
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3952
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSYSAPI NTSTATUS NTAPI NtSetInformationFile(IN HANDLE hFile, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID FileInformationBuffer, IN ULONG FileInformationBufferLength, IN FILE_INFORMATION_CLASS FileInfoClass)
Definition: iofunc.c:3096
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define FILE_WRITE_ATTRIBUTES
Definition: nt_native.h:649
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define IN
Definition: typedefs.h:39
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
uint32_t ULONG
Definition: typedefs.h:59
_In_ LPCSTR lpFileName
Definition: winbase.h:3071
#define WINAPI
Definition: msvc.h:6
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:106
const char * LPCSTR
Definition: xmlstorage.h:183
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
_Inout_ PUNICODE_STRING LinkTarget
Definition: zwfuncs.h:292