some more logging
[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_THROW(RepoException("Error trying to read from " + url.asString()));
604     }
605     catch ( const Exception &e )
606     {
607       ZYPP_THROW(Exception("Unknown error reading from " + url.asString()));
608     }
609     
610     return repo::RepoType::NONE;
611   }
612
613   ////////////////////////////////////////////////////////////////////////////
614
615   void RepoManager::cleanCache( const RepoInfo &info,
616                                 const ProgressData::ReceiverFnc & progressrcv )
617   {
618     ProgressData progress;
619     callback::SendReport<ProgressReport> report;
620     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
621     progress.name(str::form(_("Cleaning repository '%s' cache"), info.alias().c_str()));
622     
623     cache::CacheStore store(_pimpl->options.repoCachePath);
624     
625     if ( !store.isCached(info.alias()) )
626       return;
627    
628     MIL << info.alias() << " cleaning cache..." << endl;
629     data::RecordId id = store.lookupRepository(info.alias());
630     
631     CombinedProgressData subprogrcv(progress);
632     
633     store.cleanRepository(id, subprogrcv);
634     store.commit();
635   }
636
637   ////////////////////////////////////////////////////////////////////////////
638
639   bool RepoManager::isCached( const RepoInfo &info ) const
640   {
641     cache::CacheStore store(_pimpl->options.repoCachePath);
642     return store.isCached(info.alias());
643   }
644
645   RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
646   {
647     cache::CacheStore store(_pimpl->options.repoCachePath);
648     data::RecordId id = store.lookupRepository(info.alias());
649     RepoStatus cache_status = store.repositoryStatus(id);
650     return cache_status;
651   }
652
653   Repository RepoManager::createFromCache( const RepoInfo &info,
654                                            const ProgressData::ReceiverFnc & progressrcv )
655   {
656     callback::SendReport<ProgressReport> report;
657     ProgressData progress;
658     progress.sendTo(ProgressReportAdaptor( progressrcv, report ));
659     //progress.sendTo( progressrcv );
660     progress.name(str::form(_("Reading repository '%s' cache"), info.alias().c_str()));
661     
662     cache::CacheStore store(_pimpl->options.repoCachePath);
663
664     if ( ! store.isCached( info.alias() ) )
665       ZYPP_THROW(RepoNotCachedException());
666
667     MIL << "Repository " << info.alias() << " is cached" << endl;
668
669     data::RecordId id = store.lookupRepository(info.alias());
670     
671     CombinedProgressData subprogrcv(progress);
672     
673     repo::cached::RepoOptions opts( info, _pimpl->options.repoCachePath, id );
674     opts.readingResolvablesProgress = subprogrcv;
675     repo::cached::RepoImpl::Ptr repoimpl =
676         new repo::cached::RepoImpl( opts );
677
678     repoimpl->resolvables();
679     // read the resolvables from cache
680     return Repository(repoimpl);
681   }
682
683   ////////////////////////////////////////////////////////////////////////////
684
685   /**
686    * Generate a non existing filename in a directory, using a base
687    * name. For example if a directory contains 3 files
688    *
689    * |-- bar
690    * |-- foo
691    * `-- moo
692    *
693    * If you try to generate a unique filename for this directory,
694    * based on "ruu" you will get "ruu", but if you use the base
695    * "foo" you will get "foo_1"
696    *
697    * \param dir Directory where the file needs to be unique
698    * \param basefilename string to base the filename on.
699    */
700   static Pathname generate_non_existing_name( const Pathname &dir,
701                                               const std::string &basefilename )
702   {
703     string final_filename = basefilename;
704     int counter = 1;
705     while ( PathInfo(dir + final_filename).isExist() )
706     {
707       final_filename = basefilename + "_" + str::numstring(counter);
708       counter++;
709     }
710     return dir + Pathname(final_filename);
711   }
712
713   ////////////////////////////////////////////////////////////////////////////
714
715   /**
716    * \short Generate a related filename from a repo info
717    *
718    * From a repo info, it will try to use the alias as a filename
719    * escaping it if necessary. Other fallbacks can be added to
720    * this function in case there is no way to use the alias
721    */
722   static std::string generate_filename( const RepoInfo &info )
723   {
724     std::string fnd="/";
725     std::string rep="_";
726     std::string filename = info.alias();
727     // replace slashes with underscores
728     size_t pos = filename.find(fnd);
729     while(pos!=string::npos)
730     {
731       filename.replace(pos,fnd.length(),rep);
732       pos = filename.find(fnd,pos+rep.length());
733     }
734     filename = Pathname(filename).extend(".repo").asString();
735     MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
736     return filename;
737   }
738
739
740   ////////////////////////////////////////////////////////////////////////////
741
742   void RepoManager::addRepository( const RepoInfo &info,
743                                    const ProgressData::ReceiverFnc & progressrcv )
744   {
745     assert_alias(info);
746
747     ProgressData progress(100);
748     callback::SendReport<ProgressReport> report;
749     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
750     progress.name(str::form(_("Adding repository '%s'"), info.alias().c_str()));
751     progress.toMin();
752
753     std::list<RepoInfo> repos = knownRepositories();
754     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
755           it != repos.end();
756           ++it )
757     {
758       if ( info.alias() == (*it).alias() )
759         ZYPP_THROW(RepoAlreadyExistsException(info.alias()));
760     }
761
762     RepoInfo tosave = info;
763     
764     // check the first url for now
765     if ( ZConfig::instance().repo_add_probe() || ( tosave.type() == RepoType::NONE ) )
766     {
767       RepoType probedtype;
768       probedtype = probe(*tosave.baseUrlsBegin());
769       if ( tosave.baseUrlsSize() > 0 )
770       {
771         if ( probedtype == RepoType::NONE )
772           ZYPP_THROW(RepoUnknownTypeException());
773         else
774           tosave.setType(probedtype);
775       }
776     }
777     
778     progress.set(50);
779
780     // assert the directory exists
781     filesystem::assert_dir(_pimpl->options.knownReposPath);
782
783     Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath,
784                                                     generate_filename(tosave));
785     // now we have a filename that does not exists
786     MIL << "Saving repo in " << repofile << endl;
787
788     std::ofstream file(repofile.c_str());
789     if (!file) {
790       ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
791     }
792
793     tosave.dumpRepoOn(file);
794     progress.toMax();
795     MIL << "done" << endl;
796   }
797
798   void RepoManager::addRepositories( const Url &url,
799                                      const ProgressData::ReceiverFnc & progressrcv )
800   {
801     std::list<RepoInfo> knownrepos = knownRepositories();
802     std::list<RepoInfo> repos = readRepoFile(url);
803     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
804           it != repos.end();
805           ++it )
806     {
807       // look if the alias is in the known repos.
808       for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
809           kit != knownrepos.end();
810           ++kit )
811       {
812         if ( (*it).alias() == (*kit).alias() )
813         {
814           ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
815           ZYPP_THROW(RepoAlreadyExistsException((*it).alias()));
816         }
817       }
818     }
819
820     string filename = Pathname(url.getPathName()).basename();
821
822     if ( filename == Pathname() )
823       ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
824
825     // assert the directory exists
826     filesystem::assert_dir(_pimpl->options.knownReposPath);
827
828     Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath, filename);
829     // now we have a filename that does not exists
830     MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
831
832     std::ofstream file(repofile.c_str());
833     if (!file) {
834       ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
835     }
836
837     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
838           it != repos.end();
839           ++it )
840     {
841       MIL << "Saving " << (*it).alias() << endl;
842       (*it).dumpRepoOn(file);
843     }
844     MIL << "done" << endl;
845   }
846
847   ////////////////////////////////////////////////////////////////////////////
848
849   void RepoManager::removeRepository( const RepoInfo & info,
850                                       const ProgressData::ReceiverFnc & progressrcv)
851   {
852     ProgressData progress;
853     callback::SendReport<ProgressReport> report;
854     progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
855     progress.name(str::form(_("Removing repository '%s'"), info.alias().c_str()));
856     
857     MIL << "Going to delete repo " << info.alias() << endl;
858
859     std::list<RepoInfo> repos = knownRepositories();
860     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
861           it != repos.end();
862           ++it )
863     {
864       // they can be the same only if the provided is empty, that means
865       // the provided repo has no alias
866       // then skip
867       if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
868         continue;
869
870       // TODO match by url
871
872       // we have a matcing repository, now we need to know
873       // where it does come from.
874       RepoInfo todelete = *it;
875       if (todelete.filepath().empty())
876       {
877         ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
878       }
879       else
880       {
881         // figure how many repos are there in the file:
882         std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
883         if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
884         {
885           // easy, only this one, just delete the file
886           if ( filesystem::unlink(todelete.filepath()) != 0 )
887           {
888             ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
889           }
890           MIL << todelete.alias() << " sucessfully deleted." << endl;
891         }
892         else
893         {
894           // there are more repos in the same file
895           // write them back except the deleted one.
896           //TmpFile tmp;
897           //std::ofstream file(tmp.path().c_str());
898
899           // assert the directory exists
900           filesystem::assert_dir(todelete.filepath().dirname());
901
902           std::ofstream file(todelete.filepath().c_str());
903           if (!file) {
904             //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
905             ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
906           }
907           for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
908                 fit != filerepos.end();
909                 ++fit )
910           {
911             if ( (*fit).alias() != todelete.alias() )
912               (*fit).dumpRepoOn(file);
913           }
914         }
915
916         CombinedProgressData subprogrcv(progress);
917         
918         // now delete it from cache
919         cleanCache(todelete, subprogrcv);
920
921         MIL << todelete.alias() << " sucessfully deleted." << endl;
922         return;
923       } // else filepath is empty
924
925     }
926     // should not be reached on a sucess workflow
927     ZYPP_THROW(RepoNotFoundException(info));
928   }
929
930   ////////////////////////////////////////////////////////////////////////////
931
932   void RepoManager::modifyRepository( const std::string &alias,
933                                       const RepoInfo & newinfo,
934                                       const ProgressData::ReceiverFnc & progressrcv )
935   {
936     RepoInfo toedit = getRepositoryInfo(alias);
937
938     if (toedit.filepath().empty())
939     {
940       ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
941     }
942     else
943     {
944       // figure how many repos are there in the file:
945       std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
946
947       // there are more repos in the same file
948       // write them back except the deleted one.
949       //TmpFile tmp;
950       //std::ofstream file(tmp.path().c_str());
951
952       // assert the directory exists
953       filesystem::assert_dir(toedit.filepath().dirname());
954
955       std::ofstream file(toedit.filepath().c_str());
956       if (!file) {
957         //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
958         ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
959       }
960       for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
961             fit != filerepos.end();
962             ++fit )
963       {
964           // if the alias is different, dump the original
965           // if it is the same, dump the provided one
966           if ( (*fit).alias() != toedit.alias() )
967             (*fit).dumpRepoOn(file);
968           else
969             newinfo.dumpRepoOn(file);
970       }
971     }
972   }
973
974   ////////////////////////////////////////////////////////////////////////////
975
976   RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
977                                            const ProgressData::ReceiverFnc & progressrcv )
978   {
979     std::list<RepoInfo> repos = knownRepositories();
980     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
981           it != repos.end();
982           ++it )
983     {
984       if ( (*it).alias() == alias )
985         return *it;
986     }
987     RepoInfo info;
988     info.setAlias(info.alias());
989     ZYPP_THROW(RepoNotFoundException(info));
990   }
991
992   ////////////////////////////////////////////////////////////////////////////
993
994   RepoInfo RepoManager::getRepositoryInfo( const Url & url,
995                                            const url::ViewOption & urlview,
996                                            const ProgressData::ReceiverFnc & progressrcv )
997   {
998     std::list<RepoInfo> repos = knownRepositories();
999     for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1000           it != repos.end();
1001           ++it )
1002     {
1003       for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1004           urlit != (*it).baseUrlsEnd();
1005           ++urlit)
1006       {
1007         if ((*urlit).asString(urlview) == url.asString(urlview))
1008           return *it;
1009       }
1010     }
1011     RepoInfo info;
1012     info.setAlias(info.alias());
1013     info.setBaseUrl(url);
1014     ZYPP_THROW(RepoNotFoundException(info));
1015   }
1016
1017   ////////////////////////////////////////////////////////////////////////////
1018
1019   std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1020   {
1021     return str << *obj._pimpl;
1022   }
1023
1024   /////////////////////////////////////////////////////////////////
1025 } // namespace zypp
1026 ///////////////////////////////////////////////////////////////////