00001
00002
00003
00004
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
00031
00033
00035
00036
00037
00038
00039
00040
00041
00042 MediaNFS::MediaNFS( const Url & url_r,
00043 const Pathname & attach_point_hint_r )
00044 : MediaHandler( url_r, attach_point_hint_r,
00045 "/",
00046 false )
00047 {
00048 MIL << "MediaNFS::MediaNFS(" << url_r << ", " << attach_point_hint_r << ")" << endl;
00049 }
00050
00052
00053
00054
00055
00056
00057
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
00122
00123
00124 if ( !contains_lock ) {
00125 optionList.push_back( "nolock" );
00126 }
00127
00128
00129
00130
00131
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
00148
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
00177
00178
00179
00180
00181 bool
00182 MediaNFS::isAttached() const
00183 {
00184 return checkAttached(true);
00185 }
00186
00188
00189
00190
00191
00192
00193
00194
00195 void MediaNFS::releaseFrom( bool eject )
00196 {
00197 Mount mount;
00198 mount.umount(attachPoint().asString());
00199 }
00200
00201
00203
00204
00205
00206
00207
00208
00209 void MediaNFS::getFile (const Pathname & filename) const
00210 {
00211 MediaHandler::getFile( filename );;
00212 }
00213
00215
00216
00217
00218
00219
00220
00221 void MediaNFS::getDir( const Pathname & dirname, bool recurse_r ) const
00222 {
00223 MediaHandler::getDir( dirname, recurse_r );
00224 }
00225
00227
00228
00229
00230
00231
00232
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
00244
00245
00246
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 }
00261 }