- Added reattach method prototypes (dummy implementation)
[platform/upstream/libzypp.git] / zypp / media / MediaAccess.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaAccess.cc
10  *
11 */
12
13 #include <ctype.h>
14
15 #include <iostream>
16
17 #include "zypp/base/Logger.h"
18
19 #include "zypp/media/MediaException.h"
20 #include "zypp/media/MediaAccess.h"
21 #include "zypp/media/MediaHandler.h"
22
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"
31
32 using namespace std;
33
34 namespace zypp {
35   namespace media {
36
37 ///////////////////////////////////////////////////////////////////
38 //
39 //      CLASS NAME : MediaAccess
40 //
41 ///////////////////////////////////////////////////////////////////
42
43 const Pathname MediaAccess::_noPath; // empty path
44
45 ///////////////////////////////////////////////////////////////////
46 // constructor
47 MediaAccess::MediaAccess ()
48     : _handler (0)
49 {
50 }
51
52 // destructor
53 MediaAccess::~MediaAccess()
54 {
55   try
56     {
57       close(); // !!! make sure handler gets properly deleted.
58     }
59   catch(...) {}
60 }
61
62 AttachedMedia
63 MediaAccess::attachedMedia() const
64 {
65         return _handler ? _handler->attachedMedia()
66                         : AttachedMedia();
67 }
68
69 bool
70 MediaAccess::isSharedMedia() const
71 {
72         return _handler ? _handler->isSharedMedia()
73                         : false;
74 }
75
76 bool
77 MediaAccess::dependsOnParent(MediaAccessId parentId) const
78 {
79         return _handler ? _handler->dependsOnParent(parentId)
80                         : false;
81 }
82
83 // open URL
84 void
85 MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
86 {
87     if(!url.isValid()) {
88         MIL << "Url is not valid" << endl;
89         ZYPP_THROW(MediaBadUrlException(url));
90     }
91
92     close();
93
94     std::string scheme = url.getScheme();
95
96     MIL << "Trying scheme '" << scheme << "'" << endl;
97
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);
114     else
115     {
116         ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
117     }
118
119     // check created handler
120     if ( !_handler ){
121       ERR << "Failed to create media handler" << endl;
122       ZYPP_THROW(MediaSystemException(url, "Failed to create media handler"));
123     }
124
125     MIL << "Opened: " << *this << endl;
126 }
127
128 // Type of media if open, otherwise NONE.
129 std::string
130 MediaAccess::protocol() const
131 {
132   if ( !_handler )
133     return "unknown";
134
135   return _handler->protocol();
136 }
137
138 ///////////////////////////////////////////////////////////////////
139 //
140 //
141 //      METHOD NAME : MediaAccess::url
142 //      METHOD TYPE : Url
143 //
144 Url MediaAccess::url() const
145 {
146   if ( !_handler )
147     return Url();
148
149   return _handler->url();
150 }
151
152 // close handler
153 void
154 MediaAccess::close ()
155 {
156   ///////////////////////////////////////////////////////////////////
157   // !!! make shure handler gets properly deleted.
158   // I.e. release attached media before deleting the handler.
159   ///////////////////////////////////////////////////////////////////
160   if ( _handler ) {
161     try {
162       _handler->release();
163     }
164     catch (const MediaException & excpt_r)
165     {
166       ZYPP_CAUGHT(excpt_r);
167       WAR << "Close: " << *this << " (" << excpt_r << ")" << endl;
168       ZYPP_RETHROW(excpt_r);
169     }
170     MIL << "Close: " << *this << " (OK)" << endl;
171     delete _handler;
172     _handler = 0;
173   }
174 }
175
176
177 // attach media
178 void MediaAccess::attach (bool next)
179 {
180   if ( !_handler ) {
181     ZYPP_THROW(MediaNotOpenException("attach"));
182   }
183   _handler->attach(next);
184 }
185
186 void MediaAccess::reattach(const Pathname &new_attach_point)
187 {
188   if ( !_handler ) {
189     ZYPP_THROW(MediaNotOpenException("reattach"));
190   }
191   _handler->reattach(new_attach_point);
192 }
193
194 // True if media is open and attached.
195 bool
196 MediaAccess::isAttached() const
197 {
198   return( _handler && _handler->isAttached() );
199 }
200
201 // local directory that corresponds to medias url
202 // If media is not open an empty pathname.
203 const Pathname &
204 MediaAccess::localRoot() const
205 {
206   if ( !_handler )
207     return _noPath;
208
209   return _handler->localRoot();
210 }
211
212 // Short for 'localRoot() + pathname', but returns an empty
213 // * pathname if media is not open.
214 Pathname
215 MediaAccess::localPath( const Pathname & pathname ) const
216 {
217   if ( !_handler )
218     return _noPath;
219
220   return _handler->localPath( pathname );
221 }
222
223 void
224 MediaAccess::disconnect()
225 {
226   if ( !_handler )
227     ZYPP_THROW(MediaNotOpenException("disconnect"));
228
229   _handler->disconnect();
230 }
231
232 // release attached media
233 void
234 MediaAccess::release( bool eject )
235 {
236   if ( !_handler )
237     return;
238
239   _handler->release( eject );
240 }
241
242
243 // provide file denoted by path to attach dir
244 //
245 // filename is interpreted relative to the attached url
246 // and a path prefix is preserved to destination
247 void
248 MediaAccess::provideFile( const Pathname & filename, bool cached, bool checkonly) const
249 {
250   if ( cached ) {
251     PathInfo pi( localPath( filename ) );
252     if ( pi.isExist() )
253       return;
254   }
255
256   if(checkonly)
257     ZYPP_THROW(MediaFileNotFoundException(url(), filename));
258
259   if ( !_handler ) {
260     ZYPP_THROW(MediaNotOpenException("provideFile(" + filename.asString() + ")"));
261   }
262
263   _handler->provideFile( filename );
264 }
265
266 void
267 MediaAccess::releaseFile( const Pathname & filename ) const
268 {
269   if ( !_handler )
270     return;
271
272   _handler->releaseFile( filename );
273 }
274
275 // provide directory tree denoted by path to attach dir
276 //
277 // dirname is interpreted relative to the attached url
278 // and a path prefix is preserved to destination
279 void
280 MediaAccess::provideDir( const Pathname & dirname ) const
281 {
282   if ( !_handler ) {
283     ZYPP_THROW(MediaNotOpenException("provideDir(" + dirname.asString() + ")"));
284   }
285
286   _handler->provideDir( dirname );
287 }
288
289 void
290 MediaAccess::provideDirTree( const Pathname & dirname ) const
291 {
292   if ( !_handler ) {
293     ZYPP_THROW(MediaNotOpenException("provideDirTree(" + dirname.asString() + ")"));
294   }
295
296   _handler->provideDirTree( dirname );
297 }
298
299 void
300 MediaAccess::releaseDir( const Pathname & dirname ) const
301 {
302   if ( !_handler )
303     return;
304
305   _handler->releaseDir( dirname );
306 }
307
308 void
309 MediaAccess::releasePath( const Pathname & pathname ) const
310 {
311   if ( !_handler )
312     return;
313
314   _handler->releasePath( pathname );
315 }
316
317 // Return content of directory on media
318 void
319 MediaAccess::dirInfo( list<string> & retlist, const Pathname & dirname, bool dots ) const
320 {
321   retlist.clear();
322
323   if ( !_handler ) {
324     ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
325   }
326
327   _handler->dirInfo( retlist, dirname, dots );
328 }
329
330 // Return content of directory on media
331 void
332 MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
333 {
334   retlist.clear();
335
336   if ( !_handler ) {
337     ZYPP_THROW(MediaNotOpenException("dirInfo(" + dirname.asString() + ")"));
338   }
339
340   _handler->dirInfo( retlist, dirname, dots );
341 }
342
343 std::ostream &
344 MediaAccess::dumpOn( std::ostream & str ) const
345 {
346   if ( !_handler )
347     return str << "MediaAccess( closed )";
348
349   str << _handler->protocol() << "(" << *_handler << ")";
350   return str;
351 }
352
353 void MediaAccess::getFile( const Url &from, const Pathname &to )
354 {
355   DBG << "From: " << from << endl << "To: " << to << endl;
356
357   Pathname path = from.getPathData();
358   Pathname dir = path.dirname();
359   string base = path.basename();
360
361   Url u = from;
362   u.setPathData( dir.asString() );
363
364   MediaAccess media;
365
366   try {
367     media.open( u );
368     media.attach();
369     media._handler->provideFileCopy( base, to );
370     media.release();
371   }
372   catch (const MediaException & excpt_r)
373   {
374     ZYPP_RETHROW(excpt_r);
375   }
376 }
377     std::ostream & operator<<( std::ostream & str, const MediaAccess & obj )
378     { return obj.dumpOn( str ); }
379
380 ///////////////////////////////////////////////////////////////////
381   } // namespace media
382 } // namespace zypp