libzypp  17.36.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>
21 #include <utility>
22 #include <zypp-core/base/UserRequestException>
23 
24 #include <zypp-core/ui/ProgressData>
25 
26 namespace zypp
27 {
32  {
34  std::string _name;
35 
39  MediaProductEntry( Pathname dir_r = "/", std::string name_r = std::string() )
40  : _dir(std::move(dir_r)), _name(std::move(name_r))
41  {
42  }
43 
44  bool operator<( const MediaProductEntry &rhs ) const
45  {
46  int res = _name.compare( rhs._name );
47  return ( res < 0 || ( res == 0 && _dir < rhs._dir ) );
48  }
49  };
50 
54  using MediaProductSet = std::set<MediaProductEntry>;
55 
59  template <class TOutputIterator>
60  static void scanProductsFile( const Pathname & file_r, TOutputIterator result )
61  {
62  std::ifstream pfile( file_r.asString().c_str() );
63  while ( pfile.good() ) {
64 
65  std::string value = str::getline( pfile, str::TRIM );
66  if ( pfile.bad() ) {
67  ERR << "Error parsing " << file_r << std::endl;
68  ZYPP_THROW(Exception("Error parsing " + file_r.asString()));
69  }
70  if ( pfile.fail() ) {
71  break; // no data on last line
72  }
73  std::string tag = str::stripFirstWord( value, true );
74 
75  if ( tag.size() ) {
76  *result = MediaProductEntry( tag, value );
77  }
78  }
79  }
80 
89  template <class TOutputIterator>
90  void productsInMedia( const Url & url_r, TOutputIterator result )
91  {
92  media::MediaManager media_mgr;
93  // open the media
94  media::MediaAccessId id = media_mgr.open(url_r);
95  media_mgr.attach(id);
96  Pathname products_file = Pathname("media.1/products");
97 
98  try {
99  media_mgr.provideFile (id, OnMediaLocation(products_file) );
100  products_file = media_mgr.localPath (id, products_file);
101  scanProductsFile (products_file, result);
102  }
103  catch ( const Exception & excpt ) {
104  ZYPP_CAUGHT(excpt);
105  MIL << "No products description found on the Url" << std::endl;
106  }
107  media_mgr.release(id, "");
108  }
109 
118  void productsInMedia( const Url & url_r, MediaProductSet &set )
119  {
120  productsInMedia(url_r, std::inserter(set, set.end()));
121  }
122 
123 } // ns zypp
124 
125 #endif
126 
127 // vim: set ts=2 sts=2 sw=2 et ai:
#define MIL
Definition: Logger.h:100
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition: String.cc:479
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:424
Describes a resource file located on a medium.
bool operator<(const MediaProductEntry &rhs) const
Definition: MediaProducts.h:44
Definition: Arch.h:363
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
Represents an available product in media.
Definition: MediaProducts.h:31
#define ERR
Definition: Logger.h:102
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.
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:30
std::string stripFirstWord(std::string &line, const bool ltrim_first)
Definition: String.cc:264
const std::string & asString() const
String representation.
Definition: Pathname.h:93
static void scanProductsFile(const Pathname &file_r, TOutputIterator result)
FIXME: add a comment here...
Definition: MediaProducts.h:60
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
std::set< MediaProductEntry > MediaProductSet
A set of available products in media.
Definition: MediaProducts.h:54
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:440
Manages access to the &#39;physical&#39; media, e.g CDROM drives, Disk volumes, directory trees...
Definition: MediaManager.h:453
Base class for Exception.
Definition: Exception.h:146
MediaProductEntry(Pathname dir_r="/", std::string name_r=std::string())
Ctor.
Definition: MediaProducts.h:39
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
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:92
void productsInMedia(const Url &url_r, TOutputIterator result)
Available products in a url location.
Definition: MediaProducts.h:90