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"
30 #include "zypp/media/MediaISO.h"
37 ///////////////////////////////////////////////////////////////////
39 // CLASS NAME : MediaAccess
41 ///////////////////////////////////////////////////////////////////
43 const Pathname MediaAccess::_noPath; // empty path
45 ///////////////////////////////////////////////////////////////////
47 MediaAccess::MediaAccess ()
53 MediaAccess::~MediaAccess()
57 close(); // !!! make sure handler gets properly deleted.
63 MediaAccess::attachedMedia() const
65 return _handler ? _handler->attachedMedia()
70 MediaAccess::isSharedMedia() const
72 return _handler ? _handler->isSharedMedia()
77 MediaAccess::resetParentId()
79 if( _handler) _handler->resetParentId();
83 MediaAccess::dependsOnParent() const
85 return _handler ? _handler->dependsOnParent() : false;
89 MediaAccess::dependsOnParent(MediaAccessId parentId,
90 bool exactIdMatch) const
92 return _handler ? _handler->dependsOnParent(parentId, exactIdMatch)
98 MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
101 MIL << "Url is not valid" << endl;
102 ZYPP_THROW(MediaBadUrlException(url));
107 std::string scheme = url.getScheme();
109 MIL << "Trying scheme '" << scheme << "'" << endl;
112 ** WARNING: Don't forget to update MediaAccess::downloads(url)
113 ** if you are adding a new url scheme / handler!
115 if (scheme == "cd" || scheme == "dvd")
116 _handler = new MediaCD (url,preferred_attach_point);
117 else if (scheme == "nfs")
118 _handler = new MediaNFS (url,preferred_attach_point);
119 else if (scheme == "iso")
120 _handler = new MediaISO (url,preferred_attach_point);
121 else if (scheme == "file" || scheme == "dir")
122 _handler = new MediaDIR (url,preferred_attach_point);
123 else if (scheme == "hd")
124 _handler = new MediaDISK (url,preferred_attach_point);
125 else if (scheme == "smb")
126 _handler = new MediaSMB (url,preferred_attach_point);
127 else if (scheme == "cifs")
128 _handler = new MediaCIFS (url,preferred_attach_point);
129 else if (scheme == "ftp" || scheme == "http" || scheme == "https")
130 _handler = new MediaCurl (url,preferred_attach_point);
133 ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
136 // check created handler
138 ERR << "Failed to create media handler" << endl;
139 ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
142 MIL << "Opened: " << *this << endl;
147 MediaAccess::downloads(const Url &url)
149 std::string scheme( url.getScheme());
150 return (scheme == "ftp" || scheme == "http" || scheme == "https");
155 MediaAccess::canBeVolatile(const Url &url)
157 std::string scheme( url.getScheme());
158 return ! (scheme == "cd" || scheme == "dvd");
161 // Type of media if open, otherwise NONE.
163 MediaAccess::protocol() const
168 return _handler->protocol();
172 MediaAccess::downloads() const
174 return _handler ? _handler->downloads() : false;
177 ///////////////////////////////////////////////////////////////////
180 // METHOD NAME : MediaAccess::url
183 Url MediaAccess::url() const
188 return _handler->url();
193 MediaAccess::close ()
195 ///////////////////////////////////////////////////////////////////
196 // !!! make shure handler gets properly deleted.
197 // I.e. release attached media before deleting the handler.
198 ///////////////////////////////////////////////////////////////////
203 catch (const MediaException & excpt_r)
205 ZYPP_CAUGHT(excpt_r);
206 WAR << "Close: " << *this << " (" << excpt_r << ")" << endl;
207 ZYPP_RETHROW(excpt_r);
209 MIL << "Close: " << *this << " (OK)" << endl;
217 void MediaAccess::attach (bool next)
220 ZYPP_THROW(MediaNotOpenException("attach"));
222 _handler->attach(next);
225 // True if media is open and attached.
227 MediaAccess::isAttached() const
229 return( _handler && _handler->isAttached() );
232 // local directory that corresponds to medias url
233 // If media is not open an empty pathname.
235 MediaAccess::localRoot() const
240 return _handler->localRoot();
243 // Short for 'localRoot() + pathname', but returns an empty
244 // * pathname if media is not open.
246 MediaAccess::localPath( const Pathname & pathname ) const
251 return _handler->localPath( pathname );
255 MediaAccess::disconnect()
258 ZYPP_THROW(MediaNotOpenException("disconnect"));
260 _handler->disconnect();
263 // release attached media
265 MediaAccess::release( bool eject )
270 _handler->release( eject );
274 // provide file denoted by path to attach dir
276 // filename is interpreted relative to the attached url
277 // and a path prefix is preserved to destination
279 MediaAccess::provideFile( const Pathname & filename, bool cached, bool checkonly) const
282 PathInfo pi( localPath( filename ) );
288 ZYPP_THROW(MediaFileNotFoundException(url(), filename));
291 ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
294 _handler->provideFile( filename );
298 MediaAccess::releaseFile( const Pathname & filename ) const
303 _handler->releaseFile( filename );
306 // provide directory tree denoted by path to attach dir
308 // dirname is interpreted relative to the attached url
309 // and a path prefix is preserved to destination
311 MediaAccess::provideDir( const Pathname & dirname ) const
314 ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
317 _handler->provideDir( dirname );
321 MediaAccess::provideDirTree( const Pathname & dirname ) const
324 ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
327 _handler->provideDirTree( dirname );
331 MediaAccess::releaseDir( const Pathname & dirname ) const
336 _handler->releaseDir( dirname );
340 MediaAccess::releasePath( const Pathname & pathname ) const
345 _handler->releasePath( pathname );
348 // Return content of directory on media
350 MediaAccess::dirInfo( list<string> & retlist, const Pathname & dirname, bool dots ) const
355 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
358 _handler->dirInfo( retlist, dirname, dots );
361 // Return content of directory on media
363 MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
368 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
371 _handler->dirInfo( retlist, dirname, dots );
374 // return if a file exists
376 MediaAccess::doesFileExist( const Pathname & filename ) const
379 ZYPP_THROW(MediaNotOpenException("doesFileExist(" + filename.asString() + ")"));
382 return _handler->doesFileExist( filename );
386 MediaAccess::dumpOn( std::ostream & str ) const
389 return str << "MediaAccess( closed )";
391 str << _handler->protocol() << "(" << *_handler << ")";
395 void MediaAccess::getFile( const Url &from, const Pathname &to )
397 DBG << "From: " << from << endl << "To: " << to << endl;
399 Pathname path = from.getPathData();
400 Pathname dir = path.dirname();
401 string base = path.basename();
404 u.setPathData( dir.asString() );
411 media._handler->provideFileCopy( base, to );
414 catch (const MediaException & excpt_r)
416 ZYPP_RETHROW(excpt_r);
419 std::ostream & operator<<( std::ostream & str, const MediaAccess & obj )
420 { return obj.dumpOn( str ); }
422 ///////////////////////////////////////////////////////////////////