Imported Upstream version 17.0.0
[platform/upstream/libzypp.git] / zypp / MediaProducts.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/MediaProducts.h
10  * Functions to find out products in media
11  */
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 "zypp/base/UserRequestException.h"
22
23 #include "zypp/ProgressData.h"
24
25 namespace zypp
26 {
27   /**
28    * \short Represents an available product in media
29    */
30   struct MediaProductEntry
31   {
32     Pathname    _dir;
33     std::string _name;
34
35     /**
36      * \short Ctor
37      */
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
50   /**
51    * A set of available products in media
52    */
53   typedef std::set<MediaProductEntry> MediaProductSet;
54
55   /**
56    * FIXME: add a comment here...
57    */
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
80   /**
81    * \short Available products in a url location
82    *
83    * \param url_r url to inspect
84    * \param result output iterator where \ref MediaProductEntry
85    * items will be inserted.
86    * \throws MediaException If accessng the media fails
87    */
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
109  /**
110    * \short Available products in a url location
111    *
112    * \param url_r url to inspect
113    * \param set ef MediaProductEntry set where
114    * items will be inserted.
115    * \throws MediaException If accessng the media fails
116    */
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: