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::dependsOnParent(MediaAccessId parentId) const
79 return _handler ? _handler->dependsOnParent(parentId)
85 MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
88 MIL << "Url is not valid" << endl;
89 ZYPP_THROW(MediaBadUrlException(url));
94 std::string scheme = url.getScheme();
96 MIL << "Trying scheme '" << scheme << "'" << endl;
98 if (scheme == "cd" || scheme == "dvd")
99 _handler = new MediaCD (url,preferred_attach_point);
100 else if (scheme == "nfs")
101 _handler = new MediaNFS (url,preferred_attach_point);
102 else if (scheme == "iso")
103 _handler = new MediaISO (url,preferred_attach_point);
104 else if (scheme == "file" || scheme == "dir")
105 _handler = new MediaDIR (url,preferred_attach_point);
106 else if (scheme == "hd")
107 _handler = new MediaDISK (url,preferred_attach_point);
108 else if (scheme == "smb")
109 _handler = new MediaSMB (url,preferred_attach_point);
110 else if (scheme == "cifs")
111 _handler = new MediaCIFS (url,preferred_attach_point);
112 else if (scheme == "ftp" || scheme == "http" || scheme == "https")
113 _handler = new MediaCurl (url,preferred_attach_point);
116 ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
119 // check created handler
121 ERR << "Failed to create media handler" << endl;
122 ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
125 MIL << "Opened: " << *this << endl;
128 // Type of media if open, otherwise NONE.
130 MediaAccess::protocol() const
135 return _handler->protocol();
138 ///////////////////////////////////////////////////////////////////
141 // METHOD NAME : MediaAccess::url
144 Url MediaAccess::url() const
149 return _handler->url();
154 MediaAccess::close ()
156 ///////////////////////////////////////////////////////////////////
157 // !!! make shure handler gets properly deleted.
158 // I.e. release attached media before deleting the handler.
159 ///////////////////////////////////////////////////////////////////
164 catch (const MediaException & excpt_r)
166 ZYPP_CAUGHT(excpt_r);
167 WAR << "Close: " << *this << " (" << excpt_r << ")" << endl;
168 ZYPP_RETHROW(excpt_r);
170 MIL << "Close: " << *this << " (OK)" << endl;
178 void MediaAccess::attach (bool next)
181 ZYPP_THROW(MediaNotOpenException("attach"));
183 _handler->attach(next);
186 void MediaAccess::reattach(const Pathname &new_attach_point)
189 ZYPP_THROW(MediaNotOpenException("reattach"));
191 _handler->reattach(new_attach_point);
194 // True if media is open and attached.
196 MediaAccess::isAttached() const
198 return( _handler && _handler->isAttached() );
201 // local directory that corresponds to medias url
202 // If media is not open an empty pathname.
204 MediaAccess::localRoot() const
209 return _handler->localRoot();
212 // Short for 'localRoot() + pathname', but returns an empty
213 // * pathname if media is not open.
215 MediaAccess::localPath( const Pathname & pathname ) const
220 return _handler->localPath( pathname );
224 MediaAccess::disconnect()
227 ZYPP_THROW(MediaNotOpenException("disconnect"));
229 _handler->disconnect();
232 // release attached media
234 MediaAccess::release( bool eject )
239 _handler->release( eject );
243 // provide file denoted by path to attach dir
245 // filename is interpreted relative to the attached url
246 // and a path prefix is preserved to destination
248 MediaAccess::provideFile( const Pathname & filename, bool cached, bool checkonly) const
251 PathInfo pi( localPath( filename ) );
257 ZYPP_THROW(MediaFileNotFoundException(url(), filename));
260 ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
263 _handler->provideFile( filename );
267 MediaAccess::releaseFile( const Pathname & filename ) const
272 _handler->releaseFile( filename );
275 // provide directory tree denoted by path to attach dir
277 // dirname is interpreted relative to the attached url
278 // and a path prefix is preserved to destination
280 MediaAccess::provideDir( const Pathname & dirname ) const
283 ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
286 _handler->provideDir( dirname );
290 MediaAccess::provideDirTree( const Pathname & dirname ) const
293 ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
296 _handler->provideDirTree( dirname );
300 MediaAccess::releaseDir( const Pathname & dirname ) const
305 _handler->releaseDir( dirname );
309 MediaAccess::releasePath( const Pathname & pathname ) const
314 _handler->releasePath( pathname );
317 // Return content of directory on media
319 MediaAccess::dirInfo( list<string> & retlist, const Pathname & dirname, bool dots ) const
324 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
327 _handler->dirInfo( retlist, dirname, dots );
330 // Return content of directory on media
332 MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
337 ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
340 _handler->dirInfo( retlist, dirname, dots );
344 MediaAccess::dumpOn( std::ostream & str ) const
347 return str << "MediaAccess( closed )";
349 str << _handler->protocol() << "(" << *_handler << ")";
353 void MediaAccess::getFile( const Url &from, const Pathname &to )
355 DBG << "From: " << from << endl << "To: " << to << endl;
357 Pathname path = from.getPathData();
358 Pathname dir = path.dirname();
359 string base = path.basename();
362 u.setPathData( dir.asString() );
369 media._handler->provideFileCopy( base, to );
372 catch (const MediaException & excpt_r)
374 ZYPP_RETHROW(excpt_r);
377 std::ostream & operator<<( std::ostream & str, const MediaAccess & obj )
378 { return obj.dumpOn( str ); }
380 ///////////////////////////////////////////////////////////////////