Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / RepoManager.h
index c91d2a2..3d0bdfd 100644 (file)
 #include <list>
 
 #include "zypp/base/PtrTypes.h"
+#include "zypp/base/Iterator.h"
+#include "zypp/base/Flags.h"
+
 #include "zypp/Pathname.h"
 #include "zypp/ZConfig.h"
 #include "zypp/RepoInfo.h"
 #include "zypp/repo/RepoException.h"
 #include "zypp/repo/RepoType.h"
+#include "zypp/repo/ServiceType.h"
+#include "zypp/ServiceInfo.h"
 #include "zypp/RepoStatus.h"
 #include "zypp/ProgressData.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
-  class Service; //predef
 
    /**
     * Parses \a repo_file and returns a list of \ref RepoInfo objects
@@ -78,7 +82,18 @@ namespace zypp
     Pathname repoPackagesCachePath;
     Pathname knownReposPath;
     Pathname knownServicesPath;
+    Pathname pluginsPath;
     bool probe;
+    /**
+     * Target distro ID to be used when refreshing repo index services.
+     * Repositories not maching this ID will be skipped/removed.
+     *
+     * If empty, \ref Target::targetDistribution() will be used instead.
+     */
+    std::string servicesTargetDistro;
+
+    /** remembers root_r value for later use */
+    Pathname rootDir;
   };
 
 
@@ -95,11 +110,16 @@ namespace zypp
     /** Implementation  */
     class Impl;
 
-    /** service typedefs */
-    typedef std::set<Service> ServiceSet;
+    /** ServiceInfo typedefs */
+    typedef std::set<ServiceInfo> ServiceSet;
     typedef ServiceSet::const_iterator ServiceConstIterator;
     typedef ServiceSet::size_type ServiceSizeType;
 
+    /** RepoInfo typedefs */
+    typedef std::set<RepoInfo> RepoSet;
+    typedef RepoSet::const_iterator RepoConstIterator;
+    typedef RepoSet::size_type RepoSizeType;
+
   public:
    RepoManager( const RepoManagerOptions &options = RepoManagerOptions() );
    /** Dtor */
@@ -118,20 +138,51 @@ namespace zypp
       BuildForced
     };
 
-    enum RepoRemovePolicy
+    /** Flags for tuning RefreshService */
+    enum RefreshServiceBit
     {
-
+      RefreshService_restoreStatus     = (1<<0),       ///< Force restoring repo enabled/disabled status
+      RefreshService_forceRefresh      = (1<<1),       ///< Force refresh even if TTL is not reached
     };
+    ZYPP_DECLARE_FLAGS(RefreshServiceFlags,RefreshServiceBit);
 
-   /**
-    * \short List known repositories.
-    *
-    * The known repositories are read from
-    * \ref RepoManagerOptions::knownReposPath passed on the Ctor.
-    * Which defaults to ZYpp global settings.
-    * \return found list<RepoInfo>
+    /** Options tuning RefreshService */
+    typedef RefreshServiceFlags RefreshServiceOptions;
+
+
+    /** \name Known repositories.
+     *
+     * The known repositories are read from
+     * \ref RepoManagerOptions::knownReposPath passed on the Ctor.
+     * Which defaults to ZYpp global settings.
+     */
+   //@{
+    bool repoEmpty() const;
+    RepoSizeType repoSize() const;
+    RepoConstIterator repoBegin() const;
+    RepoConstIterator repoEnd() const;
+
+    /** List of known repositories. */
+    std::list<RepoInfo> knownRepositories() const
+    { return std::list<RepoInfo>(repoBegin(),repoEnd()); }
+
+    /** Find RepoInfo by alias or return \ref RepoInfo::noRepo. */
+    RepoInfo getRepo( const std::string & alias ) const;
+    /** \overload Take alias from RepoInfo. */
+    RepoInfo getRepo( const RepoInfo & info_r ) const
+    { return getRepo( info_r.alias() ); }
+
+    /** Return whether there is a known repository for \c alias. */
+    bool hasRepo( const std::string & alias ) const;
+    /** \overload Take alias from RepoInfo. */
+    bool hasRepo( const RepoInfo & info_r ) const
+    { return hasRepo( info_r.alias() ); }
+
+    /** Some stupid string but suitable as alias for your url if nothing better is available.
+     * Something like \c "http-download.opensuse.org-83df67e5"
     */
