1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaHandler.h
12 #ifndef ZYPP_MEDIA_MEDIAHANDLERL_H
13 #define ZYPP_MEDIA_MEDIAHANDLERL_H
19 #include "zypp/Pathname.h"
20 #include "zypp/PathInfo.h"
22 #warning FIXME use real Url class
23 // #include "zypp/@Review/Url.h"
24 typedef std::string Url;
26 #include "zypp/media/MediaAccess.h"
31 ///////////////////////////////////////////////////////////////////
33 // CLASS NAME : MediaHandler
35 * @short Abstract base class for 'physical' MediaHandler like MediaCD, etc.
37 * Handles the requests forwarded by @ref MediaAccess. The public interface
38 * contains nonvirtual methods, which should do common sanitychecks and
39 * logging. For the real action they call virtual methods overloaded by the
43 friend std::ostream & operator<<( std::ostream & str, const MediaHandler & obj );
46 typedef shared_ptr<MediaHandler> Ptr;
47 typedef shared_ptr<const MediaHandler> constPtr;
52 * this is where the media will be actually "mounted"
53 * all files are provided 'below' this directory.
55 Pathname _attachPoint;
58 * If a default attach point was created, it has to be
59 * removed on destuction.
61 bool _tmp_attachPoint;
64 * The local directory that corresponds to the media url.
65 * With NFS it's the '_attachPoint', as the directory on the
66 * server is mounted. With CD/DVD it's 'attach point+_url.path()'
67 * because the CDs root directory is mounted. And with CIFS
68 * it's '_url.path() without the shares name'.
73 * True if concrete handler downloads files to the local
74 * filesystem. If true releaseFile/Dir will delete them.
79 * True, if media is attached.
93 const Pathname & attachPoint() const { return _attachPoint; }
97 ///////////////////////////////////////////////////////////////////
99 // Real action interface to be overloaded by concrete handler.
101 ///////////////////////////////////////////////////////////////////
104 * Call concrete handler to attach the media.
106 * Asserted that not already attached, and attachPoint is a directory.
108 * @param next try next available device in turn until end of device
109 * list is reached (for media which are accessible through multiple
110 * devices like cdroms).
112 * \throws MediaException
115 virtual void attachTo(bool next = false) = 0;
118 * Call concrete handler to disconnect media.
120 * Asserted that media is attached.
122 * This is useful for media which e.g. holds open a connection to a
123 * server like FTP. After calling disconnect() the media object still is
124 * valid and files are present.
126 * After calling disconnect() it's not possible to call provideFile() or
127 * provideDir() anymore.
129 * \throws MediaException
132 virtual void disconnectFrom() { return; }
135 * Call concrete handler to release the media.
136 * If eject is true, physically eject the media (i.e. CD-ROM).
138 * Asserted that media is attached.
140 * \throws MediaException
143 virtual void releaseFrom( bool eject ) = 0;
146 * Call concrete handler to physically eject the media (i.e. CD-ROM)
147 * in case the media is not attached..
149 * Asserted that media is not attached.
151 virtual void forceEject() {}
154 * Call concrete handler to provide file below attach point.
156 * Default implementation provided, that returns whether a file
157 * is located at '_localRoot + filename'.
159 * Asserted that media is attached.
161 * \throws MediaException
164 virtual void getFile( const Pathname & filename ) const = 0;
167 * Call concrete handler to provide a file under a different place
168 * in the file system (usually not under attach point) as a copy.
169 * Media must be attached before by callee.
171 * Default implementation provided that calls getFile(srcFilename)
172 * and copies the result around.
174 * \throws MediaException
177 virtual void getFileCopy( const Pathname & srcFilename, const Pathname & targetFilename ) const;
181 * Call concrete handler to provide directory content (not recursive!)
182 * below attach point.
184 * Return E_not_supported_by_media if media does not support retrieval of
187 * Default implementation provided, that returns whether a directory
188 * is located at '_localRoot + dirname'.
190 * Asserted that media is attached.
192 * \throws MediaException
195 virtual void getDir( const Pathname & dirname, bool recurse_r ) const = 0;
198 * Call concrete handler to provide a content list of directory on media
199 * via retlist. If dots is false entries starting with '.' are not reported.
201 * Return E_not_supported_by_media if media does not support retrieval of
204 * Default implementation provided, that returns the content of a
205 * directory at '_localRoot + dirnname' retrieved via 'readdir'.
207 * Asserted that media is attached and retlist is empty.
209 * \throws MediaException
212 virtual void getDirInfo( std::list<std::string> & retlist,
213 const Pathname & dirname, bool dots = true ) const = 0;
216 * Basically the same as getDirInfo above. The content list is returned as
217 * PathInfo::dircontent, which includes name and filetype of each directory
218 * entry. Retrieving the filetype usg. requires an additional ::stat call for
219 * each entry, thus it's more expensive than a simple readdir.
221 * Asserted that media is attached and retlist is empty.
223 * \throws MediaException
226 virtual void getDirInfo( PathInfo::dircontent & retlist,
227 const Pathname & dirname, bool dots = true ) const = 0;
232 * Retrieve and if available scan dirname/directory.yast.
234 * Asserted that media is attached.
236 * \throws MediaException
239 void getDirectoryYast( std::list<std::string> & retlist,
240 const Pathname & dirname, bool dots = true ) const;
243 * Retrieve and if available scan dirname/directory.yast.
245 * Asserted that media is attached.
247 * \throws MediaException
250 void getDirectoryYast( PathInfo::dircontent & retlist,
251 const Pathname & dirname, bool dots = true ) const;
256 * If the concrete media handler provides a nonempty
257 * attach_point, it must be an existing directory.
259 * On an empty attach_point, MediaHandler will create
260 * a temporay directory, which will be erased from
263 * On any error, the attach_point is set to an empty Pathname,
264 * which should lead to E_bad_attachpoint.
266 MediaHandler ( const Url& url_r,
267 const Pathname & attach_point_r,
268 const Pathname & urlpath_below_attachpoint_r,
269 const bool does_download_r );
272 * Contolling MediaAccess takes care, that attached media is released
273 * prior to deleting this.
275 virtual ~MediaHandler();
279 ///////////////////////////////////////////////////////////////////
281 // MediaAccess interface. Does common checks and logging.
282 // Invokes real action if necessary.
284 ///////////////////////////////////////////////////////////////////
287 * Protocol hint for MediaAccess.
289 #warning FIXME uncomment once real Url class is implemented
291 Url::Protocol protocol() const { return _url.protocol(); }
297 Url url() const { return _url; }
300 * Use concrete handler to attach the media.
302 * @param next try next available device in turn until end of device
303 * list is reached (for media which are accessible through multiple
304 * devices like cdroms).
306 * \throws MediaException
309 void attach(bool next);
312 * True if media is attached.
314 bool isAttached() const { return _isAttached; }
317 * Return the local directory that corresponds to medias url,
318 * no matter if media isAttached or not. Files requested will
319 * be available at 'localRoot() + filename' or better
320 * 'localPath( filename )'.
322 * Returns empty pathname if E_bad_attachpoint
324 const Pathname & localRoot() const { return _localRoot; }
327 * Files provided will be available at 'localPath(filename)'.
329 * Returns empty pathname if E_bad_attachpoint
331 Pathname localPath( const Pathname & pathname ) const;
334 * Use concrete handler to isconnect media.
336 * This is useful for media which e.g. holds open a connection to a
337 * server like FTP. After calling disconnect() the media object still is
338 * valid and files are present.
340 * After calling disconnect() it's not possible to call provideFile() or
341 * provideDir() anymore.
343 * \throws MediaException
349 * Use concrete handler to release the media.
350 * @param eject if true, physically eject the media * (i.e. CD-ROM)
352 * \throws MediaException
355 void release( bool eject = false );
358 * Use concrete handler to provide file denoted by path below
359 * 'localRoot'. Filename is interpreted relative to the
360 * attached url and a path prefix is preserved.
362 * \throws MediaException
365 void provideFile( Pathname filename ) const;
368 * Call concrete handler to provide a copy of a file under a different place
369 * in the file system (usually not under attach point) as a copy.
370 * Media must be attached before by callee.
372 * @param srcFilename Filename of source file on the media
373 * @param targetFilename Filename for the target in the file system
375 * \throws MediaException
378 void provideFileCopy( Pathname srcFilename, Pathname targetFilename) const;
381 * Use concrete handler to provide directory denoted
382 * by path below 'localRoot' (not recursive!).
383 * dirname is interpreted relative to the
384 * attached url and a path prefix is preserved.
386 * \throws MediaException
389 void provideDir( Pathname dirname ) const;
392 * Use concrete handler to provide directory tree denoted
393 * by path below 'localRoot' (recursive!!).
394 * dirname is interpreted relative to the
395 * attached url and a path prefix is preserved.
397 * \throws MediaException
400 void provideDirTree( Pathname dirname ) const;
403 * Remove filename below localRoot IFF handler downloads files
404 * to the local filesystem. Never remove anything from media.
406 * \throws MediaException
409 void releaseFile( const Pathname & filename ) const { return releasePath( filename ); }
412 * Remove directory tree below localRoot IFF handler downloads files
413 * to the local filesystem. Never remove anything from media.
415 * \throws MediaException
418 void releaseDir( const Pathname & dirname ) const { return releasePath( dirname ); }
421 * Remove pathname below localRoot IFF handler downloads files
422 * to the local filesystem. Never remove anything from media.
424 * If pathname denotes a directory it is recursively removed.
425 * If pathname is empty or '/' everything below the localRoot
426 * is recursively removed.
427 * If pathname denotes a file it is unlinked.
429 * \throws MediaException
432 void releasePath( Pathname pathname ) const;
437 * Return content of directory on media via retlist. If dots is false
438 * entries starting with '.' are not reported.
440 * The request is forwarded to the concrete handler,
441 * which may atempt to retieve the content e.g. via 'readdir'
443 * <B>Caution:</B> This is not supported by all media types.
444 * Be prepared to handle E_not_supported_by_media.
446 * \throws MediaException
449 void dirInfo( std::list<std::string> & retlist,
450 const Pathname & dirname, bool dots = true ) const;
453 * Basically the same as dirInfo above. The content is returned as
454 * PathInfo::dircontent, which includes name and filetype of each directory
455 * entry. Retrieving the filetype usg. requires an additional ::stat call for
456 * each entry, thus it's more expensive than a simple readdir.
458 * <B>Caution:</B> This is not supported by all media types.
459 * Be prepared to handle E_not_supported_by_media.
461 * \throws MediaException
464 void dirInfo( PathInfo::dircontent & retlist,
465 const Pathname & dirname, bool dots = true ) const;
468 ///////////////////////////////////////////////////////////////////
470 #define MEDIA_HANDLER_API \
472 virtual void attachTo (bool next = false); \
473 virtual void releaseFrom( bool eject ); \
474 virtual void getFile( const Pathname & filename ) const; \
475 virtual void getDir( const Pathname & dirname, bool recurse_r ) const; \
476 virtual void getDirInfo( std::list<std::string> & retlist, \
477 const Pathname & dirname, bool dots = true ) const; \
478 virtual void getDirInfo( PathInfo::dircontent & retlist, \
479 const Pathname & dirname, bool dots = true ) const;
485 #endif // ZYPP_MEDIA_MEDIAHANDLERL_H