ignore
[platform/upstream/libzypp.git] / zypp / SourceCache.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/SourceCache.cc
10  *
11 */
12 #include <iostream>
13 #include <fstream>
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/Exception.h"
16 #include "zypp/base/String.h"
17
18 #include "zypp/SourceCache.h"
19 #include "zypp/source/Builtin.h"
20 #include "zypp/media/MediaAccess.h"
21 #include "zypp/SourceFactory.h"
22 #include "zypp/SourceManager.h"
23 #include "zypp/Pathname.h"
24
25 using std::endl;
26 using namespace zypp::source;
27
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 { /////////////////////////////////////////////////////////////////
31
32
33   ///////////////////////////////////////////////////////////////////
34   //
35   //    CLASS NAME : SourceCache
36   //
37   ///////////////////////////////////////////////////////////////////
38   Pathname SourceCache::_cache_dir = "/var/adm/ZYPP/SourceCache";
39   unsigned SourceCache::_next_cache_id = 0;
40
41   ///////////////////////////////////////////////////////////////////
42   //
43   //    METHOD NAME : SourceCache::SourceCache
44   //    METHOD TYPE : Ctor
45   //
46   SourceCache::SourceCache()
47   {}
48
49   ///////////////////////////////////////////////////////////////////
50   //
51   //    METHOD NAME : SourceCache::~SourceCache
52   //    METHOD TYPE : Dtor
53   //
54   SourceCache::~SourceCache()
55   {}
56
57   void SourceCache::setCacheDir( const Pathname & dir_r )
58   {
59     _cache_dir = dir_r;
60   }
61
62   void SourceCache::storeSource(Source_Ref src)
63   {
64     if (0 != assert_dir(_cache_dir, 0700))
65       ZYPP_THROW(Exception("Cannot create cache directory"));
66     Pathname cache_dir = _cache_dir + str::hexstring(_next_cache_id++);
67     if (0 != assert_dir(cache_dir, 0700))
68       ZYPP_THROW(Exception("Cannot create cache directory"));
69     src.storeMetadata(cache_dir);
70     Url url = src.url();
71     Pathname path = src.path();
72     std::string alias = src.alias();
73     std::ofstream data((cache_dir + "source_info").asString().c_str());
74     data << url.asCompleteString() << endl;
75     data << path.asString() << endl;
76     data << alias << endl;
77   }
78
79   void SourceCache::restoreSources()
80   {
81     std::list<std::string> contents;
82     if (0 != readdir( contents, _cache_dir, false))
83       ZYPP_THROW(Exception("Cannot read contents of the cache directory"));
84     for (std::list<std::string>::const_iterator it = contents.begin();
85       it != contents.end(); it++)
86     {
87       Pathname cache_dir = _cache_dir + *it;
88       std::ifstream data((cache_dir + "source_info").asString().c_str());
89       std::string url;
90       std::string path;
91       std::string alias;
92       getline(data, url);
93       getline(data, path);
94       getline(data, alias);
95
96       Source_Ref newsrc( SourceFactory().createFrom(url, path, alias, cache_dir) );
97       SourceManager::sourceManager()->addSource(newsrc);
98     }
99   }
100
101   void SourceCache::removeSource(unsigned id)
102   {
103     Pathname cache_dir = _cache_dir + str::hexstring(_next_cache_id++);
104     if (0 != recursive_rmdir(cache_dir))
105       ZYPP_THROW(Exception("Cannot delete directory with cached metadata"));
106   }
107
108   void SourceCache::removeSource(const Url & url_r, const Pathname & path_r)
109   {
110     std::list<std::string> contents;
111     if (0 != readdir( contents, _cache_dir, false))
112       ZYPP_THROW(Exception("Cannot read contents of the cache directory"));
113     for (std::list<std::string>::const_iterator it = contents.begin();
114       it != contents.end(); it++)
115     {
116       Pathname cache_dir = _cache_dir + *it;
117       std::ifstream data((cache_dir + "source_info").asString().c_str());
118       std::string url;
119       std::string path;
120       getline(data, url);
121       getline(data, path);
122       if (url == url_r.asCompleteString() && path == path_r)
123       {
124         if (0 != recursive_rmdir(cache_dir))
125           ZYPP_THROW(Exception("Cannot delete directory with cached metadata"));
126         return;
127       }
128     }
129     ZYPP_THROW(Exception("Specified source not stored in the cache"));
130   }
131
132   /******************************************************************
133   **
134   **    FUNCTION NAME : operator<<
135   **    FUNCTION TYPE : std::ostream &
136   */
137   std::ostream & operator<<( std::ostream & str, const SourceCache & obj )
138   {
139     return str << "SourceCache";
140   }
141
142
143   /////////////////////////////////////////////////////////////////
144 } // namespace zypp
145 ///////////////////////////////////////////////////////////////////