ReactOS 0.4.16-dev-336-gb667d82
hardlink.c File Reference
#include <k32.h>
#include <debug.h>
Include dependency graph for hardlink.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

BOOL WINAPI CreateHardLinkW (IN LPCWSTR lpFileName, IN LPCWSTR lpExistingFileName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
 
BOOL WINAPI CreateHardLinkA (IN LPCSTR lpFileName, IN LPCSTR lpExistingFileName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file hardlink.c.

Function Documentation

◆ CreateHardLinkA()

BOOL WINAPI CreateHardLinkA ( IN LPCSTR  lpFileName,
IN LPCSTR  lpExistingFileName,
IN LPSECURITY_ATTRIBUTES  lpSecurityAttributes 
)

Definition at line 136 of file hardlink.c.

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}
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
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
unsigned int BOOL
Definition: ntddk_ex.h:94
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
_In_ LPCSTR lpFileName
Definition: winbase.h:3096

Referenced by test_filenames().

◆ CreateHardLinkW()

BOOL WINAPI CreateHardLinkW ( IN LPCWSTR  lpFileName,
IN LPCWSTR  lpExistingFileName,
IN LPSECURITY_ATTRIBUTES  lpSecurityAttributes 
)

Definition at line 23 of file hardlink.c.

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}
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 TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#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
@ 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
#define _SEH2_FINALLY
Definition: pseh2_64.h:114
#define _SEH2_END
Definition: pseh2_64.h:155
#define _SEH2_TRY
Definition: pseh2_64.h:55
#define _SEH2_LEAVE
Definition: pseh2_64.h:167
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:167
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
uint32_t ULONG
Definition: typedefs.h:59
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:106
_Inout_ PUNICODE_STRING LinkTarget
Definition: zwfuncs.h:292

Referenced by BtrfsRecv::cmd_link(), and CreateHardLinkA().