MediaNFS.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00013 #include <iostream>
00014 
00015 #include "zypp/base/Logger.h"
00016 #include "zypp/base/String.h"
00017 #include "zypp/media/MediaNFS.h"
00018 #include "zypp/media/Mount.h"
00019 
00020 #include <dirent.h>
00021 
00022 using namespace std;
00023 
00024 namespace zypp {
00025   namespace media {
00026 
00028     //
00029     //  CLASS NAME : MediaNFS
00030     //
00032     
00034     //
00035     //
00036     //  METHOD NAME : MediaNFS::MediaNFS
00037     //  METHOD TYPE : Constructor
00038     //
00039     //  DESCRIPTION :
00040     //
00041     MediaNFS::MediaNFS( const Url &      url_r,
00042                         const Pathname & attach_point_hint_r )
00043       : MediaHandler( url_r, attach_point_hint_r,
00044                       "/", // urlpath at attachpoint
00045                       false ) // does_download
00046     {
00047         MIL << "MediaNFS::MediaNFS(" << url_r << ", " << attach_point_hint_r << ")" << endl;
00048     }
00049 
00051     //
00052     //
00053     //  METHOD NAME : MediaNFS::attachTo
00054     //  METHOD TYPE : PMError
00055     //
00056     //  DESCRIPTION : Asserted that not already attached, and attachPoint is a directory.
00057     //
00058     void MediaNFS::attachTo(bool next)
00059     {
00060       if(_url.getHost().empty())
00061         ZYPP_THROW(MediaBadUrlEmptyHostException(_url));
00062       if(next)
00063         ZYPP_THROW(MediaNotSupportedException(_url));
00064 
00065       string path = _url.getHost();
00066       path += ':';
00067       path += Pathname(_url.getPathName()).asString();
00068 
00069       MediaSourceRef media( new MediaSource("nfs", path));
00070       AttachedMedia  ret( findAttachedMedia( media));
00071 
00072       if( ret.mediaSource &&
00073           ret.attachPoint &&
00074           !ret.attachPoint->empty())
00075       {
00076         DBG << "Using a shared media "
00077             << ret.mediaSource->name
00078             << " attached on "
00079             << ret.attachPoint->path
00080             << endl;
00081 
00082         removeAttachPoint();
00083         setAttachPoint(ret.attachPoint);
00084         setMediaSource(ret.mediaSource);
00085         return;
00086       }
00087 
00088       const char* const filesystem = "nfs";
00089       std::string       mountpoint = attachPoint().asString();
00090       Mount mount;
00091 
00092       if( !isUseableAttachPoint(attachPoint()))
00093       {
00094         mountpoint = createAttachPoint().asString();
00095         if( mountpoint.empty())
00096           ZYPP_THROW( MediaBadAttachPointException(url()));
00097         setAttachPoint( mountpoint, true);
00098       }
00099 
00100       string options = _url.getQueryParam("mountoptions");
00101       if(options.empty())
00102       {
00103         options="ro";
00104       }
00105     
00106       // Add option "nolock", unless option "lock" or "unlock" is already set.
00107       // This should prevent the mount command hanging when the portmapper isn't
00108       // running.
00109       vector<string> optionList;
00110       str::split( options, std::back_inserter(optionList), "," );
00111       vector<string>::const_iterator it;
00112       for( it = optionList.begin(); it != optionList.end(); ++it ) {
00113         if ( *it == "lock" || *it == "nolock" ) break;
00114       }
00115       if ( it == optionList.end() ) {
00116         optionList.push_back( "nolock" );
00117         options = str::join( optionList, "," );
00118       }
00119 
00120       mount.mount(path,mountpoint,filesystem,options);
00121 
00122       setMediaSource(media);
00123 
00124       // wait for /etc/mtab update ...
00125       // (shouldn't be needed)
00126       int limit = 3;
00127       bool mountsucceeded;
00128       while( !(mountsucceeded=isAttached()) && --limit)
00129       {
00130         sleep(1);
00131       }
00132 
00133       if( !mountsucceeded)
00134       {
00135         setMediaSource(MediaSourceRef());
00136         try
00137         {
00138           mount.umount(attachPoint().asString());
00139         }
00140         catch (const MediaException & excpt_r)
00141         {
00142           ZYPP_CAUGHT(excpt_r);
00143         }
00144         ZYPP_THROW(MediaMountException(
00145           "Unable to verify that the media was mounted",
00146           path, mountpoint
00147         ));
00148       }
00149     }
00150 
00152     //
00153     //  METHOD NAME : MediaNFS::isAttached
00154     //  METHOD TYPE : bool
00155     //
00156     //  DESCRIPTION : Override check if media is attached.
00157     //
00158     bool
00159     MediaNFS::isAttached() const
00160     {
00161       return checkAttached(true);
00162     }
00163 
00165     //
00166     //
00167     //  METHOD NAME : MediaNFS::releaseFrom
00168     //  METHOD TYPE : PMError
00169     //
00170     //  DESCRIPTION : Asserted that media is attached.
00171     //
00172     void MediaNFS::releaseFrom( bool eject )
00173     {
00174       Mount mount;
00175       mount.umount(attachPoint().asString());
00176     }
00177 
00178 
00180     //
00181     //  METHOD NAME : MediaNFS::getFile
00182     //  METHOD TYPE : PMError
00183     //
00184     //  DESCRIPTION : Asserted that media is attached.
00185     //
00186     void MediaNFS::getFile (const Pathname & filename) const
00187     {
00188       MediaHandler::getFile( filename );;
00189     }
00190     
00192     //
00193     //  METHOD NAME : MediaNFS::getDir
00194     //  METHOD TYPE : PMError
00195     //
00196     //  DESCRIPTION : Asserted that media is attached.
00197     //
00198     void MediaNFS::getDir( const Pathname & dirname, bool recurse_r ) const
00199     {
00200       MediaHandler::getDir( dirname, recurse_r );
00201     }
00202     
00204     //
00205     //
00206     //  METHOD NAME : MediaNFS::getDirInfo
00207     //  METHOD TYPE : PMError
00208     //
00209     //  DESCRIPTION : Asserted that media is attached and retlist is empty.
00210     //
00211     void MediaNFS::getDirInfo( std::list<std::string> & retlist,
00212                               const Pathname & dirname, bool dots ) const
00213     {
00214       MediaHandler::getDirInfo( retlist, dirname, dots );
00215     }
00216     
00218     //
00219     //
00220     //  METHOD NAME : MediaNFS::getDirInfo
00221     //  METHOD TYPE : PMError
00222     //
00223     //  DESCRIPTION : Asserted that media is attached and retlist is empty.
00224     //
00225     void MediaNFS::getDirInfo( filesystem::DirContent & retlist,
00226                            const Pathname & dirname, bool dots ) const
00227     {
00228       MediaHandler::getDirInfo( retlist, dirname, dots );
00229     }
00230 
00231   } // namespace media
00232 } // namespace zypp

Generated on Thu Jul 6 00:07:21 2006 for zypp by  doxygen 1.4.6