14 #include <sys/ioctl.h> 15 #include <linux/cdrom.h> 38 #define REPORT_EJECT_ERRORS 0 44 #define EJECT_TOOL_PATH "/bin/eject" 58 typedef std::list<MediaSource> DeviceList;
65 DeviceList systemDetectDevices(
bool supportingDVD_r )
74 ERR <<
"Can't create udev context." << endl;
81 ERR <<
"Can't create udev list entry." << endl;
85 ::udev_enumerate_add_match_subsystem( enumerate,
"block" );
86 ::udev_enumerate_add_match_property( enumerate,
"ID_CDROM",
"1" );
87 ::udev_enumerate_scan_devices( enumerate );
89 struct udev_list_entry * entry = 0;
90 udev_list_entry_foreach( entry, ::udev_enumerate_get_list_entry( enumerate ) )
93 ::udev_list_entry_get_name( entry ) ),
94 ::udev_device_unref );
97 ERR <<
"Can't create udev device." << endl;
101 if ( supportingDVD_r && ! ::udev_device_get_property_value( device,
"ID_CDROM_DVD" ) )
106 const char * devnodePtr( ::udev_device_get_devnode( device ) );
109 ERR <<
"Got NULL devicenode." << endl;
116 PathInfo devnode( devnodePtr );
117 if ( devnode.isBlk() )
119 MediaSource media(
"cdrom", devnode.path().asString(), devnode.devMajor(), devnode.devMinor() );
120 DBG <<
"Found (udev): " << media << std::endl;
121 detected.push_back( media );
124 if ( detected.empty() )
126 WAR <<
"Did not find any CD/DVD device." << endl;
137 :
MediaHandler( url_r, attach_point_hint_r, url_r.getPathName(), false )
139 , _lastdev_tried( -1 )
141 MIL <<
"MediaCD::MediaCD(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
145 ERR <<
"Unsupported schema in the Url: " << url_r.
asString() << endl;
150 if ( ! devices.empty() )
152 std::vector<std::string> words;
153 str::split( devices, std::back_inserter(words),
"," );
154 for (
const std::string & device : words )
156 if ( device.empty() )
161 DBG <<
"use device (delayed verify)" << device << endl;
166 DBG <<
"going to use on-demand device list" << endl;
172 ERR <<
"Unable to find any cdrom drive for " <<
_url.
asString() << endl;
185 int fd = ::open( device_r.c_str(), O_RDONLY|O_NONBLOCK|O_CLOEXEC );
190 res = ::ioctl( fd, CDROMEJECT );
198 WAR <<
"Unable to open '" << device_r
199 <<
"' (" <<
::strerror( errno ) <<
")" << endl;
203 WAR <<
"Eject " << device_r
204 <<
" failed (" <<
::strerror( errno ) <<
")" << endl;
207 #if defined(EJECT_TOOL_PATH) 208 DBG <<
"Try to eject " << device_r <<
" using " 213 cmd[1] = device_r.c_str();
223 if(eject.
close() != 0)
225 WAR <<
"Eject of " << device_r <<
" failed." << std::endl;
232 MIL <<
"Eject of " << device_r <<
" successful." << endl;
244 int fd = ::open( device_r.c_str(), O_RDONLY|O_NONBLOCK|O_CLOEXEC );
246 WAR <<
"Unable to open '" << device_r <<
"' (" <<
::strerror( errno ) <<
")" << endl;
249 int res = ::ioctl( fd, CDROMCLOSETRAY );
252 WAR <<
"Close tray " << device_r <<
" failed (" <<
::strerror( errno ) <<
")" << endl;
255 DBG <<
"Close tray " << device_r << endl;
262 DeviceList detected( systemDetectDevices( supportingDVD_r ) );
264 if ( detected.empty() )
266 WAR <<
"CD/DVD drive detection with UDEV failed! Guessing..." << std::endl;
269 if ( dvdinfo.
isBlk() )
272 DBG <<
"Found (GUESS): " << media << std::endl;
273 detected.push_back( media );
279 DBG <<
"Found (GUESS): " << media << std::endl;
280 detected.push_back( media );
289 DBG <<
"creating on-demand device list" << endl;
291 std::string device(
"/dev/cdrom" );
301 if ( detected.empty() )
307 for(
const auto & d : detected )
310 if ( media.equals( d ) )
349 if ( options.empty() )
355 std::list<std::string> filesystems;
357 filesystems.push_back(
"iso9660");
361 filesystems.push_back(
"udf");
366 bool mountsucceeded =
false;
367 for ( DeviceList::iterator it =
_devices.begin() ; ! mountsucceeded && it !=
_devices.end() ; ++it, ++count )
369 DBG <<
"count " << count << endl;
372 DBG <<
"skipping device " << it->name << endl;
382 if ( ! dinfo.
isBlk() )
384 WAR <<
"skipping non block device: " << dinfo << endl;
387 DBG <<
"trying device " << dinfo << endl;
397 DBG <<
"Using a shared media " 406 mountsucceeded =
true;
413 MountEntries::const_iterator e;
414 for( e = entries.begin(); e != entries.end(); ++e)
416 bool is_device =
false;
420 if( dev_path.compare(0,
sizeof(
"/dev/")-1,
"/dev/") == 0 &&
421 dev_info(e->src) && dev_info.isBlk())
426 if( is_device && media->maj_nr == dev_info.devMajor() &&
427 media->min_nr == dev_info.devMinor())
432 DBG <<
"Using a system mounted media " 443 mountsucceeded =
true;
456 for(std::list<std::string>::iterator fsit = filesystems.begin()
457 ; !mountsucceeded && fsit != filesystems.end()
468 mount.
mount(it->name, mountpoint, *fsit, options);
475 while( !(mountsucceeded=
isAttached()) && --limit)
477 WAR <<
"Wait for /proc/mounts update and retry...." << endl;
497 "Unable to verify that the media was mounted",
557 if (!ejectDev.empty())
567 if (!ejectDev.empty())
572 #if REPORT_EJECT_ERRORS 589 #if REPORT_EJECT_ERRORS 590 bool ejected =
false;
599 if ( media->name != ejectDev_r )
606 if( ! dinfo.
isBlk() )
608 WAR <<
"skipping non block device: " << dinfo << endl;
611 DBG <<
"trying device " << dinfo << endl;
620 #if REPORT_EJECT_ERRORS 628 #if REPORT_EJECT_ERRORS 682 const Pathname & dirname,
bool dots )
const 720 if ( ! devices.empty() )
728 devices.push_back( it.name );
732 MIL <<
"got " << devices.size() <<
" detected devices, current: " 733 << (index < devices.size() ? devices[index] :
"<none>")
734 <<
"(" << index <<
")" << endl;
std::string getScheme() const
Returns the scheme name of the URL.
const Pathname & path() const
Return current Pathname.
std::string asString(const DefaultIntegral< Tp, TInitial > &obj)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Store and operate with byte count.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
std::string receiveLine()
Read one line from the input stream.
unsigned int devMinor() const
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
std::string asString() const
Returns a default string representation of the Url object.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
const std::string & asString() const
String representation.
std::list< DirEntry > DirContent
Returned by readdir.
int close()
Wait for the progamm to complete.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Base class for Exception.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
unsigned int devMajor() const
Wrapper class for ::stat/::lstat.
std::string strerror(int errno_r)
Return string describing the error_r code.
Easy-to use interface to the ZYPP dependency resolver.