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 const char* const filesystem = "nfs";
90 std::string mountpoint = attachPoint().asString();
93 if( !isUseableAttachPoint(attachPoint()))
95 mountpoint = createAttachPoint().asString();
96 if( mountpoint.empty())
97 ZYPP_THROW( MediaBadAttachPointException(url()));
98 setAttachPoint( mountpoint, true);
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, "," );
143 mount.mount(path,mountpoint,filesystem,options);
145 setMediaSource(media);
147 // wait for /etc/mtab update ...
148 // (shouldn't be needed)
151 while( !(mountsucceeded=isAttached()) && --limit)
158 setMediaSource(MediaSourceRef());
161 mount.umount(attachPoint().asString());
163 catch (const MediaException & excpt_r)
165 ZYPP_CAUGHT(excpt_r);
167 ZYPP_THROW(MediaMountException(
168 "Unable to verify that the media was mounted",
174 ///////////////////////////////////////////////////////////////////
176 // METHOD NAME : MediaNFS::isAttached
177 // METHOD TYPE : bool
179 // DESCRIPTION : Override check if media is attached.
182 MediaNFS::isAttached() const
184 return checkAttached(true);
187 ///////////////////////////////////////////////////////////////////
190 // METHOD NAME : MediaNFS::releaseFrom
191 // METHOD TYPE : void
193 // DESCRIPTION : Asserted that media is attached.
195 void MediaNFS::releaseFrom( const std::string & ejectDev )
198 mount.umount(attachPoint().asString());
201 ///////////////////////////////////////////////////////////////////
203 // METHOD NAME : MediaNFS::getFile
204 // METHOD TYPE : PMError
206 // DESCRIPTION : Asserted that media is attached.
208 void MediaNFS::getFile (const Pathname & filename) const
210 MediaHandler::getFile( filename );;
213 ///////////////////////////////////////////////////////////////////
215 // METHOD NAME : MediaNFS::getDir
216 // METHOD TYPE : PMError
218 // DESCRIPTION : Asserted that media is attached.
220 void MediaNFS::getDir( const Pathname & dirname, bool recurse_r ) const
222 MediaHandler::getDir( dirname, recurse_r );
225 ///////////////////////////////////////////////////////////////////
228 // METHOD NAME : MediaNFS::getDirInfo
229 // METHOD TYPE : PMError
231 // DESCRIPTION : Asserted that media is attached and retlist is empty.
233 void MediaNFS::getDirInfo( std::list<std::string> & retlist,
234 const Pathname & dirname, bool dots ) const
236 MediaHandler::getDirInfo( retlist, dirname, dots );
239 ///////////////////////////////////////////////////////////////////
242 // METHOD NAME : MediaNFS::getDirInfo
243 // METHOD TYPE : PMError
245 // DESCRIPTION : Asserted that media is attached and retlist is empty.
247 void MediaNFS::getDirInfo( filesystem::DirContent & retlist,
248 const Pathname & dirname, bool dots ) const
250 MediaHandler::getDirInfo( retlist, dirname, dots );
253 bool MediaNFS::getDoesFileExist( const Pathname & filename ) const
255 return MediaHandler::getDoesFileExist( filename );