ReactOS  0.4.14-dev-115-g4576127
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 }
PUNICODE_STRING WINAPI Basep8BitStringToStaticUnicodeString(IN LPCSTR String)
Definition: utils.c:188
BOOLEAN WINAPI Basep8BitStringToDynamicUnicodeString(OUT PUNICODE_STRING UnicodeString, IN LPCSTR String)
Definition: utils.c:225
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
_In_ LPCSTR lpFileName
Definition: winbase.h:3018

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;
32  HANDLE hTarget = INVALID_HANDLE_VALUE;
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 
46  _SEH2_TRY
47  {
48  /* Get target UNC path */
49  if (!RtlDosPathNameToNtPathName_U(lpExistingFileName,
50  &LinkTarget,
51  NULL,
52  NULL))
53  {
54  /* Set the error and fail */
57  }
58 
59  /* Open target */
61  &LinkTarget,
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;
103  Status = NtSetInformationFile(hTarget,
104  &IoStatusBlock,
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 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_WRITE_ATTRIBUTES
Definition: nt_native.h:649
#define FILE_SHARE_READ
Definition: compat.h:125
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:263
_SEH2_TRY
Definition: create.c:4250
_Inout_ PUNICODE_STRING LinkTarget
Definition: zwfuncs.h:292
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
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:3951
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:409
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define SYNCHRONIZE
Definition: nt_native.h:61
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
_SEH2_FINALLY
Definition: create.c:4395
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
struct _FILE_LINK_INFORMATION FILE_LINK_INFORMATION
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
unsigned int ULONG
Definition: retypes.h:1
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:106
#define _SEH2_LEAVE
Definition: filesup.c:20
#define FILE_OPEN_REPARSE_POINT
Definition: from_kernel.h:46
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:3042
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)
_In_ LPCSTR lpFileName
Definition: winbase.h:3018

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