00001
00002
00003
00004
00005
00006
00007
00008
00013 #include <ctype.h>
00014
00015 #include <iostream>
00016
00017 #include "zypp/base/Logger.h"
00018
00019 #include "zypp/media/MediaException.h"
00020 #include "zypp/media/MediaAccess.h"
00021 #include "zypp/media/MediaHandler.h"
00022
00023 #include "zypp/media/MediaNFS.h"
00024 #include "zypp/media/MediaCD.h"
00025 #include "zypp/media/MediaDIR.h"
00026 #include "zypp/media/MediaDISK.h"
00027 #include "zypp/media/MediaSMB.h"
00028 #include "zypp/media/MediaCIFS.h"
00029 #include "zypp/media/MediaCurl.h"
00030 #include "zypp/media/MediaISO.h"
00031
00032 using namespace std;
00033
00034 namespace zypp {
00035 namespace media {
00036
00038
00039
00040
00042
00043 const Pathname MediaAccess::_noPath;
00044
00046
00047 MediaAccess::MediaAccess ()
00048 : _handler (0)
00049 {
00050 }
00051
00052
00053 MediaAccess::~MediaAccess()
00054 {
00055 try
00056 {
00057 close();
00058 }
00059 catch(...) {}
00060 }
00061
00062 AttachedMedia
00063 MediaAccess::attachedMedia() const
00064 {
00065 return _handler ? _handler->attachedMedia()
00066 : AttachedMedia();
00067 }
00068
00069 bool
00070 MediaAccess::isSharedMedia() const
00071 {
00072 return _handler ? _handler->isSharedMedia()
00073 : false;
00074 }
00075
00076 void
00077 MediaAccess::resetParentId()
00078 {
00079 if( _handler) _handler->resetParentId();
00080 }
00081
00082 bool
00083 MediaAccess::dependsOnParent() const
00084 {
00085 return _handler ? _handler->dependsOnParent() : false;
00086 }
00087
00088 bool
00089 MediaAccess::dependsOnParent(MediaAccessId parentId,
00090 bool exactIdMatch) const
00091 {
00092 return _handler ? _handler->dependsOnParent(parentId, exactIdMatch)
00093 : false;
00094 }
00095
00096
00097 void
00098 MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
00099 {
00100 if(!url.isValid()) {
00101 MIL << "Url is not valid" << endl;
00102 ZYPP_THROW(MediaBadUrlException(url));
00103 }
00104
00105 close();
00106
00107 std::string scheme = url.getScheme();
00108
00109 MIL << "Trying scheme '" << scheme << "'" << endl;
00110
00111
00112
00113
00114
00115 if (scheme == "cd" || scheme == "dvd")
00116 _handler = new MediaCD (url,preferred_attach_point);
00117 else if (scheme == "nfs")
00118 _handler = new MediaNFS (url,preferred_attach_point);
00119 else if (scheme == "iso")
00120 _handler = new MediaISO (url,preferred_attach_point);
00121 else if (scheme == "file" || scheme == "dir")
00122 _handler = new MediaDIR (url,preferred_attach_point);
00123 else if (scheme == "hd")
00124 _handler = new MediaDISK (url,preferred_attach_point);
00125 else if (scheme == "smb")
00126 _handler = new MediaSMB (url,preferred_attach_point);
00127 else if (scheme == "cifs")
00128 _handler = new MediaCIFS (url,preferred_attach_point);
00129 else if (scheme == "ftp" || scheme == "http" || scheme == "https")
00130 _handler = new MediaCurl (url,preferred_attach_point);
00131 else
00132 {
00133 ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
00134 }
00135
00136
00137 if ( !_handler ){
00138 ERR << "Failed to create media handler" << endl;
00139 ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
00140 }
00141
00142 MIL << "Opened: " << *this << endl;
00143 }
00144
00145
00146 bool
00147 MediaAccess::downloads(const Url &url)
00148 {
00149 std::string scheme( url.getScheme());
00150 return (scheme == "ftp" || scheme == "http" || scheme == "https");
00151 }
00152
00153
00154 bool
00155 MediaAccess::canBeVolatile(const Url &url)
00156 {
00157 std::string scheme( url.getScheme());
00158 return ! (scheme == "cd" || scheme == "dvd");
00159 }
00160
00161
00162 std::string
00163 MediaAccess::protocol() const
00164 {
00165 if ( !_handler )
00166 return "unknown";
00167
00168 return _handler->protocol();
00169 }
00170
00171 bool
00172 MediaAccess::downloads() const
00173 {
00174 return _handler ? _handler->downloads() : false;
00175 }
00176
00178
00179
00180
00181
00182
00183 Url MediaAccess::url() const
00184 {
00185 if ( !_handler )
00186 return Url();
00187
00188 return _handler->url();
00189 }
00190
00191
00192 void
00193 MediaAccess::close ()
00194 {
00196
00197
00199 if ( _handler ) {
00200 try {
00201 _handler->release();
00202 }
00203 catch (const MediaException & excpt_r)
00204 {
00205 ZYPP_CAUGHT(excpt_r);
00206 WAR << "Close: " << *this << " (" << excpt_r << ")" << endl;
00207 ZYPP_RETHROW(excpt_r);
00208 }
00209 MIL << "Close: " << *this << " (OK)" << endl;
00210 delete _handler;
00211 _handler = 0;
00212 }
00213 }
00214
00215
00216
00217 void MediaAccess::attach (bool next)
00218 {
00219 if ( !_handler ) {
00220 ZYPP_THROW(MediaNotOpenException("attach"));
00221 }
00222 _handler->attach(next);
00223 }
00224
00225
00226 bool
00227 MediaAccess::isAttached() const
00228 {
00229 return( _handler && _handler->isAttached() );
00230 }
00231
00232
00233
00234 Pathname
00235 MediaAccess::localRoot() const
00236 {
00237 if ( !_handler )
00238 return _noPath;
00239
00240 return _handler->localRoot();
00241 }
00242
00243
00244
00245 Pathname
00246 MediaAccess::localPath( const Pathname & pathname ) const
00247 {
00248 if ( !_handler )
00249 return _noPath;
00250
00251 return _handler->localPath( pathname );
00252 }
00253
00254 void
00255 MediaAccess::disconnect()
00256 {
00257 if ( !_handler )
00258 ZYPP_THROW(MediaNotOpenException("disconnect"));
00259
00260 _handler->disconnect();
00261 }
00262
00263
00264 void
00265 MediaAccess::release( bool eject )
00266 {
00267 if ( !_handler )
00268 return;
00269
00270 _handler->release( eject );
00271 }
00272
00273
00274
00275
00276
00277
00278 void
00279 MediaAccess::provideFile( const Pathname & filename, bool cached, bool checkonly) const
00280 {
00281 if ( cached ) {
00282 PathInfo pi( localPath( filename ) );
00283 if ( pi.isExist() )
00284 return;
00285 }
00286
00287 if(checkonly)
00288 ZYPP_THROW(MediaFileNotFoundException(url(), filename));
00289
00290 if ( !_handler ) {
00291 ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
00292 }
00293
00294 _handler->provideFile( filename );
00295 }
00296
00297 void
00298 MediaAccess::releaseFile( const Pathname & filename ) const
00299 {
00300 if ( !_handler )
00301 return;
00302
00303 _handler->releaseFile( filename );
00304 }
00305
00306
00307
00308
00309
00310 void
00311 MediaAccess::provideDir( const Pathname & dirname ) const
00312 {
00313 if ( !_handler ) {
00314 ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
00315 }
00316
00317 _handler->provideDir( dirname );
00318 }
00319
00320 void
00321 MediaAccess::provideDirTree( const Pathname & dirname ) const
00322 {
00323 if ( !_handler ) {
00324 ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
00325 }
00326
00327 _handler->provideDirTree( dirname );
00328 }
00329
00330 void
00331 MediaAccess::releaseDir( const Pathname & dirname ) const
00332 {
00333 if ( !_handler )
00334 return;
00335
00336 _handler->releaseDir( dirname );
00337 }
00338
00339 void
00340 MediaAccess::releasePath( const Pathname & pathname ) const
00341 {
00342 if ( !_handler )
00343 return;
00344
00345 _handler->releasePath( pathname );
00346 }
00347
00348
00349 void
00350 MediaAccess::dirInfo( list<string> & retlist, const Pathname & dirname, bool dots ) const
00351 {
00352 retlist.clear();
00353
00354 if ( !_handler ) {
00355 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
00356 }
00357
00358 _handler->dirInfo( retlist, dirname, dots );
00359 }
00360
00361
00362 void
00363 MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
00364 {
00365 retlist.clear();
00366
00367 if ( !_handler ) {
00368 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
00369 }
00370
00371 _handler->dirInfo( retlist, dirname, dots );
00372 }
00373
00374 std::ostream &
00375 MediaAccess::dumpOn( std::ostream & str ) const
00376 {
00377 if ( !_handler )
00378 return str << "MediaAccess( closed )";
00379
00380 str << _handler->protocol() << "(" << *_handler << ")";
00381 return str;
00382 }
00383
00384 void MediaAccess::getFile( const Url &from, const Pathname &to )
00385 {
00386 DBG << "From: " << from << endl << "To: " << to << endl;
00387
00388 Pathname path = from.getPathData();
00389 Pathname dir = path.dirname();
00390 string base = path.basename();
00391
00392 Url u = from;
00393 u.setPathData( dir.asString() );
00394
00395 MediaAccess media;
00396
00397 try {
00398 media.open( u );
00399 media.attach();
00400 media._handler->provideFileCopy( base, to );
00401 media.release();
00402 }
00403 catch (const MediaException & excpt_r)
00404 {
00405 ZYPP_RETHROW(excpt_r);
00406 }
00407 }
00408 std::ostream & operator<<( std::ostream & str, const MediaAccess & obj )
00409 { return obj.dumpOn( str ); }
00410
00412 }
00413 }