1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaAccess.cc
17 #include "zypp/base/Logger.h"
19 #include "zypp/media/MediaException.h"
20 #include "zypp/media/MediaAccess.h"
21 #include "zypp/media/MediaHandler.h"
23 #include "zypp/media/MediaNFS.h"
24 #include "zypp/media/MediaCD.h"
25 #include "zypp/media/MediaDIR.h"
26 #include "zypp/media/MediaDISK.h"
27 #include "zypp/media/MediaSMB.h"
28 #include "zypp/media/MediaCIFS.h"
29 #include "zypp/media/MediaCurl.h"
36 ///////////////////////////////////////////////////////////////////
38 // CLASS NAME : MediaAccess
40 ///////////////////////////////////////////////////////////////////
42 const Pathname MediaAccess::_noPath; // empty path
44 ///////////////////////////////////////////////////////////////////
46 MediaAccess::MediaAccess ()
52 MediaAccess::~MediaAccess()
56 close(); // !!! make sure handler gets properly deleted.
62 MediaAccess::attachedMedia() const
64 return _handler ? _handler->attachedMedia()
69 MediaAccess::isSharedMedia() const
71 return _handler ? _handler->isSharedMedia()
77 MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
80 MIL << "Url is not valid" << endl;
81 ZYPP_THROW(MediaBadUrlException(url));
86 std::string scheme = url.getScheme();
88 MIL << "Trying scheme '" << scheme << "'" << endl;
90 if (scheme == "cd" || scheme == "dvd")
91 _handler = new MediaCD (url,preferred_attach_point);
92 else if (scheme == "nfs")
93 _handler = new MediaNFS (url,preferred_attach_point);
94 // else if (scheme == "iso")
95 // _handler = new MediaISO (url,preferred_attach_point);
96 else if (scheme == "file" || scheme == "dir")
97 _handler = new MediaDIR (url,preferred_attach_point);
98 else if (scheme == "hd")
99 _handler = new MediaDISK (url,preferred_attach_point);
100 else if (scheme == "smb")
101 _handler = new MediaSMB (url,preferred_attach_point);
102 else if (scheme == "cifs")
103 _handler = new MediaCIFS (url,preferred_attach_point);
104 else if (scheme == "ftp" || scheme == "http" || scheme == "https")
105 _handler = new MediaCurl (url,preferred_attach_point);
108 ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
111 // check created handler
113 ERR << "Failed to create media handler" << endl;
114 ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
117 MIL << "Opened: " << *this << endl;
120 // Type of media if open, otherwise NONE.
122 MediaAccess::protocol() const
127 return _handler->protocol();
130 ///////////////////////////////////////////////////////////////////
133 // METHOD NAME : MediaAccess::url
136 Url MediaAccess::url() const
141 return _handler->url();
146 MediaAccess::close ()
148 ///////////////////////////////////////////////////////////////////
149 // !!! make shure handler gets properly deleted.
150 // I.e. release attached media before deleting the handler.
151 ///////////////////////////////////////////////////////////////////
156 catch (const MediaException & excpt_r)
158 ZYPP_CAUGHT(excpt_r);
159 WAR << "Close: " << *this << " (" << excpt_r << ")" << endl;
160 ZYPP_RETHROW(excpt_r);
162 MIL << "Close: " << *this << " (OK)" << endl;
170 void MediaAccess::attach (bool next)
173 ZYPP_THROW(MediaNotOpenException("attach"));
175 _handler->attach(next);
178 // True if media is open and attached.
180 MediaAccess::isAttached() const
182 return( _handler && _handler->isAttached() );
185 // local directory that corresponds to medias url
186 // If media is not open an empty pathname.
188 MediaAccess::localRoot() const
193 return _handler->localRoot();
196 // Short for 'localRoot() + pathname', but returns an empty
197 // * pathname if media is not open.
199 MediaAccess::localPath( const Pathname & pathname ) const
204 return _handler->localPath( pathname );
208 MediaAccess::disconnect()
211 ZYPP_THROW(MediaNotOpenException("disconnect"));
213 _handler->disconnect();
216 // release attached media
218 MediaAccess::release( bool eject )
223 _handler->release( eject );
227 // provide file denoted by path to attach dir
229 // filename is interpreted relative to the attached url
230 // and a path prefix is preserved to destination
232 MediaAccess::provideFile( const Pathname & filename, bool cached, bool checkonly) const
235 PathInfo pi( localPath( filename ) );
241 ZYPP_THROW(MediaFileNotFoundException(url(), filename));
244 ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
247 _handler->provideFile( filename );
251 MediaAccess::releaseFile( const Pathname & filename ) const
256 _handler->releaseFile( filename );
259 // provide directory tree denoted by path to attach dir
261 // dirname is interpreted relative to the attached url
262 // and a path prefix is preserved to destination
264 MediaAccess::provideDir( const Pathname & dirname ) const
267 ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
270 _handler->provideDir( dirname );
274 MediaAccess::provideDirTree( const Pathname & dirname ) const
277 ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
280 _handler->provideDirTree( dirname );
284 MediaAccess::releaseDir( const Pathname & dirname ) const
289 _handler->releaseDir( dirname );
293 MediaAccess::releasePath( const Pathname & pathname ) const
298 _handler->releasePath( pathname );
301 // Return content of directory on media
303 MediaAccess::dirInfo( list<string> & retlist, const Pathname & dirname, bool dots ) const
308 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
311 _handler->dirInfo( retlist, dirname, dots );
314 // Return content of directory on media
316 MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
321 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
324 _handler->dirInfo( retlist, dirname, dots );
328 MediaAccess::dumpOn( std::ostream & str ) const
331 return str << "MediaAccess( closed )";
333 str << _handler->protocol() << "(" << *_handler << ")";
337 void MediaAccess::getFile( const Url &from, const Pathname &to )
339 DBG << "From: " << from << endl << "To: " << to << endl;
341 Pathname path = from.getPathData();
342 Pathname dir = path.dirname();
343 string base = path.basename();
346 u.setPathData( dir.asString() );
353 media._handler->provideFileCopy( base, to );
356 catch (const MediaException & excpt_r)
358 ZYPP_RETHROW(excpt_r);
361 std::ostream & operator<<( std::ostream & str, const MediaAccess & obj )
362 { return obj.dumpOn( str ); }
364 ///////////////////////////////////////////////////////////////////