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"
21 #include "zypp/base/PtrTypes.h"
25 #include "zypp/media/MediaSource.h"
26 #include "zypp/media/MediaException.h"
32 ///////////////////////////////////////////////////////////////////
34 // CLASS NAME : MediaHandler
36 * @short Abstract base class for 'physical' MediaHandler like MediaCD, etc.
38 * Handles the requests forwarded by @ref MediaAccess. The public interface
39 * contains nonvirtual methods, which should do common sanitychecks and
40 * logging. For the real action they call virtual methods overloaded by the
44 friend std::ostream & operator<<( std::ostream & str, const MediaHandler & obj );
47 typedef shared_ptr<MediaHandler> Ptr;
48 typedef shared_ptr<const MediaHandler> constPtr;
52 * The attached media source.
54 MediaSourceRef _mediaSource;
57 * this is where the media will be actually "mounted"
58 * all files are provided 'below' this directory.
60 AttachPointRef _attachPoint;
63 * The relative root directory of the data on the media.
64 * See also _localRoot and urlpath_below_attachpoint_r
65 * constructor argument.
67 Pathname _relativeRoot;
70 * The local directory that corresponds to the media url.
71 * With NFS it's the '_attachPoint', as the directory on the
72 * server is mounted. With CD/DVD it's 'attach point+_relativeRoot'
73 * because the CDs root directory is mounted. And with CIFS
74 * it's '_url.path() without the shares name'.
79 * True if concrete handler downloads files to the local
80 * filesystem. If true releaseFile/Dir will delete them.
84 /** timestamp of the the last attach verification */
85 mutable time_t _attach_mtime;
94 * Access Id of media handler we depend on.
96 MediaAccessId _parentId;
99 * Return the currently used attach point.
101 Pathname attachPoint() const;
104 * Set a new attach point and update localRoot.
106 void setAttachPoint(const Pathname &path, bool _temporary);
109 * Set a (shared) attach point and update localRoot.
111 void setAttachPoint(const AttachPointRef &ref);
114 * Try to create a default / temporary attach point.
115 * \return The name of the new attach point or empty path name.
117 Pathname createAttachPoint() const;
120 * Remove unused attach point.
122 void removeAttachPoint();
124 bool isUseableAttachPoint(const Pathname &path) const;
126 std::string mediaSourceName() const
128 return _mediaSource ? _mediaSource->name : "";
131 void setMediaSource(const MediaSourceRef &ref);
134 * MediaAccess (MediaManager) needs access to the
135 * attachedMedia() function to deliver a shared
136 * media source and its attach point to other
137 * media handler instances.
139 friend class MediaAccess;
142 * Ask the media manager if specified media source
143 * is already attached.
146 findAttachedMedia(const MediaSourceRef &media) const;
148 bool dependsOnParent(MediaAccessId parentId);
151 * Returns the attached media. Used by MediaManager
152 * to find other handlers using the same source.
154 AttachedMedia attachedMedia() const;
156 bool isSharedMedia() const;
158 bool checkAttached(bool aDevice,
159 bool fsType=false) const;
161 void reattach(const Pathname &new_attach_point);
162 virtual void reattachTo(const Pathname &new_attach_point);
166 ///////////////////////////////////////////////////////////////////
168 // Real action interface to be overloaded by concrete handler.
170 ///////////////////////////////////////////////////////////////////
173 * Call concrete handler to attach the media.
175 * Asserted that not already attached, and attachPoint is a directory.
177 * @param next try next available device in turn until end of device
178 * list is reached (for media which are accessible through multiple
179 * devices like cdroms).
181 * \throws MediaException
184 virtual void attachTo(bool next = false) = 0;
187 * Call concrete handler to disconnect media.
189 * Asserted that media is attached.
191 * This is useful for media which e.g. holds open a connection to a
192 * server like FTP. After calling disconnect() the media object still is
193 * valid and files are present.
195 * After calling disconnect() it's not possible to call provideFile() or
196 * provideDir() anymore.
198 * \throws MediaException
201 virtual void disconnectFrom() { return; }
204 * Call concrete handler to release the media.
206 * If eject is true, and the media is used in one handler
207 * instance only, physically eject the media (i.e. CD-ROM).
209 * Asserted that media is attached.
211 * \throws MediaException
214 virtual void releaseFrom( bool eject ) = 0;
217 * Call concrete handler to physically eject the media (i.e. CD-ROM)
218 * in case the media is not attached..
220 * Asserted that media is not attached.
222 virtual void forceEject() {}
225 * Call concrete handler to provide file below attach point.
227 * Default implementation provided, that returns whether a file
228 * is located at '_localRoot + filename'.
230 * Asserted that media is attached.
232 * \throws MediaException
235 virtual void getFile( const Pathname & filename ) const = 0;
238 * Call concrete handler to provide a file under a different place
239 * in the file system (usually not under attach point) as a copy.
240 * Media must be attached before by callee.
242 * Default implementation provided that calls getFile(srcFilename)
243 * and copies the result around.
245 * \throws MediaException
248 virtual void getFileCopy( const Pathname & srcFilename, const Pathname & targetFilename ) const;
252 * Call concrete handler to provide directory content (not recursive!)
253 * below attach point.
255 * Return E_not_supported_by_media if media does not support retrieval of
258 * Default implementation provided, that returns whether a directory
259 * is located at '_localRoot + dirname'.
261 * Asserted that media is attached.
263 * \throws MediaException
266 virtual void getDir( const Pathname & dirname, bool recurse_r ) const = 0;
269 * Call concrete handler to provide a content list of directory on media
270 * via retlist. If dots is false entries starting with '.' are not reported.
272 * Return E_not_supported_by_media if media does not support retrieval of
275 * Default implementation provided, that returns the content of a
276 * directory at '_localRoot + dirnname' retrieved via 'readdir'.
278 * Asserted that media is attached and retlist is empty.
280 * \throws MediaException
283 virtual void getDirInfo( std::list<std::string> & retlist,
284 const Pathname & dirname, bool dots = true ) const = 0;
287 * Basically the same as getDirInfo above. The content list is returned as
288 * filesystem::DirContent, which includes name and filetype of each directory
289 * entry. Retrieving the filetype usg. requires an additional ::stat call for
290 * each entry, thus it's more expensive than a simple readdir.
292 * Asserted that media is attached and retlist is empty.
294 * \throws MediaException
297 virtual void getDirInfo( filesystem::DirContent & retlist,
298 const Pathname & dirname, bool dots = true ) const = 0;
303 * Retrieve and if available scan dirname/directory.yast.
305 * Asserted that media is attached.
307 * \throws MediaException
310 void getDirectoryYast( std::list<std::string> & retlist,
311 const Pathname & dirname, bool dots = true ) const;
314 * Retrieve and if available scan dirname/directory.yast.
316 * Asserted that media is attached.
318 * \throws MediaException
321 void getDirectoryYast( filesystem::DirContent & retlist,
322 const Pathname & dirname, bool dots = true ) const;
327 * If the concrete media handler provides a nonempty
328 * attach_point, it must be an existing directory.
330 * On an empty attach_point, MediaHandler will create
331 * a temporay directory, which will be erased from
334 * On any error, the attach_point is set to an empty Pathname,
335 * which should lead to E_bad_attachpoint.
337 MediaHandler ( const Url& url_r,
338 const Pathname & attach_point_r,
339 const Pathname & urlpath_below_attachpoint_r,
340 const bool does_download_r );
343 * Contolling MediaAccess takes care, that attached media is released
344 * prior to deleting this.
346 virtual ~MediaHandler();
351 ///////////////////////////////////////////////////////////////////
353 // MediaAccess interface. Does common checks and logging.
354 // Invokes real action if necessary.
356 ///////////////////////////////////////////////////////////////////
359 * Protocol hint for MediaAccess.
361 std::string protocol() const { return _url.getScheme(); }
366 Url url() const { return _url; }
369 * Use concrete handler to attach the media.
371 * @param next try next available device in turn until end of device
372 * list is reached (for media which are accessible through multiple
373 * devices like cdroms).
375 * \throws MediaException
378 void attach(bool next);
381 * True if media is attached.
383 virtual bool isAttached() const { return _mediaSource; }
386 * Return the local directory that corresponds to medias url,
387 * no matter if media isAttached or not. Files requested will
388 * be available at 'localRoot() + filename' or better
389 * 'localPath( filename )'.
391 * Returns empty pathname if E_bad_attachpoint
393 const Pathname & localRoot() const { return _localRoot; }
396 * Files provided will be available at 'localPath(filename)'.
398 * Returns empty pathname if E_bad_attachpoint
400 Pathname localPath( const Pathname & pathname ) const;
403 * Use concrete handler to isconnect media.
405 * This is useful for media which e.g. holds open a connection to a
406 * server like FTP. After calling disconnect() the media object still is
407 * valid and files are present.
409 * After calling disconnect() it's not possible to call provideFile() or
410 * provideDir() anymore.
412 * \throws MediaException
418 * Use concrete handler to release the media.
419 * @param eject if true, physically eject the media * (i.e. CD-ROM)
421 * \throws MediaException
424 void release( bool eject = false );
427 * Use concrete handler to provide file denoted by path below
428 * 'localRoot'. Filename is interpreted relative to the
429 * attached url and a path prefix is preserved.
431 * \throws MediaException
434 void provideFile( Pathname filename ) const;
437 * Call concrete handler to provide a copy of a file under a different place
438 * in the file system (usually not under attach point) as a copy.
439 * Media must be attached before by callee.
441 * @param srcFilename Filename of source file on the media
442 * @param targetFilename Filename for the target in the file system
444 * \throws MediaException
447 void provideFileCopy( Pathname srcFilename, Pathname targetFilename) const;
450 * Use concrete handler to provide directory denoted
451 * by path below 'localRoot' (not recursive!).
452 * dirname is interpreted relative to the
453 * attached url and a path prefix is preserved.
455 * \throws MediaException
458 void provideDir( Pathname dirname ) const;
461 * Use concrete handler to provide directory tree denoted
462 * by path below 'localRoot' (recursive!!).
463 * dirname is interpreted relative to the
464 * attached url and a path prefix is preserved.
466 * \throws MediaException
469 void provideDirTree( Pathname dirname ) const;
472 * Remove filename below localRoot IFF handler downloads files
473 * to the local filesystem. Never remove anything from media.
475 * \throws MediaException
478 void releaseFile( const Pathname & filename ) const { return releasePath( filename ); }
481 * Remove directory tree below localRoot IFF handler downloads files
482 * to the local filesystem. Never remove anything from media.
484 * \throws MediaException
487 void releaseDir( const Pathname & dirname ) const { return releasePath( dirname ); }
490 * Remove pathname below localRoot IFF handler downloads files
491 * to the local filesystem. Never remove anything from media.
493 * If pathname denotes a directory it is recursively removed.
494 * If pathname is empty or '/' everything below the localRoot
495 * is recursively removed.
496 * If pathname denotes a file it is unlinked.
498 * \throws MediaException
501 void releasePath( Pathname pathname ) const;
506 * Return content of directory on media via retlist. If dots is false
507 * entries starting with '.' are not reported.
509 * The request is forwarded to the concrete handler,
510 * which may atempt to retieve the content e.g. via 'readdir'
512 * <B>Caution:</B> This is not supported by all media types.
513 * Be prepared to handle E_not_supported_by_media.
515 * \throws MediaException
518 void dirInfo( std::list<std::string> & retlist,
519 const Pathname & dirname, bool dots = true ) const;
522 * Basically the same as dirInfo above. The content is returned as
523 * filesystem::DirContent, which includes name and filetype of each directory
524 * entry. Retrieving the filetype usg. requires an additional ::stat call for
525 * each entry, thus it's more expensive than a simple readdir.
527 * <B>Caution:</B> This is not supported by all media types.
528 * Be prepared to handle E_not_supported_by_media.
530 * \throws MediaException
533 void dirInfo( filesystem::DirContent & retlist,
534 const Pathname & dirname, bool dots = true ) const;
537 ///////////////////////////////////////////////////////////////////
539 #define MEDIA_HANDLER_API \
541 virtual void attachTo (bool next = false); \
542 virtual void releaseFrom( bool eject ); \
543 virtual void getFile( const Pathname & filename ) const; \
544 virtual void getDir( const Pathname & dirname, bool recurse_r ) const; \
545 virtual void getDirInfo( std::list<std::string> & retlist, \
546 const Pathname & dirname, bool dots = true ) const; \
547 virtual void getDirInfo( filesystem::DirContent & retlist, \
548 const Pathname & dirname, bool dots = true ) const;
554 #endif // ZYPP_MEDIA_MEDIAHANDLERL_H