libzypp  17.5.1
MediaProducts.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_MEDIAPRODUCTS_H_
13 #define ZYPP_MEDIAPRODUCTS_H_
14 
15 #include <iterator>
16 #include <iostream>
17 #include <fstream>
18 #include "zypp/ZConfig.h"
19 #include "zypp/base/Logger.h"
22 
23 #include "zypp/ProgressData.h"
24 
25 namespace zypp
26 {
31  {
33  std::string _name;
34 
38  MediaProductEntry( const Pathname & dir_r = "/", const std::string & name_r = std::string() )
39  : _dir(dir_r), _name(name_r)
40  {
41  }
42 
43  bool operator<( const MediaProductEntry &rhs ) const
44  {
45  int res = _name.compare( rhs._name );
46  return ( res < 0 || ( res == 0 && _dir < rhs._dir ) );
47  }
48  };
49 
53  typedef std::set<MediaProductEntry> MediaProductSet;
54 
58  template <class TOutputIterator>
59  static void scanProductsFile( const Pathname & file_r, TOutputIterator result )
60  {
61  std::ifstream pfile( file_r.asString().c_str() );
62  while ( pfile.good() ) {
63 
64  std::string value = str::getline( pfile, str::TRIM );
65  if ( pfile.bad() ) {
66  ERR << "Error parsing " << file_r << std::endl;
67  ZYPP_THROW(Exception("Error parsing " + file_r.asString()));
68  }
69  if ( pfile.fail() ) {
70  break; // no data on last line
71  }
72  std::string tag = str::stripFirstWord( value, true );
73 
74  if ( tag.size() ) {
75  *result = MediaProductEntry( tag, value );
76  }
77  }
78  }
79 
88  template <class TOutputIterator>
89  void productsInMedia( const Url & url_r, TOutputIterator result )
90  {
91  media::MediaManager media_mgr;
92  // open the media
93  media::MediaId id = media_mgr.open(url_r);
94  media_mgr.attach(id);
95  Pathname products_file = Pathname("media.1/products");
96 
97  try {
98  media_mgr.provideFile (id, products_file);
99  products_file = media_mgr.localPath (id, products_file);
100  scanProductsFile (products_file, result);
101  }
102  catch ( const Exception & excpt ) {
103  ZYPP_CAUGHT(excpt);
104  MIL << "No products description found on the Url" << std::endl;
105  }
106  media_mgr.release(id, "");
107  }
108 
117  void productsInMedia( const Url & url_r, MediaProductSet &set )
118  {
119  productsInMedia(url_r, std::inserter(set, set.end()));
120  }
121 
122 } // ns zypp
123 
124 #endif
125 
126 // vim: set ts=2 sts=2 sw=2 et ai:
#define MIL
Definition: Logger.h:64
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
MediaAccessId MediaId
Definition: MediaManager.h:39
bool operator<(const MediaProductEntry &rhs) const
Definition: MediaProducts.h:43
Represents an available product in media.
Definition: MediaProducts.h:30
#define ERR
Definition: Logger.h:66
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for &#39;localRoot() + pathname&#39;, but returns an empty pathname if media is not attached...
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
std::string stripFirstWord(std::string &line, const bool ltrim_first)
Definition: String.cc:261
std::set< MediaProductEntry > MediaProductSet
A set of available products in media.
Definition: MediaProducts.h:53
const std::string & asString() const
String representation.
Definition: Pathname.h:90
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition: String.cc:391
static void scanProductsFile(const Pathname &file_r, TOutputIterator result)
FIXME: add a comment here...
Definition: MediaProducts.h:59
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:396
Manages access to the &#39;physical&#39; media, e.g CDROM drives, Disk volumes, directory trees...
Definition: MediaManager.h:473
Base class for Exception.
Definition: Exception.h:145
MediaProductEntry(const Pathname &dir_r="/", const std::string &name_r=std::string())
Ctor.
Definition: MediaProducts.h:38
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
Url manipulation class.
Definition: Url.h:87
void productsInMedia(const Url &url_r, TOutputIterator result)
Available products in a url location.
Definition: MediaProducts.h:89
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 ...