zypp/RepoInfo.cc
[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 <iostream>
14 #include <fstream>
15 #include <list>
16 #include <algorithm>
17 #include "zypp/base/InputStream.h"
18 #include "zypp/base/Logger.h"
19 #include "zypp/base/Gettext.h"
20 #include "zypp/base/Function.h"
21 #include "zypp/PathInfo.h"
22 #include "zypp/TmpPath.h"
23
24 #include "zypp/repo/RepoException.h"
25 #include "zypp/RepoManager.h"
26
27 #include "zypp/cache/CacheStore.h"
28 #include "zypp/repo/cached/RepoImpl.h"
29 #include "zypp/media/MediaManager.h"
30 #include "zypp/MediaSetAccess.h"
31
32 #include "zypp/parser/RepoFileReader.h"
33 #include "zypp/repo/yum/Downloader.h"
34 #include "zypp/parser/yum/RepoParser.h"
35 #include "zypp/parser/plaindir/RepoParser.h"
36 #include "zypp/repo/susetags/Downloader.h"
37 #include "zypp/parser/susetags/RepoParser.h"
38
39 #include "zypp/ZYppCallbacks.h"
40
41 using namespace std;
42 using namespace zypp;
43 using namespace zypp::repo;
44 using namespace zypp::filesystem;
45
46 using namespace zypp::repo;
47
48 ///////////////////////////////////////////////////////////////////
49 namespace zypp
50 { /////////////////////////////////////////////////////////////////
51
52   ///////////////////////////////////////////////////////////////////
53   //
54   //    CLASS NAME : RepoManagerOptions
55   //
56   ///////////////////////////////////////////////////////////////////
57
58   RepoManagerOptions::RepoManagerOptions()
59   {
60     repoCachePath    = ZConfig::instance().repoCachePath();
61     repoRawCachePath = ZConfig::instance().repoMetadataPath();
62     knownReposPath   = ZConfig::instance().knownReposPath();
63   }
64
65   ////////////////////////////////////////////////////////////////////////////
66
67   /**
68     * \short Simple callback to collect the results
69     *
70     * Classes like RepoFileParser call the callback
71     * once per each repo in a file.
72     *
73     * Passing this functor as callback, you can collect
74     * all resuls at the end, without dealing with async
75     * code.
76     */
77     struct RepoCollector
78     {
79       RepoCollector()
80       {
81         MIL << endl;
82       }
83
84       ~RepoCollector()
85       {
86         MIL << endl;
87       }
88
89       bool collect( const RepoInfo &repo )
90       {
91         //MIL << "here in collector: " << repo.alias() << endl;
92         repos.push_back(repo);
93         //MIL << "added: " << repo.alias() << endl;
94         return true;
95       }
96
97       RepoInfoList repos;
98     };
99
100   ////////////////////////////////////////////////////////////////////////////
101
102    /**
103     * \short Internal version of clean cache
104     *
105     * Takes an extra CacheStore reference, so we avoid internally
106     * having 2 CacheStores writing to the same database.
107     */
108   static void cleanCacheInternal( cache::CacheStore &store,
109                                   const RepoInfo &info,
110                                   const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() )
111   {
112     ProgressData progress;
113     callback::SendReport<ProgressReport> report;
114     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
115     progress.name(str::form(_("Cleaning repository '%s' cache"), info.name().c_str()));
116
117     if ( !store.isCached(info.alias()) )
118       return;
119    
120     MIL << info.alias() << " cleaning cache..." << endl;
121     data::RecordId id = store.lookupRepository(info.alias());
122     
123     CombinedProgressData subprogrcv(progress);
124     
125     store.cleanRepository(id, subprogrcv);
126   }
127   
128   ////////////////////////////////////////////////////////////////////////////
129   
130   /**
131    * Reads RepoInfo's from a repo file.
132    *
133    * \param file pathname of the file to read.
134    */
135   static std::list<RepoInfo> repositories_in_file( const Pathname & file )
136   {
137     MIL << "repo file: " << file << endl;
138     RepoCollector collector;
139     parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
140     return collector.repos;
141   }
142
143   ////////////////////////////////////////////////////////////////////////////
144
145   std::list<RepoInfo> readRepoFile(const Url & repo_file)
146    {
147      // no interface to download a specific file, using workaround:
148      //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
149      Url url(repo_file);
150      Pathname path(url.getPathName());
151      url.setPathName ("/");
152      MediaSetAccess access(url);
153      Pathname local = access.provideFile(path);
154
155      DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
156
157      return repositories_in_file(local);
158    }
159
160   ////////////////////////////////////////////////////////////////////////////
161
162   /**
163    * \short List of RepoInfo's from a directory
164    *
165    * Goes trough every file in a directory and adds all
166    * RepoInfo's contained in that file.
167    *
168    * \param dir pathname of the directory to read.
169    */
170   static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
171   {
172     MIL << "directory " << dir << endl;
173     list<RepoInfo> repos;
174     list<Pathname> entries;
175     if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
176       ZYPP_THROW(Exception("failed to read directory"));
177
178     for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
179     {
180       list<RepoInfo> tmp = repositories_in_file( *it );
181       repos.insert( repos.end(), tmp.begin(), tmp.end() );
182
183       //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
184       //MIL << "ok" << endl;
185     }
186     return repos;
187   }
188
189   ////////////////////////////////////////////////////////////////////////////
190
191   static void assert_alias( const RepoInfo &info )
192   {
193     if (info.alias().empty())
194         ZYPP_THROW(RepoNoAliasException());
195   }
196
197   ////////////////////////////////////////////////////////////////////////////
198
199   static void assert_urls( const RepoInfo &info )
200   {
201     if (info.baseUrlsEmpty())
202         ZYPP_THROW(RepoNoUrlException());
203   }
204
205   ////////////////////////////////////////////////////////////////////////////
206
207   /**
208    * \short Calculates the raw cache path for a repository
209    */
210   static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
211   {
212     assert_alias(info);
213     return opt.repoRawCachePath + info.alias();
214   }
215
216   ///////////////////////////////////////////////////////////////////
217   //
218   //    CLASS NAME : RepoManager::Impl
219   //
220   ///////////////////////////////////////////////////////////////////
221
222   /**
223    * \short RepoManager implementation.
224    */
225   struct RepoManager::Impl
226   {
227     Impl( const RepoManagerOptions &opt )
228       : options(opt)
229     {
230
231     }
232
233     Impl()
234     {
235
236     }
237
238     RepoManagerOptions options;
239
240   public:
241     /** Offer default Impl. */
242     static shared_ptr<Impl> nullimpl()
243     {
244       static shared_ptr<Impl> _nullimpl( new Impl );
245       return _nullimpl;
246     }
247
248   private:
249     friend Impl * rwcowClone<Impl>( const Impl * rhs );
250     /** clone for RWCOW_pointer */
251     Impl * clone() const
252     { return new Impl( *this ); }
253   };
254   ///////////////////////////////////////////////////////////////////
255
256   /** \relates RepoManager::Impl Stream output */
257   inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
258   {
259     return str << "RepoManager::Impl";
260   }
261
262   ///////////////////////////////////////////////////////////////////
263   //
264   //    CLASS NAME : RepoManager
265   //
266   ///////////////////////////////////////////////////////////////////
267
268   RepoManager::RepoManager( const RepoManagerOptions &opt )
269   : _pimpl( new Impl(opt) )
270   {}
271
272   ////////////////////////////////////////////////////////////////////////////
273
274   RepoManager::~RepoManager()
275   {}
276
277   ////////////////////////////////////////////////////////////////////////////
278
279   std::list<RepoInfo> RepoManager::knownRepositories() const
280   {
281     MIL << endl;
282
283     if ( PathInfo(_pimpl->options.knownReposPath).isExist() )
284     {
285       RepoInfoList repos = repositories_in_dir(_pimpl->options.knownReposPath);
286       for ( RepoInfoList::iterator it = repos.begin();
287             it != repos.end();
288             ++it )
289       {
290         // set the metadata path for the repo
291         Pathname metadata_path = rawcache_path_for_repoinfo(_pimpl->options, (*it));
292         (*it).setMetadataPath(metadata_path);
293       }
294       return repos;
295     }
296     else
297       return std::list<RepoInfo>();
298
299     MIL << endl;
300   }
301
302   ////////////////////////////////////////////////////////////////////////////
303
304   RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
305   {
306     Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
307     RepoType repokind = info.type();
308     RepoStatus status;
309
310     switch ( repokind.toEnum() )
311     {
312       case RepoType::NONE_e:
313       // unknown, probe the local metadata
314         repokind = probe(rawpath.asUrl());
315       break;
316       default:
317       break;
318     }
319
320     switch ( repokind.toEnum() )
321     {
322       case RepoType::RPMMD_e :
323       {
324         status = RepoStatus( rawpath + "/repodata/repomd.xml");
325       }
326       break;
327
328       case RepoType::YAST2_e :
329       {
330         status = RepoStatus( rawpath + "/content");
331       }
332       break;
333
334       case RepoType::RPMPLAINDIR_e :
335       {
336         if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
337           status = RepoStatus( rawpath + "/cookie");
338       }
339       break;
340
341       case RepoType::NONE_e :
342         // Return default RepoStatus in case of RepoType::NONE
343         // indicating it should be created?
344         // ZYPP_THROW(RepoUnknownTypeException());
345         break;
346     }
347     return status;
348   }
349
350   void RepoManager::refreshMetadata( const RepoInfo &info,
351                                      RawMetadataRefreshPolicy policy,
352                                      const ProgressData::ReceiverFnc & progress )
353   {
354     assert_alias(info);
355     assert_urls(info);
356
357     RepoStatus oldstatus;
358     RepoStatus newstatus;
359     // try urls one by one
360     for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
361     {
362       try
363       {
364         Url url(*it);
365
366         MIL << "Going to try to check and refresh metadata from " << url << endl;
367
368         repo::RepoType repokind = info.type();
369
370         // if the type is unknown, try probing.
371         switch ( repokind.toEnum() )
372         {
373           case RepoType::NONE_e:
374             // unknown, probe it
375             repokind = probe(*it);
376           break;
377           default:
378           break;
379         }
380
381         Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
382         filesystem::assert_dir(rawpath);
383         oldstatus = metadataStatus(info);
384
385         // create temp dir as sibling of rawpath
386         filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
387
388         if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
389              ( repokind.toEnum() == RepoType::YAST2_e ) )
390         {
391           MediaSetAccess media(url);
392           shared_ptr<repo::Downloader> downloader_ptr;
393
394           if ( repokind.toEnum() == RepoType::RPMMD_e )
395             downloader_ptr.reset(new yum::Downloader(info.path()));
396           else
397             downloader_ptr.reset( new susetags::Downloader(info.path()));
398
399           /**
400            * Given a downloader, sets the other repos raw metadata
401            * path as cache paths for the fetcher, so if another
402            * repo has the same file, it will not download it
403            * but copy it from the other repository
404            */
405           std::list<RepoInfo> repos = knownRepositories();
406           for ( std::list<RepoInfo>::const_iterator it = repos.begin();
407                 it != repos.end();
408                 ++it )
409           {
410             downloader_ptr->addCachePath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
411           }
412
413           RepoStatus newstatus = downloader_ptr->status(media);
414           bool refresh = false;
415           if ( oldstatus.checksum() == newstatus.checksum() )
416           {
417             MIL << "repo has not changed" << endl;
418             if ( policy == RefreshForced )
419             {
420               MIL << "refresh set to forced" << endl;
421               refresh = true;
422             }
423           }
424           else
425           {
426             MIL << "repo has changed, going to refresh" << endl;
427             refresh = true;
428           }
429           if ( refresh )
430             downloader_ptr->download( media, tmpdir.path());
431           else
432             return;
433           // no error
434         }
435         else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
436         {
437           RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
438           bool refresh = false;
439           if ( oldstatus.checksum() == newstatus.checksum() )
440           {
441             MIL << "repo has not changed" << endl;
442             if ( policy == RefreshForced )
443             {
444               MIL << "refresh set to forced" << endl;
445               refresh = true;
446             }
447           }
448           else
449           {
450             MIL << "repo has changed, going to refresh" << endl;
451             refresh = true;
452           }
453
454           if ( refresh )
455           {
456             std::ofstream file(( tmpdir.path() + "/cookie").c_str());
457             if (!file) {
458               ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
459             }
460             file << url << endl;
461             file << newstatus.checksum() << endl;
462
463             file.close();
464           }
465           else
466             return;
467           // no error
468         }
469         else
470         {
471           ZYPP_THROW(RepoUnknownTypeException());
472         }
473
474         // ok we have the metadata, now exchange
475         // the contents
476         TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
477         filesystem::rename( rawpath, oldmetadata.path() );
478         // move the just downloaded there
479         filesystem::rename( tmpdir.path(), rawpath );
480         // we are done.
481         return;
482       }
483       catch ( const Exception &e )
484       {
485         ZYPP_CAUGHT(e);
486         ERR << "Trying another url..." << endl;
487       }
488     } // for every url
489     ERR << "No more urls..." << endl;
490     ZYPP_THROW(RepoException(_("Valid metadata not found at specified URL(s)")));
491   }
492
493   ////////////////////////////////////////////////////////////////////////////
494
495   void RepoManager::cleanMetadata( const RepoInfo &info,
496                                    const ProgressData::ReceiverFnc & progress )
497   {
498     filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
499   }
500
501   void RepoManager::buildCache( const RepoInfo &info,
502                                 CacheBuildPolicy policy,
503                                 const ProgressData::ReceiverFnc & progressrcv )
504   {
505     ProgressData progress(100);
506     callback::SendReport<ProgressReport> report;
507     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
508     progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
509     progress.toMin();
510
511     assert_alias(info);
512     Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
513
514     cache::CacheStore store(_pimpl->options.repoCachePath);
515
516     RepoStatus raw_metadata_status = metadataStatus(info);
517     if ( raw_metadata_status.empty() )
518     {
519       ZYPP_THROW(RepoMetadataException(info));
520     }
521
522     if ( store.isCached( info.alias() ) )
523     {
524       MIL << info.alias() << " is already cached." << endl;
525       data::RecordId id = store.lookupRepository(info.alias());
526       RepoStatus cache_status = store.repositoryStatus(id);
527
528       if ( cache_status.checksum() == raw_metadata_status.checksum() )
529       {
530         MIL << info.alias() << " cache is up to date with metadata." << endl;
531         if ( policy == BuildIfNeeded ) {
532           progress.toMax();
533           return;
534         }
535         else {
536           MIL << info.alias() << " cache rebuild is forced" << endl;
537         }
538       }
539       
540       cleanCacheInternal( store, info);
541     }
542
543     MIL << info.alias() << " building cache..." << endl;
544     data::RecordId id = store.lookupOrAppendRepository(info.alias());
545     // do we have type?
546     repo::RepoType repokind = info.type();
547
548     // if the type is unknown, try probing.
549     switch ( repokind.toEnum() )
550     {
551       case RepoType::NONE_e:
552         // unknown, probe the local metadata
553         repokind = probe(rawpath.asUrl());
554       break;
555       default:
556       break;
557     }
558
559     
560     switch ( repokind.toEnum() )
561     {
562       case RepoType::RPMMD_e :
563       {
564         CombinedProgressData subprogrcv( progress, 100);
565         parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
566         parser.parse(rawpath);
567           // no error
568       }
569       break;
570       case RepoType::YAST2_e :
571       {
572         CombinedProgressData subprogrcv( progress, 100);
573         parser::susetags::RepoParser parser(id, store, subprogrcv);
574         parser.parse(rawpath);
575         // no error
576       }
577       break;
578       case RepoType::RPMPLAINDIR_e :
579       {
580         CombinedProgressData subprogrcv( progress, 100);
581         InputStream is(rawpath + "cookie");
582         string buffer;
583         getline( is.stream(), buffer);
584         Url url(buffer);
585         parser::plaindir::RepoParser parser(id, store, subprogrcv);
586         parser.parse(url.getPathName());
587       }
588       break;
589       default:
590         ZYPP_THROW(RepoUnknownTypeException());
591     }
592
593     // update timestamp and checksum
594     store.updateRepositoryStatus(id, raw_metadata_status);
595
596     MIL << "Commit cache.." << endl;
597     store.commit();
598     //progress.toMax();
599   }
600
601   ////////////////////////////////////////////////////////////////////////////
602
603   repo::RepoType RepoManager::probe( const Url &url ) const
604   {
605     if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
606     {
607       // Handle non existing local directory in advance, as
608       // MediaSetAccess does not support it.
609       return repo::RepoType::NONE;
610     }
611
612     try
613     {
614       MediaSetAccess access(url);
615       if ( access.doesFileExist("/repodata/repomd.xml") )
616         return repo::RepoType::RPMMD;
617       if ( access.doesFileExist("/content") )
618         return repo::RepoType::YAST2;
619   
620       // if it is a local url of type dir
621       if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
622       {
623         Pathname path = Pathname(url.getPathName());
624         if ( PathInfo(path).isDir() )
625         {
626           // allow empty dirs for now
627           return repo::RepoType::RPMPLAINDIR;
628         }
629       }
630     }
631     catch ( const media::MediaException &e )
632     {
633       ZYPP_CAUGHT(e);
634       RepoException enew("Error trying to read from " + url.asString());
635       enew.remember(e);
636       ZYPP_THROW(enew);
637     }
638     catch ( const Exception &e )
639     {
640       ZYPP_CAUGHT(e);
641       Exception enew("Unknown error reading from " + url.asString());
642       enew.remember(e);
643       ZYPP_THROW(enew);
644     }
645
646     return repo::RepoType::NONE;
647   }
648     
649   ////////////////////////////////////////////////////////////////////////////
650   
651   void RepoManager::cleanCache( const RepoInfo &info,
652                                 const ProgressData::ReceiverFnc & progressrcv )
653   {
654     cache::CacheStore store(_pimpl->options.repoCachePath);
655     cleanCacheInternal( store, info, progressrcv );
656     store.commit();
657   }
658
659   ////////////////////////////////////////////////////////////////////////////
660
661   bool RepoManager::isCached( const RepoInfo &info ) const
662   {
663     cache::CacheStore store(_pimpl->options.repoCachePath);
664     return store.isCached(info.alias());
665   }
666
667   RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
668   {
669     cache::CacheStore store(_pimpl->options.repoCachePath);
670     data::RecordId id = store.lookupRepository(info.alias());
671     RepoStatus cache_status = store.repositoryStatus(id);
672     return cache_status;
673   }
674
675   Repository RepoManager::createFromCache( const RepoInfo &info,
676                                            const ProgressData::ReceiverFnc & progressrcv )
677   {
678     callback::SendReport<ProgressReport> report;
679     ProgressData progress;
680     progress.sendTo(ProgressReportAdaptor( progressrcv, report ));
681     //progress.sendTo( progressrcv );
682     progress.name(str::form(_("Reading repository '%s' cache"), info.name().c_str()));
683     
684     cache::CacheStore store(_pimpl->options.repoCachePath);
685
686     if ( ! store.isCached( info.alias() ) )
687       ZYPP_THROW(RepoNotCachedException());
688
689     MIL << "Repository " << info.alias() << " is cached" << endl;
690
691     data::RecordId id = store.lookupRepository(info.alias());
692     
693     CombinedProgressData subprogrcv(progress);
694     
695     repo::cached::RepoOptions opts( info, _pimpl->options.repoCachePath, id );
696     opts.readingResolvablesProgress = subprogrcv;
697     repo::cached::RepoImpl::Ptr repoimpl =
698         new repo::cached::RepoImpl( opts );
699
700     repoimpl->resolvables();
701     // read the resolvables from cache
702     return Repository(repoimpl);
703   }
704
705   ////////////////////////////////////////////////////////////////////////////
706
707   /**
708    * Generate a non existing filename in a directory, using a base
709    * name. For example if a directory contains 3 files
710    *
711    * |-- bar
712    * |-- foo
713    * `-- moo
714    *
715    * If you try to generate a unique filename for this directory,
716    * based on "ruu" you will get "ruu", but if you use the base
717    * "foo" you will get "foo_1"
718    *
719    * \param dir Directory where the file needs to be unique
720    * \param basefilename string to base the filename on.
721    */
722   static Pathname generate_non_existing_name( const Pathname &dir,
723                                               const std::string &basefilename )
724   {
725     string final_filename = basefilename;
726     int counter = 1;
727     while ( PathInfo(dir + final_filename).isExist() )
728     {
729       final_filename = basefilename + "_" + str::numstring(counter);
730       counter++;
731     }
732     return dir + Pathname(final_filename);
733   }
734
735   ////////////////////////////////////////////////////////////////////////////
736
737   /**
738    * \short Generate a related filename from a repo info
739    *
740    * From a repo info, it will try to use the alias as a filename
741    * escaping it if necessary. Other fallbacks can be added to
742    * this function in case there is no way to use the alias
743    */
744   static std::string generate_filename( const RepoInfo &info )
745   {
746     std::string fnd="/";
747     std::string rep="_";
748     std::string filename = info.alias();
749     // replace slashes with underscores
750     size_t pos = filename.find(fnd);
751     while(pos!=string::npos)
752     {
753       filename.replace(pos,fnd.length(),rep);
754       pos = filename.find(fnd,pos+rep.length());
755     }
756     filename = Pathname(filename).extend(".repo").asString();
757     MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
758     return filename;
759   }
760
761
762   ////////////////////////////////////////////////////////////////////////////
763
764   void RepoManager::addRepository( const RepoInfo &info,
765                                    const ProgressData::ReceiverFnc & progressrcv )
766   {
767     assert_alias(info);
768
769     ProgressData progress(100);
770     callback::SendReport<ProgressReport> report;
771     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
772     progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
773     progress.toMin();
774
775     std::list<RepoInfo> repos = knownRepositories();
776     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
777           it != repos.end();
778           ++it )
779     {
780       if ( info.alias() == (*it).alias() )
781         ZYPP_THROW(RepoAlreadyExistsException(info.alias()));
782     }
783
784     RepoInfo tosave = info;
785     
786     // check the first url for now
787     if ( ZConfig::instance().repo_add_probe() || ( tosave.type() == RepoType::NONE ) )
788     {
789       RepoType probedtype;
790       probedtype = probe(*tosave.baseUrlsBegin());
791       if ( tosave.baseUrlsSize() > 0 )
792       {
793         if ( probedtype == RepoType::NONE )
794           ZYPP_THROW(RepoUnknownTypeException());
795         else
796           tosave.setType(probedtype);
797       }
798     }
799     
800     progress.set(50);
801
802     // assert the directory exists
803     filesystem::assert_dir(_pimpl->options.knownReposPath);
804
805     Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath,
806                                                     generate_filename(tosave));
807     // now we have a filename that does not exists
808     MIL << "Saving repo in " << repofile << endl;
809
810     std::ofstream file(repofile.c_str());
811     if (!file) {
812       ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
813     }
814
815     tosave.dumpRepoOn(file);
816     progress.toMax();
817     MIL << "done" << endl;
818   }
819
820   void RepoManager::addRepositories( const Url &url,
821                                      const ProgressData::ReceiverFnc & progressrcv )
822   {
823     std::list<RepoInfo> knownrepos = knownRepositories();
824     std::list<RepoInfo> repos = readRepoFile(url);
825     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
826           it != repos.end();
827           ++it )
828     {
829       // look if the alias is in the known repos.
830       for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
831           kit != knownrepos.end();
832           ++kit )
833       {
834         if ( (*it).alias() == (*kit).alias() )
835         {
836           ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
837           ZYPP_THROW(RepoAlreadyExistsException((*it).alias()));
838         }
839       }
840     }
841
842     string filename = Pathname(url.getPathName()).basename();
843
844     if ( filename == Pathname() )
845       ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
846
847     // assert the directory exists
848     filesystem::assert_dir(_pimpl->options.knownReposPath);
849
850     Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath, filename);
851     // now we have a filename that does not exists
852     MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
853
854     std::ofstream file(repofile.c_str());
855     if (!file) {
856       ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
857     }
858
859     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
860           it != repos.end();
861           ++it )
862     {
863       MIL << "Saving " << (*it).alias() << endl;
864       (*it).dumpRepoOn(file);
865     }
866     MIL << "done" << endl;
867   }
868
869   ////////////////////////////////////////////////////////////////////////////
870
871   void RepoManager::removeRepository( const RepoInfo & info,
872                                       const ProgressData::ReceiverFnc & progressrcv)
873   {
874     ProgressData progress;
875     callback::SendReport<ProgressReport> report;
876     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
877     progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
878     
879     MIL << "Going to delete repo " << info.alias() << endl;
880
881     std::list<RepoInfo> repos = knownRepositories();
882     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
883           it != repos.end();
884           ++it )
885     {
886       // they can be the same only if the provided is empty, that means
887       // the provided repo has no alias
888       // then skip
889       if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
890         continue;
891
892       // TODO match by url
893
894       // we have a matcing repository, now we need to know
895       // where it does come from.
896       RepoInfo todelete = *it;
897       if (todelete.filepath().empty())
898       {
899         ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
900       }
901       else
902       {
903         // figure how many repos are there in the file:
904         std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
905         if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
906         {
907           // easy, only this one, just delete the file
908           if ( filesystem::unlink(todelete.filepath()) != 0 )
909           {
910             ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
911           }
912           MIL << todelete.alias() << " sucessfully deleted." << endl;
913         }
914         else
915         {
916           // there are more repos in the same file
917           // write them back except the deleted one.
918           //TmpFile tmp;
919           //std::ofstream file(tmp.path().c_str());
920
921           // assert the directory exists
922           filesystem::assert_dir(todelete.filepath().dirname());
923
924           std::ofstream file(todelete.filepath().c_str());
925           if (!file) {
926             //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
927             ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
928           }
929           for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
930                 fit != filerepos.end();
931                 ++fit )
932           {
933             if ( (*fit).alias() != todelete.alias() )
934               (*fit).dumpRepoOn(file);
935           }
936         }
937
938         CombinedProgressData subprogrcv(progress);
939         
940         // now delete it from cache
941         cleanCache( todelete, subprogrcv);
942
943         MIL << todelete.alias() << " sucessfully deleted." << endl;
944         return;
945       } // else filepath is empty
946
947     }
948     // should not be reached on a sucess workflow
949     ZYPP_THROW(RepoNotFoundException(info));
950   }
951
952   ////////////////////////////////////////////////////////////////////////////
953
954   void RepoManager::modifyRepository( const std::string &alias,
955                                       const RepoInfo & newinfo,
956                                       const ProgressData::ReceiverFnc & progressrcv )
957   {
958     RepoInfo toedit = getRepositoryInfo(alias);
959
960     if (toedit.filepath().empty())
961     {
962       ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
963     }
964     else
965     {
966       // figure how many repos are there in the file:
967       std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
968
969       // there are more repos in the same file
970       // write them back except the deleted one.
971       //TmpFile tmp;
972       //std::ofstream file(tmp.path().c_str());
973
974       // assert the directory exists
975       filesystem::assert_dir(toedit.filepath().dirname());
976
977       std::ofstream file(toedit.filepath().c_str());
978       if (!file) {
979         //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
980         ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
981       }
982       for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
983             fit != filerepos.end();
984             ++fit )
985       {
986           // if the alias is different, dump the original
987           // if it is the same, dump the provided one
988           if ( (*fit).alias() != toedit.alias() )
989             (*fit).dumpRepoOn(file);
990           else
991             newinfo.dumpRepoOn(file);
992       }
993     }
994   }
995
996   ////////////////////////////////////////////////////////////////////////////
997
998   RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
999                                            const ProgressData::ReceiverFnc & progressrcv )
1000   {
1001     std::list<RepoInfo> repos = knownRepositories();
1002     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1003           it != repos.end();
1004           ++it )
1005     {
1006       if ( (*it).alias() == alias )
1007         return *it;
1008     }
1009     RepoInfo info;
1010     info.setAlias(info.alias());
1011     ZYPP_THROW(RepoNotFoundException(info));
1012   }
1013
1014   ////////////////////////////////////////////////////////////////////////////
1015
1016   RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1017                                            const url::ViewOption & urlview,
1018                                            const ProgressData::ReceiverFnc & progressrcv )
1019   {
1020     std::list<RepoInfo> repos = knownRepositories();
1021     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1022           it != repos.end();
1023           ++it )
1024     {
1025       for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1026           urlit != (*it).baseUrlsEnd();
1027           ++urlit)
1028       {
1029         if ((*urlit).asString(urlview) == url.asString(urlview))
1030           return *it;
1031       }
1032     }
1033     RepoInfo info;
1034     info.setAlias(info.alias());
1035     info.setBaseUrl(url);
1036     ZYPP_THROW(RepoNotFoundException(info));
1037   }
1038
1039   ////////////////////////////////////////////////////////////////////////////
1040
1041   std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1042   {
1043     return str << *obj._pimpl;
1044   }
1045
1046   /////////////////////////////////////////////////////////////////
1047 } // namespace zypp
1048 ///////////////////////////////////////////////////////////////////