This commit is contained in:
Enrico Turri 2020-03-10 12:03:26 +01:00
commit 33470f7f6d
2 changed files with 50 additions and 15 deletions

View file

@ -21,6 +21,7 @@
#include <pwd.h> #include <pwd.h>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/filesystem/convenience.hpp> #include <boost/filesystem/convenience.hpp>
#include <boost/process.hpp>
#endif #endif
namespace Slic3r { namespace Slic3r {
@ -329,27 +330,40 @@ void RemovableDriveManager::eject_drive()
auto it_drive_data = this->find_last_save_path_drive_data(); auto it_drive_data = this->find_last_save_path_drive_data();
if (it_drive_data != m_current_drives.end()) { if (it_drive_data != m_current_drives.end()) {
std::string correct_path(m_last_save_path); std::string correct_path(m_last_save_path);
#ifndef __APPLE__
for (size_t i = 0; i < correct_path.size(); ++i) for (size_t i = 0; i < correct_path.size(); ++i)
if (correct_path[i]==' ') { if (correct_path[i]==' ') {
correct_path = correct_path.insert(i,1,'\\'); correct_path = correct_path.insert(i,1,'\\');
++ i; ++ i;
} }
#endif
//std::cout<<"Ejecting "<<(*it).name<<" from "<< correct_path<<"\n"; //std::cout<<"Ejecting "<<(*it).name<<" from "<< correct_path<<"\n";
// there is no usable command in c++ so terminal command is used instead // there is no usable command in c++ so terminal command is used instead
// but neither triggers "succesful safe removal messege" // but neither triggers "succesful safe removal messege"
std::string command = BOOST_LOG_TRIVIAL(info) << "Ejecting started";
boost::process::ipstream istd_err;
boost::process::child child(
#if __APPLE__ #if __APPLE__
//this->eject_device(m_last_save_path); boost::process::search_path("diskutil"), "eject", correct_path.c_str(), (boost::process::std_out & boost::process::std_err) > istd_err);
"diskutil unmount "; //Another option how to eject at mac. Currently not working.
//used insted of system() command;
//this->eject_device(correct_path);
#else #else
"umount "; boost::process::search_path("umount"), correct_path.c_str(), (boost::process::std_out & boost::process::std_err) > istd_err);
#endif #endif
command += correct_path; std::string line;
int err = system(command.c_str()); while (child.running() && std::getline(istd_err, line)) {
if (err) { BOOST_LOG_TRIVIAL(trace) << line;
BOOST_LOG_TRIVIAL(error) << "Ejecting " << m_last_save_path << " failed"; }
// wait for command to finnish (blocks ui thread)
child.wait();
int err = child.exit_code();
if(err)
{
BOOST_LOG_TRIVIAL(error) << "Ejecting failed";
return; return;
} }
BOOST_LOG_TRIVIAL(info) << "Ejecting finished";
assert(m_callback_evt_handler); assert(m_callback_evt_handler);
if (m_callback_evt_handler) if (m_callback_evt_handler)

View file

@ -4,10 +4,22 @@
#import <AppKit/AppKit.h> #import <AppKit/AppKit.h>
#import <DiskArbitration/DiskArbitration.h> #import <DiskArbitration/DiskArbitration.h>
static void eject_callback(DADiskRef disk, DADissenterRef dissenter, void *context)
{
NSLog(@"eject successfull");
}
static void unmount_callback(DADiskRef disk, DADissenterRef dissenter, void *context)
{
//if (dissenter) {
//?
//} else {
DADiskEject(disk, kDADiskEjectOptionDefault, eject_callback, context);
//}
}
@implementation RemovableDriveManagerMM @implementation RemovableDriveManagerMM
-(instancetype) init -(instancetype) init
{ {
self = [super init]; self = [super init];
@ -59,9 +71,17 @@
CFTypeRef mediaEjectableKey = CFDictionaryGetValue(descDict,kDADiskDescriptionMediaEjectableKey); CFTypeRef mediaEjectableKey = CFDictionaryGetValue(descDict,kDADiskDescriptionMediaEjectableKey);
BOOL ejectable = [mediaEjectableKey boolValue]; BOOL ejectable = [mediaEjectableKey boolValue];
CFTypeRef deviceProtocolName = CFDictionaryGetValue(descDict,kDADiskDescriptionDeviceProtocolKey); CFTypeRef deviceProtocolName = CFDictionaryGetValue(descDict,kDADiskDescriptionDeviceProtocolKey);
CFTypeRef deviceModelKey = CFDictionaryGetValue(descDict, kDADiskDescriptionDeviceModelKey); CFTypeRef deviceModelKey = CFDictionaryGetValue(descDict, kDADiskDescriptionDeviceModelKey);
//debug logging
/*
if (deviceProtocolName)
NSLog(@"%@",(CFStringRef)deviceProtocolName);
if (deviceModelKey)
NSLog(@"-%@",(CFStringRef)deviceModelKey);
*/
if (mediaEjectableKey != nullptr) { if (mediaEjectableKey != nullptr) {
BOOL op = ejectable && (CFEqual(deviceProtocolName, CFSTR("USB")) || CFEqual(deviceModelKey, CFSTR("SD Card Reader"))); BOOL op = ejectable && (CFEqual(deviceProtocolName, CFSTR("USB")) || CFEqual(deviceModelKey, CFSTR("SD Card Reader")) || CFEqual(deviceProtocolName, CFSTR("Secure Digital")));
//!CFEqual(deviceModelKey, CFSTR("Disk Image")); //!CFEqual(deviceModelKey, CFSTR("Disk Image"));
if (op) if (op)
[result addObject:volURL.path]; [result addObject:volURL.path];
@ -86,7 +106,8 @@
if (err == 0) if (err == 0)
disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)url); disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)url);
if( err == 0) if( err == 0)
DADiskUnmount(disk, kDADiskUnmountOptionDefault, nullptr, nullptr); //DADiskUnmount(disk, kDADiskUnmountOptionDefault, nullptr, nullptr);
DADiskUnmount(disk, kDADiskUnmountOptionWhole | kDADiskUnmountOptionForce, unmount_callback, nullptr);
if (disk != nullptr) if (disk != nullptr)
CFRelease(disk); CFRelease(disk);
if (session != nullptr) if (session != nullptr)