Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / MediaSetAccess.cc
index 401e94b..a7c5f8d 100644 (file)
@@ -11,6 +11,8 @@
 #include <fstream>
 
 #include "zypp/base/LogTools.h"
+#include "zypp/base/Regex.h"
+#include "zypp/base/UserRequestException.h"
 #include "zypp/ZYppCallbacks.h"
 #include "zypp/MediaSetAccess.h"
 #include "zypp/PathInfo.h"
@@ -21,47 +23,32 @@ using namespace std;
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////
-
-  ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
-    : _checksum(checksum)
-  {
-  }
-
-  bool ChecksumFileChecker::operator()( const Pathname &file )
-  {
-    // FIXME probably this funcionality should be in CheckSum itself
-    CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
-    if ( real_checksum == _checksum )
-    {
-      return true;
-    }
-    else
-    {
-      ERR << "Got " << real_checksum << ", expected " << _checksum << std::endl;
-      return false;
-    }
-  }
 
-  bool NullFileChecker::operator()(const Pathname &file )
-  {
-    return true;
-  }
+IMPL_PTR_TYPE(MediaSetAccess);
 
+///////////////////////////////////////////////////////////////////
 
-  MediaSetAccess::MediaSetAccess(  const Url &url, const Pathname &path )
-      : _url(url),
-        _path(path)
-  {
-    MIL << "initializing.." << std::endl;
-    //std::vector<media::MediaVerifierRef> single_media;
-    //single_media[0] = media::MediaVerifierRef(new media::NoVerifier());
-    //_verifiers = single_media;
-  }
+  MediaSetAccess::MediaSetAccess(const Url &url,
+                                 const Pathname & prefered_attach_point)
+      : _url(url)
+      , _prefAttachPoint(prefered_attach_point)
+  {}
 
+  MediaSetAccess::MediaSetAccess(const std::string & label_r,
+                                 const Url &url,
+                                 const Pathname & prefered_attach_point)
+      : _url(url)
+      , _prefAttachPoint(prefered_attach_point)
+      , _label( label_r )
+  {}
 
   MediaSetAccess::~MediaSetAccess()
   {
+    try
+    {
+      release();
+    }
+    catch(...) {} // don't let exception escape a dtor.
   }
 
 
@@ -75,8 +62,6 @@ namespace zypp
       media_mgr.addVerifier( id, verifier );
       // remove any saved verifier for this media
       _verifiers.erase(media_nr);
-      //if (! noattach && ! media_mgr.isAttached(id))
-      //media_mgr.attach(id);
     }
     else
     {
@@ -86,91 +71,175 @@ namespace zypp
     }
   }
 
-//       callback::SendReport<source::DownloadFileReport> report;
-//       DownloadProgressFileReceiver download_report( report );
-//       SourceFactory source_factory;
-//       Url file_url( url().asString() + file_r.asString() );
-//       report->start( source_factory.createFrom(this), file_url );
-//       callback::TempConnect<media::DownloadProgressReport> tmp_download( download_report );
-//       Pathname file = provideJustFile( file_r, media_nr, cached, checkonly );
-//       report->finish( file_url, source::DownloadFileReport::NO_ERROR, "" );
-//       return file;
+  void MediaSetAccess::releaseFile( const OnMediaLocation & on_media_file )
+  {
+    releaseFile( on_media_file.filename(), on_media_file.medianr() );
+  }
 
+  void MediaSetAccess::releaseFile( const Pathname & file, unsigned media_nr)
+  {
+    media::MediaManager media_mgr;
+    media::MediaAccessId media;
 
-  Pathname MediaSetAccess::provideFile( const OnMediaLocation & on_media_file )
+    media = getMediaAccessId( media_nr);
+    DBG << "Going to release file " << file
+        << " from media number " << media_nr << endl;
+
+    if ( ! media_mgr.isAttached(media) )
+      return; //disattached media is free
+
+    media_mgr.releaseFile (media, file);
+  }
+
+  void MediaSetAccess::dirInfo( filesystem::DirContent &retlist, const Pathname &dirname,
+                                bool dots, unsigned media_nr )
   {
-    return provideFile( on_media_file.filename(), on_media_file.medianr() );
+    media::MediaManager media_mgr;
+    media::MediaAccessId media;
+    media = getMediaAccessId(media_nr);
+
+    // try to attach the media
+    if ( ! media_mgr.isAttached(media) )
+        media_mgr.attach(media);
+
+    media_mgr.dirInfo(media, retlist, dirname, dots);
   }
 
