move lot of stuff to ZYppCommon
[platform/upstream/libzypp.git] / zypp / RepoManager.cc
index 2414aca..a9f4dc1 100644 (file)
@@ -31,6 +31,8 @@
 #include "zypp/repo/cached/RepoImpl.h"
 #include "zypp/media/MediaManager.h"
 #include "zypp/MediaSetAccess.h"
+#include "zypp/ExternalProgram.h"
+#include "zypp/ManagedFile.h"
 
 #include "zypp/parser/RepoFileReader.h"
 #include "zypp/repo/yum/Downloader.h"
@@ -41,7 +43,9 @@
 
 #include "zypp/ZYppCallbacks.h"
 
+#include "sat/Pool.h"
 #include "satsolver/pool.h"
+#include "satsolver/repo.h"
 #include "satsolver/repo_solv.h"
 
 using namespace std;
@@ -115,24 +119,23 @@ namespace zypp
                                   const RepoInfo &info,
                                   const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() )
   {
-    ProgressData progress;
-    callback::SendReport<ProgressReport> report;
-    progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
-    progress.name(str::form(_("Cleaning repository '%s' cache"), info.name().c_str()));
-
-    if ( !store.isCached(info.alias()) )
-      return;
-   
-    MIL << info.alias() << " cleaning cache..." << endl;
-    data::RecordId id = store.lookupRepository(info.alias());
-    
-    CombinedProgressData subprogrcv(progress);
-    
-    store.cleanRepository(id, subprogrcv);
+//     ProgressData progress;
+//     callback::SendReport<ProgressReport> report;
+//     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
+//     progress.name(str::form(_("Cleaning repository '%s' cache"), info.name().c_str()));
+//
+//     if ( !store.isCached(info.alias()) )
+//       return;
+//
+//     MIL << info.alias() << " cleaning cache..." << endl;
+//
+//     CombinedProgressData subprogrcv(progress);
+//
+//     store.cleanRepository(info.alias(), subprogrcv);
   }
-  
+
   ////////////////////////////////////////////////////////////////////////////
