1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/SourceCache.cc
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/Exception.h"
16 #include "zypp/base/String.h"
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"
26 using namespace zypp::source;
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////
35 // CLASS NAME : SourceCache
37 ///////////////////////////////////////////////////////////////////
38 Pathname SourceCache::_cache_dir = "/var/adm/ZYPP/SourceCache";
39 unsigned SourceCache::_next_cache_id = 0;
41 ///////////////////////////////////////////////////////////////////
43 // METHOD NAME : SourceCache::SourceCache
46 SourceCache::SourceCache()
49 ///////////////////////////////////////////////////////////////////
51 // METHOD NAME : SourceCache::~SourceCache
54 SourceCache::~SourceCache()
57 void SourceCache::setCacheDir( const Pathname & dir_r )
62 void SourceCache::storeSource(Source_Ref src)
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);
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;
79 void SourceCache::restoreSources()
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++)
87 Pathname cache_dir = _cache_dir + *it;
88 std::ifstream data((cache_dir + "source_info").asString().c_str());
96 Source_Ref newsrc( SourceFactory().createFrom(url, path, alias, cache_dir) );
97 SourceManager::sourceManager()->addSource(newsrc);
101 void SourceCache::removeSource(unsigned id)
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"));
108 void SourceCache::removeSource(const Url & url_r, const Pathname & path_r)
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++)
116 Pathname cache_dir = _cache_dir + *it;
117 std::ifstream data((cache_dir + "source_info").asString().c_str());
122 if (url == url_r.asCompleteString() && path == path_r)
124 if (0 != recursive_rmdir(cache_dir))
125 ZYPP_THROW(Exception("Cannot delete directory with cached metadata"));
129 ZYPP_THROW(Exception("Specified source not stored in the cache"));
132 /******************************************************************
134 ** FUNCTION NAME : operator<<
135 ** FUNCTION TYPE : std::ostream &
137 std::ostream & operator<<( std::ostream & str, const SourceCache & obj )
139 return str << "SourceCache";
143 /////////////////////////////////////////////////////////////////
145 ///////////////////////////////////////////////////////////////////