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