-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJunction.cpp
174 lines (137 loc) · 6.45 KB
/
Junction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <windows.h>
#include <stdio.h>
//******************************************************************************
//** Junction Utilities
//******************************************************************************
/**
* Functions used to create and analyze Windows Junction Points.
* Contains code by Inv Softworks LLC, www.flexhex.com.
* http://www.flexhex.com/docs/articles/hard-links.phtml
*
******************************************************************************/
#define REPARSE_MOUNTPOINT_HEADER_SIZE 8
typedef struct {
DWORD ReparseTag;
DWORD ReparseDataLength;
WORD Reserved;
WORD ReparseTargetLength;
WORD ReparseTargetMaximumLength;
WORD Reserved1;
WCHAR ReparseTarget[1];
} REPARSE_MOUNTPOINT_DATA_BUFFER, *PREPARSE_MOUNTPOINT_DATA_BUFFER;
#define FSCTL_GET_REPARSE_POINT 0x000900a8
#define FSCTL_SET_REPARSE_POINT 0x000900a4
//**************************************************************************
//** OpenDirectory
//**************************************************************************
/** Used get a handle for a given path.
*/
HANDLE OpenDirectory(const wchar_t* path, BOOL bReadWrite) {
//Obtain restore privilege in case we don't have it
HANDLE hToken;
TOKEN_PRIVILEGES tp;
::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken);
::LookupPrivilegeValue(NULL,
(bReadWrite ? SE_RESTORE_NAME : SE_BACKUP_NAME),
&tp.Privileges[0].Luid);
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
::AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
::CloseHandle(hToken);
//Open the directory
DWORD dwAccess = bReadWrite ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_READ;
HANDLE hDir = ::CreateFileW(path, dwAccess, 0, NULL, OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
return hDir;
}
#define DIR_ATTR (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)
//**************************************************************************
//** IsDirectoryJunction
//**************************************************************************
/** Used to determine whether a given path represents a Junction point.
*/
BOOL IsDirectoryJunction(const wchar_t* path) {
DWORD dwAttr = ::GetFileAttributesW(path);
if (dwAttr == -1) return FALSE; // Not exists
if ((dwAttr & DIR_ATTR) != DIR_ATTR) return FALSE; // Not dir or no reparse point
HANDLE hDir = OpenDirectory(path, FALSE);
if (hDir == INVALID_HANDLE_VALUE) return FALSE; // Failed to open directory
BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
REPARSE_MOUNTPOINT_DATA_BUFFER& ReparseBuffer = (REPARSE_MOUNTPOINT_DATA_BUFFER&)buf;
DWORD dwRet;
BOOL br = ::DeviceIoControl(hDir, FSCTL_GET_REPARSE_POINT, NULL, 0, &ReparseBuffer,
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dwRet, NULL);
::CloseHandle(hDir);
return br ? (ReparseBuffer.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) : FALSE;
}
//**************************************************************************
//** GetTarget
//**************************************************************************
/** Used to return the path for a given junction.
*/
LPCWSTR GetTarget(const wchar_t* path) {
if (!IsDirectoryJunction(path)) {
return NULL; // Throw Error: no junction here
}
// Open for reading only (see OpenDirectory definition above)
HANDLE hDir = OpenDirectory(path, FALSE);
BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; // We need a large buffer
REPARSE_MOUNTPOINT_DATA_BUFFER& ReparseBuffer = (REPARSE_MOUNTPOINT_DATA_BUFFER&)buf;
DWORD dwRet;
if (::DeviceIoControl(hDir, FSCTL_GET_REPARSE_POINT, NULL, 0, &ReparseBuffer,
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dwRet, NULL)) {
// Success
::CloseHandle(hDir);
LPCWSTR pPath = ReparseBuffer.ReparseTarget;
if (wcsncmp(pPath, L"\\??\\", 4) == 0) pPath += 4; // Skip 'non-parsed' prefix
return pPath;
}
else { // Error
DWORD dr = ::GetLastError();
::CloseHandle(hDir);
// Some error action (throw or MessageBox)
}
return NULL;
}
//**************************************************************************
//** CreateJunction
//**************************************************************************
/** Used to create a junction point.
*
static void CreateJunction(LPCSTR szJunction, LPCSTR szPath) {
BYTE buf[sizeof(REPARSE_MOUNTPOINT_DATA_BUFFER) + MAX_PATH * sizeof(WCHAR)];
REPARSE_MOUNTPOINT_DATA_BUFFER& ReparseBuffer = (REPARSE_MOUNTPOINT_DATA_BUFFER&)buf;
char szTarget[MAX_PATH] = "\\??\\";
strcat(szTarget, szPath);
strcat(szTarget, "\\");
if (!::CreateDirectory(szJunction, NULL)) throw ::GetLastError();
// Obtain SE_RESTORE_NAME privilege (required for opening a directory)
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tp;
try {
if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) throw ::GetLastError();
if (!::LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid)) throw ::GetLastError();
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!::AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) throw ::GetLastError();
}
catch (DWORD) { } // Ignore errors
if (hToken) ::CloseHandle(hToken);
HANDLE hDir = ::CreateFile(szJunction, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hDir == INVALID_HANDLE_VALUE) throw ::GetLastError();
memset(buf, 0, sizeof(buf));
ReparseBuffer.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
int len = ::MultiByteToWideChar(CP_ACP, 0, szTarget, -1, ReparseBuffer.ReparseTarget, MAX_PATH);
ReparseBuffer.ReparseTargetMaximumLength = (len--) * sizeof(WCHAR);
ReparseBuffer.ReparseTargetLength = len * sizeof(WCHAR);
ReparseBuffer.ReparseDataLength = ReparseBuffer.ReparseTargetLength + 12;
DWORD dwRet;
if (!::DeviceIoControl(hDir, FSCTL_SET_REPARSE_POINT, &ReparseBuffer, ReparseBuffer.ReparseDataLength+REPARSE_MOUNTPOINT_HEADER_SIZE, NULL, 0, &dwRet, NULL)) {
DWORD dr = ::GetLastError();
::CloseHandle(hDir);
::RemoveDirectory(szJunction);
throw dr;
}
::CloseHandle(hDir);
} // CreateJunction
*/