add mkdir for services and modifyServices, which is used during refresh
[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 #include "zypp/base/InputStream.h"
21 #include "zypp/base/Logger.h"
22 #include "zypp/base/Gettext.h"
23 #include "zypp/base/Function.h"
24 #include "zypp/base/Regex.h"
25 #include "zypp/PathInfo.h"
26 #include "zypp/TmpPath.h"
27 #include "zypp/Service.h"
28
29 #include "zypp/repo/RepoException.h"
30 #include "zypp/RepoManager.h"
31
32 #include "zypp/media/MediaManager.h"
33 #include "zypp/MediaSetAccess.h"
34 #include "zypp/ExternalProgram.h"
35 #include "zypp/ManagedFile.h"
36
37 #include "zypp/parser/RepoFileReader.h"
38 #include "zypp/parser/ServiceFileReader.h"
39 #include "zypp/repo/yum/Downloader.h"
40 #include "zypp/parser/yum/RepoParser.h"
41 #include "zypp/repo/susetags/Downloader.h"
42 #include "zypp/parser/susetags/RepoParser.h"
43 #include "zypp/parser/plaindir/RepoParser.h"
44
45 #include "zypp/ZYppCallbacks.h"
46
47 #include "sat/Pool.h"
48 #include "satsolver/pool.h"
49 #include "satsolver/repo.h"
50 #include "satsolver/repo_solv.h"
51
52 using namespace std;
53 using namespace zypp;
54 using namespace zypp::repo;
55 using namespace zypp::filesystem;
56
57 using namespace zypp::repo;
58
59 ///////////////////////////////////////////////////////////////////
60 namespace zypp
61 { /////////////////////////////////////////////////////////////////
62
63   ///////////////////////////////////////////////////////////////////
64   //
65   //    CLASS NAME : RepoManagerOptions
66   //
67   ///////////////////////////////////////////////////////////////////
68
69   RepoManagerOptions::RepoManagerOptions( const Pathname & root_r )
70   {
71     repoCachePath         = Pathname::assertprefix( root_r, ZConfig::instance().repoCachePath() );
72     repoRawCachePath      = Pathname::assertprefix( root_r, ZConfig::instance().repoMetadataPath() );
73     repoSolvCachePath     = Pathname::assertprefix( root_r, ZConfig::instance().repoSolvfilesPath() );
74     repoPackagesCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoPackagesPath() );
75     knownReposPath        = Pathname::assertprefix( root_r, ZConfig::instance().knownReposPath() );
76     knownServicesPath     = Pathname::assertprefix( root_r, ZConfig::instance().knownServicesPath() );
77     probe                 = ZConfig::instance().repo_add_probe();
78   }
79
80   RepoManagerOptions RepoManagerOptions::makeTestSetup( const Pathname & root_r )
81   {
82     RepoManagerOptions ret;
83     ret.repoCachePath         = root_r;
84     ret.repoRawCachePath      = root_r/"raw";
85     ret.repoSolvCachePath     = root_r/"solv";
86     ret.repoPackagesCachePath = root_r/"packages";
87     ret.knownReposPath        = root_r/"repos.d";
88     ret.knownServicesPath        = root_r/"services.d";
89     return ret;
90   }
91
92   ////////////////////////////////////////////////////////////////////////////
93
94   /**
95     * \short Simple callback to collect the results
96     *
97     * Classes like RepoFileParser call the callback
98     * once per each repo in a file.
99     *
100     * Passing this functor as callback, you can collect
101     * all resuls at the end, without dealing with async
102     * code.
103     */
104     struct RepoCollector
105     {
106       RepoCollector()
107       {}
108
109       ~RepoCollector()
110       {}
111
112       bool collect( const RepoInfo &repo )
113       {
114         repos.push_back(repo);
115         return true;
116       }
117
118       RepoInfoList repos;
119     };
120
121   ////////////////////////////////////////////////////////////////////////////
122
123   /**
124    * Reads RepoInfo's from a repo file.
125    *
126    * \param file pathname of the file to read.
127    */
128   static std::list<RepoInfo> repositories_in_file( const Pathname & file )
129   {
130     MIL << "repo file: " << file << endl;
131     RepoCollector collector;
132     parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
133     return collector.repos;
134   }
135
136   ////////////////////////////////////////////////////////////////////////////
137
138   std::list<RepoInfo> readRepoFile(const Url & repo_file)
139    {
140      // no interface to download a specific file, using workaround:
141      //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
142      Url url(repo_file);
143      Pathname path(url.getPathName());
144      url.setPathName ("/");
145      MediaSetAccess access(url);
146      Pathname local = access.provideFile(path);
147
148      DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
149
150      return repositories_in_file(local);
151    }
152
153   ////////////////////////////////////////////////////////////////////////////
154
155   /**
156    * \short List of RepoInfo's from a directory
157    *
158    * Goes trough every file ending with ".repo" in a directory and adds all
159    * RepoInfo's contained in that file.
160    *
161    * \param dir pathname of the directory to read.
162    */
163   static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
164   {
165     MIL << "directory " << dir << endl;
166     list<RepoInfo> repos;
167     list<Pathname> entries;
168     if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
169       ZYPP_THROW(Exception("failed to read directory"));
170
171     str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
172     for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
173     {
174       if (str::regex_match(it->extension(), allowedRepoExt))
175       {
176         list<RepoInfo> tmp = repositories_in_file( *it );
177         repos.insert( repos.end(), tmp.begin(), tmp.end() );
178
179         //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
180         //MIL << "ok" << endl;
181       }
182     }
183     return repos;
184   }
185
186   ////////////////////////////////////////////////////////////////////////////
187
188   static void assert_alias( const RepoInfo &info )
189   {
190     if (info.alias().empty())
191         ZYPP_THROW(RepoNoAliasException());
192   }
193
194   ////////////////////////////////////////////////////////////////////////////
195
196   static void assert_urls( const RepoInfo &info )
197   {
198     if (info.baseUrlsEmpty())
199         ZYPP_THROW(RepoNoUrlException());
200   }
201
202   ////////////////////////////////////////////////////////////////////////////
203
204   /**
205    * \short Calculates the raw cache path for a repository
206    */
207   static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
208   {
209     assert_alias(info);
210     return opt.repoRawCachePath / info.escaped_alias();
211   }
212
213   /**
214    * \short Calculates the packages cache path for a repository
215    */
216   static Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
217   {
218     assert_alias(info);
219     return opt.repoPackagesCachePath / info.escaped_alias();
220   }
221
222   static Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info)
223   {
224     assert_alias(info);
225     return opt.repoSolvCachePath / info.escaped_alias();
226   }
227
228   ///////////////////////////////////////////////////////////////////
229   //
230   //    CLASS NAME : RepoManager::Impl
231   //
232   ///////////////////////////////////////////////////////////////////
233
234   /**
235    * \short RepoManager implementation.
236    */
237   struct RepoManager::Impl
238   {
239     Impl( const RepoManagerOptions &opt )
240       : options(opt)
241     {
242
243     }
244
245     Impl()
246     {
247
248     }
249
250     RepoManagerOptions options;
251
252     map<string, RepoInfo> _repoinfos;
253     
254     ServiceSet services;
255
256   public:
257     /** Offer default Impl. */
258     static shared_ptr<Impl> nullimpl()
259     {
260       static shared_ptr<Impl> _nullimpl( new Impl );
261       return _nullimpl;
262     }
263
264     void saveService( const Service& service );
265   
266     Pathname generateNonExistingName( const Pathname &dir,
267                                          const std::string &basefilename ) const;
268   
269     std::string generateFilename( const RepoInfo &info ) const;
270     std::string generateFilename( const Service &info ) const;
271
272     struct ServiceCollector {
273       ServiceCollector(ServiceSet& services_) : services(services_) {}
274       bool collect(Service service) { services.insert(service); return true; }
275       private:
276         ServiceSet& services;
277     };
278
279     void knownServices();
280
281   private:
282     friend Impl * rwcowClone<Impl>( const Impl * rhs );
283     /** clone for RWCOW_pointer */
284     Impl * clone() const
285     { return new Impl( *this ); }
286   };
287
288   ///////////////////////////////////////////////////////////////////
289
290   /** \relates RepoManager::Impl Stream output */
291   inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
292   {
293     return str << "RepoManager::Impl";
294   }
295
296   ///////////////////////////////////////////////////////////////////
297   //
298   //    CLASS NAME : RepoManager
299   //
300   ///////////////////////////////////////////////////////////////////
301
302   RepoManager::RepoManager( const RepoManagerOptions &opt )
303   : _pimpl( new Impl(opt) )
304   {}
305
306   ////////////////////////////////////////////////////////////////////////////
307
308   RepoManager::~RepoManager()
309   {}
310
311   ////////////////////////////////////////////////////////////////////////////
312
313   std::list<RepoInfo> RepoManager::knownRepositories() const
314   {
315     MIL << endl;
316
317     if ( PathInfo(_pimpl->options.knownReposPath).isExist() )
318     {
319       RepoInfoList repos = repositories_in_dir(_pimpl->options.knownReposPath);
320       for ( RepoInfoList::iterator it = repos.begin();
321             it != repos.end();
322             ++it )
323       {
324         // set the metadata path for the repo
325         Pathname metadata_path = rawcache_path_for_repoinfo(_pimpl->options, (*it));
326         (*it).setMetadataPath(metadata_path);
327
328         // set the downloaded packages path for the repo
329         Pathname packages_path = packagescache_path_for_repoinfo(_pimpl->options, (*it));
330         (*it).setPackagesPath(packages_path);
331       }
332       return repos;
333     }
334     else
335       return std::list<RepoInfo>();
336
337     MIL << endl;
338   }
339
340   ////////////////////////////////////////////////////////////////////////////
341
342   Pathname RepoManager::metadataPath( const RepoInfo &info ) const
343   {
344     return rawcache_path_for_repoinfo(_pimpl->options, info );
345   }
346
347   Pathname RepoManager::packagesPath( const RepoInfo &info ) const
348   {
349     return packagescache_path_for_repoinfo(_pimpl->options, info );
350   }
351
352   ////////////////////////////////////////////////////////////////////////////
353
354   RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
355   {
356     Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
357     RepoType repokind = info.type();
358     RepoStatus status;
359
360     switch ( repokind.toEnum() )
361     {
362       case RepoType::NONE_e:
363       // unknown, probe the local metadata
364         repokind = probe(rawpath.asUrl());
365       break;
366       default:
367       break;
368     }
369
370     switch ( repokind.toEnum() )
371     {
372       case RepoType::RPMMD_e :
373       {
374         status = RepoStatus( rawpath + "/repodata/repomd.xml");
375       }
376       break;
377
378       case RepoType::YAST2_e :
379       {
380         status = RepoStatus( rawpath + "/content") && (RepoStatus( rawpath + "/media.1/media"));
381       }
382       break;
383
384       case RepoType::RPMPLAINDIR_e :
385       {
386         if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
387           status = RepoStatus( rawpath + "/cookie");
388       }
389       break;
390
391       case RepoType::NONE_e :
392         // Return default RepoStatus in case of RepoType::NONE
393         // indicating it should be created?
394         // ZYPP_THROW(RepoUnknownTypeException());
395         break;
396     }
397     return status;
398   }
399
400   void RepoManager::touchIndexFile(const RepoInfo & info)
401   {
402     Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
403
404     RepoType repokind = info.type();
405     if ( repokind.toEnum() == RepoType::NONE_e )
406       // unknown, probe the local metadata
407       repokind = probe(rawpath.asUrl());
408     // if still unknown, just return
409     if (repokind == RepoType::NONE_e)
410       return;
411
412     Pathname p;
413     switch ( repokind.toEnum() )
414     {
415       case RepoType::RPMMD_e :
416         p = Pathname(rawpath + "/repodata/repomd.xml");
417         break;
418
419       case RepoType::YAST2_e :
420         p = Pathname(rawpath + "/content");
421         break;
422
423       case RepoType::RPMPLAINDIR_e :
424         p = Pathname(rawpath + "/cookie");
425         break;
426
427       case RepoType::NONE_e :
428       default:
429         break;
430     }
431
432     // touch the file, ignore error (they are logged anyway)
433     filesystem::touch(p);
434   }
435
436   RepoManager::RefreshCheckStatus RepoManager::checkIfToRefreshMetadata(
437                                               const RepoInfo &info,
438                                               const Url &url,
439                                               RawMetadataRefreshPolicy policy )
440   {
441     assert_alias(info);
442
443     RepoStatus oldstatus;
444     RepoStatus newstatus;
445
446     try
447     {
448       MIL << "Going to try to check whether refresh is needed for " << url << endl;
449
450       repo::RepoType repokind = info.type();
451
452       // if the type is unknown, try probing.
453       switch ( repokind.toEnum() )
454       {
455         case RepoType::NONE_e:
456           // unknown, probe it
457           repokind = probe(url);
458         break;
459         default:
460         break;
461       }
462
463       Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
464       filesystem::assert_dir(rawpath);
465       oldstatus = metadataStatus(info);
466
467       // now we've got the old (cached) status, we can decide repo.refresh.delay
468       if (policy != RefreshForced && policy != RefreshIfNeededIgnoreDelay)
469       {
470         // difference in seconds
471         double diff = difftime(
472           (Date::ValueType)Date::now(),
473           (Date::ValueType)oldstatus.timestamp()) / 60;
474
475         DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
476         DBG << "current time: " << (Date::ValueType)Date::now() << endl;
477         DBG << "last refresh = " << diff << " minutes ago" << endl;
478
479         if (diff < ZConfig::instance().repo_refresh_delay())
480         {
481           MIL << "Repository '" << info.alias()
482               << "' has been refreshed less than repo.refresh.delay ("
483               << ZConfig::instance().repo_refresh_delay()
484               << ") minutes ago. Advising to skip refresh" << endl;
485           return REPO_CHECK_DELAYED;
486         }
487       }
488
489       // create temp dir as sibling of rawpath
490       filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
491
492       if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
493            ( repokind.toEnum() == RepoType::YAST2_e ) )
494       {
495         MediaSetAccess media(url);
496         shared_ptr<repo::Downloader> downloader_ptr;
497
498         if ( repokind.toEnum() == RepoType::RPMMD_e )
499           downloader_ptr.reset(new yum::Downloader(info.path()));
500         else
501           downloader_ptr.reset( new susetags::Downloader(info.path()));
502
503         RepoStatus newstatus = downloader_ptr->status(media);
504         bool refresh = false;
505         if ( oldstatus.checksum() == newstatus.checksum() )
506         {
507           MIL << "repo has not changed" << endl;
508           if ( policy == RefreshForced )
509           {
510             MIL << "refresh set to forced" << endl;
511             refresh = true;
512           }
513         }
514         else
515         {
516           MIL << "repo has changed, going to refresh" << endl;
517           refresh = true;
518         }
519
520         if (!refresh)
521           touchIndexFile(info);
522
523         return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
524       }
525       else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
526       {
527         RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
528         bool refresh = false;
529         if ( oldstatus.checksum() == newstatus.checksum() )
530         {
531           MIL << "repo has not changed" << endl;
532           if ( policy == RefreshForced )
533           {
534             MIL << "refresh set to forced" << endl;
535             refresh = true;
536           }
537         }
538         else
539         {
540           MIL << "repo has changed, going to refresh" << endl;
541           refresh = true;
542         }
543
544         if (!refresh)
545           touchIndexFile(info);
546
547         return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
548       }
549       else
550       {
551         ZYPP_THROW(RepoUnknownTypeException(info));
552       }
553     }
554     catch ( const Exception &e )
555     {
556       ZYPP_CAUGHT(e);
557       ERR << "refresh check failed for " << url << endl;
558       ZYPP_RETHROW(e);
559     }
560
561     return REFRESH_NEEDED; // default
562   }
563
564   void RepoManager::refreshMetadata( const RepoInfo &info,
565                                      RawMetadataRefreshPolicy policy,
566                                      const ProgressData::ReceiverFnc & progress )
567   {
568     assert_alias(info);
569     assert_urls(info);
570
571     // we will throw this later if no URL checks out fine
572     RepoException rexception(_("Valid metadata not found at specified URL(s)"));
573
574     // try urls one by one
575     for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
576     {
577       try
578       {
579         Url url(*it);
580
581         // check whether to refresh metadata
582         // if the check fails for this url, it throws, so another url will be checked
583         if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
584           return;
585
586         MIL << "Going to refresh metadata from " << url << endl;
587
588         repo::RepoType repokind = info.type();
589
590         // if the type is unknown, try probing.
591         switch ( repokind.toEnum() )
592         {
593           case RepoType::NONE_e:
594             // unknown, probe it
595             repokind = probe(*it);
596
597             if (repokind.toEnum() != RepoType::NONE_e)
598             {
599               // Adjust the probed type in RepoInfo
600               info.setProbedType( repokind ); // lazy init!
601               //save probed type only for repos in system
602               std::list<RepoInfo> repos = knownRepositories();
603               for ( std::list<RepoInfo>::const_iterator it = repos.begin();
604                    it != repos.end(); ++it )
605               {
606                 if ( info.alias() == (*it).alias() )
607                 {
608                   RepoInfo modifiedrepo = info;
609                   modifiedrepo.setType(repokind);
610                   modifyRepository(info.alias(),modifiedrepo);
611                 }
612               }
613             }
614           break;
615           default:
616           break;
617         }
618
619         Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
620         filesystem::assert_dir(rawpath);
621
622         // create temp dir as sibling of rawpath
623         filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
624
625         if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
626              ( repokind.toEnum() == RepoType::YAST2_e ) )
627         {
628           MediaSetAccess media(url);
629           shared_ptr<repo::Downloader> downloader_ptr;
630
631           if ( repokind.toEnum() == RepoType::RPMMD_e )
632             downloader_ptr.reset(new yum::Downloader(info.path()));
633           else
634             downloader_ptr.reset( new susetags::Downloader(info.path()));
635
636           /**
637            * Given a downloader, sets the other repos raw metadata
638            * path as cache paths for the fetcher, so if another
639            * repo has the same file, it will not download it
640            * but copy it from the other repository
641            */
642           std::list<RepoInfo> repos = knownRepositories();
643           for ( std::list<RepoInfo>::const_iterator it = repos.begin();
644                 it != repos.end();
645                 ++it )
646           {
647             Pathname cachepath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
648             if ( PathInfo(cachepath).isExist() )
649               downloader_ptr->addCachePath(cachepath);
650           }
651
652           downloader_ptr->download( media, tmpdir.path());
653         }
654         else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
655         {
656           RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
657
658           std::ofstream file(( tmpdir.path() + "/cookie").c_str());
659           if (!file) {
660             ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
661           }
662           file << url << endl;
663           file << newstatus.checksum() << endl;
664
665           file.close();
666         }
667         else
668         {
669           ZYPP_THROW(RepoUnknownTypeException());
670         }
671
672         // ok we have the metadata, now exchange
673         // the contents
674
675         TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
676         filesystem::rename( rawpath, oldmetadata.path() );
677         // move the just downloaded there
678         filesystem::rename( tmpdir.path(), rawpath );
679
680         // we are done.
681         return;
682       }
683       catch ( const Exception &e )
684       {
685         ZYPP_CAUGHT(e);
686         ERR << "Trying another url..." << endl;
687
688         // remember the exception caught for the *first URL*
689         // if all other URLs fail, the rexception will be thrown with the
690         // cause of the problem of the first URL remembered
691         if (it == info.baseUrlsBegin())
692           rexception.remember(e);
693       }
694     } // for every url
695     ERR << "No more urls..." << endl;
696     ZYPP_THROW(rexception);
697   }
698
699   ////////////////////////////////////////////////////////////////////////////
700
701   void RepoManager::cleanMetadata( const RepoInfo &info,
702                                    const ProgressData::ReceiverFnc & progressfnc )
703   {
704     ProgressData progress(100);
705     progress.sendTo(progressfnc);
706
707     filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
708     progress.toMax();
709   }
710
711   void RepoManager::cleanPackages( const RepoInfo &info,
712                                    const ProgressData::ReceiverFnc & progressfnc )
713   {
714     ProgressData progress(100);
715     progress.sendTo(progressfnc);
716
717     filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_pimpl->options, info));
718     progress.toMax();
719   }
720
721   void RepoManager::buildCache( const RepoInfo &info,
722                                 CacheBuildPolicy policy,
723                                 const ProgressData::ReceiverFnc & progressrcv )
724   {
725     assert_alias(info);
726     Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
727
728     filesystem::assert_dir(_pimpl->options.repoCachePath);
729     RepoStatus raw_metadata_status = metadataStatus(info);
730     if ( raw_metadata_status.empty() )
731     {
732        /* if there is no cache at this point, we refresh the raw
733           in case this is the first time - if it's !autorefresh,
734           we may still refresh */
735       refreshMetadata(info, RefreshIfNeeded, progressrcv );
736       raw_metadata_status = metadataStatus(info);
737     }
738
739     bool needs_cleaning = false;
740     if ( isCached( info ) )
741     {
742       MIL << info.alias() << " is already cached." << endl;
743       //data::RecordId id = store.lookupRepository(info.alias());
744       RepoStatus cache_status = cacheStatus(info);
745
746       if ( cache_status.checksum() == raw_metadata_status.checksum() )
747       {
748         MIL << info.alias() << " cache is up to date with metadata." << endl;
749         if ( policy == BuildIfNeeded ) {
750           return;
751         }
752         else {
753           MIL << info.alias() << " cache rebuild is forced" << endl;
754         }
755       }
756
757       needs_cleaning = true;
758     }
759
760     ProgressData progress(100);
761     callback::SendReport<ProgressReport> report;
762     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
763     progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
764     progress.toMin();
765
766     if (needs_cleaning)
767     {
768       cleanCache(info);
769     }
770
771     MIL << info.alias() << " building cache..." << endl;
772
773     Pathname base = solv_path_for_repoinfo( _pimpl->options, info);
774     filesystem::assert_dir(base);
775     Pathname solvfile = base / "solv";
776
777     // do we have type?
778     repo::RepoType repokind = info.type();
779
780     // if the type is unknown, try probing.
781     switch ( repokind.toEnum() )
782     {
783       case RepoType::NONE_e:
784         // unknown, probe the local metadata
785         repokind = probe(rawpath.asUrl());
786       break;
787       default:
788       break;
789     }
790
791     MIL << "repo type is " << repokind << endl;
792
793     switch ( repokind.toEnum() )
794     {
795       case RepoType::RPMMD_e :
796       case RepoType::YAST2_e :
797       case RepoType::RPMPLAINDIR_e :
798       {
799         // Take care we unlink the solvfile on exception
800         ManagedFile guard( solvfile, filesystem::unlink );
801
802         ostringstream cmd;
803         std::string toFile( str::gsub(solvfile.asString(),"\"","\\\"") );
804         if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
805         {
806           cmd << str::form( "repo2solv.sh \"%s\" > \"%s\"",
807                             str::gsub( info.baseUrlsBegin()->getPathName(),"\"","\\\"" ).c_str(),
808                             toFile.c_str() );
809         }
810         else
811         {
812           cmd << str::form( "repo2solv.sh \"%s\" > \"%s\"",
813                             str::gsub( rawpath.asString(),"\"","\\\"" ).c_str(),
814                             toFile.c_str() );
815         }
816         MIL << "Executing: " << cmd.str() << endl;
817         ExternalProgram prog( cmd.str(), ExternalProgram::Stderr_To_Stdout );
818
819         cmd << endl;
820         for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
821           WAR << "  " << output;
822           cmd << "     " << output;
823         }
824
825         int ret = prog.close();
826         if ( ret != 0 )
827         {
828           RepoException ex(str::form("Failed to cache repo (%d).", ret));
829           ex.remember( cmd.str() );
830           ZYPP_THROW(ex);
831         }
832
833         // We keep it.
834         guard.resetDispose();
835       }
836       break;
837       default:
838         ZYPP_THROW(RepoUnknownTypeException("Unhandled repository type"));
839       break;
840     }
841 #if 0
842     switch ( repokind.toEnum() )
843     {
844       case RepoType::RPMMD_e :
845       if (0)
846       {
847         CombinedProgressData subprogrcv( progress, 100);
848         parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
849         parser.parse(rawpath);
850           // no error
851       }
852       break;
853       case RepoType::YAST2_e :
854       if (0)
855       {
856         CombinedProgressData subprogrcv( progress, 100);
857         parser::susetags::RepoParser parser(id, store, subprogrcv);
858         parser.parse(rawpath);
859         // no error
860       }
861       break;
862
863       default:
864         ZYPP_THROW(RepoUnknownTypeException());
865     }
866 #endif
867     // update timestamp and checksum
868     //store.updateRepositoryStatus(id, raw_metadata_status);
869     setCacheStatus(info, raw_metadata_status);
870     MIL << "Commit cache.." << endl;
871     //store.commit();
872     //progress.toMax();
873   }
874
875   ////////////////////////////////////////////////////////////////////////////
876
877   repo::RepoType RepoManager::probe( const Url &url ) const
878   {
879     if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
880     {
881       // Handle non existing local directory in advance, as
882       // MediaSetAccess does not support it.
883       return repo::RepoType::NONE;
884     }
885
886     try
887     {
888       MediaSetAccess access(url);
889       if ( access.doesFileExist("/repodata/repomd.xml") )
890         return repo::RepoType::RPMMD;
891       if ( access.doesFileExist("/content") )
892         return repo::RepoType::YAST2;
893
894       // if it is a local url of type dir
895       if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
896       {
897         Pathname path = Pathname(url.getPathName());
898         if ( PathInfo(path).isDir() )
899         {
900           // allow empty dirs for now
901           return repo::RepoType::RPMPLAINDIR;
902         }
903       }
904     }
905     catch ( const media::MediaException &e )
906     {
907       ZYPP_CAUGHT(e);
908       RepoException enew("Error trying to read from " + url.asString());
909       enew.remember(e);
910       ZYPP_THROW(enew);
911     }
912     catch ( const Exception &e )
913     {
914       ZYPP_CAUGHT(e);
915       Exception enew("Unknown error reading from " + url.asString());
916       enew.remember(e);
917       ZYPP_THROW(enew);
918     }
919
920     return repo::RepoType::NONE;
921   }
922
923   ////////////////////////////////////////////////////////////////////////////
924
925   void RepoManager::cleanCache( const RepoInfo &info,
926                                 const ProgressData::ReceiverFnc & progressrcv )
927   {
928     ProgressData progress(100);
929     progress.sendTo(progressrcv);
930     progress.toMin();
931
932     filesystem::recursive_rmdir(solv_path_for_repoinfo(_pimpl->options, info));
933
934     progress.toMax();
935   }
936
937   ////////////////////////////////////////////////////////////////////////////
938
939   bool RepoManager::isCached( const RepoInfo &info ) const
940   {
941     return PathInfo(solv_path_for_repoinfo( _pimpl->options, info ) / "solv").isExist();
942   }
943
944   RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
945   {
946
947     Pathname cookiefile = solv_path_for_repoinfo(_pimpl->options, info) / "cookie";
948
949     return RepoStatus::fromCookieFile(cookiefile);
950   }
951
952   void RepoManager::setCacheStatus( const RepoInfo &info, const RepoStatus &status )
953   {
954     Pathname base = solv_path_for_repoinfo(_pimpl->options, info);
955     filesystem::assert_dir(base);
956     Pathname cookiefile = base / "cookie";
957
958     status.saveToCookieFile(cookiefile);
959   }
960
961   void RepoManager::loadFromCache( const RepoInfo & info,
962                                    const ProgressData::ReceiverFnc & progressrcv )
963   {
964     assert_alias(info);
965     Pathname solvfile = solv_path_for_repoinfo(_pimpl->options, info) / "solv";
966
967     if ( ! PathInfo(solvfile).isExist() )
968       ZYPP_THROW(RepoNotCachedException(info));
969
970     try
971     {
972       sat::Pool::instance().addRepoSolv( solvfile, info );
973     }
974     catch ( const Exception & exp )
975     {
976       ZYPP_CAUGHT( exp );
977       MIL << "Try to handle exception by rebuilding the solv-file" << endl;
978       cleanCache( info, progressrcv );
979       buildCache( info, BuildIfNeeded, progressrcv );
980
981       sat::Pool::instance().addRepoSolv( solvfile, info );
982     }
983   }
984
985   ////////////////////////////////////////////////////////////////////////////
986
987   /**
988    * Generate a non existing filename in a directory, using a base
989    * name. For example if a directory contains 3 files
990    *
991    * |-- bar
992    * |-- foo
993    * `-- moo
994    *
995    * If you try to generate a unique filename for this directory,
996    * based on "ruu" you will get "ruu", but if you use the base
997    * "foo" you will get "foo_1"
998    *
999    * \param dir Directory where the file needs to be unique
1000    * \param basefilename string to base the filename on.
1001    */
1002   Pathname RepoManager::Impl::generateNonExistingName( const Pathname &dir,
1003                                        const std::string &basefilename ) const
1004   {
1005     string final_filename = basefilename;
1006     int counter = 1;
1007     while ( PathInfo(dir + final_filename).isExist() )
1008     {
1009       final_filename = basefilename + "_" + str::numstring(counter);
1010       counter++;
1011     }
1012     return dir + Pathname(final_filename);
1013   }
1014
1015   ////////////////////////////////////////////////////////////////////////////
1016
1017   /**
1018    * \short Generate a related filename from a repo info
1019    *
1020    * From a repo info, it will try to use the alias as a filename
1021    * escaping it if necessary. Other fallbacks can be added to
1022    * this function in case there is no way to use the alias
1023    */
1024   std::string RepoManager::Impl::generateFilename( const RepoInfo &info ) const
1025   {
1026     std::string filename = info.alias();
1027     // replace slashes with underscores
1028     str::replaceAll( filename, "/", "_" );
1029
1030     filename = Pathname(filename).extend(".repo").asString();
1031     MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
1032     return filename;
1033   }
1034
1035   std::string RepoManager::Impl::generateFilename( const Service &info ) const
1036   {
1037     std::string filename = info.name();
1038     // replace slashes with underscores
1039     str::replaceAll( filename, "/", "_" );
1040
1041     filename = Pathname(filename).extend(".service").asString();
1042     MIL << "generating filename for service [" << info.name() << "] : '" << filename << "'" << endl;
1043     return filename;
1044   }
1045
1046   ////////////////////////////////////////////////////////////////////////////
1047
1048   void RepoManager::addRepository( const RepoInfo &info,
1049                                    const ProgressData::ReceiverFnc & progressrcv )
1050   {
1051     assert_alias(info);
1052
1053     ProgressData progress(100);
1054     callback::SendReport<ProgressReport> report;
1055     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1056     progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
1057     progress.toMin();
1058
1059     std::list<RepoInfo> repos = knownRepositories();
1060     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1061           it != repos.end();
1062           ++it )
1063     {
1064       if ( info.alias() == (*it).alias() )
1065         ZYPP_THROW(RepoAlreadyExistsException(info));
1066     }
1067
1068     RepoInfo tosave = info;
1069
1070     // check the first url for now
1071     if ( _pimpl->options.probe )
1072     {
1073       DBG << "unknown repository type, probing" << endl;
1074
1075       RepoType probedtype;
1076       probedtype = probe(*tosave.baseUrlsBegin());
1077       if ( tosave.baseUrlsSize() > 0 )
1078       {
1079         if ( probedtype == RepoType::NONE )
1080           ZYPP_THROW(RepoUnknownTypeException());
1081         else
1082           tosave.setType(probedtype);
1083       }
1084     }
1085
1086     progress.set(50);
1087
1088     // assert the directory exists
1089     filesystem::assert_dir(_pimpl->options.knownReposPath);
1090
1091     Pathname repofile = _pimpl->generateNonExistingName(
1092         _pimpl->options.knownReposPath, _pimpl->generateFilename(tosave));
1093     // now we have a filename that does not exists
1094     MIL << "Saving repo in " << repofile << endl;
1095
1096     std::ofstream file(repofile.c_str());
1097     if (!file) {
1098       ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1099     }
1100
1101     tosave.dumpRepoOn(file);
1102     progress.toMax();
1103     MIL << "done" << endl;
1104   }
1105
1106   void RepoManager::addRepositories( const Url &url,
1107                                      const ProgressData::ReceiverFnc & progressrcv )
1108   {
1109     std::list<RepoInfo> knownrepos = knownRepositories();
1110     std::list<RepoInfo> repos = readRepoFile(url);
1111     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1112           it != repos.end();
1113           ++it )
1114     {
1115       // look if the alias is in the known repos.
1116       for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
1117           kit != knownrepos.end();
1118           ++kit )
1119       {
1120         if ( (*it).alias() == (*kit).alias() )
1121         {
1122           ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1123           ZYPP_THROW(RepoAlreadyExistsException(*it));
1124         }
1125       }
1126     }
1127
1128     string filename = Pathname(url.getPathName()).basename();
1129
1130     if ( filename == Pathname() )
1131       ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
1132
1133     // assert the directory exists
1134     filesystem::assert_dir(_pimpl->options.knownReposPath);
1135
1136     Pathname repofile = _pimpl->generateNonExistingName(_pimpl->options.knownReposPath, filename);
1137     // now we have a filename that does not exists
1138     MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1139
1140     std::ofstream file(repofile.c_str());
1141     if (!file) {
1142       ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1143     }
1144
1145     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1146           it != repos.end();
1147           ++it )
1148     {
1149       MIL << "Saving " << (*it).alias() << endl;
1150       (*it).dumpRepoOn(file);
1151     }
1152     MIL << "done" << endl;
1153   }
1154
1155   ////////////////////////////////////////////////////////////////////////////
1156
1157   void RepoManager::removeRepository( const RepoInfo & info,
1158                                       const ProgressData::ReceiverFnc & progressrcv)
1159   {
1160     ProgressData progress;
1161     callback::SendReport<ProgressReport> report;
1162     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1163     progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
1164
1165     MIL << "Going to delete repo " << info.alias() << endl;
1166
1167     std::list<RepoInfo> repos = knownRepositories();
1168     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1169           it != repos.end();
1170           ++it )
1171     {
1172       // they can be the same only if the provided is empty, that means
1173       // the provided repo has no alias
1174       // then skip
1175       if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1176         continue;
1177
1178       // TODO match by url
1179
1180       // we have a matcing repository, now we need to know
1181       // where it does come from.
1182       RepoInfo todelete = *it;
1183       if (todelete.filepath().empty())
1184       {
1185         ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1186       }
1187       else
1188       {
1189         // figure how many repos are there in the file:
1190         std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1191         if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1192         {
1193           // easy, only this one, just delete the file
1194           if ( filesystem::unlink(todelete.filepath()) != 0 )
1195           {
1196             ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
1197           }
1198           MIL << todelete.alias() << " sucessfully deleted." << endl;
1199         }
1200         else
1201         {
1202           // there are more repos in the same file
1203           // write them back except the deleted one.
1204           //TmpFile tmp;
1205           //std::ofstream file(tmp.path().c_str());
1206
1207           // assert the directory exists
1208           filesystem::assert_dir(todelete.filepath().dirname());
1209
1210           std::ofstream file(todelete.filepath().c_str());
1211           if (!file) {
1212             //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1213             ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
1214           }
1215           for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1216                 fit != filerepos.end();
1217                 ++fit )
1218           {
1219             if ( (*fit).alias() != todelete.alias() )
1220               (*fit).dumpRepoOn(file);
1221           }
1222         }
1223
1224         CombinedProgressData subprogrcv(progress, 70);
1225         CombinedProgressData cleansubprogrcv(progress, 30);
1226         // now delete it from cache
1227         if ( isCached(todelete) )
1228           cleanCache( todelete, subprogrcv);
1229         // now delete metadata (#301037)
1230         cleanMetadata( todelete, cleansubprogrcv);
1231         MIL << todelete.alias() << " sucessfully deleted." << endl;
1232         return;
1233       } // else filepath is empty
1234
1235     }
1236     // should not be reached on a sucess workflow
1237     ZYPP_THROW(RepoNotFoundException(info));
1238   }
1239
1240   ////////////////////////////////////////////////////////////////////////////
1241
1242   void RepoManager::modifyRepository( const std::string &alias,
1243                                       const RepoInfo & newinfo,
1244                                       const ProgressData::ReceiverFnc & progressrcv )
1245   {
1246     RepoInfo toedit = getRepositoryInfo(alias);
1247
1248     // check if the new alias already exists when renaming the repo
1249     if (alias != newinfo.alias())
1250     {
1251       std::list<RepoInfo> repos = knownRepositories();
1252       for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1253             it != repos.end();
1254             ++it )
1255       {
1256         if ( newinfo.alias() == (*it).alias() )
1257           ZYPP_THROW(RepoAlreadyExistsException(newinfo));
1258       }
1259     }
1260
1261     if (toedit.filepath().empty())
1262     {
1263       ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1264     }
1265     else
1266     {
1267       // figure how many repos are there in the file:
1268       std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1269
1270       // there are more repos in the same file
1271       // write them back except the deleted one.
1272       //TmpFile tmp;
1273       //std::ofstream file(tmp.path().c_str());
1274
1275       // assert the directory exists
1276       filesystem::assert_dir(toedit.filepath().dirname());
1277
1278       std::ofstream file(toedit.filepath().c_str());
1279       if (!file) {
1280         //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1281         ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
1282       }
1283       for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1284             fit != filerepos.end();
1285             ++fit )
1286       {
1287           // if the alias is different, dump the original
1288           // if it is the same, dump the provided one
1289           if ( (*fit).alias() != toedit.alias() )
1290             (*fit).dumpRepoOn(file);
1291           else
1292             newinfo.dumpRepoOn(file);
1293       }
1294     }
1295   }
1296
1297   ////////////////////////////////////////////////////////////////////////////
1298
1299   RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1300                                            const ProgressData::ReceiverFnc & progressrcv )
1301   {
1302     std::list<RepoInfo> repos = knownRepositories();
1303     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1304           it != repos.end();
1305           ++it )
1306     {
1307       if ( (*it).alias() == alias )
1308         return *it;
1309     }
1310     RepoInfo info;
1311     info.setAlias(info.alias());
1312     ZYPP_THROW(RepoNotFoundException(info));
1313   }
1314
1315   ////////////////////////////////////////////////////////////////////////////
1316
1317   RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1318                                            const url::ViewOption & urlview,
1319                                            const ProgressData::ReceiverFnc & progressrcv )
1320   {
1321     std::list<RepoInfo> repos = knownRepositories();
1322     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1323           it != repos.end();
1324           ++it )
1325     {
1326       for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1327           urlit != (*it).baseUrlsEnd();
1328           ++urlit)
1329       {
1330         if ((*urlit).asString(urlview) == url.asString(urlview))
1331           return *it;
1332       }
1333     }
1334     RepoInfo info;
1335     info.setAlias(info.alias());
1336     info.setBaseUrl(url);
1337     ZYPP_THROW(RepoNotFoundException(info));
1338   }
1339
1340   void RepoManager::addService( const std::string& name, const Url& url )
1341   {
1342     Service service(name, url);
1343
1344     //check if service isn't already exist
1345     if( _pimpl->services.find(service)!= _pimpl->services.end() )
1346       return; //TODO throw exception
1347
1348     _pimpl->services.insert( service );
1349
1350     _pimpl->saveService( service );
1351   }
1352
1353   void RepoManager::removeService( const string& name)
1354   {
1355     MIL << "Going to delete repo " << name << endl;
1356
1357     const Service& service = getService( name );
1358
1359     Pathname location = service.location();
1360     if( location.empty() )
1361     {
1362       ZYPP_THROW(RepoException("Can't figure where the service is stored"));
1363     }
1364
1365     ServiceSet tmpSet;
1366     Impl::ServiceCollector collector(tmpSet);
1367
1368     parser::ServiceFileReader reader( location,
1369         bind(&Impl::ServiceCollector::collect,collector,_1) );
1370
1371     if ( tmpSet.size() == 1 ) //only one record
1372     {
1373       if ( filesystem::unlink(location) != 0 )
1374       {
1375         ZYPP_THROW(RepoException("Can't delete " + location.asString()));
1376       }
1377       MIL << name << " sucessfully deleted." << endl;
1378     }
1379     else
1380     {
1381       filesystem::assert_dir(location.dirname());
1382
1383       std::ofstream file(location.c_str());
1384
1385       for_(it, tmpSet.begin(), tmpSet.end())
1386       {
1387         if( it->name() != name )
1388           it->dumpServiceOn(file);
1389       }
1390     }
1391
1392     //now remove all repositories added by this service
1393     for_( it, service.begin(), service.end() )
1394       removeRepository(getRepositoryInfo(*it));
1395   }
1396
1397
1398   void RepoManager::Impl::saveService( const Service& service )
1399   {
1400     filesystem::assert_dir( options.knownServicesPath );
1401
1402     Pathname servfile = generateNonExistingName( options.knownServicesPath,
1403         generateFilename( service ) );
1404
1405     MIL << "saving service in " << servfile << endl;
1406
1407     std::ofstream file(servfile.c_str());
1408     if (!file) {
1409       ZYPP_THROW (Exception( "Can't open " + servfile.asString() ) );
1410     }
1411
1412     service.dumpServiceOn( file );
1413
1414     service.setLocation( servfile );
1415     MIL << "done" << endl;
1416   }
1417
1418   const Service& RepoManager::getService( const std::string& name ) const
1419   {
1420     //little trick for Set, because it is sorted by Service name
1421     Service tmpServ(name);
1422
1423     ServiceConstIterator it = _pimpl->services.find(name);
1424     if ( it == serviceEnd() )
1425       return Service::noService;
1426     else
1427       return *it;
1428   }
1429
1430   bool RepoManager::serviceEmpty() const { return _pimpl->services.empty(); }
1431
1432   RepoManager::ServiceSizeType RepoManager::serviceSize() const
1433   {
1434     return _pimpl->services.size();
1435   }
1436
1437   RepoManager::ServiceConstIterator RepoManager::serviceBegin() const
1438   {
1439     return _pimpl->services.begin();
1440   }
1441
1442   RepoManager::ServiceConstIterator RepoManager::serviceEnd() const
1443   {
1444     return _pimpl->services.end();
1445   }
1446
1447   void RepoManager::refreshServices()
1448   {
1449     //cannot user for_, because it uses const version
1450     for (std::set<Service>::iterator it = _pimpl->services.begin();
1451       it != _pimpl->services.end(); ++it)
1452     {
1453       it->refresh(*this);
1454     }
1455   }
1456
1457   void RepoManager::modifyService(const Service& service) const
1458   {
1459     MIL << "Going to modify service " << service.name() << endl;
1460
1461     Pathname location = service.location();
1462     if( location.empty() )
1463     {
1464       ZYPP_THROW(RepoException("Can't figure where the service is stored"));
1465     }
1466
1467     ServiceSet tmpSet;
1468     Impl::ServiceCollector collector(tmpSet);
1469
1470     parser::ServiceFileReader reader( location,
1471         bind(&Impl::ServiceCollector::collect,collector,_1) );
1472
1473     filesystem::assert_dir(location.dirname());
1474
1475     std::ofstream file(location.c_str());
1476
1477     for_(it, tmpSet.begin(), tmpSet.end())
1478     {
1479       if( *it != service )
1480         it->dumpServiceOn(file);
1481     }
1482
1483     service.dumpServiceOn(file);
1484
1485     file.close();
1486   }
1487
1488   void RepoManager::Impl::knownServices()
1489   {
1490     ServiceCollector collector(services);
1491     Pathname dir = options.knownServicesPath;
1492     list<Pathname> entries;
1493     
1494     if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
1495         ZYPP_THROW(Exception("failed to read directory"));
1496
1497     str::regex allowedServiceExt("^\\.service(_[0-9]+)?$");
1498     for_(it, entries.begin(), entries.end() )
1499     {
1500       parser::ServiceFileReader reader(*it, 
1501           bind(&ServiceCollector::collect, collector, _1) );
1502     }
1503   }
1504
1505   ////////////////////////////////////////////////////////////////////////////
1506
1507   std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1508   {
1509     return str << *obj._pimpl;
1510   }
1511
1512   /////////////////////////////////////////////////////////////////
1513 } // namespace zypp
1514 ///////////////////////////////////////////////////////////////////