Skip to content

Commit f793005

Browse files
committed
Corrected error handling and aded checks for exceptions.
1 parent f2b09fc commit f793005

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

test/issues/reparse_tag_file_placeholder.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
#if defined(BOOST_FILESYSTEM_HAS_MKLINK)
1313

14-
#include <boost/filesystem.hpp>
14+
#include <boost/filesystem/path.hpp>
15+
#include <boost/filesystem/operations.hpp>
1516
#include <boost/core/lightweight_test.hpp>
1617

1718
#include <cstddef>
19+
#include <exception>
1820

1921
#include <windows.h>
2022
#include <winnt.h>
@@ -76,15 +78,17 @@ bool obtain_restore_privilege()
7678
HANDLE hToken;
7779
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
7880
{
79-
std::cout << "OpenProcessToken() failed with: " << GetLastError() << std::endl;
81+
DWORD err = GetLastError();
82+
std::cout << "OpenProcessToken() failed with: " << err << std::endl;
8083
return false;
8184
}
8285

8386
TOKEN_PRIVILEGES tp;
8487
if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid))
8588
{
89+
DWORD err = GetLastError();
8690
CloseHandle(hToken);
87-
std::cout << "LookupPrivilegeValue() failed with: " << GetLastError() << std::endl;
91+
std::cout << "LookupPrivilegeValue() failed with: " << err << std::endl;
8892
return false;
8993
}
9094

@@ -93,8 +97,9 @@ bool obtain_restore_privilege()
9397

9498
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
9599
{
100+
DWORD err = GetLastError();
96101
CloseHandle(hToken);
97-
std::cout << "AdjustTokenPrivileges() failed with: " << GetLastError() << std::endl;
102+
std::cout << "AdjustTokenPrivileges() failed with: " << err << std::endl;
98103
return false;
99104
}
100105

@@ -110,23 +115,30 @@ bool create_io_reparse_file_placeholder(const wchar_t* name)
110115
}
111116

112117
HANDLE hHandle = CreateFileW(name, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_FLAG_OPEN_REPARSE_POINT, 0);
113-
114118
if (hHandle == INVALID_HANDLE_VALUE)
115119
{
116-
std::cout << "CreateFile() failed with: " << GetLastError() << std::endl;
120+
DWORD err = GetLastError();
121+
std::cout << "CreateFile() failed with: " << err << std::endl;
117122
return false;
118123
}
119124

120125
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+
}
121133
//note: IO_REPARSE_TAG_FILE_PLACEHOLDER - just to show that reparse point could be not only symlink or junction
122134
pReparse->ReparseTag = IO_REPARSE_TAG_FILE_PLACEHOLDER;
123135

124136
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);
127138
if (!ret)
128139
{
129-
std::cout << "DeviceIoControl() failed with: " << GetLastError() << std::endl;
140+
DWORD err = GetLastError();
141+
std::cout << "DeviceIoControl() failed with: " << err << std::endl;
130142
}
131143

132144
CloseHandle(hHandle);
@@ -139,8 +151,9 @@ int main()
139151
boost::filesystem::path rpt = boost::filesystem::temp_directory_path() / "reparse_point_test.txt";
140152

141153
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)));
144157

145158
return boost::report_errors();
146159
}

0 commit comments

Comments
 (0)