1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaNFS.cc
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/String.h"
18 #include "zypp/media/MediaNFS.h"
19 #include "zypp/media/Mount.h"
28 ///////////////////////////////////////////////////////////////////
30 // CLASS NAME : MediaNFS
32 ///////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////
37 // METHOD NAME : MediaNFS::MediaNFS
38 // METHOD TYPE : Constructor
42 MediaNFS::MediaNFS( const Url & url_r,
43 const Pathname & attach_point_hint_r )
44 : MediaHandler( url_r, attach_point_hint_r,
45 "/", // urlpath at attachpoint
46 false ) // does_download
48 MIL << "MediaNFS::MediaNFS(" << url_r << ", " << attach_point_hint_r << ")" << endl;
51 ///////////////////////////////////////////////////////////////////
54 // METHOD NAME : MediaNFS::attachTo
55 // METHOD TYPE : PMError
57 // DESCRIPTION : Asserted that not already attached, and attachPoint is a directory.
59 void MediaNFS::attachTo(bool next)
61 if(_url.getHost().empty())
62 ZYPP_THROW(MediaBadUrlEmptyHostException(_url));
64 ZYPP_THROW(MediaNotSupportedException(_url));
66 string path = _url.getHost();
68 path += Pathname(_url.getPathName()).asString();
70 MediaSourceRef media( new MediaSource("nfs", path));
71 AttachedMedia ret( findAttachedMedia( media));
73 if( ret.mediaSource &&
75 !ret.attachPoint->empty())
77 DBG << "Using a shared media "
78 << ret.mediaSource->name
80 << ret.attachPoint->path
84 setAttachPoint(ret.attachPoint);
85 setMediaSource(ret.mediaSource);
89 if( !isUseableAttachPoint( attachPoint() ) )
91 setAttachPoint( createAttachPoint(), true );
93 std::string mountpoint( attachPoint().asString() );
95 std::string filesystem( _url.getScheme() );
96 if ( filesystem != "nfs4" && _url.getQueryParam("type") == "nfs4" )
101 string options = _url.getQueryParam("mountoptions");
107 vector<string> optionList;
108 str::split( options, std::back_inserter(optionList), "," );
109 vector<string>::const_iterator it;
110 bool contains_lock = false, contains_soft = false,
111 contains_timeo = false, contains_hard = false;
113 for( it = optionList.begin(); it != optionList.end(); ++it ) {
114 if ( *it == "lock" || *it == "nolock" ) contains_lock = true;
115 else if ( *it == "soft") contains_soft = true;
116 else if ( *it == "hard") contains_hard = true;
117 else if ( it->find("timeo") != string::npos ) contains_timeo = true;
120 if ( !(contains_lock && contains_soft) ) {
121 // Add option "nolock", unless option "lock" or "unlock" is already set.
122 // This should prevent the mount command hanging when the portmapper isn't
124 if ( !contains_lock ) {
125 optionList.push_back( "nolock" );
127 // Add options "soft,timeo=NFS_MOUNT_TIMEOUT", unless they are set
128 // already or "hard" option is explicitly specified. This prevent
129 // the mount command from hanging when the nfs server is not responding
130 // and file transactions from an unresponsive to throw an error after
131 // a short time instead of hanging forever
132 if ( !(contains_soft || contains_hard) ) {
133 optionList.push_back( "soft" );
134 if ( !contains_timeo ) {
136 s << "timeo=" << NFS_MOUNT_TIMEOUT;
137 optionList.push_back( s.str() );
140 options = str::join( optionList, "," );
144 mount.mount(path,mountpoint,filesystem,options);
146 setMediaSource(media);
148 // wait for /etc/mtab update ...
149 // (shouldn't be needed)
152 while( !(mountsucceeded=isAttached()) && --limit)
159 setMediaSource(MediaSourceRef());
162 mount.umount(attachPoint().asString());
164 catch (const MediaException & excpt_r)
166 ZYPP_CAUGHT(excpt_r);
168 ZYPP_THROW(MediaMountException(
169 "Unable to verify that the media was mounted",
175 ///////////////////////////////////////////////////////////////////
177 // METHOD NAME : MediaNFS::isAttached
178 // METHOD TYPE : bool
180 // DESCRIPTION : Override check if media is attached.
183 MediaNFS::isAttached() const
185 return checkAttached(true);
188 ///////////////////////////////////////////////////////////////////
191 // METHOD NAME : MediaNFS::releaseFrom
192 // METHOD TYPE : void
194 // DESCRIPTION : Asserted that media is attached.
196 void MediaNFS::releaseFrom( const std::string & ejectDev )
199 mount.umount(attachPoint().asString());
202 ///////////////////////////////////////////////////////////////////
204 // METHOD NAME : MediaNFS::getFile
205 // METHOD TYPE : PMError
207 // DESCRIPTION : Asserted that media is attached.
209 void MediaNFS::getFile (const Pathname & filename) const
211 MediaHandler::getFile( filename );;
214 ///////////////////////////////////////////////////////////////////
216 // METHOD NAME : MediaNFS::getDir
217 // METHOD TYPE : PMError
219 // DESCRIPTION : Asserted that media is attached.
221 void MediaNFS::getDir( const Pathname & dirname, bool recurse_r ) const
223 MediaHandler::getDir( dirname, recurse_r );
226 ///////////////////////////////////////////////////////////////////
229 // METHOD NAME : MediaNFS::getDirInfo
230 // METHOD TYPE : PMError
232 // DESCRIPTION : Asserted that media is attached and retlist is empty.
234 void MediaNFS::getDirInfo( std::list<std::string> & retlist,
235 const Pathname & dirname, bool dots ) const
237 MediaHandler::getDirInfo( retlist, dirname, dots );
240 ///////////////////////////////////////////////////////////////////
243 // METHOD NAME : MediaNFS::getDirInfo
244 // METHOD TYPE : PMError
246 // DESCRIPTION : Asserted that media is attached and retlist is empty.
248 void MediaNFS::getDirInfo( filesystem::DirContent & retlist,
249 const Pathname & dirname, bool dots ) const
251 MediaHandler::getDirInfo( retlist, dirname, dots );
254 bool MediaNFS::getDoesFileExist( const Pathname & filename ) const
256 return MediaHandler::getDoesFileExist( filename );