3d920e3813c0ee2ce023c21e11d20c13e101ca61
[platform/upstream/libzypp.git] / zypp / repo / SUSEMediaVerifier.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9
10 #include <fstream>
11 #include "zypp/base/Logger.h"
12 #include "zypp/repo/SUSEMediaVerifier.h"
13
14 using namespace std;
15
16 namespace zypp
17 {
18 namespace repo
19 {
20
21 SUSEMediaVerifier::SUSEMediaVerifier(const std::string & vendor_r,
22                                      const std::string & id_r,
23                                      const media::MediaNr media_nr)
24    : _media_vendor(vendor_r)
25     , _media_id(id_r)
26     , _media_nr(media_nr)
27 {}
28
29 SUSEMediaVerifier::SUSEMediaVerifier( int media_nr, const Pathname &path_r )
30   : _media_nr(media_nr)
31 {
32   std::ifstream str(path_r.asString().c_str());
33   std::string vendor;
34   std::string id;
35
36   if ( str )
37   {
38     getline(str, _media_vendor);
39     getline(str, _media_id);
40   }
41   else
42   {
43     ZYPP_THROW(Exception("Can't setup media verifier using file: '"
44         + path_r.asString() + "'"));
45   }
46 }
47
48 bool SUSEMediaVerifier::isDesiredMedia(const media::MediaAccessRef &ref)
49 {
50   if (_media_vendor.empty() || _media_id.empty())
51     return true;
52
53   Pathname media_file = "/media." + str::numstring(_media_nr) + "/media";
54   ref->provideFile (media_file, 0);
55   media_file = ref->localPath(media_file);
56   std::ifstream str(media_file.asString().c_str());
57   std::string vendor;
58   std::string id;
59   getline(str, vendor);
60   getline(str, id);
61
62   bool ret = ( vendor == _media_vendor && id == _media_id  );
63   if ( !ret ) {
64     DBG << "cached vendor: " << _media_vendor << endl;
65     DBG << "repo vendor: " << vendor << endl;
66     DBG << "cached id: " << _media_id << endl;
67     DBG << "repo id: " << id << endl;
68   }
69   return ret;
70 }
71
72 }
73 }
74