-   std::list<RepoInfo> knownRepositories() const;
+    static std::string makeStupidAlias( const Url & url_r = Url() );
+   //@}
 
    /**
     * \short Status of local metadata
@@ -332,12 +383,28 @@ namespace zypp
     */
    void loadFromCache( const RepoInfo &info,
                        const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
+
+   /**
+    * Remove any subdirectories of cache directories which no longer belong
+    * to any of known repositories.
+    *
+    * These can be temporary directories left by interrupted refresh,
+    * or dirs left after changing .repo files outside of libzypp.
+    */
+   void cleanCacheDirGarbage( const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
+
    /**
     * \short Probe repo metadata type.
     *
-    * \todo FIXME Should this be private?
+    * The location to probe consists of the base \a url (you may think of it as
+    * a mountpoint) and the \a path to the repository on the mounted media
+    * (ususally \c / ).
+    */
+   repo::RepoType probe( const Url & url, const Pathname & path ) const;
+   /**
+    * \overload Using the default path \c "/".
     */
-   repo::RepoType probe( const Url &url ) const;
+   repo::RepoType probe( const Url & url ) const;
 
 
    /**
@@ -347,8 +414,11 @@ namespace zypp
     *
     * \throws repo::RepoAlreadyExistsException If the repo clash some
     *         unique attribute like alias
-    * \throws RepoUnknownType If repository type can't be determined
-    * \throws RepoException If the access to the url fails (while probing).
+    * \throws RepoUnknownType
+    *         If RepoManagerOptions::probe is true
+    *         and repository type can't be determined.
+    * \throws RepoException
+    *         If RepoManagerOptions::probe is true and access to the url fails.
     * \throws Exception On other errors.
     */
    void addRepository( const RepoInfo &info,
@@ -359,9 +429,7 @@ namespace zypp
     * \param url Url of the repo file
     *
     * \throws repo::RepoAlreadyExistsException If the repo clash some
-    * unique attribute like alias
-    *
-    * \throws RepoAlreadyExistsException
+    *         unique attribute like alias
     * \throws MediaException If the access to the url fails
     * \throws ParseException If the file parsing fails
     * \throws RepoUnknownType If repository type can't be determined
@@ -390,6 +458,10 @@ namespace zypp
     void modifyRepository( const std::string &alias,
                            const RepoInfo & newinfo,
                            const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
+    /** \overload Take alias from RepoInfo. */
+    void modifyRepository( const RepoInfo & newinfo,
+                           const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() )
+    { modifyRepository( newinfo.alias(), newinfo, progressrcv ); }
 
     /**
      * \short Find a matching repository info
@@ -430,38 +502,198 @@ namespace zypp
                                 const url::ViewOption & urlview = url::ViewOption::DEFAULTS,
                                 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
 
-    void addService( const std::string& name, const Url& url );
-
-    void removeService( const std::string& name );
 
+    /** \name Known services.
+     *
+     * The known services are read from
+     * \ref RepoManagerOptions::knownServicesPath passed on the Ctor.
+     * Which defaults to ZYpp global settings.
+     */
+    //@{
+    /**
+     * Gets true if no service is in RepoManager (so no one in specified location)
+     *
+     * \return true if any ServiceInfo is in RepoManager
+     */
     bool serviceEmpty() const;
 
+    /**
+     * Gets count of service in RepoManager (in specified location)
+     *
+     * \return count of service
+     */
     ServiceSizeType serviceSize() const;
 
+    /**
+     * Iterator to first service in internal storage.
+     * \note Iterator is immutable, so you cannot change pointed ServiceInfo
+     * \return Iterator to first service
+     */
     ServiceConstIterator serviceBegin() const;
 
+    /**
+     * Iterator to place behind last service in internal storage.
+     * \return iterator to end
+     */
     ServiceConstIterator serviceEnd() const;
 
-    const Service& getService( const std::string& name ) const;
+    /** List of known services. */
+    std::list<ServiceInfo> knownServices() const
+    { return std::list<ServiceInfo>(serviceBegin(),serviceEnd()); }
 
-    void refreshServices();
+    /**
+     * \short Finds ServiceInfo by alias or return \ref ServiceInfo::noService
+     *
+     * \param alias unique identifier of service
+     * \return information about service
+     */
+    ServiceInfo getService( const std::string & alias ) const;
 
-  protected:
-    RepoStatus rawMetadataStatus( const RepoInfo &info );
-    void setCacheStatus( const RepoInfo &info, const RepoStatus &status );
+    /** Return whether there is a known service for \c alias. */
+    bool hasService( const std::string & alias ) const;
+    //@}
 
     /**
-     * Update timestamp of repository index file for the specified repository \a info.
-     * Used in \ref checkIfToRefreshMetadata() for repo.refresh.delay feature.
+     * \short Probe the type or the service.
+     */
+    repo::ServiceType probeService( const Url &url ) const;
+
+    /**
+     * Adds new service by it's alias and url
+     *
+     * \param alias unique identifier of the service
+     * \param url url to service
+     *
+     * \throws FIXME RepoAlreadyExistException and as reponame is service name
      */
-    void touchIndexFile(const RepoInfo & info);
+    void addService( const std::string & alias, const Url& url );
+
+    /**
+     * Adds new service
+     *
+     * \param service service info
+     *
+     * \throws FIXME RepoAlreadyExistException and as reponame is service name
+     */
+    void addService( const ServiceInfo & service );
+
+    /**
+     * Removes service specified by its name
+     *
+     * \param alias unique indientifier of the service to remove
+     *
+     * \throws RepoException if service is not found or file with ServiceInfo cannot be deleted
+     * \throws Exception if file contain more services and rewrite file failed
+     */
+    void removeService( const std::string & alias );
+    /** \overload Take alias from ServiceInfo */
+    void removeService( const ServiceInfo & service );
+
+
+    /**
+     * Refreshes all enabled services.
+     *
+     * \see refreshService(ServiceInfo)
+     */
+    void refreshServices( const RefreshServiceOptions & options_r = RefreshServiceOptions() );
+
+    /**
+     * Refresh specific service.
+     *
+     * \param alias unique indientifier of the service to refresh
+     *
+     * \throws RepoException if service is not found.
+     * \throws MediaException If there's a problem downloading the repo index file.
+     */
+    void refreshService( const std::string & alias, const RefreshServiceOptions & options_r = RefreshServiceOptions() );
+    /** \overload Take alias from ServiceInfo */
+    void refreshService( const ServiceInfo & service, const RefreshServiceOptions & options_r = RefreshServiceOptions() );
+
+    /**
+     * Modifies service file (rewrites it with new values) and underlying
+     * repositories if needed.
+     *
+     * Modifications of a service can lead to rewrite of all .repo files of
+     * contained repositories. Particularily, disabling a service (changing
+     * ServiceInfo::enabled() from true to false) will disable all contained
+     * repositories. Renaming of a service will modify the "service" key
+     * of all contained repositories.
+     *
+     * \param oldAlias Old alias of the service
+     * \param service ServiceInfo object containing new data
+     *
+     * \throws RepoException if sservice with oldAlias is not known
+     * \throws Exception if have problems with files
+     */
+    void modifyService( const std::string & oldAlias, const ServiceInfo & service );
+    /** \overload Take alias from ServiceInfo. */
+    void modifyService( const ServiceInfo & service )
+    { modifyService( service.alias(), service ); }
+
+  private:
+    /**
+     * Functor thats filter RepoInfo by service which it belongs to.
+     */
+    struct MatchServiceAlias
+    {
+      public:
+        MatchServiceAlias( const std::string & alias_ ) : alias(alias_) {}
+        bool operator()( const RepoInfo & info ) const
+        { return info.service() == alias; }
+      private:
+        std::string alias;
+    };
 
   public:
 
+    /**
+     * fill to output iterator repositories in service name. This output iterator can perform
+     * any action on with Repo or service Container, because it is sets and it isn't dynamic recreate.
+     *
+     * \note Don't use this function with RepoManager::removeRepository(), it will lead to segfaults
+     *       due to invalidated internal iterators. FIXME can this be solved (using STL) so that this
+     *       warning would not be needed?
+     *
+     * \param alias service alias
+     * \param out output iterator which get all the repositories belonging to
+     *   specified service
+     *
+     * example how set priority for each RepoInfo in this service:
+     * \code
+     * //functor
+     * class ChangePriority
+     * {
+     * private:
+     *   int priority;
+     * public:
+     *   ChangePriority(int prio) : priority(prio) {}
+     *   // missing rewrite priority back via RepoManager::modifyRepo
+     *   void doIt( RepoInfo info ) { info.setPriority(priority); }
+     * }
+     *
+     * //somewhere in code
+     * ChangePriority changer(10);
+     * getRepositoriesInService(name,
+     *   boost::make_function_output_iterator(
+     *     bind(&ChangePriority::doIt, &changer, _1)));
+     * \endcode
+     */
+    template<typename OutputIterator>
+    void getRepositoriesInService( const std::string & alias,
+                                   OutputIterator out ) const
+    {
+      MatchServiceAlias filter(alias);
+
+      std::copy( boost::make_filter_iterator( filter, repoBegin(), repoEnd() ),
+                 boost::make_filter_iterator( filter, repoEnd(), repoEnd() ),
+                 out);
+    }
+
   private:
     /** Pointer to implementation */
     RWCOW_pointer<Impl> _pimpl;
   };
+  ZYPP_DECLARE_OPERATORS_FOR_FLAGS(RepoManager::RefreshServiceFlags);
   ///////////////////////////////////////////////////////////////////
 
   /** \relates RepoManager Stream output */