libzypp  13.10.6
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  {
32  Pathname _dir;
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  return ( _name < rhs._name );
46  }
47  };
48 
52  typedef std::set<MediaProductEntry> MediaProductSet;
53 
57  template <class _OutputIterator>
58  static void scanProductsFile( const Pathname & file_r, _OutputIterator result )
59  {
60  std::ifstream pfile( file_r.asString().c_str() );
61  while ( pfile.good() ) {
62 
63  std::string value = str::getline( pfile, str::TRIM );
64  if ( pfile.bad() ) {
65  ERR << "Error parsing " << file_r << std::endl;
66  ZYPP_THROW(Exception("Error parsing " + file_r.asString()));
67  }
68  if ( pfile.fail() ) {
69  break; // no data on last line
70  }
71  std::string tag = str::stripFirstWord( value, true );
72 
73  if ( tag.size() ) {
74  *result = MediaProductEntry( tag, value );
75  }
76  }
77  }
78 
87  template <class _OutputIterator>
88  void productsInMedia( const Url & url_r, _OutputIterator result )
89  {
90  media::MediaManager media_mgr;
91  // open the media
92  media::MediaId id = media_mgr.open(url_r);
93  media_mgr.attach(id);
94  Pathname products_file = Pathname("media.1/products");
95 
96  try {
97  media_mgr.provideFile (id, products_file);
98  products_file = media_mgr.localPath (id, products_file);
99  scanProductsFile (products_file, result);
100  }
101  catch ( const Exception & excpt ) {
102  ZYPP_CAUGHT(excpt);
103  MIL << "No products description found on the Url" << std::endl;
104  }
105  media_mgr.release(id, "");
106  }
107 
116  void productsInMedia( const Url & url_r, MediaProductSet &set )
117  {
118  productsInMedia(url_r, std::inserter(set, set.end()));
119  }
120 
121 } // ns zypp
122 
123 #endif
124 
125 // vim: set ts=2 sts=2 sw=2 et ai:
void productsInMedia(const Url &url_r, _OutputIterator result)
Available products in a url location.
Definition: MediaProducts.h:88
#define MIL
Definition: Logger.h:47
static void scanProductsFile(const Pathname &file_r, _OutputIterator result)
FIXME: add a comment here...
Definition: MediaProducts.h:58
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
MediaAccessId MediaId
Definition: MediaManager.h:39
Represents an available product in media.
Definition: MediaProducts.h:30
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
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:237
std::set< MediaProductEntry > MediaProductSet
A set of available products in media.
Definition: MediaProducts.h:52
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for &#39;localRoot() + pathname&#39;, but returns an empty pathname if media is not attached...
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition: String.cc:361
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:324
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:143
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
bool operator<(const MediaProductEntry &rhs) const
Definition: MediaProducts.h:43
Url manipulation class.
Definition: Url.h:87