libzypp: zypp/media/MediaNFS.cc Source File

libzypp 17.38.11

MediaNFS.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include <iostream>
14#include <sstream>
15
16#include <zypp-core/base/Logger.h>
17#include <zypp-core/base/String.h>
18#include <zypp/media/MediaNFS.h>
19#include <zypp-media/Mount>
20
21#include <dirent.h>
22
23using std::endl;
24
25namespace zypp {
26 namespace media {
27
29 //
30 // CLASS NAME : MediaNFS
31 //
33
35 //
36 //
37 // METHOD NAME : MediaNFS::MediaNFS
38 // METHOD TYPE : Constructor
39 //
40 // DESCRIPTION :
41 //
42 MediaNFS::MediaNFS(const MirroredOrigin &origin_r,
43 const Pathname & attach_point_hint_r )
44 : MediaHandler( origin_r, attach_point_hint_r,
45 "/", // urlpath at attachpoint
46 false ) // does_download
47 {
48 MIL << "MediaNFS::MediaNFS(" << origin_r.authority().url() << ", " << attach_point_hint_r << ")" << endl;
49 }
50
52 //
53 //
54 // METHOD NAME : MediaNFS::attachTo
55 // METHOD TYPE : PMError
56 //
57 // DESCRIPTION : Asserted that not already attached, and attachPoint is a directory.
58 //
59 void MediaNFS::attachTo(bool next)
60 {
61 if(_origin.authority().url().getHost().empty())
62 ZYPP_THROW(MediaBadUrlEmptyHostException(_origin.authority().url()));
63 if(next)
64 ZYPP_THROW(MediaNotSupportedException(_origin.authority().url()));
65
66 std::string path = _origin.authority().url().getHost();
67 path += ':';
68 path += Pathname(_origin.authority().url().getPathName()).asString();
69
70 MediaSourceRef media( new MediaSource("nfs", path));
71 AttachedMedia ret( findAttachedMedia( media));
72
73 if( ret.mediaSource &&
74 ret.attachPoint &&
75 !ret.attachPoint->empty())
76 {
77 DBG << "Using a shared media "
78 << ret.mediaSource->name
79 << " attached on "
80 << ret.attachPoint->path
81 << endl;
82
83 removeAttachPoint();
84 setAttachPoint(ret.attachPoint);
85 setMediaSource(ret.mediaSource);
86 return;
87 }
88
89 if( !isUseableAttachPoint( attachPoint() ) )
90 {
91 setAttachPoint( createAttachPoint(), true );
92 }
93 std::string mountpoint( attachPoint().asString() );
94
95 std::string filesystem( _origin.authority().url().getScheme() );
96 if ( filesystem != "nfs4" && _origin.authority().url().getQueryParam("type") == "nfs4" )
97 {
98 filesystem = "nfs4";
99 }
100
101 std::string options = _origin.authority().url().getQueryParam("mountoptions");
102 if(options.empty())
103 {
104 options="ro";
105 }
106
107 std::vector<std::string> optionList;
108 str::split( options, std::back_inserter(optionList), "," );
109 std::vector<std::string>::const_iterator it;
110 bool contains_lock = false, contains_soft = false,
111 contains_timeo = false, contains_hard = false;
112
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") != std::string::npos ) contains_timeo = true;
118 }
119
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
123 // running.
124 if ( !contains_lock ) {
125 optionList.push_back( "nolock" );
126 }
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 ) {
135 std::ostringstream s;
136 s << "timeo=" << NFS_MOUNT_TIMEOUT;
137 optionList.push_back( s.str() );
138 }
139 }
140 options = str::join( optionList, "," );
141 }
142
143 Mount mount;
144 mount.mount(path,mountpoint,filesystem,options);
145
146 setMediaSource(media);
147
148 // wait for /etc/mtab update ...
149 // (shouldn't be needed)
150 int limit = 3;
151 bool mountsucceeded = false;
152 while( !(mountsucceeded=isAttached()) && --limit)
153 {
154 sleep(1);
155 }
156
157 if( !mountsucceeded)
158 {
159 setMediaSource(MediaSourceRef());
160 try
161 {
162 mount.umount(attachPoint().asString());
163 }
164 catch (const MediaException & excpt_r)
165 {
166 ZYPP_CAUGHT(excpt_r);
167 }
168 ZYPP_THROW(MediaMountException(
169 "Unable to verify that the media was mounted",
170 path, mountpoint
171 ));
172 }
173 }
174
176 //
177 // METHOD NAME : MediaNFS::isAttached
178 // METHOD TYPE : bool
179 //
180 // DESCRIPTION : Override check if media is attached.
181 //
182 bool
183 MediaNFS::isAttached() const
184 {
185 return checkAttached(true);
186 }
187
189 //
190 //
191 // METHOD NAME : MediaNFS::releaseFrom
192 // METHOD TYPE : void
193 //
194 // DESCRIPTION : Asserted that media is attached.
195 //
196 void MediaNFS::releaseFrom( const std::string & ejectDev )
197 {
198 Mount mount;
199 mount.umount(attachPoint().asString());
200 }
201
203 //
204 // METHOD NAME : MediaNFS::getFile
205 // METHOD TYPE : PMError
206 //
207 // DESCRIPTION : Asserted that media is attached.
208 //
209 void MediaNFS::getFile (const OnMediaLocation &file ) const
210 {
211 MediaHandler::getFile( file );
212 }
213
215 //
216 // METHOD NAME : MediaNFS::getDir
217 // METHOD TYPE : PMError
218 //
219 // DESCRIPTION : Asserted that media is attached.
220 //
221 void MediaNFS::getDir( const Pathname & dirname, bool recurse_r ) const
222 {
223 MediaHandler::getDir( dirname, recurse_r );
224 }
225
227 //
228 //
229 // METHOD NAME : MediaNFS::getDirInfo
230 // METHOD TYPE : PMError
231 //
232 // DESCRIPTION : Asserted that media is attached and retlist is empty.
233 //
234 void MediaNFS::getDirInfo( std::list<std::string> & retlist,
235 const Pathname & dirname, bool dots ) const
236 {
237 MediaHandler::getDirInfo( retlist, dirname, dots );
238 }
239
241 //
242 //
243 // METHOD NAME : MediaNFS::getDirInfo
244 // METHOD TYPE : PMError
245 //
246 // DESCRIPTION : Asserted that media is attached and retlist is empty.
247 //
248 void MediaNFS::getDirInfo( filesystem::DirContent & retlist,
249 const Pathname & dirname, bool dots ) const
250 {
251 MediaHandler::getDirInfo( retlist, dirname, dots );
252 }
253
254 bool MediaNFS::getDoesFileExist( const Pathname & filename ) const
255 {
256 return MediaHandler::getDoesFileExist( filename );
257 }
258
259
260 } // namespace media
261} // namespace zypp
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Logger.h
DBG
#define DBG
Definition Logger.h:102
MIL
#define MIL
Definition Logger.h:103
MediaNFS.h
NFS_MOUNT_TIMEOUT
#define NFS_MOUNT_TIMEOUT
Value of NFS mount minor timeout (passed to timeo option of the NFS mount) in tenths of a second.
Definition MediaNFS.h:24
String.h
zypp::MirroredOrigin
Manages a data source characterized by an authoritative URL and a list of mirror URLs.
Definition MirroredOrigin.h:170
zypp::MirroredOrigin::authority
const OriginEndpoint & authority() const
Definition MirroredOrigin.cc:205
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition onmedialocation.h:37
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:94
zypp::media::MediaBadUrlEmptyHostException
Definition mediaexception.h:308
zypp::media::MediaException
Just inherits Exception to separate media exceptions.
Definition mediaexception.h:37
zypp::media::MediaHandler::isUseableAttachPoint
bool isUseableAttachPoint(const Pathname &path, bool mtab=true) const
Ask media manager, if the specified path is already used as attach point or if there are another atta...
Definition MediaHandler.cc:416
zypp::media::MediaHandler::getFile
virtual void getFile(const OnMediaLocation &file) const
Call concrete handler to provide file below attach point.
Definition MediaHandler.cc:1188
zypp::media::MediaHandler::MediaHandler
MediaHandler(MirroredOrigin origin_r, const Pathname &attach_point_r, Pathname urlpath_below_attachpoint_r, const bool does_download_r)
If the concrete media handler provides a nonempty attach_point, it must be an existing directory.
Definition MediaHandler.cc:55
zypp::media::MediaHandler::_origin
MirroredOrigin _origin
Contains the authority URL and mirrors.
Definition MediaHandler.h:112
zypp::media::MediaHandler::url
Url url() const
Primary Url used.
Definition MediaHandler.h:507
zypp::media::MediaHandler::getDoesFileExist
virtual bool getDoesFileExist(const Pathname &filename) const =0
check if a file exists
Definition MediaHandler.cc:1326
zypp::media::MediaHandler::createAttachPoint
Pathname createAttachPoint() const
Try to create a default / temporary attach point.
Definition MediaHandler.cc:312
zypp::media::MediaHandler::setMediaSource
void setMediaSource(const MediaSourceRef &ref)
Set new media source reference.
Definition MediaHandler.cc:432
zypp::media::MediaHandler::checkAttached
bool checkAttached(bool matchMountFs) const
Check actual mediaSource attachment against the current mount table of the system.
Definition MediaHandler.cc:479
zypp::media::MediaHandler::removeAttachPoint
void removeAttachPoint()
Remove unused attach point.
Definition MediaHandler.cc:143
zypp::media::MediaHandler::setAttachPoint
void setAttachPoint(const Pathname &path, bool temp)
Set a new attach point.
Definition MediaHandler.cc:200
zypp::media::MediaHandler::attachPoint
Pathname attachPoint() const
Return the currently used attach point.
Definition MediaHandler.cc:185
zypp::media::MediaHandler::getDir
virtual void getDir(const Pathname &dirname, bool recurse_r) const =0
Call concrete handler to provide directory content (not recursive!) below attach point.
Definition MediaHandler.cc:1219
zypp::media::MediaHandler::findAttachedMedia
AttachedMedia findAttachedMedia(const MediaSourceRef &media) const
Ask the media manager if specified media source is already attached.
Definition MediaHandler.cc:269
zypp::media::MediaHandler::getDirInfo
virtual void getDirInfo(std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const =0
Call concrete handler to provide a content list of directory on media via retlist.
Definition MediaHandler.cc:1241
zypp::media::MediaMountException
Definition mediaexception.h:56
zypp::media::MediaNFS::attachTo
void attachTo(bool next=false) override
Call concrete handler to attach the media.
Definition MediaNFS.cc:59
zypp::media::MediaNFS::getDir
void getDir(const Pathname &dirname, bool recurse_r) const override
Call concrete handler to provide directory content (not recursive!) below attach point.
Definition MediaNFS.cc:221
zypp::media::MediaNFS::getDirInfo
void getDirInfo(std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const override
Call concrete handler to provide a content list of directory on media via retlist.
Definition MediaNFS.cc:234
zypp::media::MediaNFS::MediaNFS
MediaNFS(const MirroredOrigin &origin_r, const Pathname &attach_point_hint_r)
Definition MediaNFS.cc:42
zypp::media::MediaNFS::releaseFrom
void releaseFrom(const std::string &ejectDev) override
Call concrete handler to release the media.
Definition MediaNFS.cc:196
zypp::media::MediaNFS::getFile
void getFile(const OnMediaLocation &file) const override
Call concrete handler to provide file below attach point.
Definition MediaNFS.cc:209
zypp::media::MediaNFS::getDoesFileExist
bool getDoesFileExist(const Pathname &filename) const override
check if a file exists
Definition MediaNFS.cc:254
zypp::media::MediaNFS::isAttached
bool isAttached() const override
True if media is attached.
Definition MediaNFS.cc:183
zypp::media::MediaNotSupportedException
Definition mediaexception.h:352
zypp::media::MediaSource
Media source internally used by MediaManager and MediaHandler.
Definition MediaSource.h:38
zypp::media::Mount
Interface to the mount program.
Definition mount.h:76
zypp::media::Mount::umount
void umount(const std::string &path)
umount device
Definition mount.cc:117
zypp::media::Mount::mount
void mount(const std::string &source, const std::string &target, const std::string &filesystem, const std::string &options, const Environment &environment=Environment())
mount device
Definition mount.cc:62
zypp::filesystem
Types and functions for filesystem operations.
Definition Glob.cc:24
zypp::filesystem::DirContent
std::list< DirEntry > DirContent
Returned by readdir.
Definition PathInfo.h:534
zypp::media
Definition MediaNetworkRequestExecutor.h:22
zypp::media::MediaSourceRef
zypp::RW_pointer< MediaSource > MediaSourceRef
Definition MediaSource.h:125
zypp::str::join
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition String.h:846
zypp::str::split
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition String.h:602
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::asString
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
zypp::media::AttachedMedia
A simple structure containing references to a media source and its attach point.
Definition MediaSource.h:135
zypp::media::AttachedMedia::mediaSource
MediaSourceRef mediaSource
Definition MediaSource.h:145
zypp::media::AttachedMedia::attachPoint
AttachPointRef attachPoint
Definition MediaSource.h:146