// Copyright 2015 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http ://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "stdafx.h" #ifndef _UNICODE #define _UNICODE #endif #ifndef UNICODE typedef std::string String; #else typedef std::wstring String; #endif bool CreateNativeHardlink(LPCWSTR linkname, LPCWSTR targetname) { std::wstring full_linkname = BuildFullPath(linkname, true); size_t len = full_linkname.size() * sizeof(WCHAR); typed_buffer_ptr link_info(sizeof(FILE_LINK_INFORMATION) + len - sizeof(WCHAR)); memcpy(&link_info->FileName[0], full_linkname.c_str(), len); link_info->ReplaceIfExists = TRUE; link_info->FileNameLength = len; std::wstring full_targetname = BuildFullPath(targetname, true); HANDLE hFile = OpenFileNative(full_targetname.c_str(), nullptr, MAXIMUM_ALLOWED, FILE_SHARE_READ, 0); if (hFile) { DEFINE_NTDLL(ZwSetInformationFile); IO_STATUS_BLOCK io_status = { 0 }; NTSTATUS status = fZwSetInformationFile(hFile, &io_status, link_info, link_info.size(), FileLinkInformation); CloseHandle(hFile); if (NT_SUCCESS(status)) { return true; } SetNtLastError(status); } return false; } int _tmain(int argc, _TCHAR* argv[]) { if (argc < 3) { printf("CreateHardLink hardlink target\n"); printf("Example: hello.txt goodbye.txt\n"); return 1; } else { if (CreateNativeHardlink(argv[1], argv[2])) { printf("Done\n"); } else { printf("Error creating hardlink"); return 1; } } return 0; }