MediaNFS.cc

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

Generated on Tue Sep 25 19:23:02 2007 for libzypp by  doxygen 1.5.3