+  struct ProvideFileOperation
+  {
+    Pathname result;
+    void operator()( media::MediaAccessId media, const Pathname &file )
+    {
+      media::MediaManager media_mgr;
+      media_mgr.provideFile(media, file);
+      result = media_mgr.localPath(media, file);
+    }
+  };
 
-  Pathname MediaSetAccess::provideFile(const Pathname & file, unsigned media_nr )
+  struct ProvideDirTreeOperation
   {
-    return provideFileInternal( file, media_nr, false, false);
-  }
+    Pathname result;
+    void operator()( media::MediaAccessId media, const Pathname &file )
+    {
+      media::MediaManager media_mgr;
+      media_mgr.provideDirTree(media, file);
+      result = media_mgr.localPath(media, file);
+    }
+  };
 
+  struct ProvideDirOperation
+  {
+    Pathname result;
+    void operator()( media::MediaAccessId media, const Pathname &file )
+    {
+      media::MediaManager media_mgr;
+      media_mgr.provideDir(media, file);
+      result = media_mgr.localPath(media, file);
+    }
+  };
 
-  Pathname  MediaSetAccess::provideFile(const Pathname & file, unsigned media_nr, FileChecker checker )
+  struct ProvideFileExistenceOperation
   {
-    Pathname p = provideFileInternal( file, media_nr, false, false);
-    
-    if ( ! checker(p) )
+    bool result;
+    ProvideFileExistenceOperation()
+        : result(false)
+    {}
+
+    void operator()( media::MediaAccessId media, const Pathname &file )
     {
-      ZYPP_THROW(Exception("Error checker"));
+      media::MediaManager media_mgr;
+      result = media_mgr.doesFileExist(media, file);
     }
-    return p;
+  };
+
+
+
+  Pathname MediaSetAccess::provideFile( const OnMediaLocation & resource, ProvideFileOptions options, const Pathname &deltafile )
+  {
+    ProvideFileOperation op;
+    provide( boost::ref(op), resource, options, deltafile );
+    return op.result;
+  }
+
+  Pathname MediaSetAccess::provideFile(const Pathname & file, unsigned media_nr, ProvideFileOptions options )
+  {
+    OnMediaLocation resource;
+    ProvideFileOperation op;
+    resource.setLocation(file, media_nr);
+    provide( boost::ref(op), resource, options, Pathname() );
+    return op.result;
   }
 
+  bool MediaSetAccess::doesFileExist(const Pathname & file, unsigned media_nr )
+  {
+    ProvideFileExistenceOperation op;
+    OnMediaLocation resource;
+    resource.setLocation(file, media_nr);
+    provide( boost::ref(op), resource, PROVIDE_DEFAULT, Pathname());
+    return op.result;
+  }
 
