move lot of stuff to ZYppCommon
[platform/upstream/libzypp.git] / zypp / RepoManager.cc
index e3031f3..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,6 +43,7 @@
 
 #include "zypp/ZYppCallbacks.h"
 
+#include "sat/Pool.h"
 #include "satsolver/pool.h"
 #include "satsolver/repo.h"
 #include "satsolver/repo_solv.h"
@@ -116,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.
    *
@@ -319,7 +321,7 @@ namespace zypp
   }
 
   ////////////////////////////////////////////////////////////////////////////
-      
+
   RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
   {
     Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
@@ -528,7 +530,7 @@ namespace zypp
       ERR << "refresh check failed for " << url << endl;
       ZYPP_RETHROW(e);
     }
-    
+
     return true; // default
   }
 
@@ -635,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
@@ -666,7 +668,7 @@ namespace zypp
     assert_alias(info);
     Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
 
-    Pathname base = _pimpl->options.repoCachePath + Pathname(info.alias());
+    Pathname base = _pimpl->options.repoCachePath + info.alias();
     Pathname solvfile = base.extend(".solv");
 
     //cache::SolvStore store(_pimpl->options.repoCachePath);
@@ -694,7 +696,7 @@ namespace zypp
           MIL << info.alias() << " cache rebuild is forced" << endl;
         }
       }
-      
+
       needs_cleaning = true;
     }
 
@@ -732,20 +734,32 @@ namespace zypp
       break;
     }
 
-    
+    MIL << "repo type is " << repokind << endl;
+
     switch ( repokind.toEnum() )
     {
       case RepoType::RPMMD_e :
       case RepoType::YAST2_e :
       {
-        string cmd = "repo2solv.sh \"";
-       cmd += rawpath.asString() + "\" > " + solvfile.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
@@ -813,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" ) )
       {
@@ -842,9 +856,9 @@ namespace zypp
 
     return repo::RepoType::NONE;
   }
-    
+
   ////////////////////////////////////////////////////////////////////////////
-  
+
   void RepoManager::cleanCache( const RepoInfo &info,
                                 const ProgressData::ReceiverFnc & progressrcv )
   {
@@ -863,29 +877,11 @@ namespace zypp
 
   RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
   {
-    Pathname name = _pimpl->options.repoCachePath;
-    RepoStatus status;
-    Pathname base = _pimpl->options.repoCachePath + Pathname(info.alias());
-    Pathname solvfile = base.extend(".solv");
-    Pathname cookiefile = base.extend(".cookie");
-
-    std::ifstream file(cookiefile.c_str());
-    if (!file) {
-      ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) );
-    }
 
-    std::string buffer;
-    while(file && !file.eof()) {
-      getline(file, buffer);
-    }
-
-    std::vector<std::string> words;
-    if ( str::split( buffer, std::back_inserter(words) ) != 2 )
-      ZYPP_THROW (Exception( "corrupt file " + cookiefile.asString() ) );
+    Pathname base = _pimpl->options.repoCachePath + info.alias();
+    Pathname cookiefile = base.extend(".cookie");
 
-    status.setTimestamp(Date(str::strtonum<time_t>(words[1])));
-    status.setChecksum(words[0]);
-    return status;
+    return RepoStatus::fromCookieFile(cookiefile);
   }
 
   void RepoManager::setCacheStatus( const string &alias, const RepoStatus &status )
@@ -893,57 +889,25 @@ namespace zypp
     Pathname base = _pimpl->options.repoCachePath + alias;
     Pathname cookiefile = base.extend(".cookie");
 
-    std::ofstream file(cookiefile.c_str());
-    if (!file) {
-      ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) );
-    }
-    file << status;
-    file.close();
+    status.saveToCookieFile(cookiefile);
   }
 
   map<data::RecordId, Repo *> repo2solv;
 
-  Repository RepoManager::createFromCache( const RepoInfo &info,
-                                           const ProgressData::ReceiverFnc & progressrcv )
+  void RepoManager::loadFromCache( const std::string &alias,
+                                   const ProgressData::ReceiverFnc & progressrcv )
   {
-    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());
+    sat::Pool satpool( sat::Pool::instance() );
 
-    MIL << "Repository " << info.alias() << " is cached" << endl;
-
-    //sat::Pool satpool( sat::Pool::instance() );
-
-    //Pathname name = _pimpl->options.repoCachePath + Pathname(info.alias()).extend(".solv");
-      
-    try
-    {
-      //satpool.addRepoSolv(name);
-    }
-    catch ( const Exception &e )
-    {
-        
-    }
+    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 );
   }
-
+      
+  
   ////////////////////////////////////////////////////////////////////////////
 
   /**
@@ -1120,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();
@@ -1183,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;