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 std::string mountpoint( attachPoint().asString() );
92 if( !isUseableAttachPoint(attachPoint()))
94 mountpoint = createAttachPoint().asString();
95 if( mountpoint.empty())
96 ZYPP_THROW( MediaBadAttachPointException(url()));
97 setAttachPoint( mountpoint, true);
100 std::string filesystem( _url.getScheme() );
101 if ( filesystem != "nfs4" && _url.getQueryParam("type") == "nfs4" )
106 string options = _url.getQueryParam("mountoptions");
112 vector<string> optionList;
113 str::split( options, std::back_inserter(optionList), "," );
114 vector<string>::const_iterator it;
115 bool contains_lock = false, contains_soft = false,
116 contains_timeo = false, contains_hard = false;
118 for( it = optionList.begin(); it != optionList.end(); ++it ) {
119 if ( *it == "lock" || *it == "nolock" ) contains_lock = true;
120 else if ( *it == "soft") contains_soft = true;
121 else if ( *it == "hard") contains_hard = true;
122 else if ( it->find("timeo") != string::npos ) contains_timeo = true;
125 if ( !(contains_lock && contains_soft) ) {
126 // Add option "nolock", unless option "lock" or "unlock" is already set.
127 // This should prevent the mount command hanging when the portmapper isn't
129 if ( !contains_lock ) {
130 optionList.push_back( "nolock" );
132 // Add options "soft,timeo=NFS_MOUNT_TIMEOUT", unless they are set
133 // already or "hard" option is explicitly specified. This prevent
134 // the mount command from hanging when the nfs server is not responding
135 // and file transactions from an unresponsive to throw an error after
136 // a short time instead of hanging forever
137 if ( !(contains_soft || contains_hard) ) {
138 optionList.push_back( "soft" );
139 if ( !contains_timeo ) {
141 s << "timeo=" << NFS_MOUNT_TIMEOUT;
142 optionList.push_back( s.str() );
145 options = str::join( optionList, "," );
148 mount.mount(path,mountpoint,filesystem,options);
150 setMediaSource(media);
152 // wait for /etc/mtab update ...
153 // (shouldn't be needed)
156 while( !(mountsucceeded=isAttached()) && --limit)
163 setMediaSource(MediaSourceRef());
166 mount.umount(attachPoint().asString());
168 catch (const MediaException & excpt_r)
170 ZYPP_CAUGHT(excpt_r);
172 ZYPP_THROW(MediaMountException(
173 "Unable to verify that the media was mounted",
179 ///////////////////////////////////////////////////////////////////
181 // METHOD NAME : MediaNFS::isAttached
182 // METHOD TYPE : bool
184 // DESCRIPTION : Override check if media is attached.
187 MediaNFS::isAttached() const
189 return checkAttached(true);
192 ///////////////////////////////////////////////////////////////////
195 // METHOD NAME : MediaNFS::releaseFrom
196 // METHOD TYPE : void
198 // DESCRIPTION : Asserted that media is attached.
200 void MediaNFS::releaseFrom( const std::string & ejectDev )
203 mount.umount(attachPoint().asString());
206 ///////////////////////////////////////////////////////////////////
208 // METHOD NAME : MediaNFS::getFile
209 // METHOD TYPE : PMError
211 // DESCRIPTION : Asserted that media is attached.
213 void MediaNFS::getFile (const Pathname & filename, const ByteCount &expectedFileSize_r) const
215 MediaHandler::getFile( filename, expectedFileSize_r );
218 ///////////////////////////////////////////////////////////////////
220 // METHOD NAME : MediaNFS::getDir
221 // METHOD TYPE : PMError
223 // DESCRIPTION : Asserted that media is attached.
225 void MediaNFS::getDir( const Pathname & dirname, bool recurse_r ) const
227 MediaHandler::getDir( dirname, recurse_r );
230 ///////////////////////////////////////////////////////////////////
233 // METHOD NAME : MediaNFS::getDirInfo
234 // METHOD TYPE : PMError
236 // DESCRIPTION : Asserted that media is attached and retlist is empty.
238 void MediaNFS::getDirInfo( std::list<std::string> & retlist,
239 const Pathname & dirname, bool dots ) const
241 MediaHandler::getDirInfo( retlist, dirname, dots );
244 ///////////////////////////////////////////////////////////////////
247 // METHOD NAME : MediaNFS::getDirInfo
248 // METHOD TYPE : PMError
250 // DESCRIPTION : Asserted that media is attached and retlist is empty.
252 void MediaNFS::getDirInfo( filesystem::DirContent & retlist,
253 const Pathname & dirname, bool dots ) const
255 MediaHandler::getDirInfo( retlist, dirname, dots );
258 bool MediaNFS::getDoesFileExist( const Pathname & filename ) const
260 return MediaHandler::getDoesFileExist( filename );