-  
+
   /**
    * Reads RepoInfo's from a repo file.
    *
@@ -318,7 +321,7 @@ namespace zypp
   }
 
   ////////////////////////////////////////////////////////////////////////////
-      
+
   RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
   {
     Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
@@ -527,7 +530,7 @@ namespace zypp
       ERR << "refresh check failed for " << url << endl;
       ZYPP_RETHROW(e);
     }
-    
+
     return true; // default
   }
 
@@ -634,7 +637,7 @@ namespace zypp
       {
         ZYPP_CAUGHT(e);
         ERR << "Trying another url..." << endl;
-        
+
         // remember the exception caught for the *first URL*
         // if all other URLs fail, the rexception will be thrown with the
         // cause of the problem of the first URL remembered
@@ -665,7 +668,10 @@ namespace zypp
     assert_alias(info);
     Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
 
-    cache::SolvStore store(_pimpl->options.repoCachePath);
+    Pathname base = _pimpl->options.repoCachePath + info.alias();
+    Pathname solvfile = base.extend(".solv");
+
+    //cache::SolvStore store(_pimpl->options.repoCachePath);
 
     RepoStatus raw_metadata_status = metadataStatus(info);
     if ( raw_metadata_status.empty() )
@@ -674,11 +680,11 @@ namespace zypp
     }
 
     bool needs_cleaning = false;
-    if ( store.isCached( info.alias() ) )
+    if ( isCached( info ) )
     {
       MIL << info.alias() << " is already cached." << endl;
-      data::RecordId id = store.lookupRepository(info.alias());
-      RepoStatus cache_status = store.repositoryStatus(info.alias());
+      //data::RecordId id = store.lookupRepository(info.alias());
+      RepoStatus cache_status = cacheStatus(info);
 
       if ( cache_status.checksum() == raw_metadata_status.checksum() )
       {
@@ -690,7 +696,7 @@ namespace zypp
           MIL << info.alias() << " cache rebuild is forced" << endl;
         }
       }
-      
+
       needs_cleaning = true;
     }
 
@@ -702,17 +708,18 @@ namespace zypp
 
     if (needs_cleaning)
     {
-      Pathname name = _pimpl->options.repoCachePath;
-      data::RecordId id = store.lookupRepository(info.alias());
-      ostringstream os;
-      os << id.get();
-      name += os.str() + ".solv";
-      unlink (name);
-      cleanCacheInternal( store, info);
+//       Pathname name = _pimpl->options.repoCachePath;
+//       //data::RecordId id = store.lookupRepository(info.alias());
+//       ostringstream os;
+//       os << id.get();
+//       name += os.str() + ".solv";
+//       unlink (name);
+//       cleanCacheInternal( store, info);
+      cleanCache(info);
     }
 
     MIL << info.alias() << " building cache..." << endl;
-    data::RecordId id = store.lookupOrAppendRepository(info.alias());
+    //data::RecordId id = store.lookupOrAppendRepository(info.alias());
     // do we have type?
     repo::RepoType repokind = info.type();
 
@@ -727,27 +734,35 @@ namespace zypp
       break;
     }
 
-    
+    MIL << "repo type is " << repokind << endl;
+
     switch ( repokind.toEnum() )
     {
       case RepoType::RPMMD_e :
       case RepoType::YAST2_e :
       {
-        Pathname name = _pimpl->options.repoCachePath;
-       ostringstream os;
-       os << id.get();
-       name += os.str() + ".solv";
-        string cmd = "repo2solv.sh \"";
-       cmd += rawpath.asString() + "\" > " + name.asString();
-       int ret = system (cmd.c_str());
-        if (WIFEXITED (ret) && WEXITSTATUS (ret) != 0)
-         ZYPP_THROW(RepoUnknownTypeException());
+        MIL << "Executing solv converter" << endl;
+        // Take care we unlink the solvfile on exception
+        ManagedFile guard( solvfile, filesystem::unlink );
+
+        string cmd( str::form( "repo2solv.sh \"%s\" > '%s'", rawpath.c_str(), solvfile.c_str() ) );
+        ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
+        for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
+          MIL << "  " << output;
+        }
+        int ret = prog.close();
+        if ( ret != 0 )
+          ZYPP_THROW(RepoUnknownTypeException());
+
+        // We keep it.
+        guard.resetDispose();
       }
       break;
       default:
+        ZYPP_THROW(Exception("Unhandled repostory type"));
       break;
     }
-    
+#if 0
     switch ( repokind.toEnum() )
     {
       case RepoType::RPMMD_e :
@@ -768,6 +783,7 @@ namespace zypp
         // no error
       }
       break;
+#endif
 #if 0
       case RepoType::RPMPLAINDIR_e :
       {
@@ -780,16 +796,16 @@ namespace zypp
         parser.parse(url.getPathName());
       }
       break;
-#endif
+
       default:
         ZYPP_THROW(RepoUnknownTypeException());
     }
-
+#endif
     // update timestamp and checksum
-    store.updateRepositoryStatus(id, raw_metadata_status);
-
+    //store.updateRepositoryStatus(id, raw_metadata_status);
+    setCacheStatus(info.alias(), raw_metadata_status);
     MIL << "Commit cache.." << endl;
-    store.commit();
+    //store.commit();
     //progress.toMax();
   }
 
@@ -811,7 +827,7 @@ namespace zypp
         return repo::RepoType::RPMMD;
       if ( access.doesFileExist("/content") )
         return repo::RepoType::YAST2;
-  
+
       // if it is a local url of type dir
       if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
       {
@@ -840,9 +856,9 @@ namespace zypp
 
     return repo::RepoType::NONE;
   }
-    
+
   ////////////////////////////////////////////////////////////////////////////
-  
+
   void RepoManager::cleanCache( const RepoInfo &info,
                                 const ProgressData::ReceiverFnc & progressrcv )
   {
@@ -861,53 +877,37 @@ namespace zypp
 
   RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
   {
-    Pathname name = _pimpl->options.repoCachePath;
-    return RepoStatus(name + Pathname(info.alias()).extend(".solv"));
-  }
 
-  map<data::RecordId, Repo *> repo2solv;
+    Pathname base = _pimpl->options.repoCachePath + info.alias();
+    Pathname cookiefile = base.extend(".cookie");
 
-  Repository RepoManager::createFromCache( const RepoInfo &info,
-                                           const ProgressData::ReceiverFnc & progressrcv )
+    return RepoStatus::fromCookieFile(cookiefile);
+  }
+
+  void RepoManager::setCacheStatus( const string &alias, const RepoStatus &status )
   {
-    callback::SendReport<ProgressReport> report;
-    ProgressData progress;
-    progress.sendTo(ProgressReportAdaptor( progressrcv, report ));
-    //progress.sendTo( progressrcv );
-    progress.name(str::form(_("Reading repository '%s' cache"), info.name().c_str()));
-    
-    //_pimpl->options.repoCachePath
-    if ( ! isCached( info ) )
-      ZYPP_THROW(RepoNotCachedException());
+    Pathname base = _pimpl->options.repoCachePath + alias;
+    Pathname cookiefile = base.extend(".cookie");
 
-    MIL << "Repository " << info.alias() << " is cached" << endl;
+    status.saveToCookieFile(cookiefile);
+  }
 
-    //sat::Pool satpool( sat::Pool::instance() );
+  map<data::RecordId, Repo *> repo2solv;
 
-    //Pathname name = _pimpl->options.repoCachePath + Pathname(info.alias()).extend(".solv");
-      
-    try
-    {
-      //satpool.addRepoSolv(name);
-    }
-    catch ( const Exception &e )
-    {
-        
-    }
+  void RepoManager::loadFromCache( const std::string &alias,
+                                   const ProgressData::ReceiverFnc & progressrcv )
+  {
+    sat::Pool satpool( sat::Pool::instance() );
+
+    Pathname solvfile = (_pimpl->options.repoCachePath + alias).extend(".solv");
     
-    CombinedProgressData subprogrcv(progress);
+    if ( ! PathInfo(solvfile).isExist() )
+      ZYPP_THROW(RepoNotCachedException());
     
-//     repo::cached::RepoOptions opts( info, _pimpl->options.repoCachePath, id );
-//     opts.readingResolvablesProgress = subprogrcv;
-//     //opts.repo = repo;
-//     repo::cached::RepoImpl::Ptr repoimpl =
-//         new repo::cached::RepoImpl( opts );
-// 
-//     repoimpl->resolvables();
-    // read the resolvables from cache
-    return Repository::noRepository;
+    sat::Repo repo = satpool.addRepoSolv(solvfile, alias );
   }
-
+      
+  
   ////////////////////////////////////////////////////////////////////////////
 
   /**
@@ -1084,7 +1084,7 @@ namespace zypp
     callback::SendReport<ProgressReport> report;
     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
     progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
-    
+
     MIL << "Going to delete repo " << info.alias() << endl;
 
     std::list<RepoInfo> repos = knownRepositories();
@@ -1147,7 +1147,8 @@ namespace zypp
         CombinedProgressData subprogrcv(progress, 70);
         CombinedProgressData cleansubprogrcv(progress, 30);
         // now delete it from cache
-        cleanCache( todelete, subprogrcv);
+        if ( isCached(todelete) )
+          cleanCache( todelete, subprogrcv);
         // now delete metadata (#301037)
         cleanMetadata( todelete, cleansubprogrcv);
         MIL << todelete.alias() << " sucessfully deleted." << endl;