Skip to content

Commit

Permalink
test on windows and fix compile errors
Browse files Browse the repository at this point in the history
- fix boost::spirit::qi usage
- copy close_handle declaration from util.cpp
- initialize std::unique_ptr with HANDLE but use HANDLE directly
  • Loading branch information
p8p committed Aug 23, 2018
1 parent ab6f37c commit e2ff824
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 52 deletions.
97 changes: 51 additions & 46 deletions src/common/is_hdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@
#elif defined(_WIN32) and (_WIN32_WINNT >= 0x0601)
#include <windows.h>
#include <winioctl.h>
#include <regex>
#include <memory>
#include "util.h"
#include <boost/range/iterator_range.hpp>
#include <boost/spirit/include/qi_raw.hpp>
#include <boost/spirit/include/qi_char_.hpp>
#include <boost/spirit/include/qi_lit.hpp>
#include <boost/spirit/include/qi_difference.hpp>
#include <boost/spirit/include/qi_klenee.hpp>
#include <boost/spirit/include/qi_sequance.hpp>
#include <boost/spirit/include/qi_kleene.hpp>
#include <boost/spirit/include/qi_sequence.hpp>
#endif
#include <boost/optional.hpp>

namespace tools
{
#if defined(__GLIBC__)
Expand Down Expand Up @@ -86,20 +83,28 @@ namespace tools
return boost::none;
}
#elif defined(_WIN32) and (_WIN32_WINNT >= 0x0601)

struct close_handle
{
void operator()(HANDLE handle) const noexcept
{
CloseHandle(handle);
}
};
//file path to logical volume
boost::optional<std::string> fp2lv(const char *fp)
{
std::unique_ptr<HANDLE, close_handle> h{
CreateFile(
fp,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
nullptr
)
};
std::unique_ptr<void, close_handle> h_ptr;
HANDLE h = CreateFile(
fp,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
nullptr
);
h_ptr.reset(h);
if(h != INVALID_HANDLE_VALUE)
{
DWORD p_size = GetFinalPathNameByHandleA(
Expand All @@ -112,20 +117,20 @@ namespace tools
DWORD r_size = GetFinalPathNameByHandleA(
h,
&p[0],
p_size,
p_size + 1,
VOLUME_NAME_NT | FILE_NAME_NORMALIZED
);
namespace qi = boost::spirit::qi;
boost::iterator_range<const char> m{};
boost::iterator_range<std::string::const_iterator> m{};
bool success = qi::parse(
p.begin(),
p.end(),
(qi::lit("\\\\Device\\\\") >> *(qi::char_ - "\\\\")),
p.cbegin(),
p.cend(),
(qi::lit("\\Device\\") >> qi::raw[*(qi::char_ - "\\")]),
m
);
if(success and m.begin() != m.end())
if(success and not m.empty())
{
return *m;
return std::string(m.begin(), m.end());
}
}
return boost::none;
Expand All @@ -136,17 +141,17 @@ namespace tools
{
std::string lv_path = "\\\\?\\";
lv_path += lv;
std::unique_ptr<HANDLE, close_handle> h{
CreateFile(
lv_path.c_str(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr
)
};
std::unique_ptr<void, close_handle> h_ptr;
HANDLE h = CreateFile(
lv_path.c_str(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
h_ptr.reset(h);
if(h != INVALID_HANDLE_VALUE)
{
VOLUME_DISK_EXTENTS r;
Expand Down Expand Up @@ -185,17 +190,17 @@ namespace tools
{
std::string pv_path = "\\\\?\\";
pv_path += pv;
std::unique_ptr<HANDLE, close_handle> h{
CreateFile(
pv_path.c_str(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr
)
};
std::unique_ptr<void, close_handle> h_ptr;
HANDLE h = CreateFile(
pv_path.c_str(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
h_ptr.reset(h);
if(h != INVALID_HANDLE_VALUE)
{
STORAGE_PROPERTY_QUERY q{
Expand Down
21 changes: 15 additions & 6 deletions tests/unit_tests/is_hdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@ TEST(is_hdd, linux_os_root)
#elif defined(_WIN32) and (_WIN32_WINNT >= 0x0601)
TEST(is_hdd, win_os_c)
{
bool result;
std::string path = "\\\\?\\C:\\Windows\\System32\\cmd.exe";
EXPECT_TRUE(tools::is_hdd(path.c_str(), result));
EXPECT_TRUE(tools::is_hdd(path.c_str()));
path = "\\\\?\\C:\\";
EXPECT_TRUE(tools::is_hdd(path.c_str(), result));
EXPECT_TRUE(tools::is_hdd(path.c_str()));
path = "C:\\";
EXPECT_TRUE(tools::is_hdd(path.c_str(), result));
EXPECT_TRUE(tools::is_hdd(path.c_str()));
}
TEST(is_hdd, win_os_ssd)
{
std::string path = "C:\\test_ssd\\";
auto r = tools::is_hdd(path.c_str());
EXPECT_TRUE(r);
EXPECT_FALSE(r.value());
path = "C:\\test_ssd\\test.txt";
r = tools::is_hdd(path.c_str());
EXPECT_TRUE(r);
EXPECT_FALSE(r.value());
}
#else
TEST(is_hdd, unknown_os)
{
bool result;
std::string path = "";
EXPECT_FALSE(tools::is_hdd(path.c_str(), result));
EXPECT_FALSE(tools::is_hdd(path.c_str()));
}
#endif

0 comments on commit e2ff824

Please sign in to comment.