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" || scheme == "nfs4")
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" << endl;
142 // disable if it does not exist
143 if ( use_aria && ! MediaAria2c::existsAria2cmd() )
145 WAR << "aria2c not found. Falling back to curl" << endl;
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;
168 // Type of media if open, otherwise NONE.
170 MediaAccess::protocol() const
175 return _handler->protocol();
179 MediaAccess::downloads() const
181 return _handler ? _handler->downloads() : false;
184 ///////////////////////////////////////////////////////////////////
187 // METHOD NAME : MediaAccess::url
190 Url MediaAccess::url() const
195 return _handler->url();
200 MediaAccess::close ()
202 ///////////////////////////////////////////////////////////////////
203 // !!! make shure handler gets properly deleted.
204 // I.e. release attached media before deleting the handler.
205 ///////////////////////////////////////////////////////////////////
210 catch (const MediaException & excpt_r)
212 ZYPP_CAUGHT(excpt_r);
213 WAR << "Close: " << *this << " (" << excpt_r << ")" << endl;
214 ZYPP_RETHROW(excpt_r);
216 MIL << "Close: " << *this << " (OK)" << endl;
224 void MediaAccess::attach (bool next)
227 ZYPP_THROW(MediaNotOpenException("attach"));
229 _handler->attach(next);
232 // True if media is open and attached.
234 MediaAccess::isAttached() const
236 return( _handler && _handler->isAttached() );
240 bool MediaAccess::hasMoreDevices() const
242 return _handler && _handler->hasMoreDevices();
247 MediaAccess::getDetectedDevices(std::vector<std::string> & devices,
248 unsigned int & index) const
252 _handler->getDetectedDevices(devices, index);
256 if (!devices.empty())
262 // local directory that corresponds to medias url
263 // If media is not open an empty pathname.
265 MediaAccess::localRoot() const
270 return _handler->localRoot();
273 // Short for 'localRoot() + pathname', but returns an empty
274 // * pathname if media is not open.
276 MediaAccess::localPath( const Pathname & pathname ) const
281 return _handler->localPath( pathname );
285 MediaAccess::disconnect()
288 ZYPP_THROW(MediaNotOpenException("disconnect"));
290 _handler->disconnect();
295 MediaAccess::release( const std::string & ejectDev )
300 _handler->release( ejectDev );
303 // provide file denoted by path to attach dir
305 // filename is interpreted relative to the attached url
306 // and a path prefix is preserved to destination
308 MediaAccess::provideFile( const Pathname & filename ) const
311 ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
314 _handler->provideFile( filename );
318 MediaAccess::releaseFile( const Pathname & filename ) const
323 _handler->releaseFile( filename );
326 // provide directory tree denoted by path to attach dir
328 // dirname is interpreted relative to the attached url
329 // and a path prefix is preserved to destination
331 MediaAccess::provideDir( const Pathname & dirname ) const
334 ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
337 _handler->provideDir( dirname );
341 MediaAccess::provideDirTree( const Pathname & dirname ) const
344 ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
347 _handler->provideDirTree( dirname );
351 MediaAccess::releaseDir( const Pathname & dirname ) const
356 _handler->releaseDir( dirname );
360 MediaAccess::releasePath( const Pathname & pathname ) const
365 _handler->releasePath( pathname );
368 // Return content of directory on media
370 MediaAccess::dirInfo( std::list<std::string> & retlist, const Pathname & dirname, bool dots ) const
375 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
378 _handler->dirInfo( retlist, dirname, dots );
381 // Return content of directory on media
383 MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
388 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
391 _handler->dirInfo( retlist, dirname, dots );
394 // return if a file exists
396 MediaAccess::doesFileExist( const Pathname & filename ) const
399 ZYPP_THROW(MediaNotOpenException("doesFileExist(" + filename.asString() + ")"));
402 return _handler->doesFileExist( filename );
406 MediaAccess::dumpOn( std::ostream & str ) const
409 return str << "MediaAccess( closed )";
411 str << _handler->protocol() << "(" << *_handler << ")";
415 void MediaAccess::getFile( const Url &from, const Pathname &to )
417 DBG << "From: " << from << endl << "To: " << to << endl;
419 Pathname path = from.getPathData();
420 Pathname dir = path.dirname();
421 string base = path.basename();
424 u.setPathData( dir.asString() );
431 media._handler->provideFileCopy( base, to );
434 catch (const MediaException & excpt_r)
436 ZYPP_RETHROW(excpt_r);
440 std::ostream & operator<<( std::ostream & str, const MediaAccess & obj )
441 { return obj.dumpOn( str ); }
443 ///////////////////////////////////////////////////////////////////