#include "RemovableDriveManager.hpp" #include #include #if _WIN32 #include #include DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED); #else //linux includes #include #include #include #include #include #endif namespace Slic3r { namespace GUI { std::vector RemovableDriveManager::currentDrives; #if _WIN32 void RemovableDriveManager::searchForDrives(std::vector& newDrives) { newDrives.clear(); newDrives.reserve(26); DWORD drivesMask = GetLogicalDrives(); for (size_t i = 0; i < 26; i++) { if(drivesMask & (1 << i)) { std::string path (1,(char)('A' + i)); path+=":"; UINT driveType = GetDriveTypeA(path.c_str()); //std::cout << "found drive" << (char)('A' + i) << ": type:" < 0) { newDrives.push_back(DriveData(volumeName, path)); } } } } else if(driveType == 3)//disks and usb drives { } } } } void RemovableDriveManager::ejectDrive(std::string path) { if (!update() || !isDriveMounted(path)) return; path = "\\\\.\\"+path; HANDLE handle = CreateFileA(path.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 " << path << " 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 " << path << "\n"; else std::cerr << "Ejecting " << path << " failed "<< deviceControlRetVal << " " << GetLastError() <<" \n"; for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it) { if ((*it).path == path) { currentDrives.erase(it); break; } } } #else void RemovableDriveManager::searchForDrives(std::vector& newDrives) { struct stat buf; std::string path(std::getenv("USER")); std::string pp(path); newDrives.clear(); newDrives.reserve(26); //search /media/* folder stat("/media/",&buf); std::cout << "/media ID: " <& newDrives,const std::string path, const dev_t parentDevID) { glob_t globbuf; globbuf.gl_offs = 2; std::cout<<"searching "<& newDrives) { currentDrives.clear(); 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) { return true; } } return false; } std::string RemovableDriveManager::getLastDrivePath() { if (!currentDrives.empty()) { return currentDrives.back().path; } return ""; } void RemovableDriveManager::getAllDrives(std::vector& drives) { drives.clear(); drives.reserve(26); for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it) { drives.push_back(*it); } } }}