From e2ff82472b794ba3269560485555fa5c5fe1a475 Mon Sep 17 00:00:00 2001 From: p8p Date: Wed, 22 Aug 2018 05:50:23 -0700 Subject: [PATCH] test on windows and fix compile errors - fix boost::spirit::qi usage - copy close_handle declaration from util.cpp - initialize std::unique_ptr with HANDLE but use HANDLE directly --- src/common/is_hdd.cpp | 97 +++++++++++++++++++------------------ tests/unit_tests/is_hdd.cpp | 21 +++++--- 2 files changed, 66 insertions(+), 52 deletions(-) diff --git a/src/common/is_hdd.cpp b/src/common/is_hdd.cpp index 19e02f59422..3cbbe8c4e0b 100644 --- a/src/common/is_hdd.cpp +++ b/src/common/is_hdd.cpp @@ -37,18 +37,15 @@ #elif defined(_WIN32) and (_WIN32_WINNT >= 0x0601) #include #include - #include - #include - #include "util.h" #include + #include #include #include #include - #include - #include + #include + #include #endif #include - namespace tools { #if defined(__GLIBC__) @@ -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 fp2lv(const char *fp) { - std::unique_ptr h{ - CreateFile( - fp, - 0, - FILE_SHARE_READ | FILE_SHARE_WRITE, - nullptr, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, - nullptr - ) - }; + std::unique_ptr 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( @@ -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 m{}; + boost::iterator_range 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; @@ -136,17 +141,17 @@ namespace tools { std::string lv_path = "\\\\?\\"; lv_path += lv; - std::unique_ptr h{ - CreateFile( - lv_path.c_str(), - 0, - FILE_SHARE_READ | FILE_SHARE_WRITE, - nullptr, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - nullptr - ) - }; + std::unique_ptr 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; @@ -185,17 +190,17 @@ namespace tools { std::string pv_path = "\\\\?\\"; pv_path += pv; - std::unique_ptr h{ - CreateFile( - pv_path.c_str(), - 0, - FILE_SHARE_READ | FILE_SHARE_WRITE, - nullptr, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - nullptr - ) - }; + std::unique_ptr 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{ diff --git a/tests/unit_tests/is_hdd.cpp b/tests/unit_tests/is_hdd.cpp index 29bc8b5246c..e7011d5d78d 100644 --- a/tests/unit_tests/is_hdd.cpp +++ b/tests/unit_tests/is_hdd.cpp @@ -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