libzypp 17.37.10

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>
20#include <zypp/media/MediaManager.h>
21#include <utility>
22#include <zypp-core/base/UserRequestException>
23
24#include <zypp-core/ui/ProgressData>
25
26 namespace zypp
27{
31 struct MediaProductEntry
32 {
33 Pathname _dir;
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:
MediaManager.h
ZConfig.h
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition onmedialocation.h:37
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:93
zypp::media::MediaManager
Manages access to the 'physical' media, e.g CDROM drives, Disk volumes, directory trees,...
Definition MediaManager.h:454
zypp::media::MediaManager::open
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
Definition MediaManager.cc:318
zypp::media::MediaManager::attach
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
Definition MediaManager.cc:447
zypp::media::MediaManager::provideFile
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
Definition MediaManager.cc:707
zypp::media::MediaManager::release
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
Definition MediaManager.cc:516
zypp::media::MediaManager::localPath
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for 'localRoot() + pathname', but returns an empty pathname if media is not attached.
Definition MediaManager.cc:696
std
Definition Arch.h:364
zypp::media::MediaAccessId
unsigned int MediaAccessId
Media manager access Id type.
Definition MediaSource.h:30
zypp::str::TRIM
@ TRIM
Definition String.h:571
zypp::str::stripFirstWord
std::string stripFirstWord(std::string &line, const bool ltrim_first)
Definition String.cc:266
zypp::str::getline
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
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::scanProductsFile
static void scanProductsFile(const Pathname &file_r, TOutputIterator result)
FIXME: add a comment here...
Definition MediaProducts.h:60
zypp::MediaProductSet
std::set< MediaProductEntry > MediaProductSet
A set of available products in media.
Definition MediaProducts.h:54
zypp::productsInMedia
void productsInMedia(const Url &url_r, TOutputIterator result)
Available products in a url location.
Definition MediaProducts.h:90
zypp::MediaProductEntry
Represents an available product in media.
Definition MediaProducts.h:32
zypp::MediaProductEntry::_dir
Pathname _dir
Definition MediaProducts.h:33
zypp::MediaProductEntry::_name
std::string _name
Definition MediaProducts.h:34
zypp::MediaProductEntry::MediaProductEntry
MediaProductEntry(Pathname dir_r="/", std::string name_r=std::string())
Ctor.
Definition MediaProducts.h:39
zypp::MediaProductEntry::operator<
bool operator<(const MediaProductEntry &rhs) const
Definition MediaProducts.h:44
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
MIL
#define MIL
Definition Logger.h:100
ERR
#define ERR
Definition Logger.h:102
Logger.h