refactoring

This commit is contained in:
David Kocik 2019-11-27 11:33:36 +01:00
parent 4822b577d2
commit fdf159af42
3 changed files with 58 additions and 75 deletions

View file

@ -360,7 +360,7 @@ std::string AppConfig::get_last_output_dir(const std::string &alt) const
{ {
if (GUI::RemovableDriveManager::getInstance().update()) if (GUI::RemovableDriveManager::getInstance().update())
{ {
return GUI::RemovableDriveManager::getInstance().getLastDrivePath(); return GUI::RemovableDriveManager::getInstance().get_last_drive_path();
} }
const auto it = m_storage.find(""); const auto it = m_storage.find("");
if (it != m_storage.end()) { if (it != m_storage.end()) {

View file

@ -1,14 +1,12 @@
#include "RemovableDriveManager.hpp" #include "RemovableDriveManager.hpp"
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include "boost/nowide/convert.hpp"
#if _WIN32 #if _WIN32
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>
#include <winioctl.h>
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED); 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
#else #else
@ -23,16 +21,13 @@ DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
namespace Slic3r { namespace Slic3r {
namespace GUI { namespace GUI {
std::vector<DriveData> RemovableDriveManager::currentDrives; std::vector<DriveData> RemovableDriveManager::m_current_drives;
#if _WIN32 #if _WIN32
void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives) void RemovableDriveManager::search_for_drives()
{ {
newDrives.clear(); m_current_drives.clear();
newDrives.reserve(26); m_current_drives.reserve(26);
DWORD drivesMask = GetLogicalDrives(); DWORD drivesMask = GetLogicalDrives();
for (size_t i = 0; i < 26; i++) for (size_t i = 0; i < 26; i++)
{ {
@ -65,7 +60,7 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
//std::cout << std::string(volumeName.begin(), volumeName.end()) << " " << std::string(fileSystemName.begin(), fileSystemName.end()) << " " << freeSpace.QuadPart << "\n"; //std::cout << std::string(volumeName.begin(), volumeName.end()) << " " << std::string(fileSystemName.begin(), fileSystemName.end()) << " " << freeSpace.QuadPart << "\n";
if (freeSpace.QuadPart > 0) if (freeSpace.QuadPart > 0)
{ {
newDrives.push_back(DriveData(volumeName, path)); m_current_drives.push_back(DriveData(boost::nowide::narrow(volumeName), path));
} }
} }
} }
@ -77,49 +72,51 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
} }
} }
void RemovableDriveManager::ejectDrive(std::string path) void RemovableDriveManager::eject_drive(const std::string &path)
{ {
if (!update() || !isDriveMounted(path))
return;
path = "\\\\.\\"+path; //if (!update() || !is_drive_mounted(path))
HANDLE handle = CreateFileA(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); if(m_current_drives.empty())
if(handle == INVALID_HANDLE_VALUE)
{
std::cerr << "Ejecting " << path << " failed " << GetLastError() << " \n";
return; return;
} for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
DWORD deviceControlRetVal(0);
BOOL error = DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, nullptr, 0,nullptr , 0, &deviceControlRetVal, nullptr);
CloseHandle(handle);
if(error != 0)
std::cout << "Ejected " << path << "\n";
else
std::cerr << "Ejecting " << path << " failed "<< deviceControlRetVal << " " << GetLastError() <<" \n";
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
{ {
if ((*it).path == path) if ((*it).path == path)
{ {
currentDrives.erase(it); std::string mpath = "\\\\.\\" + path;
HANDLE handle = CreateFileA(mpath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE)
{
std::cerr << "Ejecting " << mpath << " failed " << GetLastError() << " \n";
return;
}
DWORD deviceControlRetVal(0);
BOOL error = DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr);
CloseHandle(handle);
if (error != 0)
std::cout << "Ejected " << mpath << "\n";
else
std::cerr << "Ejecting " << mpath << " failed " << deviceControlRetVal << " " << GetLastError() << " \n";
m_current_drives.erase(it);
break; break;
} }
} }
} }
#else #else
void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives) void RemovableDriveManager::search_for_drives()
{ {
struct stat buf; struct stat buf;
std::string path(std::getenv("USER")); std::string path(std::getenv("USER"));
std::string pp(path); std::string pp(path);
newDrives.clear(); m_current_drives.clear();
newDrives.reserve(26); m_current_Drives.reserve(26);
//search /media/* folder //search /media/* folder
stat("/media/",&buf); stat("/media/",&buf);
std::cout << "/media ID: " <<buf.st_dev << "\n"; std::cout << "/media ID: " <<buf.st_dev << "\n";
searchPath(newDrives, "/media/*", buf.st_dev); search_path("/media/*", buf.st_dev);
//search /media/USERNAME/* folder //search /media/USERNAME/* folder
pp = "/media/"+pp; pp = "/media/"+pp;
@ -127,18 +124,18 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
stat(pp.c_str() ,&buf); stat(pp.c_str() ,&buf);
std::cout << pp <<" ID: " <<buf.st_dev << "\n"; std::cout << pp <<" ID: " <<buf.st_dev << "\n";
searchPath(newDrives, path, buf.st_dev); searchPath(path, buf.st_dev);
//search /run/media/USERNAME/* folder //search /run/media/USERNAME/* folder
path = "/run" + path; path = "/run" + path;
pp = "/run"+pp; pp = "/run"+pp;
stat(pp.c_str() ,&buf); stat(pp.c_str() ,&buf);
std::cout << pp <<" ID: " <<buf.st_dev << "\n"; std::cout << pp <<" ID: " <<buf.st_dev << "\n";
searchPath(newDrives, path, buf.st_dev); searchPath(path, buf.st_dev);
std::cout << "found drives:" <<newDrives.size() << "\n"; std::cout << "found drives:" <<newDrives.size() << "\n";
} }
void RemovableDriveManager::searchPath(std::vector<DriveData>& newDrives,const std::string path, const dev_t parentDevID) void RemovableDriveManager::search_path(const std::string &path,const dev_t &parentDevID)
{ {
glob_t globbuf; glob_t globbuf;
globbuf.gl_offs = 2; globbuf.gl_offs = 2;
@ -159,17 +156,17 @@ void RemovableDriveManager::searchPath(std::vector<DriveData>& newDrives,const s
std::cout << buf.st_dev << "\n"; std::cout << buf.st_dev << "\n";
if(buf.st_dev != parentDevID)// not same file system if(buf.st_dev != parentDevID)// not same file system
{ {
newDrives.push_back(DriveData(name,globbuf.gl_pathv[i])); m_current_drives.push_back(DriveData(name,globbuf.gl_pathv[i]));
} }
} }
globfree(&globbuf); globfree(&globbuf);
} }
void RemovableDriveManager::ejectDrive(std::string path) void RemovableDriveManager::eject_drive(const std::string &path)
{ {
if (currentDrives.empty()) if (m_current_drives.empty())
return; return;
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it) for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
{ {
if((*it).path == path) if((*it).path == path)
{ {
@ -180,7 +177,7 @@ void RemovableDriveManager::ejectDrive(std::string path)
int errsv = errno; int errsv = errno;
std::cerr<<"Ejecting failed Error "<< errsv<<"\n"; std::cerr<<"Ejecting failed Error "<< errsv<<"\n";
} }
currentDrives.erase(it); m_current_drives.erase(it);
break; break;
} }
@ -190,22 +187,14 @@ void RemovableDriveManager::ejectDrive(std::string path)
#endif #endif
bool RemovableDriveManager::update() bool RemovableDriveManager::update()
{ {
searchForDrives(currentDrives); search_for_drives();
return !currentDrives.empty(); return !m_current_drives.empty();
} }
void RemovableDriveManager::updateCurrentDrives(const std::vector<DriveData>& newDrives)
bool RemovableDriveManager::is_drive_mounted(const std::string &path)
{ {
currentDrives.clear(); for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
currentDrives.reserve(26);
for (auto it = newDrives.begin(); it != newDrives.end(); ++it)
{
currentDrives.push_back(*it);
}
}
bool RemovableDriveManager::isDriveMounted(std::string path)
{
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
{ {
if ((*it).path == path) if ((*it).path == path)
{ {
@ -215,21 +204,16 @@ bool RemovableDriveManager::isDriveMounted(std::string path)
return false; return false;
} }
std::string RemovableDriveManager::getLastDrivePath() std::string RemovableDriveManager::get_last_drive_path()
{ {
if (!currentDrives.empty()) if (!m_current_drives.empty())
{ {
return currentDrives.back().path; return m_current_drives.back().path;
} }
return ""; return "";
} }
void RemovableDriveManager::getAllDrives(std::vector<DriveData>& drives) std::vector<DriveData> RemovableDriveManager::get_all_drives()
{ {
drives.clear(); return m_current_drives;
drives.reserve(26);
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
{
drives.push_back(*it);
}
} }
}} }}

View file

@ -25,18 +25,17 @@ public:
//update() searches for removable devices, returns false if empty. //update() searches for removable devices, returns false if empty.
static bool update(); static bool update();
static bool isDriveMounted(std::string path); static bool is_drive_mounted(const std::string &path);
static void ejectDrive(std::string path); static void eject_drive(const std::string &path);
static std::string getLastDrivePath(); static std::string get_last_drive_path();
static void getAllDrives(std::vector<DriveData>& drives); static std::vector<DriveData> get_all_drives();
private: private:
RemovableDriveManager(){} RemovableDriveManager(){}
static void searchForDrives(std::vector<DriveData>& newDrives); static void search_for_drives();
static void updateCurrentDrives(const std::vector<DriveData>& newDrives); static std::vector<DriveData> m_current_drives;
static std::vector<DriveData> currentDrives;
#if _WIN32 #if _WIN32
#else #else
static void searchPath(std::vector<DriveData>& newDrives,const std::string path, const dev_t parentDevID); static void search_path(const std::string &path, const dev_t &parentDevID);
#endif #endif
}; };
}} }}