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