From 150f0c29d00ffa709faa35e742dc1dd682a32b76 Mon Sep 17 00:00:00 2001 From: Marius Tomaschewski Date: Thu, 2 Mar 2006 16:15:51 +0000 Subject: [PATCH] - Implemented setAttachPrefix(), desired as replacement for dangerous reattach() -- marked deprecated now. --- zypp/media/MediaHandler.cc | 64 ++++++++++++++++++++++++++++++++++++++++------ zypp/media/MediaHandler.h | 23 +++++++++++------ zypp/media/MediaManager.cc | 11 +++++++- zypp/media/MediaManager.h | 28 +++++++++++++++++++- 4 files changed, 108 insertions(+), 18 deletions(-) diff --git a/zypp/media/MediaHandler.cc b/zypp/media/MediaHandler.cc index 0fea09c..7e210ed 100644 --- a/zypp/media/MediaHandler.cc +++ b/zypp/media/MediaHandler.cc @@ -28,7 +28,7 @@ using namespace std; namespace zypp { namespace media { - + Pathname MediaHandler::_attachPrefix(""); /////////////////////////////////////////////////////////////////// // @@ -230,6 +230,34 @@ MediaHandler::findAttachedMedia(const MediaSourceRef &media) const return MediaManager().findAttachedMedia(media); } +/////////////////////////////////////////////////////////////////// +// +// +// METHOD NAME : MediaHandler::setAttachPrefix +// METHOD TYPE : void +// +// DESCRIPTION : +// +bool +MediaHandler::setAttachPrefix(const Pathname &attach_prefix) +{ + if( attach_prefix.empty()) + { + MIL << "Reseting to built-in attach point prefixes." + << std::endl; + MediaHandler::_attachPrefix = attach_prefix; + return true; + } + else + if( MediaHandler::checkAttachPoint(attach_prefix, false, true)) + { + MIL << "Setting user defined attach point prefix: " + << attach_prefix << std::endl; + MediaHandler::_attachPrefix = attach_prefix; + return true; + } + return false; +} /////////////////////////////////////////////////////////////////// // @@ -250,7 +278,12 @@ MediaHandler::createAttachPoint() const }; Pathname apoint; - Pathname aroot; + Pathname aroot( MediaHandler::_attachPrefix); + + if( !aroot.empty()) + { + apoint = createAttachPoint(aroot); + } for ( const char ** def = defmounts; *def && apoint.empty(); ++def ) { aroot = *def; if( aroot.empty()) @@ -474,13 +507,21 @@ void MediaHandler::attach( bool next ) // that checks the media against /etc/mtab ... setMediaSource(MediaSourceRef()); + // + // FIXME: special temp handling because reattach + // may set path to a prefix directory + // note: reattach is deprecated.... + // + AttachPoint ap( attachPointHint()); + if( ap.temp) + ap.path = ""; + setAttachPoint(ap.path, ap.temp); + attachTo( next ); // pass to concrete handler MIL << "Attached: " << *this << endl; } - - /////////////////////////////////////////////////////////////////// // // @@ -569,7 +610,7 @@ void MediaHandler::release( bool eject ) else { DBG << "Releasing shared media reference only" << std::endl; _mediaSource.reset(NULL); - //setAttachPoint("", true); + setAttachPoint("", true); } MIL << "Released: " << *this << endl; } @@ -577,13 +618,14 @@ void MediaHandler::release( bool eject ) bool MediaHandler::checkAttachPoint(const Pathname &apoint) const { - return checkAttachPoint( apoint, true, false); + return MediaHandler::checkAttachPoint( apoint, true, false); } +// STATIC bool MediaHandler::checkAttachPoint(const Pathname &apoint, bool emptydir, - bool writeable) const + bool writeable) { if( apoint.empty() || !apoint.absolute()) { @@ -591,6 +633,12 @@ MediaHandler::checkAttachPoint(const Pathname &apoint, << std::endl; return false; } + if( apoint == "/") + { + ERR << "Attach point '" << apoint << "' is not allowed" + << std::endl; + return false; + } PathInfo ainfo(apoint); if( !ainfo.isDir()) @@ -652,7 +700,7 @@ MediaHandler::reattach(const Pathname &attach_point, { // check if the new attach point root hint is // a writable directory; may contains files. - if( !checkAttachPoint(attach_point, false, true)) + if( !MediaHandler::checkAttachPoint(attach_point, false, true)) ZYPP_THROW( MediaBadAttachPointException(url())); } } diff --git a/zypp/media/MediaHandler.h b/zypp/media/MediaHandler.h index 4cc2cb2..fc748c8 100644 --- a/zypp/media/MediaHandler.h +++ b/zypp/media/MediaHandler.h @@ -47,17 +47,24 @@ class MediaHandler { typedef shared_ptr Ptr; typedef shared_ptr constPtr; + static bool setAttachPrefix(const Pathname &attach_prefix); + private: + /** + * User defined default attach point prefix. + */ + static Pathname _attachPrefix; + /** * The attached media source. */ - MediaSourceRef _mediaSource; + MediaSourceRef _mediaSource; /** * This is where the media will be actually attached ("mounted"). * All files are provided bellow this + _relativeRoot directory. **/ - AttachPointRef _attachPoint; + AttachPointRef _attachPoint; /** * The user provided attach point or the last attach point from @@ -67,23 +74,23 @@ class MediaHandler { * dir, true => create temp attach point bellow of dir * dir, false => user specified attach point (not removed) */ - AttachPoint _AttachPointHint; + AttachPoint _AttachPointHint; /** * The relative root directory of the data on the media. * See also localRoot() and urlpath_below_attachpoint_r * constructor argument. */ - Pathname _relativeRoot; + Pathname _relativeRoot; /** * True if concrete handler downloads files to the local * filesystem. If true releaseFile/Dir will delete them. **/ - bool _does_download; + bool _does_download; /** timestamp of the the last attach verification */ - mutable time_t _attach_mtime; + mutable time_t _attach_mtime; protected: /** @@ -135,9 +142,9 @@ class MediaHandler { virtual bool checkAttachPoint(const Pathname &apoint) const; - bool checkAttachPoint(const Pathname &apoint, + static bool checkAttachPoint(const Pathname &apoint, bool empty_dir, - bool writeable) const; + bool writeable); bool isUseableAttachPoint(const Pathname &path) const; diff --git a/zypp/media/MediaManager.cc b/zypp/media/MediaManager.cc index 8e1c3a5..80ddbeb 100644 --- a/zypp/media/MediaManager.cc +++ b/zypp/media/MediaManager.cc @@ -11,8 +11,8 @@ */ #include #include +#include #include -//#include #include #include @@ -315,6 +315,15 @@ namespace zypp } // --------------------------------------------------------------- + bool + MediaManager::setAttachPrefix(const Pathname &attach_prefix) + { + MutexLock glock(g_Mutex); + + return MediaHandler::setAttachPrefix(attach_prefix); + } + + // --------------------------------------------------------------- void MediaManager::attach(MediaAccessId accessId, bool next) { diff --git a/zypp/media/MediaManager.h b/zypp/media/MediaManager.h index 45e2ee3..e2096ea 100644 --- a/zypp/media/MediaManager.h +++ b/zypp/media/MediaManager.h @@ -14,6 +14,7 @@ #include +#include #include #include #include @@ -152,6 +153,12 @@ namespace zypp /** * Opens the media access for specified with the url. * + * If the \p preferred_attach_point parameter does not + * point to a usable attach point directory, the media + * manager automatically creates a temporary attach + * point in a default directory. This default directory + * can be changed using setAttachPrefix() function. + * * \param url The media access url. * \param preferred_attach_point The preferred, already * existing directory, where the media should be @@ -220,6 +227,21 @@ namespace zypp public: /** + * Set or resets the directory name, where the media manager + * handlers create their temporary attach points (see open() + * function). + * It has effect to newly created temporary attach points only. + * + * \param attach_prefix The new prefix for temporary attach + * points, or empty pathname to reset to defaults. + * \return True on success, false if the \p attach_prefix + * parameters contains a path name, that does not + * point to a writable directory. + */ + bool + setAttachPrefix(const Pathname &attach_prefix); + + /** * Attach the media using the concrete handler. * * \throws MediaNotOpenException for invalid access id. @@ -230,6 +252,10 @@ namespace zypp /** * Reattach to a new attach point. * + * \deprecated This function will be removed, because the + * reattach function has race conditions (e.g. open file + * in the old attach point). Use setAttachPrefix() instead. + * * \param accessId A media access Id. * \param attach_point A new attach point directory. * \param temporary Whether to reattach to a temporary @@ -243,7 +269,7 @@ namespace zypp void reattach(MediaAccessId accessId, const Pathname &attach_point, - bool temporary = false); + bool temporary = false) ZYPP_DEPRECATED; /** * Release the attached media and optionally eject. -- 2.7.4