Imported Upstream version 15.19.0
[platform/upstream/libzypp.git] / zypp / RepoManager.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoManager.cc
10  *
11 */
12
13 #include <cstdlib>
14 #include <iostream>
15 #include <fstream>
16 #include <sstream>
17 #include <list>
18 #include <map>
19 #include <algorithm>
20
21 #include "zypp/base/InputStream.h"
22 #include "zypp/base/LogTools.h"
23 #include "zypp/base/Gettext.h"
24 #include "zypp/base/DefaultIntegral.h"
25 #include "zypp/base/Function.h"
26 #include "zypp/base/Regex.h"
27 #include "zypp/PathInfo.h"
28 #include "zypp/TmpPath.h"
29
30 #include "zypp/ServiceInfo.h"
31 #include "zypp/repo/RepoException.h"
32 #include "zypp/RepoManager.h"
33
34 #include "zypp/media/MediaManager.h"
35 #include "zypp/media/CredentialManager.h"
36 #include "zypp/MediaSetAccess.h"
37 #include "zypp/ExternalProgram.h"
38 #include "zypp/ManagedFile.h"
39
40 #include "zypp/parser/RepoFileReader.h"
41 #include "zypp/parser/ServiceFileReader.h"
42 #include "zypp/repo/ServiceRepos.h"
43 #include "zypp/repo/yum/Downloader.h"
44 #include "zypp/repo/susetags/Downloader.h"
45 #include "zypp/repo/PluginServices.h"
46
47 #include "zypp/Target.h" // for Target::targetDistribution() for repo index services
48 #include "zypp/ZYppFactory.h" // to get the Target from ZYpp instance
49 #include "zypp/HistoryLog.h" // to write history :O)
50
51 #include "zypp/ZYppCallbacks.h"
52
53 #include "sat/Pool.h"
54
55 using std::endl;
56 using std::string;
57 using namespace zypp::repo;
58
59 #define OPT_PROGRESS const ProgressData::ReceiverFnc & = ProgressData::ReceiverFnc()
60
61 ///////////////////////////////////////////////////////////////////
62 namespace zypp
63 {
64   ///////////////////////////////////////////////////////////////////
65   namespace
66   {
67     /** Simple media mounter to access non-downloading URLs e.g. for non-local plaindir repos.
68      * \ingroup g_RAII
69      */
70     class MediaMounter
71     {
72       public:
73         /** Ctor provides media access. */
74         MediaMounter( const Url & url_r )
75         {
76           media::MediaManager mediamanager;
77           _mid = mediamanager.open( url_r );
78           mediamanager.attach( _mid );
79         }
80
81         /** Ctor releases the media. */
82         ~MediaMounter()
83         {
84           media::MediaManager mediamanager;
85           mediamanager.release( _mid );
86           mediamanager.close( _mid );
87         }
88
89         /** Convert a path relative to the media into an absolute path.
90          *
91          * Called without argument it returns the path to the medias root directory.
92         */
93         Pathname getPathName( const Pathname & path_r = Pathname() ) const
94         {
95           media::MediaManager mediamanager;
96           return mediamanager.localPath( _mid, path_r );
97         }
98
99       private:
100         media::MediaAccessId _mid;
101     };
102     ///////////////////////////////////////////////////////////////////
103
104     /** Check if alias_r is present in repo/service container. */
105     template <class Iterator>
106     inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
107     {
108       for_( it, begin_r, end_r )
109         if ( it->alias() == alias_r )
110           return true;
111       return false;
112     }
113     /** \overload */
114     template <class Container>
115     inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
116     { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
117
118     /** Find alias_r in repo/service container. */
119     template <class Iterator>
120     inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
121     {
122       for_( it, begin_r, end_r )
123         if ( it->alias() == alias_r )
124           return it;
125       return end_r;
126     }
127     /** \overload */
128     template <class Container>
129     inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
130     { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
131     /** \overload */
132     template <class Container>
133     inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
134     { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
135
136
137     /** \short Generate a related filename from a repo/service infos alias */
138     inline std::string filenameFromAlias( const std::string & alias_r, const std::string & stem_r )
139     {
140       std::string filename( alias_r );
141       // replace slashes with underscores
142       str::replaceAll( filename, "/", "_" );
143
144       filename = Pathname(filename).extend("."+stem_r).asString();
145       MIL << "generating filename for " << stem_r << " [" << alias_r << "] : '" << filename << "'" << endl;
146       return filename;
147     }
148
149     /**
150      * \short Simple callback to collect the results
151      *
152      * Classes like RepoFileReader call the callback
153      * once per each repo in a file.
154      *
155      * Passing this functor as callback, you can collect
156      * all results at the end, without dealing with async
157      * code.
158      *
159      * If targetDistro is set, all repos with non-empty RepoInfo::targetDistribution()
160      * will be skipped.
161      *
162      * \todo do this through a separate filter
163      */
164     struct RepoCollector : private base::NonCopyable
165     {
166       RepoCollector()
167       {}
168
169       RepoCollector(const std::string & targetDistro_)
170         : targetDistro(targetDistro_)
171       {}
172
173       bool collect( const RepoInfo &repo )
174       {
175         // skip repositories meant for other distros than specified
176         if (!targetDistro.empty()
177             && !repo.targetDistribution().empty()
178             && repo.targetDistribution() != targetDistro)
179         {
180           MIL
181             << "Skipping repository meant for '" << repo.targetDistribution()
182             << "' distribution (current distro is '"
183             << targetDistro << "')." << endl;
184
185           return true;
186         }
187
188         repos.push_back(repo);
189         return true;
190       }
191
192       RepoInfoList repos;
193       std::string targetDistro;
194     };
195     ////////////////////////////////////////////////////////////////////////////
196
197     /**
198      * Reads RepoInfo's from a repo file.
199      *
200      * \param file pathname of the file to read.
201      */
202     std::list<RepoInfo> repositories_in_file( const Pathname & file )
203     {
204       MIL << "repo file: " << file << endl;
205       RepoCollector collector;
206       parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
207       return std::move(collector.repos);
208     }
209
210     ////////////////////////////////////////////////////////////////////////////
211
212     /**
213      * \short List of RepoInfo's from a directory
214      *
215      * Goes trough every file ending with ".repo" in a directory and adds all
216      * RepoInfo's contained in that file.
217      *
218      * \param dir pathname of the directory to read.
219      */
220     std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
221     {
222       MIL << "directory " << dir << endl;
223       std::list<RepoInfo> repos;
224       bool nonroot( geteuid() != 0 );
225       if ( nonroot && ! PathInfo(dir).userMayRX() )
226       {
227         JobReport::warning( formatNAC(_("Cannot read repo directory '%1%': Permission denied")) % dir );
228       }
229       else
230       {
231         std::list<Pathname> entries;
232         if ( filesystem::readdir( entries, dir, false ) != 0 )
233         {
234           // TranslatorExplanation '%s' is a pathname
235           ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
236         }
237
238         str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
239         for ( std::list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
240         {
241           if ( str::regex_match(it->extension(), allowedRepoExt) )
242           {
243             if ( nonroot && ! PathInfo(*it).userMayR() )
244             {
245               JobReport::warning( formatNAC(_("Cannot read repo file '%1%': Permission denied")) % *it );
246             }
247             else
248             {
249               const std::list<RepoInfo> & tmp( repositories_in_file( *it ) );
250               repos.insert( repos.end(), tmp.begin(), tmp.end() );
251             }
252           }
253         }
254       }
255       return repos;
256     }
257
258     ////////////////////////////////////////////////////////////////////////////
259
260     inline void assert_alias( const RepoInfo & info )
261     {
262       if ( info.alias().empty() )
263         ZYPP_THROW( RepoNoAliasException( info ) );
264       // bnc #473834. Maybe we can match the alias against a regex to define
265       // and check for valid aliases
266       if ( info.alias()[0] == '.')
267         ZYPP_THROW(RepoInvalidAliasException(
268           info, _("Repository alias cannot start with dot.")));
269     }
270
271     inline void assert_alias( const ServiceInfo & info )
272     {
273       if ( info.alias().empty() )
274         ZYPP_THROW( ServiceNoAliasException( info ) );
275       // bnc #473834. Maybe we can match the alias against a regex to define
276       // and check for valid aliases
277       if ( info.alias()[0] == '.')
278         ZYPP_THROW(ServiceInvalidAliasException(
279           info, _("Service alias cannot start with dot.")));
280     }
281
282     ////////////////////////////////////////////////////////////////////////////
283
284     inline void assert_urls( const RepoInfo & info )
285     {
286       if ( info.baseUrlsEmpty() )
287         ZYPP_THROW( RepoNoUrlException( info ) );
288     }
289
290     inline void assert_url( const ServiceInfo & info )
291     {
292       if ( ! info.url().isValid() )
293         ZYPP_THROW( ServiceNoUrlException( info ) );
294     }
295
296     ////////////////////////////////////////////////////////////////////////////
297
298     /**
299      * \short Calculates the raw cache path for a repository, this is usually
300      * /var/cache/zypp/alias
301      */
302     inline Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
303     {
304       assert_alias(info);
305       return opt.repoRawCachePath / info.escaped_alias();
306     }
307
308     /**
309      * \short Calculates the raw product metadata path for a repository, this is
310      * inside the raw cache dir, plus an optional path where the metadata is.
311      *
312      * It should be different only for repositories that are not in the root of
313      * the media.
314      * for example /var/cache/zypp/alias/addondir
315      */
316     inline Pathname rawproductdata_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
317     {
318       assert_alias(info);
319       return opt.repoRawCachePath / info.escaped_alias() / info.path();
320     }
321
322     /**
323      * \short Calculates the packages cache path for a repository
324      */
325     inline Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
326     {
327       assert_alias(info);
328       return opt.repoPackagesCachePath / info.escaped_alias();
329     }
330
331     /**
332      * \short Calculates the solv cache path for a repository
333      */
334     inline Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info)
335     {
336       assert_alias(info);
337       return opt.repoSolvCachePath / info.escaped_alias();
338     }
339
340     ////////////////////////////////////////////////////////////////////////////
341
342     /** Functor collecting ServiceInfos into a ServiceSet. */
343     class ServiceCollector
344     {
345     public:
346       typedef std::set<ServiceInfo> ServiceSet;
347
348       ServiceCollector( ServiceSet & services_r )
349       : _services( services_r )
350       {}
351
352       bool operator()( const ServiceInfo & service_r ) const
353       {
354         _services.insert( service_r );
355         return true;
356       }
357
358     private:
359       ServiceSet & _services;
360     };
361     ////////////////////////////////////////////////////////////////////////////
362
363   } // namespace
364   ///////////////////////////////////////////////////////////////////
365
366   std::list<RepoInfo> readRepoFile( const Url & repo_file )
367   {
368     // no interface to download a specific file, using workaround:
369     //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
370     Url url(repo_file);
371     Pathname path(url.getPathName());
372     url.setPathName ("/");
373     MediaSetAccess access(url);
374     Pathname local = access.provideFile(path);
375
376     DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
377
378     return repositories_in_file(local);
379   }
380
381   ///////////////////////////////////////////////////////////////////
382   //
383   //    class RepoManagerOptions
384   //
385   ////////////////////////////////////////////////////////////////////
386
387   RepoManagerOptions::RepoManagerOptions( const Pathname & root_r )
388   {
389     repoCachePath         = Pathname::assertprefix( root_r, ZConfig::instance().repoCachePath() );
390     repoRawCachePath      = Pathname::assertprefix( root_r, ZConfig::instance().repoMetadataPath() );
391     repoSolvCachePath     = Pathname::assertprefix( root_r, ZConfig::instance().repoSolvfilesPath() );
392     repoPackagesCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoPackagesPath() );
393     knownReposPath        = Pathname::assertprefix( root_r, ZConfig::instance().knownReposPath() );
394     knownServicesPath     = Pathname::assertprefix( root_r, ZConfig::instance().knownServicesPath() );
395     pluginsPath           = Pathname::assertprefix( root_r, ZConfig::instance().pluginsPath() );
396     probe                 = ZConfig::instance().repo_add_probe();
397
398     rootDir = root_r;
399   }
400
401   RepoManagerOptions RepoManagerOptions::makeTestSetup( const Pathname & root_r )
402   {
403     RepoManagerOptions ret;
404     ret.repoCachePath         = root_r;
405     ret.repoRawCachePath      = root_r/"raw";
406     ret.repoSolvCachePath     = root_r/"solv";
407     ret.repoPackagesCachePath = root_r/"packages";
408     ret.knownReposPath        = root_r/"repos.d";
409     ret.knownServicesPath     = root_r/"services.d";
410     ret.pluginsPath           = root_r/"plugins";
411     ret.rootDir = root_r;
412     return ret;
413   }
414
415   std:: ostream & operator<<( std::ostream & str, const RepoManagerOptions & obj )
416   {
417 #define OUTS(X) str << "  " #X "\t" << obj.X << endl
418     str << "RepoManagerOptions (" << obj.rootDir << ") {" << endl;
419     OUTS( repoRawCachePath );
420     OUTS( repoSolvCachePath );
421     OUTS( repoPackagesCachePath );
422     OUTS( knownReposPath );
423     OUTS( knownServicesPath );
424     OUTS( pluginsPath );
425     str << "}" << endl;
426 #undef OUTS
427     return str;
428   }
429
430   ///////////////////////////////////////////////////////////////////
431   /// \class RepoManager::Impl
432   /// \brief RepoManager implementation.
433   ///
434   ///////////////////////////////////////////////////////////////////
435   struct RepoManager::Impl
436   {
437   public:
438     Impl( const RepoManagerOptions &opt )
439       : _options(opt)
440     {
441       init_knownServices();
442       init_knownRepositories();
443     }
444
445     ~Impl()
446     {
447       // trigger appdata refresh if some repos change
448       if ( _reposDirty && geteuid() == 0 && ( _options.rootDir.empty() || _options.rootDir == "/" ) )
449       {
450         try {
451           std::list<Pathname> entries;
452           filesystem::readdir( entries, _options.pluginsPath/"appdata", false );
453           if ( ! entries.empty() )
454           {
455             ExternalProgram::Arguments cmd;
456             cmd.push_back( "<" );               // discard stdin
457             cmd.push_back( ">" );               // discard stdout
458             cmd.push_back( "PROGRAM" );         // [2] - fix index below if changing!
459             for ( const auto & rinfo : repos() )
460             {
461               if ( ! rinfo.enabled() )
462                 continue;
463               cmd.push_back( "-R" );
464               cmd.push_back( rinfo.alias() );
465               cmd.push_back( "-t" );
466               cmd.push_back( rinfo.type().asString() );
467               cmd.push_back( "-p" );
468               cmd.push_back( rinfo.metadataPath().asString() );
469             }
470
471             for_( it, entries.begin(), entries.end() )
472             {
473               PathInfo pi( *it );
474               //DBG << "/tmp/xx ->" << pi << endl;
475               if ( pi.isFile() && pi.userMayRX() )
476               {
477                 // trigger plugin
478                 cmd[2] = pi.asString();         // [2] - PROGRAM
479                 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
480               }
481             }
482           }
483         }
484         catch (...) {}  // no throw in dtor
485       }
486     }
487
488   public:
489     bool repoEmpty() const              { return repos().empty(); }
490     RepoSizeType repoSize() const       { return repos().size(); }
491     RepoConstIterator repoBegin() const { return repos().begin(); }
492     RepoConstIterator repoEnd() const   { return repos().end(); }
493
494     bool hasRepo( const std::string & alias ) const
495     { return foundAliasIn( alias, repos() ); }
496
497     RepoInfo getRepo( const std::string & alias ) const
498     {
499       RepoConstIterator it( findAlias( alias, repos() ) );
500       return it == repos().end() ? RepoInfo::noRepo : *it;
501     }
502
503   public:
504     Pathname metadataPath( const RepoInfo & info ) const
505     { return rawcache_path_for_repoinfo( _options, info ); }
506
507     Pathname packagesPath( const RepoInfo & info ) const
508     { return packagescache_path_for_repoinfo( _options, info ); }
509
510     RepoStatus metadataStatus( const RepoInfo & info ) const;
511
512     RefreshCheckStatus checkIfToRefreshMetadata( const RepoInfo & info, const Url & url, RawMetadataRefreshPolicy policy );
513
514     void refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, OPT_PROGRESS );
515
516     void cleanMetadata( const RepoInfo & info, OPT_PROGRESS );
517
518     void cleanPackages( const RepoInfo & info, OPT_PROGRESS );
519
520     void buildCache( const RepoInfo & info, CacheBuildPolicy policy, OPT_PROGRESS );
521
522     repo::RepoType probe( const Url & url, const Pathname & path = Pathname() ) const;
523     repo::RepoType probeCache( const Pathname & path_r ) const;
524
525     void cleanCacheDirGarbage( OPT_PROGRESS );
526
527     void cleanCache( const RepoInfo & info, OPT_PROGRESS );
528
529     bool isCached( const RepoInfo & info ) const
530     { return PathInfo(solv_path_for_repoinfo( _options, info ) / "solv").isExist(); }
531
532     RepoStatus cacheStatus( const RepoInfo & info ) const
533     { return RepoStatus::fromCookieFile(solv_path_for_repoinfo(_options, info) / "cookie"); }
534
535     void loadFromCache( const RepoInfo & info, OPT_PROGRESS );
536
537     void addRepository( const RepoInfo & info, OPT_PROGRESS );
538
539     void addRepositories( const Url & url, OPT_PROGRESS );
540
541     void removeRepository( const RepoInfo & info, OPT_PROGRESS );
542
543     void modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, OPT_PROGRESS );
544
545     RepoInfo getRepositoryInfo( const std::string & alias, OPT_PROGRESS );
546     RepoInfo getRepositoryInfo( const Url & url, const url::ViewOption & urlview, OPT_PROGRESS );
547
548   public:
549     bool serviceEmpty() const                   { return _services.empty(); }
550     ServiceSizeType serviceSize() const         { return _services.size(); }
551     ServiceConstIterator serviceBegin() const   { return _services.begin(); }
552     ServiceConstIterator serviceEnd() const     { return _services.end(); }
553
554     bool hasService( const std::string & alias ) const
555     { return foundAliasIn( alias, _services ); }
556
557     ServiceInfo getService( const std::string & alias ) const
558     {
559       ServiceConstIterator it( findAlias( alias, _services ) );
560       return it == _services.end() ? ServiceInfo::noService : *it;
561     }
562
563   public:
564     void addService( const ServiceInfo & service );
565     void addService( const std::string & alias, const Url & url )
566     { addService( ServiceInfo( alias, url ) ); }
567
568     void removeService( const std::string & alias );
569     void removeService( const ServiceInfo & service )
570     { removeService( service.alias() ); }
571
572     void refreshServices( const RefreshServiceOptions & options_r );
573
574     void refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
575     void refreshService( const ServiceInfo & service, const RefreshServiceOptions & options_r )
576     {  refreshService( service.alias(), options_r ); }
577
578     void modifyService( const std::string & oldAlias, const ServiceInfo & newService );
579
580     repo::ServiceType probeService( const Url & url ) const;
581
582   private:
583     void saveService( ServiceInfo & service ) const;
584
585     Pathname generateNonExistingName( const Pathname & dir, const std::string & basefilename ) const;
586
587     std::string generateFilename( const RepoInfo & info ) const
588     { return filenameFromAlias( info.alias(), "repo" ); }
589
590     std::string generateFilename( const ServiceInfo & info ) const
591     { return filenameFromAlias( info.alias(), "service" ); }
592
593     void setCacheStatus( const RepoInfo & info, const RepoStatus & status )
594     {
595       Pathname base = solv_path_for_repoinfo( _options, info );
596       filesystem::assert_dir(base);
597       status.saveToCookieFile( base / "cookie" );
598     }
599
600     void touchIndexFile( const RepoInfo & info );
601
602     template<typename OutputIterator>
603     void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
604     {
605       MatchServiceAlias filter( alias );
606       std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
607                  boost::make_filter_iterator( filter, repos().end(), repos().end() ),
608                  out);
609     }
610
611   private:
612     void init_knownServices();
613     void init_knownRepositories();
614
615     const RepoSet & repos() const { return _reposX; }
616     RepoSet & reposManip()        { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
617
618   private:
619     RepoManagerOptions  _options;
620     RepoSet             _reposX;
621     ServiceSet          _services;
622
623     DefaultIntegral<bool,false> _reposDirty;
624
625   private:
626     friend Impl * rwcowClone<Impl>( const Impl * rhs );
627     /** clone for RWCOW_pointer */
628     Impl * clone() const
629     { return new Impl( *this ); }
630   };
631   ///////////////////////////////////////////////////////////////////
632
633   /** \relates RepoManager::Impl Stream output */
634   inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
635   { return str << "RepoManager::Impl"; }
636
637   ///////////////////////////////////////////////////////////////////
638
639   void RepoManager::Impl::saveService( ServiceInfo & service ) const
640   {
641     filesystem::assert_dir( _options.knownServicesPath );
642     Pathname servfile = generateNonExistingName( _options.knownServicesPath,
643                                                  generateFilename( service ) );
644     service.setFilepath( servfile );
645
646     MIL << "saving service in " << servfile << endl;
647
648     std::ofstream file( servfile.c_str() );
649     if ( !file )
650     {
651       // TranslatorExplanation '%s' is a filename
652       ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), servfile.c_str() )));
653     }
654     service.dumpAsIniOn( file );
655     MIL << "done" << endl;
656   }
657
658   /**
659    * Generate a non existing filename in a directory, using a base
660    * name. For example if a directory contains 3 files
661    *
662    * |-- bar
663    * |-- foo
664    * `-- moo
665    *
666    * If you try to generate a unique filename for this directory,
667    * based on "ruu" you will get "ruu", but if you use the base
668    * "foo" you will get "foo_1"
669    *
670    * \param dir Directory where the file needs to be unique
671    * \param basefilename string to base the filename on.
672    */
673   Pathname RepoManager::Impl::generateNonExistingName( const Pathname & dir,
674                                                        const std::string & basefilename ) const
675   {
676     std::string final_filename = basefilename;
677     int counter = 1;
678     while ( PathInfo(dir + final_filename).isExist() )
679     {
680       final_filename = basefilename + "_" + str::numstring(counter);
681       ++counter;
682     }
683     return dir + Pathname(final_filename);
684   }
685
686   ////////////////////////////////////////////////////////////////////////////
687
688   void RepoManager::Impl::init_knownServices()
689   {
690     Pathname dir = _options.knownServicesPath;
691     std::list<Pathname> entries;
692     if (PathInfo(dir).isExist())
693     {
694       if ( filesystem::readdir( entries, dir, false ) != 0 )
695       {
696         // TranslatorExplanation '%s' is a pathname
697         ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
698       }
699
700       //str::regex allowedServiceExt("^\\.service(_[0-9]+)?$");
701       for_(it, entries.begin(), entries.end() )
702       {
703         parser::ServiceFileReader(*it, ServiceCollector(_services));
704       }
705     }
706
707     repo::PluginServices(_options.pluginsPath/"services", ServiceCollector(_services));
708   }
709
710   ///////////////////////////////////////////////////////////////////
711   namespace {
712     /** Delete \a cachePath_r subdirs not matching known aliases in \a repoEscAliases_r (must be sorted!)
713      * \note bnc#891515: Auto-cleanup only zypp.conf default locations. Otherwise
714      * we'd need some magic file to identify zypp cache directories. Without this
715      * we may easily remove user data (zypper --pkg-cache-dir . download ...)
716      */
717     inline void cleanupNonRepoMetadtaFolders( const Pathname & cachePath_r,
718                                               const Pathname & defaultCachePath_r,
719                                               const std::list<std::string> & repoEscAliases_r )
720     {
721       if ( cachePath_r != defaultCachePath_r )
722         return;
723
724       std::list<std::string> entries;
725       if ( filesystem::readdir( entries, cachePath_r, false ) == 0 )
726       {
727         entries.sort();
728         std::set<std::string> oldfiles;
729         set_difference( entries.begin(), entries.end(), repoEscAliases_r.begin(), repoEscAliases_r.end(),
730                         std::inserter( oldfiles, oldfiles.end() ) );
731         for ( const std::string & old : oldfiles )
732         {
733           if ( old == Repository::systemRepoAlias() )   // don't remove the @System solv file
734             continue;
735           filesystem::recursive_rmdir( cachePath_r / old );
736         }
737       }
738     }
739   } // namespace
740   ///////////////////////////////////////////////////////////////////
741   void RepoManager::Impl::init_knownRepositories()
742   {
743     MIL << "start construct known repos" << endl;
744
745     if ( PathInfo(_options.knownReposPath).isExist() )
746     {
747       std::list<std::string> repoEscAliases;
748       std::list<RepoInfo> orphanedRepos;
749       for ( RepoInfo & repoInfo : repositories_in_dir(_options.knownReposPath) )
750       {
751         // set the metadata path for the repo
752         repoInfo.setMetadataPath( rawcache_path_for_repoinfo(_options, repoInfo) );
753         // set the downloaded packages path for the repo
754         repoInfo.setPackagesPath( packagescache_path_for_repoinfo(_options, repoInfo) );
755         // remember it
756         _reposX.insert( repoInfo );     // direct access via _reposX in ctor! no reposManip.
757
758         // detect orphaned repos belonging to a deleted service
759         const std::string & serviceAlias( repoInfo.service() );
760         if ( ! ( serviceAlias.empty() || hasService( serviceAlias ) ) )
761         {
762           WAR << "Schedule orphaned service repo for deletion: " << repoInfo << endl;
763           orphanedRepos.push_back( repoInfo );
764           continue;     // don't remember it in repoEscAliases
765         }
766
767         repoEscAliases.push_back(repoInfo.escaped_alias());
768       }
769
770       // Cleanup orphanded service repos:
771       if ( ! orphanedRepos.empty() )
772       {
773         for ( auto & repoInfo : orphanedRepos )
774         {
775           MIL << "Delete orphaned service repo " << repoInfo.alias() << endl;
776           // translators: Cleanup a repository previously owned by a meanwhile unknown (deleted) service.
777           //   %1% = service name
778           //   %2% = repository name
779           JobReport::warning( formatNAC(_("Unknown service '%1%': Removing orphaned service repository '%2%'" ))
780                               % repoInfo.service()
781                               % repoInfo.alias() );
782           try {
783             removeRepository( repoInfo );
784           }
785           catch ( const Exception & caugth )
786           {
787             JobReport::error( caugth.asUserHistory() );
788           }
789         }
790       }
791
792       // delete metadata folders without corresponding repo (e.g. old tmp directories)
793       //
794       // bnc#891515: Auto-cleanup only zypp.conf default locations. Otherwise
795       // we'd need somemagic file to identify zypp cache directories. Without this
796       // we may easily remove user data (zypper --pkg-cache-dir . download ...)
797       repoEscAliases.sort();
798       RepoManagerOptions defaultCache( _options.rootDir );
799       cleanupNonRepoMetadtaFolders( _options.repoRawCachePath,          defaultCache.repoRawCachePath,          repoEscAliases );
800       cleanupNonRepoMetadtaFolders( _options.repoSolvCachePath,         defaultCache.repoSolvCachePath,         repoEscAliases );
801       cleanupNonRepoMetadtaFolders( _options.repoPackagesCachePath,     defaultCache.repoPackagesCachePath,     repoEscAliases );
802     }
803     MIL << "end construct known repos" << endl;
804   }
805
806   ///////////////////////////////////////////////////////////////////
807
808   RepoStatus RepoManager::Impl::metadataStatus( const RepoInfo & info ) const
809   {
810     Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
811     Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
812
813     RepoType repokind = info.type();
814     // If unknown, probe the local metadata
815     if ( repokind == RepoType::NONE )
816       repokind = probeCache( productdatapath );
817
818     RepoStatus status;
819     switch ( repokind.toEnum() )
820     {
821       case RepoType::RPMMD_e :
822         status = RepoStatus( productdatapath/"repodata/repomd.xml");
823         break;
824
825       case RepoType::YAST2_e :
826         status = RepoStatus( productdatapath/"content" ) && RepoStatus( mediarootpath/"media.1/media" );
827         break;
828
829       case RepoType::RPMPLAINDIR_e :
830         status = RepoStatus::fromCookieFile( productdatapath/"cookie" );
831         break;
832
833       case RepoType::NONE_e :
834         // Return default RepoStatus in case of RepoType::NONE
835         // indicating it should be created?
836         // ZYPP_THROW(RepoUnknownTypeException());
837         break;
838     }
839     return status;
840   }
841
842
843   void RepoManager::Impl::touchIndexFile( const RepoInfo & info )
844   {
845     Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
846
847     RepoType repokind = info.type();
848     if ( repokind.toEnum() == RepoType::NONE_e )
849       // unknown, probe the local metadata
850       repokind = probeCache( productdatapath );
851     // if still unknown, just return
852     if (repokind == RepoType::NONE_e)
853       return;
854
855     Pathname p;
856     switch ( repokind.toEnum() )
857     {
858       case RepoType::RPMMD_e :
859         p = Pathname(productdatapath + "/repodata/repomd.xml");
860         break;
861
862       case RepoType::YAST2_e :
863         p = Pathname(productdatapath + "/content");
864         break;
865
866       case RepoType::RPMPLAINDIR_e :
867         p = Pathname(productdatapath + "/cookie");
868         break;
869
870       case RepoType::NONE_e :
871       default:
872         break;
873     }
874
875     // touch the file, ignore error (they are logged anyway)
876     filesystem::touch(p);
877   }
878
879
880   RepoManager::RefreshCheckStatus RepoManager::Impl::checkIfToRefreshMetadata( const RepoInfo & info, const Url & url, RawMetadataRefreshPolicy policy )
881   {
882     assert_alias(info);
883     try
884     {
885       MIL << "Going to try to check whether refresh is needed for " << url << endl;
886
887       // first check old (cached) metadata
888       Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
889       filesystem::assert_dir( mediarootpath );
890       RepoStatus oldstatus = metadataStatus( info );
891
892       if ( oldstatus.empty() )
893       {
894         MIL << "No cached metadata, going to refresh" << endl;
895         return REFRESH_NEEDED;
896       }
897
898       {
899         if ( url.schemeIsVolatile() )
900         {
901           MIL << "never refresh CD/DVD" << endl;
902           return REPO_UP_TO_DATE;
903         }
904         if ( url.schemeIsLocal() )
905         {
906           policy = RefreshIfNeededIgnoreDelay;
907         }
908       }
909
910       // now we've got the old (cached) status, we can decide repo.refresh.delay
911       if (policy != RefreshForced && policy != RefreshIfNeededIgnoreDelay)
912       {
913         // difference in seconds
914         double diff = difftime(
915           (Date::ValueType)Date::now(),
916           (Date::ValueType)oldstatus.timestamp()) / 60;
917
918         DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
919         DBG << "current time: " << (Date::ValueType)Date::now() << endl;
920         DBG << "last refresh = " << diff << " minutes ago" << endl;
921
922         if ( diff < ZConfig::instance().repo_refresh_delay() )
923         {
924           if ( diff < 0 )
925           {
926             WAR << "Repository '" << info.alias() << "' was refreshed in the future!" << endl;
927           }
928           else
929           {
930             MIL << "Repository '" << info.alias()
931                 << "' has been refreshed less than repo.refresh.delay ("
932                 << ZConfig::instance().repo_refresh_delay()
933                 << ") minutes ago. Advising to skip refresh" << endl;
934             return REPO_CHECK_DELAYED;
935           }
936         }
937       }
938
939       repo::RepoType repokind = info.type();
940       // if unknown: probe it
941       if ( repokind == RepoType::NONE )
942         repokind = probe( url, info.path() );
943
944       // retrieve newstatus
945       RepoStatus newstatus;
946       switch ( repokind.toEnum() )
947       {
948         case RepoType::RPMMD_e:
949         {
950           MediaSetAccess media( url );
951           newstatus = yum::Downloader( info, mediarootpath ).status( media );
952         }
953         break;
954
955         case RepoType::YAST2_e:
956         {
957           MediaSetAccess media( url );
958           newstatus = susetags::Downloader( info, mediarootpath ).status( media );
959         }
960         break;
961
962         case RepoType::RPMPLAINDIR_e:
963           newstatus = RepoStatus( MediaMounter(url).getPathName(info.path()) ); // dir status
964           break;
965
966         default:
967         case RepoType::NONE_e:
968           ZYPP_THROW( RepoUnknownTypeException( info ) );
969           break;
970       }
971
972       // check status
973       bool refresh = false;
974       if ( oldstatus == newstatus )
975       {
976         MIL << "repo has not changed" << endl;
977         if ( policy == RefreshForced )
978         {
979           MIL << "refresh set to forced" << endl;
980           refresh = true;
981         }
982       }
983       else
984       {
985         MIL << "repo has changed, going to refresh" << endl;
986         refresh = true;
987       }
988
989       if (!refresh)
990         touchIndexFile(info);
991
992       return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
993
994     }
995     catch ( const Exception &e )
996     {
997       ZYPP_CAUGHT(e);
998       ERR << "refresh check failed for " << url << endl;
999       ZYPP_RETHROW(e);
1000     }
1001
1002     return REFRESH_NEEDED; // default
1003   }
1004
1005
1006   void RepoManager::Impl::refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, const ProgressData::ReceiverFnc & progress )
1007   {
1008     assert_alias(info);
1009     assert_urls(info);
1010
1011     // we will throw this later if no URL checks out fine
1012     RepoException rexception( info, PL_("Valid metadata not found at specified URL",
1013                                         "Valid metadata not found at specified URLs",
1014                                         info.baseUrlsSize() ) );
1015
1016     // Suppress (interactive) media::MediaChangeReport if we in have multiple basurls (>1)
1017     media::ScopedDisableMediaChangeReport guard( info.baseUrlsSize() > 1 );
1018
1019     // try urls one by one
1020     for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
1021     {
1022       try
1023       {
1024         Url url(*it);
1025
1026         // check whether to refresh metadata
1027         // if the check fails for this url, it throws, so another url will be checked
1028         if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
1029           return;
1030
1031         MIL << "Going to refresh metadata from " << url << endl;
1032
1033         repo::RepoType repokind = info.type();
1034
1035         // if the type is unknown, try probing.
1036         if ( repokind == RepoType::NONE )
1037         {
1038           // unknown, probe it
1039           repokind = probe( *it, info.path() );
1040
1041           if (repokind.toEnum() != RepoType::NONE_e)
1042           {
1043             // Adjust the probed type in RepoInfo
1044             info.setProbedType( repokind ); // lazy init!
1045             //save probed type only for repos in system
1046             for_( it, repoBegin(), repoEnd() )
1047             {
1048               if ( info.alias() == (*it).alias() )
1049               {
1050                 RepoInfo modifiedrepo = info;
1051                 modifiedrepo.setType( repokind );
1052                 modifyRepository( info.alias(), modifiedrepo );
1053                 break;
1054               }
1055             }
1056           }
1057         }
1058
1059         Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
1060         if( filesystem::assert_dir(mediarootpath) )
1061         {
1062           Exception ex(str::form( _("Can't create %s"), mediarootpath.c_str()) );
1063           ZYPP_THROW(ex);
1064         }
1065
1066         // create temp dir as sibling of mediarootpath
1067         filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( mediarootpath ) );
1068         if( tmpdir.path().empty() )
1069         {
1070           Exception ex(_("Can't create metadata cache directory."));
1071           ZYPP_THROW(ex);
1072         }
1073
1074         if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
1075              ( repokind.toEnum() == RepoType::YAST2_e ) )
1076         {
1077           MediaSetAccess media(url);
1078           shared_ptr<repo::Downloader> downloader_ptr;
1079
1080           MIL << "Creating downloader for [ " << info.alias() << " ]" << endl;
1081
1082           if ( repokind.toEnum() == RepoType::RPMMD_e )
1083             downloader_ptr.reset(new yum::Downloader(info, mediarootpath));
1084           else
1085             downloader_ptr.reset( new susetags::Downloader(info, mediarootpath) );
1086
1087           /**
1088            * Given a downloader, sets the other repos raw metadata
1089            * path as cache paths for the fetcher, so if another
1090            * repo has the same file, it will not download it
1091            * but copy it from the other repository
1092            */
1093           for_( it, repoBegin(), repoEnd() )
1094           {
1095             Pathname cachepath(rawcache_path_for_repoinfo( _options, *it ));
1096             if ( PathInfo(cachepath).isExist() )
1097               downloader_ptr->addCachePath(cachepath);
1098           }
1099
1100           downloader_ptr->download( media, tmpdir.path() );
1101         }
1102         else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
1103         {
1104           MediaMounter media( url );
1105           RepoStatus newstatus = RepoStatus( media.getPathName( info.path() ) );        // dir status
1106
1107           Pathname productpath( tmpdir.path() / info.path() );
1108           filesystem::assert_dir( productpath );
1109           newstatus.saveToCookieFile( productpath/"cookie" );
1110         }
1111         else
1112         {
1113           ZYPP_THROW(RepoUnknownTypeException( info ));
1114         }
1115
1116         // ok we have the metadata, now exchange
1117         // the contents
1118         filesystem::exchange( tmpdir.path(), mediarootpath );
1119         reposManip();   // remember to trigger appdata refresh
1120
1121         // we are done.
1122         return;
1123       }
1124       catch ( const Exception &e )
1125       {
1126         ZYPP_CAUGHT(e);
1127         ERR << "Trying another url..." << endl;
1128
1129         // remember the exception caught for the *first URL*
1130         // if all other URLs fail, the rexception will be thrown with the
1131         // cause of the problem of the first URL remembered
1132         if (it == info.baseUrlsBegin())
1133           rexception.remember(e);
1134       }
1135     } // for every url
1136     ERR << "No more urls..." << endl;
1137     ZYPP_THROW(rexception);
1138   }
1139
1140   ////////////////////////////////////////////////////////////////////////////
1141
1142   void RepoManager::Impl::cleanMetadata( const RepoInfo & info, const ProgressData::ReceiverFnc & progressfnc )
1143   {
1144     ProgressData progress(100);
1145     progress.sendTo(progressfnc);
1146
1147     filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_options, info));
1148     progress.toMax();
1149   }
1150
1151
1152   void RepoManager::Impl::cleanPackages( const RepoInfo & info, const ProgressData::ReceiverFnc & progressfnc )
1153   {
1154     ProgressData progress(100);
1155     progress.sendTo(progressfnc);
1156
1157     filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_options, info));
1158     progress.toMax();
1159   }
1160
1161
1162   void RepoManager::Impl::buildCache( const RepoInfo & info, CacheBuildPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
1163   {
1164     assert_alias(info);
1165     Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
1166     Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
1167
1168     if( filesystem::assert_dir(_options.repoCachePath) )
1169     {
1170       Exception ex(str::form( _("Can't create %s"), _options.repoCachePath.c_str()) );
1171       ZYPP_THROW(ex);
1172     }
1173     RepoStatus raw_metadata_status = metadataStatus(info);
1174     if ( raw_metadata_status.empty() )
1175     {
1176        /* if there is no cache at this point, we refresh the raw
1177           in case this is the first time - if it's !autorefresh,
1178           we may still refresh */
1179       refreshMetadata(info, RefreshIfNeeded, progressrcv );
1180       raw_metadata_status = metadataStatus(info);
1181     }
1182
1183     bool needs_cleaning = false;
1184     if ( isCached( info ) )
1185     {
1186       MIL << info.alias() << " is already cached." << endl;
1187       RepoStatus cache_status = cacheStatus(info);
1188
1189       if ( cache_status == raw_metadata_status )
1190       {
1191         MIL << info.alias() << " cache is up to date with metadata." << endl;
1192         if ( policy == BuildIfNeeded )
1193         {
1194           // On the fly add missing solv.idx files for bash completion.
1195           const Pathname & base = solv_path_for_repoinfo( _options, info);
1196           if ( ! PathInfo(base/"solv.idx").isExist() )
1197             sat::updateSolvFileIndex( base/"solv" );
1198
1199           return;
1200         }
1201         else {
1202           MIL << info.alias() << " cache rebuild is forced" << endl;
1203         }
1204       }
1205
1206       needs_cleaning = true;
1207     }
1208
1209     ProgressData progress(100);
1210     callback::SendReport<ProgressReport> report;
1211     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1212     progress.name(str::form(_("Building repository '%s' cache"), info.label().c_str()));
1213     progress.toMin();
1214
1215     if (needs_cleaning)
1216     {
1217       cleanCache(info);
1218     }
1219
1220     MIL << info.alias() << " building cache..." << info.type() << endl;
1221
1222     Pathname base = solv_path_for_repoinfo( _options, info);
1223
1224     if( filesystem::assert_dir(base) )
1225     {
1226       Exception ex(str::form( _("Can't create %s"), base.c_str()) );
1227       ZYPP_THROW(ex);
1228     }
1229
1230     if( ! PathInfo(base).userMayW() )
1231     {
1232       Exception ex(str::form( _("Can't create cache at %s - no writing permissions."), base.c_str()) );
1233       ZYPP_THROW(ex);
1234     }
1235     Pathname solvfile = base / "solv";
1236
1237     // do we have type?
1238     repo::RepoType repokind = info.type();
1239
1240     // if the type is unknown, try probing.
1241     switch ( repokind.toEnum() )
1242     {
1243       case RepoType::NONE_e:
1244         // unknown, probe the local metadata
1245         repokind = probeCache( productdatapath );
1246       break;
1247       default:
1248       break;
1249     }
1250
1251     MIL << "repo type is " << repokind << endl;
1252
1253     switch ( repokind.toEnum() )
1254     {
1255       case RepoType::RPMMD_e :
1256       case RepoType::YAST2_e :
1257       case RepoType::RPMPLAINDIR_e :
1258       {
1259         // Take care we unlink the solvfile on exception
1260         ManagedFile guard( solvfile, filesystem::unlink );
1261         scoped_ptr<MediaMounter> forPlainDirs;
1262
1263         ExternalProgram::Arguments cmd;
1264         cmd.push_back( "repo2solv.sh" );
1265         // repo2solv expects -o as 1st arg!
1266         cmd.push_back( "-o" );
1267         cmd.push_back( solvfile.asString() );
1268         cmd.push_back( "-X" );  // autogenerate pattern from pattern-package
1269
1270         if ( repokind == RepoType::RPMPLAINDIR )
1271         {
1272           forPlainDirs.reset( new MediaMounter( *info.baseUrlsBegin() ) );
1273           // recusive for plaindir as 2nd arg!
1274           cmd.push_back( "-R" );
1275           // FIXME this does only work form dir: URLs
1276           cmd.push_back( forPlainDirs->getPathName( info.path() ).c_str() );
1277         }
1278         else
1279           cmd.push_back( productdatapath.asString() );
1280
1281         ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
1282         std::string errdetail;
1283
1284         for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
1285           WAR << "  " << output;
1286           if ( errdetail.empty() ) {
1287             errdetail = prog.command();
1288             errdetail += '\n';
1289           }
1290           errdetail += output;
1291         }
1292
1293         int ret = prog.close();
1294         if ( ret != 0 )
1295         {
1296           RepoException ex(str::form( _("Failed to cache repo (%d)."), ret ));
1297           ex.remember( errdetail );
1298           ZYPP_THROW(ex);
1299         }
1300
1301         // We keep it.
1302         guard.resetDispose();
1303         sat::updateSolvFileIndex( solvfile );   // content digest for zypper bash completion
1304       }
1305       break;
1306       default:
1307         ZYPP_THROW(RepoUnknownTypeException( info, _("Unhandled repository type") ));
1308       break;
1309     }
1310     // update timestamp and checksum
1311     setCacheStatus(info, raw_metadata_status);
1312     MIL << "Commit cache.." << endl;
1313     progress.toMax();
1314   }
1315
1316   ////////////////////////////////////////////////////////////////////////////
1317
1318
1319   /** Probe the metadata type of a repository located at \c url.
1320    * Urls here may be rewritten by \ref MediaSetAccess to reflect the correct media number.
1321    *
1322    * \note Metadata in local cache directories must be probed using \ref probeCache as
1323    * a cache path must not be rewritten (bnc#946129)
1324    */
1325   repo::RepoType RepoManager::Impl::probe( const Url & url, const Pathname & path  ) const
1326   {
1327     MIL << "going to probe the repo type at " << url << " (" << path << ")" << endl;
1328
1329     if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName()/path ).isDir() )
1330     {
1331       // Handle non existing local directory in advance, as
1332       // MediaSetAccess does not support it.
1333       MIL << "Probed type NONE (not exists) at " << url << " (" << path << ")" << endl;
1334       return repo::RepoType::NONE;
1335     }
1336
1337     // prepare exception to be thrown if the type could not be determined
1338     // due to a media exception. We can't throw right away, because of some
1339     // problems with proxy servers returning an incorrect error
1340     // on ftp file-not-found(bnc #335906). Instead we'll check another types
1341     // before throwing.
1342
1343     // TranslatorExplanation '%s' is an URL
1344     RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
1345     bool gotMediaException = false;
1346     try
1347     {
1348       MediaSetAccess access(url);
1349       try
1350       {
1351         if ( access.doesFileExist(path/"/repodata/repomd.xml") )
1352         {
1353           MIL << "Probed type RPMMD at " << url << " (" << path << ")" << endl;
1354           return repo::RepoType::RPMMD;
1355         }
1356       }
1357       catch ( const media::MediaException &e )
1358       {
1359         ZYPP_CAUGHT(e);
1360         DBG << "problem checking for repodata/repomd.xml file" << endl;
1361         enew.remember(e);
1362         gotMediaException = true;
1363       }
1364
1365       try
1366       {
1367         if ( access.doesFileExist(path/"/content") )
1368         {
1369           MIL << "Probed type YAST2 at " << url << " (" << path << ")" << endl;
1370           return repo::RepoType::YAST2;
1371         }
1372       }
1373       catch ( const media::MediaException &e )
1374       {
1375         ZYPP_CAUGHT(e);
1376         DBG << "problem checking for content file" << endl;
1377         enew.remember(e);
1378         gotMediaException = true;
1379       }
1380
1381       // if it is a non-downloading URL denoting a directory
1382       if ( ! url.schemeIsDownloading() )
1383       {
1384         MediaMounter media( url );
1385         if ( PathInfo(media.getPathName()/path).isDir() )
1386         {
1387           // allow empty dirs for now
1388           MIL << "Probed type RPMPLAINDIR at " << url << " (" << path << ")" << endl;
1389           return repo::RepoType::RPMPLAINDIR;
1390         }
1391       }
1392     }
1393     catch ( const Exception &e )
1394     {
1395       ZYPP_CAUGHT(e);
1396       // TranslatorExplanation '%s' is an URL
1397       Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
1398       enew.remember(e);
1399       ZYPP_THROW(enew);
1400     }
1401
1402     if (gotMediaException)
1403       ZYPP_THROW(enew);
1404
1405     MIL << "Probed type NONE at " << url << " (" << path << ")" << endl;
1406     return repo::RepoType::NONE;
1407   }
1408
1409   /** Probe Metadata in a local cache directory
1410    *
1411    * \note Metadata in local cache directories must not be probed using \ref probe as
1412    * a cache path must not be rewritten (bnc#946129)
1413    */
1414   repo::RepoType RepoManager::Impl::probeCache( const Pathname & path_r ) const
1415   {
1416     MIL << "going to probe the cached repo at " << path_r << endl;
1417
1418     repo::RepoType ret = repo::RepoType::NONE;
1419
1420     if ( PathInfo(path_r/"/repodata/repomd.xml").isFile() )
1421     { ret = repo::RepoType::RPMMD; }
1422     else if ( PathInfo(path_r/"/content").isFile() )
1423     { ret = repo::RepoType::YAST2; }
1424     else if ( PathInfo(path_r).isDir() )
1425     { ret = repo::RepoType::RPMPLAINDIR; }
1426
1427     MIL << "Probed cached type " << ret << " at " << path_r << endl;
1428     return ret;
1429   }
1430
1431   ////////////////////////////////////////////////////////////////////////////
1432
1433   void RepoManager::Impl::cleanCacheDirGarbage( const ProgressData::ReceiverFnc & progressrcv )
1434   {
1435     MIL << "Going to clean up garbage in cache dirs" << endl;
1436
1437     ProgressData progress(300);
1438     progress.sendTo(progressrcv);
1439     progress.toMin();
1440
1441     std::list<Pathname> cachedirs;
1442     cachedirs.push_back(_options.repoRawCachePath);
1443     cachedirs.push_back(_options.repoPackagesCachePath);
1444     cachedirs.push_back(_options.repoSolvCachePath);
1445
1446     for_( dir, cachedirs.begin(), cachedirs.end() )
1447     {
1448       if ( PathInfo(*dir).isExist() )
1449       {
1450         std::list<Pathname> entries;
1451         if ( filesystem::readdir( entries, *dir, false ) != 0 )
1452           // TranslatorExplanation '%s' is a pathname
1453           ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir->c_str())));
1454
1455         unsigned sdircount   = entries.size();
1456         unsigned sdircurrent = 1;
1457         for_( subdir, entries.begin(), entries.end() )
1458         {
1459           // if it does not belong known repo, make it disappear
1460           bool found = false;
1461           for_( r, repoBegin(), repoEnd() )
1462             if ( subdir->basename() == r->escaped_alias() )
1463             { found = true; break; }
1464
1465           if ( ! found && ( Date::now()-PathInfo(*subdir).mtime() > Date::day ) )
1466             filesystem::recursive_rmdir( *subdir );
1467
1468           progress.set( progress.val() + sdircurrent * 100 / sdircount );
1469           ++sdircurrent;
1470         }
1471       }
1472       else
1473         progress.set( progress.val() + 100 );
1474     }
1475     progress.toMax();
1476   }
1477
1478   ////////////////////////////////////////////////////////////////////////////
1479
1480   void RepoManager::Impl::cleanCache( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1481   {
1482     ProgressData progress(100);
1483     progress.sendTo(progressrcv);
1484     progress.toMin();
1485
1486     MIL << "Removing raw metadata cache for " << info.alias() << endl;
1487     filesystem::recursive_rmdir(solv_path_for_repoinfo(_options, info));
1488
1489     progress.toMax();
1490   }
1491
1492   ////////////////////////////////////////////////////////////////////////////
1493
1494   void RepoManager::Impl::loadFromCache( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1495   {
1496     assert_alias(info);
1497     Pathname solvfile = solv_path_for_repoinfo(_options, info) / "solv";
1498
1499     if ( ! PathInfo(solvfile).isExist() )
1500       ZYPP_THROW(RepoNotCachedException(info));
1501
1502     sat::Pool::instance().reposErase( info.alias() );
1503     try
1504     {
1505       Repository repo = sat::Pool::instance().addRepoSolv( solvfile, info );
1506       // test toolversion in order to rebuild solv file in case
1507       // it was written by an old libsolv-tool parser.
1508       //
1509       // Known version strings used:
1510       //  - <no string>
1511       //  - "1.0"
1512       //
1513       sat::LookupRepoAttr toolversion( sat::SolvAttr::repositoryToolVersion, repo );
1514       if ( toolversion.begin().asString().empty() )
1515       {
1516         repo.eraseFromPool();
1517         ZYPP_THROW(Exception("Solv-file was created by old parser."));
1518       }
1519       // else: up-to-date (or even newer).
1520     }
1521     catch ( const Exception & exp )
1522     {
1523       ZYPP_CAUGHT( exp );
1524       MIL << "Try to handle exception by rebuilding the solv-file" << endl;
1525       cleanCache( info, progressrcv );
1526       buildCache( info, BuildIfNeeded, progressrcv );
1527
1528       sat::Pool::instance().addRepoSolv( solvfile, info );
1529     }
1530   }
1531
1532   ////////////////////////////////////////////////////////////////////////////
1533
1534   void RepoManager::Impl::addRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1535   {
1536     assert_alias(info);
1537
1538     ProgressData progress(100);
1539     callback::SendReport<ProgressReport> report;
1540     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1541     progress.name(str::form(_("Adding repository '%s'"), info.label().c_str()));
1542     progress.toMin();
1543
1544     MIL << "Try adding repo " << info << endl;
1545
1546     RepoInfo tosave = info;
1547     if ( repos().find(tosave) != repos().end() )
1548       ZYPP_THROW(RepoAlreadyExistsException(info));
1549
1550     // check the first url for now
1551     if ( _options.probe )
1552     {
1553       DBG << "unknown repository type, probing" << endl;
1554
1555       RepoType probedtype;
1556       probedtype = probe( *tosave.baseUrlsBegin(), info.path() );
1557       if ( tosave.baseUrlsSize() > 0 )
1558       {
1559         if ( probedtype == RepoType::NONE )
1560           ZYPP_THROW(RepoUnknownTypeException(info));
1561         else
1562           tosave.setType(probedtype);
1563       }
1564     }
1565
1566     progress.set(50);
1567
1568     // assert the directory exists
1569     filesystem::assert_dir(_options.knownReposPath);
1570
1571     Pathname repofile = generateNonExistingName(
1572         _options.knownReposPath, generateFilename(tosave));
1573     // now we have a filename that does not exists
1574     MIL << "Saving repo in " << repofile << endl;
1575
1576     std::ofstream file(repofile.c_str());
1577     if (!file)
1578     {
1579       // TranslatorExplanation '%s' is a filename
1580       ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1581     }
1582
1583     tosave.dumpAsIniOn(file);
1584     tosave.setFilepath(repofile);
1585     tosave.setMetadataPath( metadataPath( tosave ) );
1586     tosave.setPackagesPath( packagesPath( tosave ) );
1587     {
1588       // We chould fix the API as we must injet those paths
1589       // into the repoinfo in order to keep it usable.
1590       RepoInfo & oinfo( const_cast<RepoInfo &>(info) );
1591       oinfo.setMetadataPath( metadataPath( tosave ) );
1592       oinfo.setPackagesPath( packagesPath( tosave ) );
1593     }
1594     reposManip().insert(tosave);
1595
1596     progress.set(90);
1597
1598     // check for credentials in Urls
1599     bool havePasswords = false;
1600     for_( urlit, tosave.baseUrlsBegin(), tosave.baseUrlsEnd() )
1601       if ( urlit->hasCredentialsInAuthority() )
1602       {
1603         havePasswords = true;
1604         break;
1605       }
1606     // save the credentials
1607     if ( havePasswords )
1608     {
1609       media::CredentialManager cm(
1610           media::CredManagerOptions(_options.rootDir) );
1611
1612       for_(urlit, tosave.baseUrlsBegin(), tosave.baseUrlsEnd())
1613         if (urlit->hasCredentialsInAuthority())
1614           //! \todo use a method calling UI callbacks to ask where to save creds?
1615           cm.saveInUser(media::AuthData(*urlit));
1616     }
1617
1618     HistoryLog().addRepository(tosave);
1619
1620     progress.toMax();
1621     MIL << "done" << endl;
1622   }
1623
1624
1625   void RepoManager::Impl::addRepositories( const Url & url, const ProgressData::ReceiverFnc & progressrcv )
1626   {
1627     std::list<RepoInfo> repos = readRepoFile(url);
1628     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1629           it != repos.end();
1630           ++it )
1631     {
1632       // look if the alias is in the known repos.
1633       for_ ( kit, repoBegin(), repoEnd() )
1634       {
1635         if ( (*it).alias() == (*kit).alias() )
1636         {
1637           ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1638           ZYPP_THROW(RepoAlreadyExistsException(*it));
1639         }
1640       }
1641     }
1642
1643     std::string filename = Pathname(url.getPathName()).basename();
1644
1645     if ( filename == Pathname() )
1646     {
1647       // TranslatorExplanation '%s' is an URL
1648       ZYPP_THROW(RepoException(str::form( _("Invalid repo file name at '%s'"), url.asString().c_str() )));
1649     }
1650
1651     // assert the directory exists
1652     filesystem::assert_dir(_options.knownReposPath);
1653
1654     Pathname repofile = generateNonExistingName(_options.knownReposPath, filename);
1655     // now we have a filename that does not exists
1656     MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1657
1658     std::ofstream file(repofile.c_str());
1659     if (!file)
1660     {
1661       // TranslatorExplanation '%s' is a filename
1662       ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1663     }
1664
1665     for ( std::list<RepoInfo>::iterator it = repos.begin();
1666           it != repos.end();
1667           ++it )
1668     {
1669       MIL << "Saving " << (*it).alias() << endl;
1670       it->setFilepath(repofile.asString());
1671       it->dumpAsIniOn(file);
1672       reposManip().insert(*it);
1673
1674       HistoryLog(_options.rootDir).addRepository(*it);
1675     }
1676
1677     MIL << "done" << endl;
1678   }
1679
1680   ////////////////////////////////////////////////////////////////////////////
1681
1682   void RepoManager::Impl::removeRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1683   {
1684     ProgressData progress;
1685     callback::SendReport<ProgressReport> report;
1686     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1687     progress.name(str::form(_("Removing repository '%s'"), info.label().c_str()));
1688
1689     MIL << "Going to delete repo " << info.alias() << endl;
1690
1691     for_( it, repoBegin(), repoEnd() )
1692     {
1693       // they can be the same only if the provided is empty, that means
1694       // the provided repo has no alias
1695       // then skip
1696       if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1697         continue;
1698
1699       // TODO match by url
1700
1701       // we have a matcing repository, now we need to know
1702       // where it does come from.
1703       RepoInfo todelete = *it;
1704       if (todelete.filepath().empty())
1705       {
1706         ZYPP_THROW(RepoException( todelete, _("Can't figure out where the repo is stored.") ));
1707       }
1708       else
1709       {
1710         // figure how many repos are there in the file:
1711         std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1712         if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1713         {
1714           // easy, only this one, just delete the file
1715           if ( filesystem::unlink(todelete.filepath()) != 0 )
1716           {
1717             // TranslatorExplanation '%s' is a filename
1718             ZYPP_THROW(RepoException( todelete, str::form( _("Can't delete '%s'"), todelete.filepath().c_str() )));
1719           }
1720           MIL << todelete.alias() << " successfully deleted." << endl;
1721         }
1722         else
1723         {
1724           // there are more repos in the same file
1725           // write them back except the deleted one.
1726           //TmpFile tmp;
1727           //std::ofstream file(tmp.path().c_str());
1728
1729           // assert the directory exists
1730           filesystem::assert_dir(todelete.filepath().dirname());
1731
1732           std::ofstream file(todelete.filepath().c_str());
1733           if (!file)
1734           {
1735             // TranslatorExplanation '%s' is a filename
1736             ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), todelete.filepath().c_str() )));
1737           }
1738           for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1739                 fit != filerepos.end();
1740                 ++fit )
1741           {
1742             if ( (*fit).alias() != todelete.alias() )
1743               (*fit).dumpAsIniOn(file);
1744           }
1745         }
1746
1747         CombinedProgressData cSubprogrcv(progress, 20);
1748         CombinedProgressData mSubprogrcv(progress, 40);
1749         CombinedProgressData pSubprogrcv(progress, 40);
1750         // now delete it from cache
1751         if ( isCached(todelete) )
1752           cleanCache( todelete, cSubprogrcv);
1753         // now delete metadata (#301037)
1754         cleanMetadata( todelete, mSubprogrcv );
1755         cleanPackages( todelete, pSubprogrcv );
1756         reposManip().erase(todelete);
1757         MIL << todelete.alias() << " successfully deleted." << endl;
1758         HistoryLog(_options.rootDir).removeRepository(todelete);
1759         return;
1760       } // else filepath is empty
1761
1762     }
1763     // should not be reached on a sucess workflow
1764     ZYPP_THROW(RepoNotFoundException(info));
1765   }
1766
1767   ////////////////////////////////////////////////////////////////////////////
1768
1769   void RepoManager::Impl::modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, const ProgressData::ReceiverFnc & progressrcv )
1770   {
1771     RepoInfo toedit = getRepositoryInfo(alias);
1772     RepoInfo newinfo( newinfo_r ); // need writable copy to upadte housekeeping data
1773
1774     // check if the new alias already exists when renaming the repo
1775     if ( alias != newinfo.alias() && hasRepo( newinfo.alias() ) )
1776     {
1777       ZYPP_THROW(RepoAlreadyExistsException(newinfo));
1778     }
1779
1780     if (toedit.filepath().empty())
1781     {
1782       ZYPP_THROW(RepoException( toedit, _("Can't figure out where the repo is stored.") ));
1783     }
1784     else
1785     {
1786       // figure how many repos are there in the file:
1787       std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1788
1789       // there are more repos in the same file
1790       // write them back except the deleted one.
1791       //TmpFile tmp;
1792       //std::ofstream file(tmp.path().c_str());
1793
1794       // assert the directory exists
1795       filesystem::assert_dir(toedit.filepath().dirname());
1796
1797       std::ofstream file(toedit.filepath().c_str());
1798       if (!file)
1799       {
1800         // TranslatorExplanation '%s' is a filename
1801         ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), toedit.filepath().c_str() )));
1802       }
1803       for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1804             fit != filerepos.end();
1805             ++fit )
1806       {
1807           // if the alias is different, dump the original
1808           // if it is the same, dump the provided one
1809           if ( (*fit).alias() != toedit.alias() )
1810             (*fit).dumpAsIniOn(file);
1811           else
1812             newinfo.dumpAsIniOn(file);
1813       }
1814
1815       if ( toedit.enabled() && !newinfo.enabled() )
1816       {
1817         // On the fly remove solv.idx files for bash completion if a repo gets disabled.
1818         const Pathname & solvidx = solv_path_for_repoinfo(_options, newinfo)/"solv.idx";
1819         if ( PathInfo(solvidx).isExist() )
1820           filesystem::unlink( solvidx );
1821       }
1822
1823       newinfo.setFilepath(toedit.filepath());
1824       reposManip().erase(toedit);
1825       reposManip().insert(newinfo);
1826       HistoryLog(_options.rootDir).modifyRepository(toedit, newinfo);
1827       MIL << "repo " << alias << " modified" << endl;
1828     }
1829   }
1830
1831   ////////////////////////////////////////////////////////////////////////////
1832
1833   RepoInfo RepoManager::Impl::getRepositoryInfo( const std::string & alias, const ProgressData::ReceiverFnc & progressrcv )
1834   {
1835     RepoConstIterator it( findAlias( alias, repos() ) );
1836     if ( it != repos().end() )
1837       return *it;
1838     RepoInfo info;
1839     info.setAlias( alias );
1840     ZYPP_THROW( RepoNotFoundException(info) );
1841   }
1842
1843
1844   RepoInfo RepoManager::Impl::getRepositoryInfo( const Url & url, const url::ViewOption & urlview, const ProgressData::ReceiverFnc & progressrcv )
1845   {
1846     for_( it, repoBegin(), repoEnd() )
1847     {
1848       for_( urlit, (*it).baseUrlsBegin(), (*it).baseUrlsEnd() )
1849       {
1850         if ( (*urlit).asString(urlview) == url.asString(urlview) )
1851           return *it;
1852       }
1853     }
1854     RepoInfo info;
1855     info.setBaseUrl( url );
1856     ZYPP_THROW( RepoNotFoundException(info) );
1857   }
1858
1859   ////////////////////////////////////////////////////////////////////////////
1860   //
1861   // Services
1862   //
1863   ////////////////////////////////////////////////////////////////////////////
1864
1865   void RepoManager::Impl::addService( const ServiceInfo & service )
1866   {
1867     assert_alias( service );
1868
1869     // check if service already exists
1870     if ( hasService( service.alias() ) )
1871       ZYPP_THROW( ServiceAlreadyExistsException( service ) );
1872
1873     // Writable ServiceInfo is needed to save the location
1874     // of the .service file. Finaly insert into the service list.
1875     ServiceInfo toSave( service );
1876     saveService( toSave );
1877     _services.insert( toSave );
1878
1879     // check for credentials in Url (username:password, not ?credentials param)
1880     if ( toSave.url().hasCredentialsInAuthority() )
1881     {
1882       media::CredentialManager cm(
1883           media::CredManagerOptions(_options.rootDir) );
1884
1885       //! \todo use a method calling UI callbacks to ask where to save creds?
1886       cm.saveInUser(media::AuthData(toSave.url()));
1887     }
1888
1889     MIL << "added service " << toSave.alias() << endl;
1890   }
1891
1892   ////////////////////////////////////////////////////////////////////////////
1893
1894   void RepoManager::Impl::removeService( const std::string & alias )
1895   {
1896     MIL << "Going to delete service " << alias << endl;
1897
1898     const ServiceInfo & service = getService( alias );
1899
1900     Pathname location = service.filepath();
1901     if( location.empty() )
1902     {
1903       ZYPP_THROW(ServiceException( service, _("Can't figure out where the service is stored.") ));
1904     }
1905
1906     ServiceSet tmpSet;
1907     parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
1908
1909     // only one service definition in the file
1910     if ( tmpSet.size() == 1 )
1911     {
1912       if ( filesystem::unlink(location) != 0 )
1913       {
1914         // TranslatorExplanation '%s' is a filename
1915         ZYPP_THROW(ServiceException( service, str::form( _("Can't delete '%s'"), location.c_str() ) ));
1916       }
1917       MIL << alias << " successfully deleted." << endl;
1918     }
1919     else
1920     {
1921       filesystem::assert_dir(location.dirname());
1922
1923       std::ofstream file(location.c_str());
1924       if( !file )
1925       {
1926         // TranslatorExplanation '%s' is a filename
1927         ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), location.c_str() )));
1928       }
1929
1930       for_(it, tmpSet.begin(), tmpSet.end())
1931       {
1932         if( it->alias() != alias )
1933           it->dumpAsIniOn(file);
1934       }
1935
1936       MIL << alias << " successfully deleted from file " << location <<  endl;
1937     }
1938
1939     // now remove all repositories added by this service
1940     RepoCollector rcollector;
1941     getRepositoriesInService( alias,
1942                               boost::make_function_output_iterator( bind( &RepoCollector::collect, &rcollector, _1 ) ) );
1943     // cannot do this directly in getRepositoriesInService - would invalidate iterators
1944     for_(rit, rcollector.repos.begin(), rcollector.repos.end())
1945       removeRepository(*rit);
1946   }
1947
1948   ////////////////////////////////////////////////////////////////////////////
1949
1950   void RepoManager::Impl::refreshServices( const RefreshServiceOptions & options_r )
1951   {
1952     // copy the set of services since refreshService
1953     // can eventually invalidate the iterator
1954     ServiceSet services( serviceBegin(), serviceEnd() );
1955     for_( it, services.begin(), services.end() )
1956     {
1957       if ( !it->enabled() )
1958         continue;
1959
1960       try {
1961         refreshService(*it, options_r);
1962       }
1963       catch ( const repo::ServicePluginInformalException & e )
1964       { ;/* ignore ServicePluginInformalException */ }
1965     }
1966   }
1967
1968   void RepoManager::Impl::refreshService( const std::string & alias, const RefreshServiceOptions & options_r )
1969   {
1970     ServiceInfo service( getService( alias ) );
1971     assert_alias( service );
1972     assert_url( service );
1973     MIL << "Going to refresh service '" << service.alias() <<  "', url: " << service.url() << ", opts: " << options_r << endl;
1974
1975     if ( service.ttl() && !options_r.testFlag( RefreshService_forceRefresh ) )
1976     {
1977       // Service defines a TTL; maybe we can re-use existing data without refresh.
1978       Date lrf = service.lrf();
1979       if ( lrf )
1980       {
1981         Date now( Date::now() );
1982         if ( lrf <= now )
1983         {
1984           if ( (lrf+=service.ttl()) > now ) // lrf+= !
1985           {
1986             MIL << "Skip: '" << service.alias() << "' metadata valid until " << lrf << endl;
1987             return;
1988           }
1989         }
1990         else
1991           WAR << "Force: '" << service.alias() << "' metadata last refresh in the future: " << lrf << endl;
1992       }
1993     }
1994
1995     // NOTE: It might be necessary to modify and rewrite the service info.
1996     // Either when probing the type, or when adjusting the repositories
1997     // enable/disable state.:
1998     bool serviceModified = false;
1999
2000     //! \todo add callbacks for apps (start, end, repo removed, repo added, repo changed)?
2001
2002     // if the type is unknown, try probing.
2003     if ( service.type() == repo::ServiceType::NONE )
2004     {
2005       repo::ServiceType type = probeService( service.url() );
2006       if ( type != ServiceType::NONE )
2007       {
2008         service.setProbedType( type ); // lazy init!
2009         serviceModified = true;
2010       }
2011     }
2012
2013     // get target distro identifier
2014     std::string servicesTargetDistro = _options.servicesTargetDistro;
2015     if ( servicesTargetDistro.empty() )
2016     {
2017       servicesTargetDistro = Target::targetDistribution( Pathname() );
2018     }
2019     DBG << "ServicesTargetDistro: " << servicesTargetDistro << endl;
2020
2021     // parse it
2022     Date::Duration origTtl = service.ttl();     // FIXME Ugly hack: const service.ttl modified when parsing
2023     RepoCollector collector(servicesTargetDistro);
2024     // FIXME Ugly hack: ServiceRepos may throw ServicePluginInformalException
2025     // which is actually a notification. Using an exception for this
2026     // instead of signal/callback is bad. Needs to be fixed here, in refreshServices()
2027     // and in zypper.
2028     std::pair<DefaultIntegral<bool,false>, repo::ServicePluginInformalException> uglyHack;
2029     try {
2030       ServiceRepos( service, bind( &RepoCollector::collect, &collector, _1 ) );
2031     }
2032     catch ( const repo::ServicePluginInformalException & e )
2033     {
2034       /* ignore ServicePluginInformalException and throw later */
2035       uglyHack.first = true;
2036       uglyHack.second = e;
2037     }
2038     if ( service.ttl() != origTtl )     // repoindex.xml changed ttl
2039     {
2040       if ( !service.ttl() )
2041         service.setLrf( Date() );       // don't need lrf when zero ttl
2042       serviceModified = true;
2043     }
2044     ////////////////////////////////////////////////////////////////////////////
2045     // On the fly remember the new repo states as defined the reopoindex.xml.
2046     // Move into ServiceInfo later.
2047     ServiceInfo::RepoStates newRepoStates;
2048
2049     // set service alias and base url for all collected repositories
2050     for_( it, collector.repos.begin(), collector.repos.end() )
2051     {
2052       // First of all: Prepend service alias:
2053       it->setAlias( str::form( "%s:%s", service.alias().c_str(), it->alias().c_str() ) );
2054       // set refrence to the parent service
2055       it->setService( service.alias() );
2056
2057       // remember the new parsed repo state
2058       newRepoStates[it->alias()] = *it;
2059
2060       // if the repo url was not set by the repoindex parser, set service's url
2061       Url url;
2062       if ( it->baseUrlsEmpty() )
2063         url = service.rawUrl();
2064       else
2065       {
2066         // service repo can contain only one URL now, so no need to iterate.
2067         url = it->rawUrl();     // raw!
2068       }
2069
2070       // libzypp currently has problem with separate url + path handling
2071       // so just append the path to the baseurl
2072       if ( !it->path().empty() )
2073       {
2074         Pathname path(url.getPathName());
2075         path /= it->path();
2076         url.setPathName( path.asString() );
2077         it->setPath("");
2078       }
2079
2080       // save the url
2081       it->setBaseUrl( url );
2082     }
2083
2084     ////////////////////////////////////////////////////////////////////////////
2085     // Now compare collected repos with the ones in the system...
2086     //
2087     RepoInfoList oldRepos;
2088     getRepositoriesInService( service.alias(), std::back_inserter( oldRepos ) );
2089
2090     ////////////////////////////////////////////////////////////////////////////
2091     // find old repositories to remove...
2092     for_( oldRepo, oldRepos.begin(), oldRepos.end() )
2093     {
2094       if ( ! foundAliasIn( oldRepo->alias(), collector.repos ) )
2095       {
2096         if ( oldRepo->enabled() )
2097         {
2098           // Currently enabled. If this was a user modification remember the state.
2099           const auto & last = service.repoStates().find( oldRepo->alias() );
2100           if ( last != service.repoStates().end() && ! last->second.enabled )
2101           {
2102             DBG << "Service removes user enabled repo " << oldRepo->alias() << endl;
2103             service.addRepoToEnable( oldRepo->alias() );
2104             serviceModified = true;
2105           }
2106           else
2107             DBG << "Service removes enabled repo " << oldRepo->alias() << endl;
2108         }
2109         else
2110           DBG << "Service removes disabled repo " << oldRepo->alias() << endl;
2111
2112         removeRepository( *oldRepo );
2113       }
2114     }
2115
2116     ////////////////////////////////////////////////////////////////////////////
2117     // create missing repositories and modify exising ones if needed...
2118     for_( it, collector.repos.begin(), collector.repos.end() )
2119     {
2120       // User explicitly requested the repo being enabled?
2121       // User explicitly requested the repo being disabled?
2122       // And hopefully not both ;) If so, enable wins.
2123
2124       TriBool toBeEnabled( indeterminate );     // indeterminate - follow the service request
2125       DBG << "Service request to " << (it->enabled()?"enable":"disable") << " service repo " << it->alias() << endl;
2126
2127       if ( options_r.testFlag( RefreshService_restoreStatus ) )
2128       {
2129         DBG << "Opt RefreshService_restoreStatus " << it->alias() << endl;
2130         // this overrides any pending request!
2131         // Remove from enable request list.
2132         // NOTE: repoToDisable is handled differently.
2133         //       It gets cleared on each refresh.
2134         service.delRepoToEnable( it->alias() );
2135         // toBeEnabled stays indeterminate!
2136       }
2137       else
2138       {
2139         if ( service.repoToEnableFind( it->alias() ) )
2140         {
2141           DBG << "User request to enable service repo " << it->alias() << endl;
2142           toBeEnabled = true;
2143           // Remove from enable request list.
2144           // NOTE: repoToDisable is handled differently.
2145           //       It gets cleared on each refresh.
2146           service.delRepoToEnable( it->alias() );
2147           serviceModified = true;
2148         }
2149         else if ( service.repoToDisableFind( it->alias() ) )
2150         {
2151           DBG << "User request to disable service repo " << it->alias() << endl;
2152           toBeEnabled = false;
2153         }
2154       }
2155
2156       RepoInfoList::iterator oldRepo( findAlias( it->alias(), oldRepos ) );
2157       if ( oldRepo == oldRepos.end() )
2158       {
2159         // Not found in oldRepos ==> a new repo to add
2160
2161         // Make sure the service repo is created with the appropriate enablement
2162         if ( ! indeterminate(toBeEnabled) )
2163           it->setEnabled( toBeEnabled );
2164
2165         DBG << "Service adds repo " << it->alias() << " " << (it->enabled()?"enabled":"disabled") << endl;
2166         addRepository( *it );
2167       }
2168       else
2169       {
2170         // ==> an exising repo to check
2171         bool oldRepoModified = false;
2172
2173         if ( indeterminate(toBeEnabled) )
2174         {
2175           // No user request: check for an old user modificaton otherwise follow service request.
2176           // NOTE: Assert toBeEnabled is boolean afterwards!
2177           if ( oldRepo->enabled() == it->enabled() )
2178             toBeEnabled = it->enabled();        // service requests no change to the system
2179           else if (options_r.testFlag( RefreshService_restoreStatus ) )
2180           {
2181             toBeEnabled = it->enabled();        // RefreshService_restoreStatus forced
2182             DBG << "Opt RefreshService_restoreStatus " << it->alias() <<  " forces " << (toBeEnabled?"enabled":"disabled") << endl;
2183           }
2184           else
2185           {
2186             const auto & last = service.repoStates().find( oldRepo->alias() );
2187             if ( last == service.repoStates().end() || last->second.enabled != it->enabled() )
2188               toBeEnabled = it->enabled();      // service request has changed since last refresh -> follow
2189             else
2190             {
2191               toBeEnabled = oldRepo->enabled(); // service request unchaned since last refresh -> keep user modification
2192               DBG << "User modified service repo " << it->alias() <<  " may stay " << (toBeEnabled?"enabled":"disabled") << endl;
2193             }
2194           }
2195         }
2196
2197         // changed enable?
2198         if ( toBeEnabled == oldRepo->enabled() )
2199         {
2200           DBG << "Service repo " << it->alias() << " stays " <<  (oldRepo->enabled()?"enabled":"disabled") << endl;
2201         }
2202         else if ( toBeEnabled )
2203         {
2204           DBG << "Service repo " << it->alias() << " gets enabled" << endl;
2205           oldRepo->setEnabled( true );
2206           oldRepoModified = true;
2207         }
2208         else
2209         {
2210           DBG << "Service repo " << it->alias() << " gets disabled" << endl;
2211           oldRepo->setEnabled( false );
2212           oldRepoModified = true;
2213         }
2214
2215         // all other attributes follow the service request:
2216
2217         // changed name (raw!)
2218         if ( oldRepo->rawName() != it->rawName() )
2219         {
2220           DBG << "Service repo " << it->alias() << " gets new NAME " << it->rawName() << endl;
2221           oldRepo->setName( it->rawName() );
2222           oldRepoModified = true;
2223         }
2224
2225         // changed autorefresh
2226         if ( oldRepo->autorefresh() != it->autorefresh() )
2227         {
2228           DBG << "Service repo " << it->alias() << " gets new AUTOREFRESH " << it->autorefresh() << endl;
2229           oldRepo->setAutorefresh( it->autorefresh() );
2230           oldRepoModified = true;
2231         }
2232
2233         // changed priority?
2234         if ( oldRepo->priority() != it->priority() )
2235         {
2236           DBG << "Service repo " << it->alias() << " gets new PRIORITY " << it->priority() << endl;
2237           oldRepo->setPriority( it->priority() );
2238           oldRepoModified = true;
2239         }
2240
2241         // changed url?
2242         // service repo can contain only one URL now, so no need to iterate.
2243         if ( oldRepo->rawUrl() != it->rawUrl() )
2244         {
2245           DBG << "Service repo " << it->alias() << " gets new URL " << it->rawUrl() << endl;
2246           oldRepo->setBaseUrl( it->rawUrl() );
2247           oldRepoModified = true;
2248         }
2249
2250         // save if modified:
2251         if ( oldRepoModified )
2252         {
2253           modifyRepository( oldRepo->alias(), *oldRepo );
2254         }
2255       }
2256     }
2257
2258     // Unlike reposToEnable, reposToDisable is always cleared after refresh.
2259     if ( ! service.reposToDisableEmpty() )
2260     {
2261       service.clearReposToDisable();
2262       serviceModified = true;
2263     }
2264
2265     // Remember original service request for next refresh
2266     if ( service.repoStates() != newRepoStates )
2267     {
2268       service.setRepoStates( std::move(newRepoStates) );
2269       serviceModified = true;
2270     }
2271
2272     ////////////////////////////////////////////////////////////////////////////
2273     // save service if modified: (unless a plugin service)
2274     if ( service.type() != ServiceType::PLUGIN )
2275     {
2276       if ( service.ttl() )
2277       {
2278         service.setLrf( Date::now() );  // remember last refresh
2279         serviceModified =  true;        // or use a cookie file
2280       }
2281
2282       if ( serviceModified )
2283       {
2284         // write out modified service file.
2285         modifyService( service.alias(), service );
2286       }
2287     }
2288
2289     if ( uglyHack.first )
2290     {
2291       throw( uglyHack.second ); // intentionally not ZYPP_THROW
2292     }
2293   }
2294
2295   ////////////////////////////////////////////////////////////////////////////
2296
2297   void RepoManager::Impl::modifyService( const std::string & oldAlias, const ServiceInfo & newService )
2298   {
2299     MIL << "Going to modify service " << oldAlias << endl;
2300
2301     // we need a writable copy to link it to the file where
2302     // it is saved if we modify it
2303     ServiceInfo service(newService);
2304
2305     if ( service.type() == ServiceType::PLUGIN )
2306     {
2307       ZYPP_THROW(ServicePluginImmutableException( service ));
2308     }
2309
2310     const ServiceInfo & oldService = getService(oldAlias);
2311
2312     Pathname location = oldService.filepath();
2313     if( location.empty() )
2314     {
2315       ZYPP_THROW(ServiceException( oldService, _("Can't figure out where the service is stored.") ));
2316     }
2317
2318     // remember: there may multiple services being defined in one file:
2319     ServiceSet tmpSet;
2320     parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
2321
2322     filesystem::assert_dir(location.dirname());
2323     std::ofstream file(location.c_str());
2324     for_(it, tmpSet.begin(), tmpSet.end())
2325     {
2326       if( *it != oldAlias )
2327         it->dumpAsIniOn(file);
2328     }
2329     service.dumpAsIniOn(file);
2330     file.close();
2331     service.setFilepath(location);
2332
2333     _services.erase(oldAlias);
2334     _services.insert(service);
2335
2336     // changed properties affecting also repositories
2337     if ( oldAlias != service.alias()                    // changed alias
2338       || oldService.enabled() != service.enabled() )    // changed enabled status
2339     {
2340       std::vector<RepoInfo> toModify;
2341       getRepositoriesInService(oldAlias, std::back_inserter(toModify));
2342       for_( it, toModify.begin(), toModify.end() )
2343       {
2344         if ( oldService.enabled() != service.enabled() )
2345         {
2346           if ( service.enabled() )
2347           {
2348             // reset to last refreshs state
2349             const auto & last = service.repoStates().find( it->alias() );
2350             if ( last != service.repoStates().end() )
2351               it->setEnabled( last->second.enabled );
2352           }
2353           else
2354             it->setEnabled( false );
2355         }
2356
2357         if ( oldAlias != service.alias() )
2358           it->setService(service.alias());
2359
2360         modifyRepository(it->alias(), *it);
2361       }
2362     }
2363
2364     //! \todo refresh the service automatically if url is changed?
2365   }
2366
2367   ////////////////////////////////////////////////////////////////////////////
2368
2369   repo::ServiceType RepoManager::Impl::probeService( const Url & url ) const
2370   {
2371     try
2372     {
2373       MediaSetAccess access(url);
2374       if ( access.doesFileExist("/repo/repoindex.xml") )
2375         return repo::ServiceType::RIS;
2376     }
2377     catch ( const media::MediaException &e )
2378     {
2379       ZYPP_CAUGHT(e);
2380       // TranslatorExplanation '%s' is an URL
2381       RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
2382       enew.remember(e);
2383       ZYPP_THROW(enew);
2384     }
2385     catch ( const Exception &e )
2386     {
2387       ZYPP_CAUGHT(e);
2388       // TranslatorExplanation '%s' is an URL
2389       Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
2390       enew.remember(e);
2391       ZYPP_THROW(enew);
2392     }
2393
2394     return repo::ServiceType::NONE;
2395   }
2396
2397   ///////////////////////////////////////////////////////////////////
2398   //
2399   //    CLASS NAME : RepoManager
2400   //
2401   ///////////////////////////////////////////////////////////////////
2402
2403   RepoManager::RepoManager( const RepoManagerOptions & opt )
2404   : _pimpl( new Impl(opt) )
2405   {}
2406
2407   RepoManager::~RepoManager()
2408   {}
2409
2410   bool RepoManager::repoEmpty() const
2411   { return _pimpl->repoEmpty(); }
2412
2413   RepoManager::RepoSizeType RepoManager::repoSize() const
2414   { return _pimpl->repoSize(); }
2415
2416   RepoManager::RepoConstIterator RepoManager::repoBegin() const
2417   { return _pimpl->repoBegin(); }
2418
2419   RepoManager::RepoConstIterator RepoManager::repoEnd() const
2420   { return _pimpl->repoEnd(); }
2421
2422   RepoInfo RepoManager::getRepo( const std::string & alias ) const
2423   { return _pimpl->getRepo( alias ); }
2424
2425   bool RepoManager::hasRepo( const std::string & alias ) const
2426   { return _pimpl->hasRepo( alias ); }
2427
2428   std::string RepoManager::makeStupidAlias( const Url & url_r )
2429   {
2430     std::string ret( url_r.getScheme() );
2431     if ( ret.empty() )
2432       ret = "repo-";
2433     else
2434       ret += "-";
2435
2436     std::string host( url_r.getHost() );
2437     if ( ! host.empty() )
2438     {
2439       ret += host;
2440       ret += "-";
2441     }
2442
2443     static Date::ValueType serial = Date::now();
2444     ret += Digest::digest( Digest::sha1(), str::hexstring( ++serial ) +url_r.asCompleteString() ).substr(0,8);
2445     return ret;
2446   }
2447
2448   RepoStatus RepoManager::metadataStatus( const RepoInfo & info ) const
2449   { return _pimpl->metadataStatus( info ); }
2450
2451   RepoManager::RefreshCheckStatus RepoManager::checkIfToRefreshMetadata( const RepoInfo &info, const Url &url, RawMetadataRefreshPolicy policy )
2452   { return _pimpl->checkIfToRefreshMetadata( info, url, policy ); }
2453
2454   Pathname RepoManager::metadataPath( const RepoInfo &info ) const
2455   { return _pimpl->metadataPath( info ); }
2456
2457   Pathname RepoManager::packagesPath( const RepoInfo &info ) const
2458   { return _pimpl->packagesPath( info ); }
2459
2460   void RepoManager::refreshMetadata( const RepoInfo &info, RawMetadataRefreshPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
2461   { return _pimpl->refreshMetadata( info, policy, progressrcv ); }
2462
2463   void RepoManager::cleanMetadata( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2464   { return _pimpl->cleanMetadata( info, progressrcv ); }
2465
2466   void RepoManager::cleanPackages( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2467   { return _pimpl->cleanPackages( info, progressrcv ); }
2468
2469   RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
2470   { return _pimpl->cacheStatus( info ); }
2471
2472   void RepoManager::buildCache( const RepoInfo &info, CacheBuildPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
2473   { return _pimpl->buildCache( info, policy, progressrcv ); }
2474
2475   void RepoManager::cleanCache( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2476   { return _pimpl->cleanCache( info, progressrcv ); }
2477
2478   bool RepoManager::isCached( const RepoInfo &info ) const
2479   { return _pimpl->isCached( info ); }
2480
2481   void RepoManager::loadFromCache( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2482   { return _pimpl->loadFromCache( info, progressrcv ); }
2483
2484   void RepoManager::cleanCacheDirGarbage( const ProgressData::ReceiverFnc & progressrcv )
2485   { return _pimpl->cleanCacheDirGarbage( progressrcv ); }
2486
2487   repo::RepoType RepoManager::probe( const Url & url, const Pathname & path ) const
2488   { return _pimpl->probe( url, path ); }
2489
2490   repo::RepoType RepoManager::probe( const Url & url ) const
2491   { return _pimpl->probe( url ); }
2492
2493   void RepoManager::addRepository( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2494   { return _pimpl->addRepository( info, progressrcv ); }
2495
2496   void RepoManager::addRepositories( const Url &url, const ProgressData::ReceiverFnc & progressrcv )
2497   { return _pimpl->addRepositories( url, progressrcv ); }
2498
2499   void RepoManager::removeRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
2500   { return _pimpl->removeRepository( info, progressrcv ); }
2501
2502   void RepoManager::modifyRepository( const std::string &alias, const RepoInfo & newinfo, const ProgressData::ReceiverFnc & progressrcv )
2503   { return _pimpl->modifyRepository( alias, newinfo, progressrcv ); }
2504
2505   RepoInfo RepoManager::getRepositoryInfo( const std::string &alias, const ProgressData::ReceiverFnc & progressrcv )
2506   { return _pimpl->getRepositoryInfo( alias, progressrcv ); }
2507
2508   RepoInfo RepoManager::getRepositoryInfo( const Url & url, const url::ViewOption & urlview, const ProgressData::ReceiverFnc & progressrcv )
2509   { return _pimpl->getRepositoryInfo( url, urlview, progressrcv ); }
2510
2511   bool RepoManager::serviceEmpty() const
2512   { return _pimpl->serviceEmpty(); }
2513
2514   RepoManager::ServiceSizeType RepoManager::serviceSize() const
2515   { return _pimpl->serviceSize(); }
2516
2517   RepoManager::ServiceConstIterator RepoManager::serviceBegin() const
2518   { return _pimpl->serviceBegin(); }
2519
2520   RepoManager::ServiceConstIterator RepoManager::serviceEnd() const
2521   { return _pimpl->serviceEnd(); }
2522
2523   ServiceInfo RepoManager::getService( const std::string & alias ) const
2524   { return _pimpl->getService( alias ); }
2525
2526   bool RepoManager::hasService( const std::string & alias ) const
2527   { return _pimpl->hasService( alias ); }
2528
2529   repo::ServiceType RepoManager::probeService( const Url &url ) const
2530   { return _pimpl->probeService( url ); }
2531
2532   void RepoManager::addService( const std::string & alias, const Url& url )
2533   { return _pimpl->addService( alias, url ); }
2534
2535   void RepoManager::addService( const ServiceInfo & service )
2536   { return _pimpl->addService( service ); }
2537
2538   void RepoManager::removeService( const std::string & alias )
2539   { return _pimpl->removeService( alias ); }
2540
2541   void RepoManager::removeService( const ServiceInfo & service )
2542   { return _pimpl->removeService( service ); }
2543
2544   void RepoManager::refreshServices( const RefreshServiceOptions & options_r )
2545   { return _pimpl->refreshServices( options_r ); }
2546
2547   void RepoManager::refreshService( const std::string & alias, const RefreshServiceOptions & options_r )
2548   { return _pimpl->refreshService( alias, options_r ); }
2549
2550   void RepoManager::refreshService( const ServiceInfo & service, const RefreshServiceOptions & options_r )
2551   { return _pimpl->refreshService( service, options_r ); }
2552
2553   void RepoManager::modifyService( const std::string & oldAlias, const ServiceInfo & service )
2554   { return _pimpl->modifyService( oldAlias, service ); }
2555
2556   ////////////////////////////////////////////////////////////////////////////
2557
2558   std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
2559   { return str << *obj._pimpl; }
2560
2561   /////////////////////////////////////////////////////////////////
2562 } // namespace zypp
2563 ///////////////////////////////////////////////////////////////////