|
| 1 | +/* |
| 2 | + * NeoSmart JunctionPoint Library |
| 3 | + * Author: Mahmoud Al-Qudsi <[email protected]> |
| 4 | + * Copyright (C) 2011 by NeoSmart Technologies |
| 5 | + * This code is released under the terms of the MIT License |
| 6 | +*/ |
| 7 | + |
| 8 | +#include "stdafx.h" |
| 9 | +#include "JunctionPoint.h" |
| 10 | +#include "Internal.h" |
| 11 | + |
| 12 | +#include <Shlobj.h> |
| 13 | +#include <atlstr.h> |
| 14 | +#include <memory> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | + |
| 18 | +namespace neosmart |
| 19 | +{ |
| 20 | + __declspec(thread) int recurseDepth = 0; |
| 21 | + |
| 22 | + bool CreateJunctionPoint(LPCTSTR origin, LPCTSTR junction) |
| 23 | + { |
| 24 | + CString nativeTarget; |
| 25 | + |
| 26 | + //Prepend \??\ to path to mark it as not-for-parsing |
| 27 | + nativeTarget.Format(_T("\\??\\%s"), origin); |
| 28 | + |
| 29 | + //This API only supports Windows-style slashes. |
| 30 | + nativeTarget.Replace(_T('/'), _T('\\')); |
| 31 | + |
| 32 | + //Make sure there's a trailing slash |
| 33 | + if(nativeTarget.Right(1) != _T("\\")) |
| 34 | + nativeTarget += _T("\\"); |
| 35 | + |
| 36 | + size_t size = sizeof(REPARSE_DATA_BUFFER) - sizeof(TCHAR) + nativeTarget.GetLength() * sizeof(TCHAR); |
| 37 | + auto_ptr<REPARSE_DATA_BUFFER> reparseBuffer((REPARSE_DATA_BUFFER*) new unsigned char[size]); |
| 38 | + |
| 39 | + //Fill the reparse buffer |
| 40 | + reparseBuffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
| 41 | + reparseBuffer->Reserved = NULL; |
| 42 | + |
| 43 | + reparseBuffer->MountPointReparseBuffer.SubstituteNameOffset = 0; |
| 44 | + reparseBuffer->MountPointReparseBuffer.SubstituteNameLength = nativeTarget.GetLength() * (int) sizeof(TCHAR); |
| 45 | + |
| 46 | + //No substitute name, point it outside the bounds of the string |
| 47 | + reparseBuffer->MountPointReparseBuffer.PrintNameOffset = reparseBuffer->MountPointReparseBuffer.SubstituteNameLength + (int) sizeof(TCHAR); |
| 48 | + reparseBuffer->MountPointReparseBuffer.PrintNameLength = 0; |
| 49 | + |
| 50 | + //Copy the actual string |
| 51 | + //_tcscpy(reparseBuffer->MountPointReparseBuffer.PathBuffer, nativeTarget); |
| 52 | + memcpy(reparseBuffer->MountPointReparseBuffer.PathBuffer, (LPCTSTR) nativeTarget, reparseBuffer->MountPointReparseBuffer.SubstituteNameLength); |
| 53 | + |
| 54 | + //Set ReparseDataLength to the size of the MountPointReparseBuffer |
| 55 | + //Kind in mind that with the padding for the union (given that SymbolicLinkReparseBuffer is larger), |
| 56 | + //this is NOT equal to sizeof(MountPointReparseBuffer) |
| 57 | + reparseBuffer->ReparseDataLength = sizeof(REPARSE_DATA_BUFFER) - REPARSE_DATA_BUFFER_HEADER_SIZE - sizeof(TCHAR) + reparseBuffer->MountPointReparseBuffer.SubstituteNameLength; |
| 58 | + |
| 59 | + //Create the junction directory |
| 60 | + CreateDirectory(junction, NULL); |
| 61 | + |
| 62 | + //Set the reparse point |
| 63 | + SafeHandle hDir; |
| 64 | + hDir.Handle = CreateFile(junction, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 65 | + |
| 66 | + if(hDir.IsInvalid()) |
| 67 | + { |
| 68 | + _tprintf(_T("Failed to open directory!\r\n")); |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + DWORD bytesReturned = 0; |
| 73 | + if(!DeviceIoControl(hDir.Handle, FSCTL_SET_REPARSE_POINT, reparseBuffer.get(), (unsigned int) size, NULL, 0, &bytesReturned, NULL)) |
| 74 | + { |
| 75 | + RemoveDirectory(junction); |
| 76 | + _tprintf(_T("Error issuing DeviceIoControl FSCTL_SET_REPARSE_POINT: 0x%x.\r\n"), GetLastError()); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + bool GetJunctionDestination(LPCTSTR path, OUT LPTSTR destination, DWORD attributes) |
| 84 | + { |
| 85 | + if(attributes == 0) |
| 86 | + attributes = GetFileAttributes(path); |
| 87 | + |
| 88 | + if(!IsDirectoryJunction(path, attributes)) |
| 89 | + return false; |
| 90 | + |
| 91 | + SafeHandle hDir; |
| 92 | + hDir.Handle = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 93 | + if (hDir.IsInvalid()) |
| 94 | + return false; // Failed to open directory |
| 95 | + |
| 96 | + BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 97 | + REPARSE_DATA_BUFFER &ReparseBuffer = (REPARSE_DATA_BUFFER&)buf; |
| 98 | + |
| 99 | + DWORD dwRet; |
| 100 | + if (!DeviceIoControl(hDir.Handle, FSCTL_GET_REPARSE_POINT, NULL, 0, &ReparseBuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dwRet, NULL)) |
| 101 | + { |
| 102 | + _tprintf(_T("Error getting reparse destination: 0x%x"), GetLastError()); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if(ReparseBuffer.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 107 | + { |
| 108 | + //Junction point |
| 109 | + ReparseBuffer.MountPointReparseBuffer.SubstituteNameLength /= sizeof(TCHAR); |
| 110 | + ReparseBuffer.MountPointReparseBuffer.SubstituteNameOffset /= sizeof(TCHAR); |
| 111 | + |
| 112 | + LPWSTR pPath = ReparseBuffer.MountPointReparseBuffer.PathBuffer + ReparseBuffer.MountPointReparseBuffer.SubstituteNameOffset; |
| 113 | + pPath[ReparseBuffer.MountPointReparseBuffer.SubstituteNameLength] = _T('\0'); |
| 114 | + |
| 115 | + if (wcsncmp(pPath, L"\\??\\", 4) == 0) |
| 116 | + pPath += 4; // Skip 'non-parsed' prefix |
| 117 | + |
| 118 | + _tcscpy_s(destination, MAX_PATH, pPath); |
| 119 | + } |
| 120 | + else if(ReparseBuffer.ReparseTag == IO_REPARSE_TAG_SYMLINK) |
| 121 | + { |
| 122 | + //Symlink |
| 123 | + ReparseBuffer.SymbolicLinkReparseBuffer.SubstituteNameLength /= sizeof(TCHAR); |
| 124 | + ReparseBuffer.SymbolicLinkReparseBuffer.SubstituteNameOffset /= sizeof(TCHAR); |
| 125 | + |
| 126 | + LPWSTR pPath = ReparseBuffer.SymbolicLinkReparseBuffer.PathBuffer + ReparseBuffer.SymbolicLinkReparseBuffer.SubstituteNameOffset; |
| 127 | + pPath[ReparseBuffer.SymbolicLinkReparseBuffer.SubstituteNameLength] = _T('\0'); |
| 128 | + |
| 129 | + if (wcsncmp(pPath, L"\\??\\", 4) == 0) |
| 130 | + pPath += 4; // Skip 'non-parsed' prefix |
| 131 | + |
| 132 | + if(ReparseBuffer.SymbolicLinkReparseBuffer.Flags & SYMLINK_FLAG_RELATIVE) |
| 133 | + { |
| 134 | + //It's a relative path. Construct the relative path and resolve destination |
| 135 | + _tcscpy_s(destination, MAX_PATH, path); |
| 136 | + PathAddBackslash(destination); |
| 137 | + _tcscat_s(destination, MAX_PATH, pPath); |
| 138 | + PathResolve(destination, NULL, PRF_REQUIREABSOLUTE); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + _tcscpy_s(destination, MAX_PATH, pPath); |
| 143 | + } |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + //Unsupported mount point |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + //We may have to do this recursively |
| 152 | + ++recurseDepth; |
| 153 | + bool result; |
| 154 | + if(recurseDepth < MAX_RECURSE_DEPTH) |
| 155 | + { |
| 156 | + DWORD newAttributes = GetFileAttributes(destination); |
| 157 | + if(IsDirectoryJunction(destination, newAttributes)) |
| 158 | + result = GetJunctionDestination(CString(destination), destination, newAttributes); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + result = false; |
| 163 | + } |
| 164 | + |
| 165 | + --recurseDepth; |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + //Doesn't support symlink'd files :( |
| 170 | + bool IsDirectoryJunction(LPCTSTR path, DWORD attributes) |
| 171 | + { |
| 172 | + if(attributes == 0) |
| 173 | + attributes = ::GetFileAttributes(path); |
| 174 | + |
| 175 | + if (attributes == INVALID_FILE_ATTRIBUTES) |
| 176 | + return false; //Doesn't exist |
| 177 | + |
| 178 | + if ((attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) != (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) |
| 179 | + return false; //Not a directory or not a reparse point |
| 180 | + |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + //This removes the reparse point but does not delete the directory! |
| 185 | + bool DeleteJunctionPoint(LPCTSTR path) |
| 186 | + { |
| 187 | + REPARSE_GUID_DATA_BUFFER reparsePoint = {0}; |
| 188 | + reparsePoint.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
| 189 | + |
| 190 | + SafeHandle hDir; |
| 191 | + hDir.Handle = CreateFile(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 192 | + |
| 193 | + //Does the path exist? |
| 194 | + if(hDir.IsInvalid()) |
| 195 | + return false; |
| 196 | + |
| 197 | + DWORD returnedBytes; |
| 198 | + DWORD result = DeviceIoControl(hDir.Handle, FSCTL_DELETE_REPARSE_POINT, &reparsePoint, REPARSE_GUID_DATA_BUFFER_HEADER_SIZE, NULL, 0, &returnedBytes, NULL) != 0; |
| 199 | + if(result == 0) |
| 200 | + { |
| 201 | + _tprintf_s(_T("Failed to issue FSCTL_DELETE_REPARSE_POINT. Last error: 0x%x"), GetLastError()); |
| 202 | + return false; |
| 203 | + } |
| 204 | + else |
| 205 | + { |
| 206 | + return true; |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments