libzypp  13.10.6
MediaISO.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 
14 #include "zypp/base/Logger.h"
15 #include "zypp/media/Mount.h"
16 
17 #include "zypp/media/MediaISO.h"
18 
19 
20 #define LOSETUP_TOOL_PATH "/sbin/losetup"
21 
22 using std::string;
23 using std::endl;
24 
26 namespace zypp
27 {
28 
30  namespace media
31  {
32 
34  //
35  // MediaISO Url:
36  //
37  // Schema: iso
38  // Path name: subdir to the location of desired files inside
39  // of the ISO.
40  // Query parameters:
41  // url: The iso filename source media url pointing
42  // to a directory containing the ISO file.
43  // mnt: Prefered attach point for source media url.
44  // iso: The name of the iso file.
45  // filesystem: Optional, defaults to "auto".
46  //
48  MediaISO::MediaISO(const Url &url_r,
49  const Pathname &attach_point_hint_r)
50  : MediaHandler(url_r, attach_point_hint_r,
51  url_r.getPathName(), // urlpath below attachpoint
52  false) // does_download
53  {
54  MIL << "MediaISO::MediaISO(" << url_r << ", "
55  << attach_point_hint_r << ")" << std::endl;
56 
57  _isofile = _url.getQueryParam("iso");
58  if( _isofile.empty())
59  {
60  ERR << "Media url does not contain iso filename" << std::endl;
62  }
63 
64  _filesystem = _url.getQueryParam("filesystem");
65  if( _filesystem.empty())
66  _filesystem = "auto";
67 
68  std::string arg;
69  zypp::Url src;
70  try
71  {
72  // this percent-decodes the query parameter, it must be later encoded
73  // again before used in a Url object
74  arg = _url.getQueryParam("url");
75  if( arg.empty() && _isofile.dirname().absolute())
76  {
77  src = std::string("dir:///");
78  src.setPathName(url::encode(_isofile.dirname().asString(), URL_SAFE_CHARS));
79  _isofile = _isofile.basename();
80  }
81  else
82  {
83  src = url::encode(arg, URL_SAFE_CHARS);
84  }
85  }
86  catch(const zypp::url::UrlException &e)
87  {
88  ZYPP_CAUGHT(e);
89  ERR << "Unable to parse iso filename source media url" << std::endl;
91  ne.remember(e);
92  ZYPP_THROW(ne);
93  }
94  if( !src.isValid())
95  {
96  ERR << "Invalid iso filename source media url" << std::endl;
98  }
99  if( src.getScheme() == "iso")
100  {
101  ERR << "ISO filename source media url with iso scheme (nested iso): "
102  << src.asString() << std::endl;
104  }
105  else
106  if( !(src.getScheme() == "hd" ||
107  src.getScheme() == "dir" ||
108  src.getScheme() == "file" ||
109  src.getScheme() == "nfs" ||
110  src.getScheme() == "nfs4" ||
111  src.getScheme() == "smb" ||
112  src.getScheme() == "cifs"))
113  {
114  ERR << "ISO filename source media url scheme is not supported: "
115  << src.asString() << std::endl;
117  }
118 
119  MediaManager manager;
120 
121  _parentId = manager.open(src, _url.getQueryParam("mnt"));
122  }
123 
124  // ---------------------------------------------------------------
126  {
127  try
128  {
129  release();
130 
131  if( _parentId)
132  {
133  DBG << "Closing parent handler..." << std::endl;
134  MediaManager manager;
135  if(manager.isOpen(_parentId))
136  manager.close(_parentId);
137  _parentId = 0;
138  }
139  }
140  catch( ... )
141  {}
142  }
143 
144  // ---------------------------------------------------------------
145  bool
147  {
148  return checkAttached(false);
149  }
150 
151  // ---------------------------------------------------------------
153  {
154  const char* argv[] =
155  {
157  "-f",
158  NULL
159  };
161 
162  string out = losetup.receiveLine();
163  string device = out.substr(0, out.size() - 1); // remove the trailing endl
164  for(; out.length(); out = losetup.receiveLine())
165  DBG << "losetup: " << out;
166 
167  if (losetup.close() != 0)
168  {
169  ERR << LOSETUP_TOOL_PATH " failed to find an unused loop device." << std::endl;
171  }
172 
173  DBG << "found " << device << endl;
174  return device;
175  }
176 
177  // ---------------------------------------------------------------
178  void MediaISO::attachTo(bool next)
179  {
180  if(next)
182 
183  MediaManager manager;
184  manager.attach(_parentId);
185 
186  try
187  {
188  manager.provideFile(_parentId, _isofile);
189  }
190  catch(const MediaException &e1)
191  {
192  ZYPP_CAUGHT(e1);
193  try
194  {
195  manager.release(_parentId);
196  }
197  catch(const MediaException &e2)
198  {
199  ZYPP_CAUGHT(e2);
200  }
201 
203  "Unable to find iso filename on source media",
204  _url.asString(), attachPoint().asString()
205  );
206  e3.remember(e1);
207  ZYPP_THROW(e3);
208  }
209 
210  // if the provided file is a symlink, expand it (#274651)
211  // (this will probably work only for file/dir and cd/dvd schemes)
212  Pathname isofile = expandlink(manager.localPath(_parentId, _isofile));
213  if( isofile.empty() || !PathInfo(isofile).isFile())
214  {
216  }
217 
219  string loopdev = findUnusedLoopDevice(); // (bnc #428009)
220 
221  MediaSourceRef media( new MediaSource("iso", loopdev));
222  PathInfo dinfo(loopdev);
223  if( dinfo.isBlk())
224  {
225  media->maj_nr = dinfo.major();
226  media->min_nr = dinfo.minor();
227  }
228  else
229  ERR << loopdev << " is not a block device" << endl;
230 
231  AttachedMedia ret( findAttachedMedia( media));
232  if( ret.mediaSource &&
233  ret.attachPoint &&
234  !ret.attachPoint->empty())
235  {
236  DBG << "Using a shared media "
237  << ret.mediaSource->name
238  << " attached on "
239  << ret.attachPoint->path
240  << std::endl;
244  return;
245  }
246 
247  std::string mountpoint = attachPoint().asString();
249  {
250  mountpoint = createAttachPoint().asString();
251  if( mountpoint.empty())
253  setAttachPoint( mountpoint, true);
254  }
255 
256  std::string mountopts("ro,loop=" + loopdev);
257 
258  Mount mount;
259  mount.mount(isofile.asString(), mountpoint,
260  _filesystem, mountopts);
261 
262  setMediaSource(media);
263 
264  // wait for /etc/mtab update ...
265  // (shouldn't be needed)
266  int limit = 3;
267  bool mountsucceeded;
268  while( !(mountsucceeded=isAttached()) && --limit)
269  {
270  sleep(1);
271  }
272 
273  if( !mountsucceeded)
274  {
276  try
277  {
278  mount.umount(attachPoint().asString());
279  manager.release(_parentId);
280  }
281  catch (const MediaException & excpt_r)
282  {
283  ZYPP_CAUGHT(excpt_r);
284  }
286  "Unable to verify that the media was mounted",
287  isofile.asString(), mountpoint
288  ));
289  }
290  }
291 
292  // ---------------------------------------------------------------
293 
294  void MediaISO::releaseFrom(const std::string & ejectDev)
295  {
296  Mount mount;
297  mount.umount(attachPoint().asString());
298 
299  if( _parentId)
300  {
301  // Unmounting the iso already succeeded,
302  // so don't let exceptions escape.
303  MediaManager manager;
304  try
305  {
306  manager.release(_parentId);
307  }
308  catch ( const Exception & excpt_r )
309  {
310  ZYPP_CAUGHT( excpt_r );
311  WAR << "Not been able to cleanup the parent mount." << endl;
312  }
313  }
314  // else:
315  // the media manager has reset the _parentId
316  // and will destroy the parent handler itself.
317  }
318 
319  // ---------------------------------------------------------------
320  void MediaISO::getFile(const Pathname &filename) const
321  {
322  MediaHandler::getFile(filename);
323  }
324 
325  // ---------------------------------------------------------------
326  void MediaISO::getDir(const Pathname &dirname,
327  bool recurse_r) const
328  {
329  MediaHandler::getDir(dirname, recurse_r);
330  }
331 
332  // ---------------------------------------------------------------
333  void MediaISO::getDirInfo(std::list<std::string> &retlist,
334  const Pathname &dirname,
335  bool dots) const
336  {
337  MediaHandler::getDirInfo( retlist, dirname, dots );
338  }
339 
340  // ---------------------------------------------------------------
342  const Pathname &dirname,
343  bool dots) const
344  {
345  MediaHandler::getDirInfo(retlist, dirname, dots);
346  }
347 
348  bool MediaISO::getDoesFileExist( const Pathname & filename ) const
349  {
350  return MediaHandler::getDoesFileExist( filename );
351  }
352 
354  } // namespace media
356 
358 } // namespace zypp
360 
361 // vim: set ts=2 sts=2 sw=2 ai et:
362 
virtual void releaseFrom(const std::string &ejectDev="")
Call concrete handler to release the media.
Definition: MediaISO.cc:294
#define MIL
Definition: Logger.h:47
Interface to the mount program.
Definition: Mount.h:69
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
virtual void getDir(const Pathname &dirname, bool recurse_r) const =0
Call concrete handler to provide directory content (not recursive!) below attach point.
virtual bool getDoesFileExist(const Pathname &filename) const
check if a file exists
Definition: MediaISO.cc:348
bool isOpen(MediaAccessId accessId) const
Query if the media access is open / exists.
virtual void getFile(const Pathname &filename) const
Call concrete handler to provide file below attach point.
Definition: MediaISO.cc:320
zypp::RW_pointer< MediaSource > MediaSourceRef
Definition: MediaSource.h:124
virtual bool isAttached() const
True if media is attached.
Definition: MediaISO.cc:146
std::string findUnusedLoopDevice()
Definition: MediaISO.cc:152
void setAttachPoint(const Pathname &path, bool temp)
Set a new attach point.
std::string _filesystem
Definition: MediaISO.h:40
Pathname createAttachPoint() const
Try to create a default / temporary attach point.
std::string encode(const std::string &str, const std::string &safe, EEncoding eflag)
Encodes a string using URL percent encoding.
Definition: UrlUtils.cc:32
virtual void getDirInfo(std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const
Call concrete handler to provide a content list of directory on media via retlist.
Definition: MediaISO.cc:333
Base class for all URL exceptions.
Definition: UrlException.h:31
bool isValid() const
Verifies the Url.
Definition: Url.cc:483
void provideFile(MediaAccessId accessId, const Pathname &filename) const
Provide provide file denoted by relative path below of the &#39;attach point&#39; of the specified media and ...
#define ERR
Definition: Logger.h:49
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:491
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:66
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:89
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
Definition: Url.cc:758
AttachPointRef attachPoint
Definition: MediaSource.h:145
AttachedMedia findAttachedMedia(const MediaSourceRef &media) const
Ask the media manager if specified media source is already attached.
MediaSourceRef mediaSource
Definition: MediaSource.h:144
Abstract base class for &#39;physical&#39; MediaHandler like MediaCD, etc.
Definition: MediaHandler.h:45
A simple structure containing references to a media source and its attach point.
Definition: MediaSource.h:133
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
const Url _url
Url to handle.
Definition: MediaHandler.h:110
void setMediaSource(const MediaSourceRef &ref)
Set new media source reference.
Just inherits Exception to separate media exceptions.
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for &#39;localRoot() + pathname&#39;, but returns an empty pathname if media is not attached...
#define WAR
Definition: Logger.h:48
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:544
#define URL_SAFE_CHARS
Characters that are safe for URL without percent-encoding.
Definition: UrlUtils.h:22
std::string receiveLine()
Read one line from the input stream.
std::string getQueryParam(const std::string &param, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
Definition: Url.cc:654
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...
Thrown if /sbin/losetup fails to find an unused loop device for mounting an .iso image.
virtual void getDir(const Pathname &dirname, bool recurse_r) const
Call concrete handler to provide directory content (not recursive!) below attach point.
Definition: MediaISO.cc:326
void removeAttachPoint()
Remove unused attach point.
virtual void attachTo(bool next=false)
Call concrete handler to attach the media.
Definition: MediaISO.cc:178
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
std::string asString(const Patch::SeverityFlag &obj)
Definition: Patch.cc:149
Media source internally used by MediaManager and MediaHandler.
Definition: MediaSource.h:36
int close()
Wait for the progamm to complete.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:324
Manages access to the &#39;physical&#39; media, e.g CDROM drives, Disk volumes, directory trees...
Definition: MediaManager.h:473
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.
virtual bool getDoesFileExist(const Pathname &filename) const =0
check if a file exists
Pathname attachPoint() const
Return the currently used attach point.
Base class for Exception.
Definition: Exception.h:143
MediaAccessId _parentId
Access Id of media handler we depend on.
Definition: MediaHandler.h:115
virtual void getFile(const Pathname &filename) const =0
Call concrete handler to provide file below attach point.
#define LOSETUP_TOOL_PATH
Definition: MediaISO.cc:20
bool checkAttached(bool matchMountFs) const
Check actual mediaSource attachment against the current mount table of the system.
Pathname expandlink(const Pathname &path_r)
Recursively follows the symlink pointed to by path_r and returns the Pathname to the real file or dir...
Definition: PathInfo.cc:874
std::string getScheme() const
Returns the scheme name of the URL.
Definition: Url.cc:527
void release(const std::string &ejectDev="")
Use concrete handler to release the media.
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
Url url() const
Url used.
Definition: MediaHandler.h:506
Url manipulation class.
Definition: Url.h:87
void umount(const std::string &path)
umount device
Definition: Mount.cc:162
void close(MediaAccessId accessId)
Close the media access with specified id.
#define DBG
Definition: Logger.h:46
MediaISO(const Url &url_r, const Pathname &attach_point_hint_r)
Definition: MediaISO.cc:48