Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenhardlink.c
Go to the documentation of this file.
00001 /* $Id: hardlink.c 55790 2012-02-21 19:18:57Z ion $ 00002 * 00003 * COPYRIGHT: See COPYING in the top level directory 00004 * PROJECT: ReactOS system libraries 00005 * FILE: dll/win32/kernel32/client/file/hardlink.c 00006 * PURPOSE: Hardlink functions 00007 * PROGRAMMER: Thomas Weidenmueller (w3seek@users.sourceforge.net) 00008 * Pierre Schweitzer (pierre.schweitzer@reactos.org) 00009 */ 00010 00011 /* INCLUDES *****************************************************************/ 00012 00013 #include <k32.h> 00014 #define NDEBUG 00015 #include <debug.h> 00016 00017 /* FUNCTIONS ****************************************************************/ 00018 00019 /* 00020 * @implemented 00021 */ 00022 BOOL 00023 WINAPI 00024 CreateHardLinkW(IN LPCWSTR lpFileName, 00025 IN LPCWSTR lpExistingFileName, 00026 IN LPSECURITY_ATTRIBUTES lpSecurityAttributes) 00027 { 00028 NTSTATUS Status; 00029 BOOL Ret = FALSE; 00030 ULONG NeededSize; 00031 IO_STATUS_BLOCK IoStatusBlock; 00032 OBJECT_ATTRIBUTES ObjectAttributes; 00033 HANDLE hTarget = INVALID_HANDLE_VALUE; 00034 PFILE_LINK_INFORMATION LinkInformation = NULL; 00035 UNICODE_STRING LinkTarget, LinkName; 00036 00037 /* Initialize */ 00038 LinkTarget.Buffer = LinkName.Buffer = NULL; 00039 00040 /* Validate parameters */ 00041 if (!(lpFileName) || !(lpExistingFileName)) 00042 { 00043 SetLastError(ERROR_INVALID_PARAMETER); 00044 return FALSE; 00045 } 00046 00047 _SEH2_TRY 00048 { 00049 /* Get target UNC path */ 00050 if (!RtlDosPathNameToNtPathName_U(lpExistingFileName, 00051 &LinkTarget, 00052 NULL, 00053 NULL)) 00054 { 00055 /* Set the error and fail */ 00056 SetLastError(ERROR_PATH_NOT_FOUND); 00057 _SEH2_LEAVE; 00058 } 00059 00060 /* Open target */ 00061 InitializeObjectAttributes(&ObjectAttributes, 00062 &LinkTarget, 00063 OBJ_CASE_INSENSITIVE, 00064 NULL, 00065 lpSecurityAttributes ? 00066 lpSecurityAttributes->lpSecurityDescriptor : 00067 NULL); 00068 Status = NtOpenFile(&hTarget, 00069 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, 00070 &ObjectAttributes, 00071 &IoStatusBlock, 00072 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 00073 FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT); 00074 if (!NT_SUCCESS(Status)) 00075 { 00076 /* Convert the error and fail */ 00077 BaseSetLastNTError(Status); 00078 _SEH2_LEAVE; 00079 } 00080 00081 /* Get UNC path name for link */ 00082 if (!RtlDosPathNameToNtPathName_U(lpFileName, &LinkName, NULL, NULL)) 00083 { 00084 /* Set the error and fail */ 00085 SetLastError(ERROR_PATH_NOT_FOUND); 00086 _SEH2_LEAVE; 00087 } 00088 00089 /* Allocate data for link */ 00090 NeededSize = sizeof(FILE_LINK_INFORMATION) + LinkName.Length; 00091 LinkInformation = RtlAllocateHeap(RtlGetProcessHeap(), 0, NeededSize); 00092 if (!LinkInformation) 00093 { 00094 /* We're out of memory */ 00095 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 00096 _SEH2_LEAVE; 00097 } 00098 00099 /* Setup data for link and create it */ 00100 RtlMoveMemory(LinkInformation->FileName, LinkName.Buffer, LinkName.Length); 00101 LinkInformation->ReplaceIfExists = FALSE; 00102 LinkInformation->RootDirectory = 0; 00103 LinkInformation->FileNameLength = LinkName.Length; 00104 Status = NtSetInformationFile(hTarget, 00105 &IoStatusBlock, 00106 LinkInformation, 00107 NeededSize, 00108 FileLinkInformation); 00109 if (NT_SUCCESS(Status)) 00110 { 00111 /* Set success code */ 00112 Ret = TRUE; 00113 } 00114 else 00115 { 00116 /* Convert error code and return default (FALSE) */ 00117 BaseSetLastNTError(Status); 00118 } 00119 } 00120 _SEH2_FINALLY 00121 { 00122 /* Cleanup all allocations */ 00123 if (LinkTarget.Buffer) RtlFreeHeap(RtlGetProcessHeap(), 0, LinkTarget.Buffer); 00124 if (hTarget != INVALID_HANDLE_VALUE) NtClose(hTarget); 00125 if (LinkName.Buffer) RtlFreeHeap(RtlGetProcessHeap(), 0, LinkName.Buffer); 00126 if (LinkInformation) RtlFreeHeap(RtlGetProcessHeap(), 0, LinkInformation); 00127 } 00128 _SEH2_END; 00129 return Ret; 00130 } 00131 00132 /* 00133 * @implemented 00134 */ 00135 BOOL 00136 WINAPI 00137 CreateHardLinkA(IN LPCSTR lpFileName, 00138 IN LPCSTR lpExistingFileName, 00139 IN LPSECURITY_ATTRIBUTES lpSecurityAttributes) 00140 { 00141 BOOL Ret; 00142 PUNICODE_STRING lpFileNameW; 00143 UNICODE_STRING ExistingFileNameW; 00144 00145 /* Convert the filename to unicode, using MAX_PATH limitations */ 00146 lpFileNameW = Basep8BitStringToStaticUnicodeString(lpFileName); 00147 if (!lpFileNameW) return FALSE; 00148 00149 /* Check if there's an existing name as well */ 00150 if (lpExistingFileName) 00151 { 00152 /* We're already using the static string above, so do this dynamically */ 00153 if (!Basep8BitStringToDynamicUnicodeString(&ExistingFileNameW, 00154 lpExistingFileName)) 00155 { 00156 /* Out of memory -- fail */ 00157 return FALSE; 00158 } 00159 } 00160 else 00161 { 00162 /* No existing file name */ 00163 ExistingFileNameW.Buffer = NULL; 00164 } 00165 00166 /* Call the Wide function, and then free our dynamic string */ 00167 Ret = CreateHardLinkW(lpFileNameW->Buffer, 00168 ExistingFileNameW.Buffer, 00169 lpSecurityAttributes); 00170 RtlFreeUnicodeString(&ExistingFileNameW); 00171 return Ret; 00172 } 00173 00174 /* EOF */ Generated on Mon May 28 2012 04:24:04 for ReactOS by
1.7.6.1
|