-  Pathname MediaSetAccess::provideFileInternal(const Pathname & file, unsigned media_nr, bool cached, bool checkonly )
+  void MediaSetAccess::provide( ProvideOperation op,
+                                const OnMediaLocation &resource,
+                                ProvideFileOptions options,
+                                const Pathname &deltafile )
   {
+    Pathname file(resource.filename());
+    unsigned media_nr(resource.medianr());
+
     callback::SendReport<media::MediaChangeReport> report;
     media::MediaManager media_mgr;
-    // get the mediaId, but don't try to attach it here
-    media::MediaAccessId media = getMediaAccessId( media_nr);
+
+    media::MediaAccessId media;
 
     do
     {
+      // get the mediaId, but don't try to attach it here
+      media = getMediaAccessId( media_nr);
+      bool deltafileset = false;
+
       try
       {
-        DBG << "Going to try to provide file " << file
-            << " from media number " << media_nr << endl;        
+        DBG << "Going to try to provide " << (resource.optional() ? "optional" : "") << " file " << file
+            << " from media number " << media_nr << endl;
         // try to attach the media
         if ( ! media_mgr.isAttached(media) )
           media_mgr.attach(media);
-        media_mgr.provideFile (media, file, false, false);
+       media_mgr.setDeltafile(media, deltafile);
+       deltafileset = true;
+        op(media, file);
+       media_mgr.setDeltafile(media, Pathname());
         break;
       }
-      catch ( Exception & excp )
+      catch ( media::MediaException & excp )
       {
         ZYPP_CAUGHT(excp);
-        media::MediaChangeReport::Action user;
+       if (deltafileset)
+         media_mgr.setDeltafile(media, Pathname());
+        media::MediaChangeReport::Action user = media::MediaChangeReport::ABORT;
+        unsigned int devindex = 0;
+        vector<string> devices;
+        media_mgr.getDetectedDevices(media, devices, devindex);
+
         do
         {
-          DBG << "Media couldn't provide file " << file << " , releasing." << endl;
-          try
-          {
-            media_mgr.release (media, false);
-          }
-          catch (const Exception & excpt_r)
-          {
-              ZYPP_CAUGHT(excpt_r);
-              MIL << "Failed to release media " << media << endl;
-          }
-          
-          /*MIL << "Releasing all _medias of all sources" << endl;
-          try
+          if (user != media::MediaChangeReport::EJECT) // no use in calling this again
           {
-            //zypp::SourceManager::sourceManager()->releaseAllSources();
+            DBG << "Media couldn't provide file " << file << " , releasing." << endl;
+            try
+            {
+              media_mgr.release(media);
+            }
+            catch (const Exception & excpt_r)
+            {
+                ZYPP_CAUGHT(excpt_r);
+                MIL << "Failed to release media " << media << endl;
+            }
           }
-          catch (const zypp::Exception& excpt_r)
-          {
-              ZYPP_CAUGHT(excpt_r);
-              ERR << "Failed to release all sources" << endl;
-          }*/
 
           // set up the reason
           media::MediaChangeReport::Error reason = media::MediaChangeReport::INVALID;
-          
+
           if( typeid(excp) == typeid( media::MediaFileNotFoundException )  ||
               typeid(excp) == typeid( media::MediaNotAFileException ) )
           {
@@ -181,47 +250,73 @@ namespace zypp
           {
             reason = media::MediaChangeReport::WRONG;
           }
+          else if( typeid(excp) == typeid( media::MediaTimeoutException) ||
+                   typeid(excp) == typeid( media::MediaTemporaryProblemException))
+          {
+            reason = media::MediaChangeReport::IO_SOFT;
+          }
 
-          user  = checkonly ? media::MediaChangeReport::ABORT :
-            report->requestMedia (
-              Source_Ref::noSource,
+          // Propagate the original error if _no_ callback receiver is connected, or
+         // non_interactive mode (for optional files) is used (except for wrong media).
+          if ( ! callback::SendReport<media::MediaChangeReport>::connected()
+            || (( options & PROVIDE_NON_INTERACTIVE ) && reason != media::MediaChangeReport::WRONG ) )
+          {
+              MIL << "Can't provide file. Non-Interactive mode." << endl;
+              ZYPP_RETHROW(excp);
+          }
+          else
+          {
+            // release all media before requesting another (#336881)
+            media_mgr.releaseAll();
+
+            user = report->requestMedia (
+              _url,
               media_nr,
+              _label,
               reason,
-              excp.asUserString()
+              excp.asUserString(),
+              devices,
+              devindex
             );
+          }
 
-          DBG << "ProvideFile exception caught, callback answer: " << user << endl;
+          MIL << "ProvideFile exception caught, callback answer: " << user << endl;
 
           if( user == media::MediaChangeReport::ABORT )
           {
             DBG << "Aborting" << endl;
-            ZYPP_RETHROW ( excp );
+            AbortRequestException aexcp("Aborting requested by user");
+            aexcp.remember(excp);
+            ZYPP_THROW(aexcp);
           }
           else if ( user == media::MediaChangeReport::IGNORE )
           {
             DBG << "Skipping" << endl;
-            ZYPP_THROW ( source::SkipRequestedException("User-requested skipping of a file") );
-          }
+           SkipRequestException nexcp("User-requested skipping of a file");
+           nexcp.remember(excp);
+           ZYPP_THROW(nexcp);
+         }
           else if ( user == media::MediaChangeReport::EJECT )
           {
             DBG << "Eject: try to release" << endl;
-            try
-            {
-              //zypp::SourceManager::sourceManager()->releaseAllSources();
-            }
-            catch (const zypp::Exception& excpt_r)
-            {
-              ZYPP_CAUGHT(excpt_r);
-              ERR << "Failed to release all sources" << endl;
-            }
-            media_mgr.release (media, true); // one more release needed for eject
-            // FIXME: this will not work, probably
+           try
+           {
+             media_mgr.releaseAll();
+             media_mgr.release (media, devindex < devices.size() ? devices[devindex] : "");
+           }
+           catch ( const Exception & e)
+           {
+             ZYPP_CAUGHT(e);
+           }
           }
           else if ( user == media::MediaChangeReport::RETRY  ||
             user == media::MediaChangeReport::CHANGE_URL )
           {
             // retry
             DBG << "Going to try again" << endl;
+            // invalidate current media access id
+            media_mgr.close(media);
+            _medias.erase(media_nr);
 
             // not attaching, media set will do that for us
             // this could generate uncaught exception (#158620)
@@ -237,8 +332,24 @@ namespace zypp
 
       // retry or change URL
     } while( true );
+  }
 
-    return media_mgr.localPath( media, file );
+  Pathname MediaSetAccess::provideDir(const Pathname & dir,
+                                      bool recursive,
+                                      unsigned media_nr,
+                                      ProvideFileOptions options )
+  {
+    OnMediaLocation resource;
+    resource.setLocation(dir, media_nr);
+    if ( recursive )
+    {
+        ProvideDirTreeOperation op;
+        provide( boost::ref(op), resource, options, Pathname());
+        return op.result;
+    }
+    ProvideDirOperation op;
+    provide( boost::ref(op), resource, options, Pathname());
+    return op.result;
   }
 
   media::MediaAccessId MediaSetAccess::getMediaAccessId (media::MediaNr medianr)
@@ -248,13 +359,11 @@ namespace zypp
     if (_medias.find(medianr) != _medias.end())
     {
       media::MediaAccessId id = _medias[medianr];
-      //if (! noattach && ! media_mgr.isAttached(id))
-      //media_mgr.attach(id);
       return id;
     }
     Url url;
     url = rewriteUrl (_url, medianr);
-    media::MediaAccessId id = media_mgr.open(url, _path);
+    media::MediaAccessId id = media_mgr.open(url, _prefAttachPoint);
     _medias[medianr] = id;
 
     try
@@ -277,7 +386,7 @@ namespace zypp
       ZYPP_CAUGHT(e);
       WAR << "Verifier not found" << endl;
     }
-    
+
     return id;
   }
 
@@ -296,12 +405,13 @@ namespace zypp
       // code has to be adapted together with the MediaISO change.
       // maybe some MediaISOURL interface should be used.
       std::string isofile = url_r.getQueryParam("iso");
-      boost::regex e("^(.*(cd|dvd))([0-9]+)(\\.iso)$", boost::regex::icase);
-      boost::smatch what;
-      if(boost::regex_match(isofile, what, e, boost::match_extra))
+      str::regex e("^(.*)(cd|dvd|media)[0-9]+\\.iso$", str::regex::icase);
+
+      str::smatch what;
+      if(str::regex_match(isofile, what, e))
       {
         Url url( url_r);
-        isofile = what[1] + str::numstring(medianr) + what[4];
+        isofile = what[1] + what[2] + str::numstring(medianr) + ".iso";
         url.setQueryParam("iso", isofile);
         DBG << "Url rewrite result: " << url << endl;
         return url;
@@ -310,12 +420,12 @@ namespace zypp
     else
     {
       std::string pathname = url_r.getPathName();
-      boost::regex e("^(.*(cd|dvd))([0-9]+)(/?)$", boost::regex::icase);
-      boost::smatch what;
-      if(boost::regex_match(pathname, what, e, boost::match_extra))
+      str::regex e("^(.*)(cd|dvd|media)[0-9]+(/)?$", str::regex::icase);
+      str::smatch what;
+      if(str::regex_match(pathname, what, e))
       {
         Url url( url_r);
-        pathname = what[1] + str::numstring(medianr) + what[4];
+        pathname = what[1] + what[2] + str::numstring(medianr) + what[3];
         url.setPathName(pathname);
         DBG << "Url rewrite result: " << url << endl;
         return url;
@@ -324,15 +434,20 @@ namespace zypp
     return url_r;
   }
 
+  void MediaSetAccess::release()
+  {
+    DBG << "Releasing all media IDs held by this MediaSetAccess" << endl;
+    media::MediaManager manager;
+    for (MediaMap::const_iterator m = _medias.begin(); m != _medias.end(); ++m)
+      manager.release(m->second, "");
+  }
+
   std::ostream & MediaSetAccess::dumpOn( std::ostream & str ) const
   {
-    str << "MediaSetAccess (URL='" << _url << "', path='" << _path << "')";
+    str << "MediaSetAccess (URL='" << _url << "', attach_point_hint='" << _prefAttachPoint << "')";
     return str;
   }
 
-//     media::MediaVerifierRef MediaSetAccess::verifier(unsigned media_nr)
-//     { return media::MediaVerifierRef(new media::NoVerifier()); }
-
 /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////