1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaAccess.cc
17 #include "zypp/base/Logger.h"
18 #include "zypp/ExternalProgram.h"
20 #include "zypp/media/MediaException.h"
21 #include "zypp/media/MediaAccess.h"
22 #include "zypp/media/MediaHandler.h"
24 #include "zypp/media/MediaNFS.h"
25 #include "zypp/media/MediaCD.h"
26 #include "zypp/media/MediaDIR.h"
27 #include "zypp/media/MediaDISK.h"
28 #include "zypp/media/MediaSMB.h"
29 #include "zypp/media/MediaCIFS.h"
30 #include "zypp/media/MediaCurl.h"
31 #include "zypp/media/MediaAria2c.h"
32 #include "zypp/media/MediaISO.h"
39 ///////////////////////////////////////////////////////////////////
41 // CLASS NAME : MediaAccess
43 ///////////////////////////////////////////////////////////////////
45 const Pathname MediaAccess::_noPath; // empty path
47 ///////////////////////////////////////////////////////////////////
49 MediaAccess::MediaAccess ()
55 MediaAccess::~MediaAccess()
59 close(); // !!! make sure handler gets properly deleted.
65 MediaAccess::attachedMedia() const
67 return _handler ? _handler->attachedMedia()
72 MediaAccess::isSharedMedia() const
74 return _handler ? _handler->isSharedMedia()
79 MediaAccess::resetParentId()
81 if( _handler) _handler->resetParentId();
85 MediaAccess::dependsOnParent() const
87 return _handler ? _handler->dependsOnParent() : false;
91 MediaAccess::dependsOnParent(MediaAccessId parentId,
92 bool exactIdMatch) const
94 return _handler ? _handler->dependsOnParent(parentId, exactIdMatch)
100 MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
103 MIL << "Url is not valid" << endl;
104 ZYPP_THROW(MediaBadUrlException(url));
109 std::string scheme = url.getScheme();
111 MIL << "Trying scheme '" << scheme << "'" << endl;
113 ** WARNING: Don't forget to update MediaAccess::downloads(url)
114 ** if you are adding a new url scheme / handler!
116 if (scheme == "cd" || scheme == "dvd")
117 _handler = new MediaCD (url,preferred_attach_point);
118 else if (scheme == "nfs")
119 _handler = new MediaNFS (url,preferred_attach_point);
120 else if (scheme == "iso")
121 _handler = new MediaISO (url,preferred_attach_point);
122 else if (scheme == "file" || scheme == "dir")
123 _handler = new MediaDIR (url,preferred_attach_point);
124 else if (scheme == "hd")
125 _handler = new MediaDISK (url,preferred_attach_point);
126 else if (scheme == "smb")
127 _handler = new MediaSMB (url,preferred_attach_point);
128 else if (scheme == "cifs")
129 _handler = new MediaCIFS (url,preferred_attach_point);
130 else if (scheme == "ftp" || scheme == "http" || scheme == "https")
132 // Another good idea would be activate MediaAria2c handler via external var
133 bool use_aria = true;
134 const char *ariaenv = getenv( "ZYPP_ARIA2C" );
135 // if user disabled it manually
136 if ( ariaenv && ( strcmp(ariaenv, "0" ) == 0 ) )
138 WAR << "aria2c manually disabled. Falling back to curl";
142 // disable if it does not exist
143 if ( ! MediaAria2c::existsAria2cmd() )
145 WAR << "aria2c not found. Falling back to curl";
150 _handler = new MediaAria2c (url,preferred_attach_point);
152 _handler = new MediaCurl (url,preferred_attach_point);
156 ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
159 // check created handler
161 ERR << "Failed to create media handler" << endl;
162 ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
165 MIL << "Opened: " << *this << endl;
170 MediaAccess::downloads(const Url &url)
172 std::string scheme( url.getScheme());
173 return (scheme == "ftp" || scheme == "http" || scheme == "https");
178 MediaAccess::canBeVolatile(const Url &url)
180 std::string scheme( url.getScheme());
181 return ! (scheme == "cd" || scheme == "dvd");
184 // Type of media if open, otherwise NONE.
186 MediaAccess::protocol() const
191 return _handler->protocol();
195 MediaAccess::downloads() const
197 return _handler ? _handler->downloads() : false;
200 ///////////////////////////////////////////////////////////////////
203 // METHOD NAME : MediaAccess::url
206 Url MediaAccess::url() const
211 return _handler->url();
216 MediaAccess::close ()
218 ///////////////////////////////////////////////////////////////////
219 // !!! make shure handler gets properly deleted.
220 // I.e. release attached media before deleting the handler.
221 ///////////////////////////////////////////////////////////////////
226 catch (const MediaException & excpt_r)
228 ZYPP_CAUGHT(excpt_r);
229 WAR << "Close: " << *this << " (" << excpt_r << ")" << endl;
230 ZYPP_RETHROW(excpt_r);
232 MIL << "Close: " << *this << " (OK)" << endl;
240 void MediaAccess::attach (bool next)
243 ZYPP_THROW(MediaNotOpenException("attach"));
245 _handler->attach(next);
248 // True if media is open and attached.
250 MediaAccess::isAttached() const
252 return( _handler && _handler->isAttached() );
256 bool MediaAccess::hasMoreDevices() const
258 return _handler && _handler->hasMoreDevices();
263 MediaAccess::getDetectedDevices(std::vector<std::string> & devices,
264 unsigned int & index) const
268 _handler->getDetectedDevices(devices, index);
272 if (!devices.empty())
278 // local directory that corresponds to medias url
279 // If media is not open an empty pathname.
281 MediaAccess::localRoot() const
286 return _handler->localRoot();
289 // Short for 'localRoot() + pathname', but returns an empty
290 // * pathname if media is not open.
292 MediaAccess::localPath( const Pathname & pathname ) const
297 return _handler->localPath( pathname );
301 MediaAccess::disconnect()
304 ZYPP_THROW(MediaNotOpenException("disconnect"));
306 _handler->disconnect();
311 MediaAccess::release( const std::string & ejectDev )
316 _handler->release( ejectDev );
319 // provide file denoted by path to attach dir
321 // filename is interpreted relative to the attached url
322 // and a path prefix is preserved to destination
324 MediaAccess::provideFile( const Pathname & filename, bool cached, bool checkonly) const
327 PathInfo pi( localPath( filename ) );
333 ZYPP_THROW(MediaFileNotFoundException(url(), filename));
336 ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
339 _handler->provideFile( filename );
343 MediaAccess::releaseFile( const Pathname & filename ) const
348 _handler->releaseFile( filename );
351 // provide directory tree denoted by path to attach dir
353 // dirname is interpreted relative to the attached url
354 // and a path prefix is preserved to destination
356 MediaAccess::provideDir( const Pathname & dirname ) const
359 ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
362 _handler->provideDir( dirname );
366 MediaAccess::provideDirTree( const Pathname & dirname ) const
369 ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
372 _handler->provideDirTree( dirname );
376 MediaAccess::releaseDir( const Pathname & dirname ) const
381 _handler->releaseDir( dirname );
385 MediaAccess::releasePath( const Pathname & pathname ) const
390 _handler->releasePath( pathname );
393 // Return content of directory on media
395 MediaAccess::dirInfo( std::list<std::string> & retlist, const Pathname & dirname, bool dots ) const
400 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
403 _handler->dirInfo( retlist, dirname, dots );
406 // Return content of directory on media
408 MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
413 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
416 _handler->dirInfo( retlist, dirname, dots );
419 // return if a file exists
421 MediaAccess::doesFileExist( const Pathname & filename ) const
424 ZYPP_THROW(MediaNotOpenException("doesFileExist(" + filename.asString() + ")"));
427 return _handler->doesFileExist( filename );
431 MediaAccess::dumpOn( std::ostream & str ) const
434 return str << "MediaAccess( closed )";
436 str << _handler->protocol() << "(" << *_handler << ")";
440 void MediaAccess::getFile( const Url &from, const Pathname &to )
442 DBG << "From: " << from << endl << "To: " << to << endl;
444 Pathname path = from.getPathData();
445 Pathname dir = path.dirname();
446 string base = path.basename();
449 u.setPathData( dir.asString() );
456 media._handler->provideFileCopy( base, to );
459 catch (const MediaException & excpt_r)
461 ZYPP_RETHROW(excpt_r);
465 std::ostream & operator<<( std::ostream & str, const MediaAccess & obj )
466 { return obj.dumpOn( str ); }
468 ///////////////////////////////////////////////////////////////////