libzypp 17.37.17
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
26namespace 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:
Base class for Exception.
Definition Exception.h:153
Describes a resource file located on a medium.
Url manipulation class.
Definition Url.h:93
const std::string & asString() const
String representation.
Definition Pathname.h:93
Manages access to the 'physical' media, e.g CDROM drives, Disk volumes, directory trees,...
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for 'localRoot() + pathname', but returns an empty pathname if media is not attached.
Definition Arch.h:364
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:266
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition String.cc:481
Easy-to use interface to the ZYPP dependency resolver.
static void scanProductsFile(const Pathname &file_r, TOutputIterator result)
FIXME: add a comment here...
std::set< MediaProductEntry > MediaProductSet
A set of available products in media.
void productsInMedia(const Url &url_r, TOutputIterator result)
Available products in a url location.
Represents an available product in media.
MediaProductEntry(Pathname dir_r="/", std::string name_r=std::string())
Ctor.
bool operator<(const MediaProductEntry &rhs) const
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define MIL
Definition Logger.h:100
#define ERR
Definition Logger.h:102