11
11
12
12
#if defined(BOOST_FILESYSTEM_HAS_MKLINK)
13
13
14
- #include < boost/filesystem.hpp>
14
+ #include < boost/filesystem/path.hpp>
15
+ #include < boost/filesystem/operations.hpp>
15
16
#include < boost/core/lightweight_test.hpp>
16
17
17
18
#include < cstddef>
19
+ #include < exception>
18
20
19
21
#include < windows.h>
20
22
#include < winnt.h>
@@ -76,15 +78,17 @@ bool obtain_restore_privilege()
76
78
HANDLE hToken;
77
79
if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken))
78
80
{
79
- std::cout << " OpenProcessToken() failed with: " << GetLastError () << std::endl;
81
+ DWORD err = GetLastError ();
82
+ std::cout << " OpenProcessToken() failed with: " << err << std::endl;
80
83
return false ;
81
84
}
82
85
83
86
TOKEN_PRIVILEGES tp;
84
87
if (!LookupPrivilegeValue (NULL , SE_RESTORE_NAME, &tp.Privileges [0 ].Luid ))
85
88
{
89
+ DWORD err = GetLastError ();
86
90
CloseHandle (hToken);
87
- std::cout << " LookupPrivilegeValue() failed with: " << GetLastError () << std::endl;
91
+ std::cout << " LookupPrivilegeValue() failed with: " << err << std::endl;
88
92
return false ;
89
93
}
90
94
@@ -93,8 +97,9 @@ bool obtain_restore_privilege()
93
97
94
98
if (!AdjustTokenPrivileges (hToken, FALSE , &tp, sizeof (TOKEN_PRIVILEGES), NULL , NULL ))
95
99
{
100
+ DWORD err = GetLastError ();
96
101
CloseHandle (hToken);
97
- std::cout << " AdjustTokenPrivileges() failed with: " << GetLastError () << std::endl;
102
+ std::cout << " AdjustTokenPrivileges() failed with: " << err << std::endl;
98
103
return false ;
99
104
}
100
105
@@ -110,23 +115,30 @@ bool create_io_reparse_file_placeholder(const wchar_t* name)
110
115
}
111
116
112
117
HANDLE hHandle = CreateFileW (name, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL , CREATE_ALWAYS, FILE_FLAG_OPEN_REPARSE_POINT, 0 );
113
-
114
118
if (hHandle == INVALID_HANDLE_VALUE)
115
119
{
116
- std::cout << " CreateFile() failed with: " << GetLastError () << std::endl;
120
+ DWORD err = GetLastError ();
121
+ std::cout << " CreateFile() failed with: " << err << std::endl;
117
122
return false ;
118
123
}
119
124
120
125
PREPARSE_DATA_BUFFER pReparse = reinterpret_cast < PREPARSE_DATA_BUFFER >(GlobalAlloc (GPTR, MAXIMUM_REPARSE_DATA_BUFFER_SIZE));
126
+ if (!pReparse)
127
+ {
128
+ DWORD err = GetLastError ();
129
+ CloseHandle (hHandle);
130
+ std::cout << " GlobalAlloc() failed with: " << err << std::endl;
131
+ return false ;
132
+ }
121
133
// note: IO_REPARSE_TAG_FILE_PLACEHOLDER - just to show that reparse point could be not only symlink or junction
122
134
pReparse->ReparseTag = IO_REPARSE_TAG_FILE_PLACEHOLDER;
123
135
124
136
DWORD dwLen;
125
- bool ret = DeviceIoControl (hHandle, FSCTL_SET_REPARSE_POINT, pReparse, pReparse->ReparseDataLength + REPARSE_DATA_BUFFER_HEADER_SIZE, NULL , 0 , &dwLen, NULL ) != 0 ;
126
-
137
+ bool ret = !!DeviceIoControl (hHandle, FSCTL_SET_REPARSE_POINT, pReparse, pReparse->ReparseDataLength + REPARSE_DATA_BUFFER_HEADER_SIZE, NULL , 0 , &dwLen, NULL );
127
138
if (!ret)
128
139
{
129
- std::cout << " DeviceIoControl() failed with: " << GetLastError () << std::endl;
140
+ DWORD err = GetLastError ();
141
+ std::cout << " DeviceIoControl() failed with: " << err << std::endl;
130
142
}
131
143
132
144
CloseHandle (hHandle);
@@ -139,8 +151,9 @@ int main()
139
151
boost::filesystem::path rpt = boost::filesystem::temp_directory_path () / " reparse_point_test.txt" ;
140
152
141
153
BOOST_TEST (create_io_reparse_file_placeholder (rpt.native ().c_str ()));
142
- BOOST_TEST (boost::filesystem::status (rpt).type () == boost::filesystem::reparse_file);
143
- BOOST_TEST (boost::filesystem::remove (rpt));
154
+ std::cout << " Created file placeholder reparse point: " << rpt.string () << std::endl;
155
+ BOOST_TEST_NO_THROW (BOOST_TEST (boost::filesystem::status (rpt).type () == boost::filesystem::reparse_file));
156
+ BOOST_TEST_NO_THROW (BOOST_TEST (boost::filesystem::remove (rpt)));
144
157
145
158
return boost::report_errors ();
146
159
}
0 commit comments