SourceCache.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include <fstream>
00014 #include "zypp/base/Logger.h"
00015 #include "zypp/base/Exception.h"
00016 #include "zypp/base/String.h"
00017 
00018 #include "zypp/SourceCache.h"
00019 #include "zypp/source/Builtin.h"
00020 #include "zypp/media/MediaAccess.h"
00021 #include "zypp/SourceFactory.h"
00022 #include "zypp/SourceManager.h"
00023 #include "zypp/Pathname.h"
00024 
00025 using std::endl;
00026 using namespace zypp::source;
00027 
00029 namespace zypp
00030 { 
00031 
00032 
00034   //
00035   //    CLASS NAME : SourceCache
00036   //
00038   Pathname SourceCache::_cache_dir = "/var/adm/ZYPP/SourceCache";
00039   unsigned SourceCache::_next_cache_id = 0;
00040 
00042   //
00043   //    METHOD NAME : SourceCache::SourceCache
00044   //    METHOD TYPE : Ctor
00045   //
00046   SourceCache::SourceCache()
00047   {}
00048 
00050   //
00051   //    METHOD NAME : SourceCache::~SourceCache
00052   //    METHOD TYPE : Dtor
00053   //
00054   SourceCache::~SourceCache()
00055   {}
00056 
00057   void SourceCache::setCacheDir( const Pathname & dir_r )
00058   {
00059     _cache_dir = dir_r;
00060   }
00061 
00062   void SourceCache::storeSource(Source_Ref src)
00063   {
00064     if (0 != assert_dir(_cache_dir, 0700))
00065       ZYPP_THROW(Exception("Cannot create cache directory"));
00066     Pathname cache_dir = _cache_dir + str::hexstring(_next_cache_id++);
00067     if (0 != assert_dir(cache_dir, 0700))
00068       ZYPP_THROW(Exception("Cannot create cache directory"));
00069     src.storeMetadata(cache_dir);
00070     Url url = src.url();
00071     Pathname path = src.path();
00072     std::string alias = src.alias();
00073     std::ofstream data((cache_dir + "source_info").asString().c_str());
00074     data << url.asCompleteString() << endl;
00075     data << path.asString() << endl;
00076     data << alias << endl;
00077   }
00078 
00079   void SourceCache::restoreSources()
00080   {
00081     std::list<std::string> contents;
00082     if (0 != readdir( contents, _cache_dir, false))
00083       ZYPP_THROW(Exception("Cannot read contents of the cache directory"));
00084     for (std::list<std::string>::const_iterator it = contents.begin();
00085       it != contents.end(); it++)
00086     {
00087       Pathname cache_dir = _cache_dir + *it;
00088       std::ifstream data((cache_dir + "source_info").asString().c_str());
00089       std::string url;
00090       std::string path;
00091       std::string alias;
00092       getline(data, url);
00093       getline(data, path);
00094       getline(data, alias);
00095 
00096       Source_Ref newsrc( SourceFactory().createFrom(url, path, alias, cache_dir, false) );
00097       SourceManager::sourceManager()->addSource(newsrc);
00098     }
00099   }
00100 
00101   void SourceCache::removeSource(unsigned id)
00102   {
00103     Pathname cache_dir = _cache_dir + str::hexstring(_next_cache_id++);
00104     if (0 != recursive_rmdir(cache_dir))
00105       ZYPP_THROW(Exception("Cannot delete directory with cached metadata"));
00106   }
00107 
00108   void SourceCache::removeSource(const Url & url_r, const Pathname & path_r)
00109   {
00110     std::list<std::string> contents;
00111     if (0 != readdir( contents, _cache_dir, false))
00112       ZYPP_THROW(Exception("Cannot read contents of the cache directory"));
00113     for (std::list<std::string>::const_iterator it = contents.begin();
00114       it != contents.end(); it++)
00115     {
00116       Pathname cache_dir = _cache_dir + *it;
00117       std::ifstream data((cache_dir + "source_info").asString().c_str());
00118       std::string url;
00119       std::string path;
00120       getline(data, url);
00121       getline(data, path);
00122       if (url == url_r.asCompleteString() && path == path_r)
00123       {
00124         if (0 != recursive_rmdir(cache_dir))
00125           ZYPP_THROW(Exception("Cannot delete directory with cached metadata"));
00126         return;
00127       }
00128     }
00129     ZYPP_THROW(Exception("Specified source not stored in the cache"));
00130   }
00131 
00132   /******************************************************************
00133   **
00134   **    FUNCTION NAME : operator<<
00135   **    FUNCTION TYPE : std::ostream &
00136   */
00137   std::ostream & operator<<( std::ostream & str, const SourceCache & obj )
00138   {
00139     return str << "SourceCache";
00140   }
00141 
00142 
00144 } // namespace zypp

Generated on Thu Jul 6 00:07:25 2006 for zypp by  doxygen 1.4.6