aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/e_repository.cc
blob: 219a20a61144b007a4ddd2bea48aeb55b9c2ea73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 * Copyright (c) 2006 Danny van Dyk <kugelfang@gentoo.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

#include <paludis/repositories/e/aa_visitor.hh>
#include <paludis/repositories/e/e_key.hh>
#include <paludis/repositories/e/e_repository.hh>
#include <paludis/repositories/e/e_repository_profile.hh>
#include <paludis/repositories/e/e_repository_news.hh>
#include <paludis/repositories/e/e_repository_sets.hh>
#include <paludis/repositories/e/e_repository_exceptions.hh>
#include <paludis/repositories/e/e_repository_entries.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/repositories/e/use_desc.hh>
#include <paludis/repositories/e/layout.hh>

#ifdef ENABLE_QA
#  include <paludis/repositories/e/qa/qa_controller.hh>
#endif

#include <paludis/repository_info.hh>
#include <paludis/config_file.hh>
#include <paludis/distribution.hh>
#include <paludis/dep_spec.hh>
#include <paludis/environment.hh>
#include <paludis/hashed_containers.hh>
#include <paludis/hook.hh>
#include <paludis/match_package.hh>
#include <paludis/query.hh>
#include <paludis/repository_name_cache.hh>
#include <paludis/syncer.hh>
#include <paludis/action.hh>

#include <paludis/util/fs_entry.hh>
#include <paludis/util/iterator.hh>
#include <paludis/util/log.hh>
#include <paludis/util/random.hh>
#include <paludis/util/options.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/map.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/set.hh>
#include <paludis/util/tr1_functional.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/is_file_with_extension.hh>

#include <paludis/digests/md5.hh>
#include <paludis/digests/rmd160.hh>
#include <paludis/digests/sha256.hh>

#include <libwrapiter/libwrapiter_forward_iterator.hh>
#include <libwrapiter/libwrapiter_output_iterator.hh>

#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <vector>
#include <list>
#include <fstream>

#include <strings.h>
#include <ctype.h>

/** \file
 * Implementation of ERepository.
 *
 * \ingroup grperepository
 */

using namespace paludis;

typedef MakeHashedMap<QualifiedPackageName, std::list<tr1::shared_ptr<const PackageDepSpec> > >::Type RepositoryMaskMap;
typedef MakeHashedMultiMap<std::string, std::string>::Type MirrorMap;
typedef MakeHashedMap<QualifiedPackageName, tr1::shared_ptr<const PackageDepSpec> >::Type VirtualsMap;
typedef std::list<RepositoryEInterface::ProfilesDescLine> ProfilesDesc;

namespace paludis
{
    /**
     * Implementation data for a ERepository.
     *
     * \ingroup grperepository
     */
    template <>
    struct Implementation<ERepository>
    {
        ERepository * const repo;
        const ERepositoryParams params;

        tr1::shared_ptr<RepositoryNameCache> names_cache;

        mutable Mutex repo_mask_mutex;
        mutable RepositoryMaskMap repo_mask;
        mutable bool has_repo_mask;

        const std::map<QualifiedPackageName, QualifiedPackageName> provide_map;

        mutable Mutex arch_flags_mutex;
        mutable tr1::shared_ptr<UseFlagNameSet> arch_flags;

        mutable Mutex mirrors_mutex;
        mutable bool has_mirrors;
        mutable MirrorMap mirrors;

        mutable Mutex profiles_desc_mutex;
        mutable bool has_profiles_desc;
        mutable ProfilesDesc profiles_desc;

        mutable Mutex use_desc_mutex;
        mutable std::list<tr1::shared_ptr<UseDesc> > use_desc;

        mutable Mutex profile_ptr_mutex;
        mutable tr1::shared_ptr<ERepositoryProfile> profile_ptr;

        mutable Mutex news_ptr_mutex;
        mutable tr1::shared_ptr<ERepositoryNews> news_ptr;

        mutable tr1::shared_ptr<ERepositorySets> sets_ptr;
        mutable tr1::shared_ptr<erepository::ERepositoryEntries> entries_ptr;
        mutable tr1::shared_ptr<erepository::Layout> layout;

        Implementation(ERepository * const, const ERepositoryParams &);
        ~Implementation();

        void need_profiles() const;
        void need_profiles_desc() const;
    };

    Implementation<ERepository>::Implementation(ERepository * const r,
            const ERepositoryParams & p) :
        repo(r),
        params(p),
        names_cache(new RepositoryNameCache(p.names_cache, r)),
        has_repo_mask(false),
        has_mirrors(false),
        has_profiles_desc(false),
        sets_ptr(new ERepositorySets(params.environment, r, p)),
        entries_ptr(erepository::ERepositoryEntriesMaker::get_instance()->find_maker(
                    params.entry_format)(params.environment, r, p)),
        layout(erepository::LayoutMaker::get_instance()->find_maker(
                    params.layout)(r, params.location, entries_ptr, params.master_repository ?
                        make_shared_ptr(new FSEntry(params.master_repository->params().location)) :
                        tr1::shared_ptr<FSEntry>()))
    {
    }

    Implementation<ERepository>::~Implementation()
    {
    }

    void
    Implementation<ERepository>::need_profiles() const
    {
        Lock l(profile_ptr_mutex);

        if (profile_ptr)
            return;

        profile_ptr.reset(new ERepositoryProfile(
                    params.environment, repo, repo->name(), *params.profiles,
                    erepository::EAPIData::get_instance()->eapi_from_string(
                        params.eapi_when_unknown)->supported->ebuild_environment_variables->env_arch));
    }

    void
    Implementation<ERepository>::need_profiles_desc() const
    {
        Lock l(profiles_desc_mutex);

        if (has_profiles_desc)
            return;

        Context context("When loading profiles.desc:");

        bool found_one(false);
        tr1::shared_ptr<const FSEntrySequence> profiles_desc_files(layout->profiles_desc_files());
        for (FSEntrySequence::Iterator p(profiles_desc_files->begin()), p_end(profiles_desc_files->end()) ;
                p != p_end ; ++p)
        {
            if (! p->exists())
                continue;

            found_one = true;

            LineConfigFile f(*p, LineConfigFileOptions());
            for (LineConfigFile::Iterator line(f.begin()), line_end(f.end()) ; line != line_end ; ++line)
            {
                std::vector<std::string> tokens;
                WhitespaceTokeniser::get_instance()->tokenise(*line,
                        std::back_inserter(tokens));
                if (tokens.size() < 3)
                    continue;

                FSEntrySequence profiles;
                profiles.push_back(layout->profiles_base_dir() / tokens.at(1));
                profiles_desc.push_back(RepositoryEInterface::ProfilesDescLine::create()
                        .arch(tokens.at(0))
                        .path(*profiles.begin())
                        .status(tokens.at(2))
                        .profile(tr1::shared_ptr<ERepositoryProfile>(new ERepositoryProfile(
                                    params.environment, repo, repo->name(), profiles,
                                    erepository::EAPIData::get_instance()->eapi_from_string(
                                        params.eapi_when_unknown)->supported->ebuild_environment_variables->env_arch))));
            }
        }

        if (! found_one)
            throw ERepositoryConfigurationError("No profiles.desc found");

        has_profiles_desc = true;
    }
}

namespace
{
    RepositoryName
    fetch_repo_name(const FSEntry & tree_root)
    {
        try
        {
            do
            {
                FSEntry name_file(tree_root);
                name_file /= "profiles";
                name_file /= "repo_name";

                if (! name_file.is_regular_file())
                    break;

                LineConfigFile f(name_file, LineConfigFileOptions());
                if (f.begin() == f.end())
                    break;
                return RepositoryName(*f.begin());

            } while (false);
        }
        catch (...)
        {
        }

        std::string modified_location(tree_root.basename());
        std::replace(modified_location.begin(), modified_location.end(), '/', '-');

        Log::get_instance()->message(ll_qa, lc_no_context, "Couldn't open repo_name file in '"
                + stringify(tree_root) + "/profiles/', falling back to generated name 'x-" + modified_location +
                "' (ignore this message if you have yet to sync this repository).");

        return RepositoryName("x-" + modified_location);
    }
}

ERepository::ERepository(const ERepositoryParams & p) :
    Repository(fetch_repo_name(p.location),
            RepositoryCapabilities::create()
            .installed_interface(0)
            .sets_interface(this)
            .syncable_interface(this)
            .use_interface(this)
            .world_interface(0)
            .environment_variable_interface(this)
            .mirrors_interface(this)
            .virtuals_interface(DistributionData::get_instance()->distribution_from_string(
                    p.environment->default_distribution())->support_old_style_virtuals ? this : 0)
            .provides_interface(0)
            .destination_interface(p.enable_destinations ? this : 0)
            .licenses_interface(this)
            .make_virtuals_interface(0)
            .e_interface(this)
#ifdef ENABLE_QA
            .qa_interface(this)
#else
            .qa_interface(0)
#endif
            .hook_interface(this)
            .manifest_interface(this),
            p.entry_format),
    PrivateImplementationPattern<ERepository>(new Implementation<ERepository>(this, p))
{
    // the info_vars and info_pkgs info is only added on demand, since it's
    // fairly slow to calculate.
    tr1::shared_ptr<RepositoryInfoSection> config_info(new RepositoryInfoSection("Configuration information"));

    config_info->add_kv("entry_format", _imp->params.entry_format);
    config_info->add_kv("layout", _imp->params.layout);
    config_info->add_kv("location", stringify(_imp->params.location));
    config_info->add_kv("profiles", join(_imp->params.profiles->begin(),
                _imp->params.profiles->end(), " "));
    config_info->add_kv("cache", stringify(_imp->params.cache));
    config_info->add_kv("write_cache", stringify(_imp->params.write_cache));
    config_info->add_kv("names_cache", stringify(_imp->params.names_cache));
    config_info->add_kv("distdir", stringify(_imp->params.distdir));
    config_info->add_kv("eclassdirs", join(_imp->params.eclassdirs->begin(),
                _imp->params.eclassdirs->end(), " "));
    config_info->add_kv("securitydir", stringify(_imp->params.securitydir));
    config_info->add_kv("setsdir", stringify(_imp->params.setsdir));
    config_info->add_kv("newsdir", stringify(_imp->params.newsdir));
    config_info->add_kv("sync", _imp->params.sync);
    config_info->add_kv("sync_options", _imp->params.sync_options);
    config_info->add_kv("buildroot", stringify(_imp->params.buildroot));
    if (_imp->params.master_repository)
        config_info->add_kv("master_repository", stringify(_imp->params.master_repository->name()));
    config_info->add_kv("format", _imp->params.entry_format);
    config_info->add_kv("layout", _imp->params.layout);
    config_info->add_kv("eapi_when_unknown", _imp->params.eapi_when_unknown);
    config_info->add_kv("eapi_when_unspecified", _imp->params.eapi_when_unspecified);
    config_info->add_kv("profile_eapi", _imp->params.profile_eapi);

    _info->add_section(config_info);
}

ERepository::~ERepository()
{
}

bool
ERepository::do_has_category_named(const CategoryNamePart & c) const
{
    return _imp->layout->has_category_named(c);
}

bool
ERepository::do_has_package_named(const QualifiedPackageName & q) const
{
    return _imp->layout->has_package_named(q);
}

tr1::shared_ptr<const CategoryNamePartSet>
ERepository::do_category_names() const
{
    return _imp->layout->category_names();
}

tr1::shared_ptr<const QualifiedPackageNameSet>
ERepository::do_package_names(const CategoryNamePart & c) const
{
    return _imp->layout->package_names(c);
}

tr1::shared_ptr<const PackageIDSequence>
ERepository::do_package_ids(const QualifiedPackageName & n) const
{
    return _imp->layout->package_ids(n);
}

bool
ERepository::repository_masked(const PackageID & id) const
{
    Lock l(_imp->repo_mask_mutex);

    if (! _imp->has_repo_mask)
    {
        Context context("When querying repository mask for '" + stringify(id) + "':");

        tr1::shared_ptr<const FSEntrySequence> repository_mask_files(_imp->layout->repository_mask_files());
        for (FSEntrySequence::Iterator p(repository_mask_files->begin()), p_end(repository_mask_files->end()) ;
                p != p_end ; ++p)
        {
            Context context_local("When reading '" + stringify(*p) + "':");

            if (p->exists())
            {
                LineConfigFile ff(*p, LineConfigFileOptions());
                for (LineConfigFile::Iterator line(ff.begin()), line_end(ff.end()) ;
                        line != line_end ; ++line)
                {
                    try
                    {
                        tr1::shared_ptr<const PackageDepSpec> a(new PackageDepSpec(*line, pds_pm_eapi_0));
                        if (a->package_ptr())
                            _imp->repo_mask[*a->package_ptr()].push_back(a);
                        else
                            Log::get_instance()->message(ll_warning, lc_context, "Loading package mask spec '"
                                    + stringify(*line) + "' failed because specification does not restrict to a "
                                    "unique package");
                    }
                    catch (const Exception & e)
                    {
                        Log::get_instance()->message(ll_warning, lc_context, "Loading package mask spec '"
                                + stringify(*line) + "' failed due to exception '" + e.message() + "' ("
                                + e.what() + ")");
                    }
                }
            }
        }

        _imp->has_repo_mask = true;
    }

    RepositoryMaskMap::iterator r(_imp->repo_mask.find(id.name()));
    if (_imp->repo_mask.end() == r)
        return false;
    else
        for (IndirectIterator<std::list<tr1::shared_ptr<const PackageDepSpec> >::const_iterator, const PackageDepSpec>
                k(r->second.begin()), k_end(r->second.end()) ; k != k_end ; ++k)
            if (match_package(*_imp->params.environment, *k, id))
                return true;

    return false;
}

UseFlagState
ERepository::do_query_use(const UseFlagName & f, const PackageID & e) const
{
    _imp->need_profiles();
    return _imp->profile_ptr->use_state_ignoring_masks(f, e);
}

bool
ERepository::do_query_use_mask(const UseFlagName & u, const PackageID & e) const
{
    _imp->need_profiles();
    return _imp->profile_ptr->use_masked(u, e) ||
        (arch_flags()->end() != arch_flags()->find(u) && use_enabled != do_query_use(u, e));
}

bool
ERepository::do_query_use_force(const UseFlagName & u, const PackageID & e) const
{
    _imp->need_profiles();
    return _imp->profile_ptr->use_forced(u, e);
}

tr1::shared_ptr<const UseFlagNameSet>
ERepository::do_arch_flags() const
{
    Lock l(_imp->arch_flags_mutex);
    if (! _imp->arch_flags)
    {
        Context context("When loading arch list:");
        _imp->arch_flags.reset(new UseFlagNameSet);

        bool found_one(false);
        tr1::shared_ptr<const FSEntrySequence> arch_list_files(_imp->layout->arch_list_files());
        for (FSEntrySequence::Iterator p(arch_list_files->begin()), p_end(arch_list_files->end()) ;
                p != p_end ; ++p)
        {
            if (! p->exists())
                continue;

            LineConfigFile archs(*p, LineConfigFileOptions());
            std::copy(archs.begin(), archs.end(), create_inserter<UseFlagName>(_imp->arch_flags->inserter()));
            found_one = true;
        }

        if (! found_one)
        {
            Log::get_instance()->message(ll_qa, lc_no_context, "Couldn't find arch.list file for repository '"
                    + stringify(name()) + "', arch flags may incorrectly show up as unmasked");
        }
    }

    return _imp->arch_flags;
}

tr1::shared_ptr<FSEntry>
ERepository::do_license_exists(const std::string & license) const
{
    tr1::shared_ptr<FSEntry> p;

    if (_imp->params.master_repository)
    {
        FSEntry l(_imp->params.master_repository->params().location / "licenses" / license);
        if (l.exists() && l.is_regular_file_or_symlink_to_regular_file())
            p.reset(new FSEntry(l));
    }

    FSEntry l(_imp->params.location / "licenses" / license);
    if (l.exists() && l.is_regular_file_or_symlink_to_regular_file())
        p.reset(new FSEntry(l));

    return p;
}

void
ERepository::need_mirrors() const
{
    Lock l(_imp->mirrors_mutex);

    if (! _imp->has_mirrors)
    {
        bool found_one(false);
        tr1::shared_ptr<const FSEntrySequence> mirror_files(_imp->layout->mirror_files());
        for (FSEntrySequence::Iterator p(mirror_files->begin()), p_end(mirror_files->end()) ;
                p != p_end ; ++p)
        {
            if (p->exists())
            {
                LineConfigFile mirrors(*p, LineConfigFileOptions());
                for (LineConfigFile::Iterator line(mirrors.begin()) ; line != mirrors.end() ; ++line)
                {
                    std::vector<std::string> ee;
                    WhitespaceTokeniser::get_instance()->tokenise(*line, std::back_inserter(ee));
                    if (! ee.empty())
                    {
                        /* pick up to five random mirrors only */
                        static Random r;
                        std::random_shuffle(next(ee.begin()), ee.end(), r);
                        if (ee.size() > 6)
                            ee.resize(6);
                        for (std::vector<std::string>::const_iterator e(next(ee.begin())),
                                e_end(ee.end()) ; e != e_end ; ++e)
                            _imp->mirrors.insert(std::make_pair(ee.at(0), *e));
                    }
                }
            }

            found_one = true;
        }

        if (! found_one)
            Log::get_instance()->message(ll_warning, lc_no_context,
                    "No thirdpartymirrors file found in '"
                    + stringify(_imp->params.location / "profiles") + "', so mirror:// SRC_URI "
                    "components cannot be fetched");

        _imp->has_mirrors = true;
    }
}

tr1::shared_ptr<SetSpecTree::ConstItem>
ERepository::do_package_set(const SetName & s) const
{
    if (s.data() == "system")
    {
        _imp->need_profiles();
        return _imp->profile_ptr->system_packages();
    }

    return _imp->sets_ptr->package_set(s);
}

tr1::shared_ptr<const SetNameSet>
ERepository::sets_list() const
{
    return _imp->sets_ptr->sets_list();
}

bool
ERepository::do_sync() const
{
    Context context("When syncing repository '" + stringify(name()) + "':");

    if (_imp->params.sync.empty())
        return false;

    std::list<std::string> sync_list;
    WhitespaceTokeniser::get_instance()->tokenise(_imp->params.sync, std::back_inserter(sync_list));

    bool ok(false);
    for (std::list<std::string>::const_iterator s(sync_list.begin()),
            s_end(sync_list.end()) ; s != s_end ; ++s)
    {
        DefaultSyncer syncer(SyncerParams::create()
                                .environment(_imp->params.environment)
                                .local(stringify(_imp->params.location))
                                .remote(*s));
        SyncOptions opts(_imp->params.sync_options);
        try
        {
            syncer.sync(opts);
        }
        catch (const SyncFailedError & e)
        {
            continue;
        }

        ok = true;
        break;
    }

    if (! ok)
        throw SyncFailedError(stringify(_imp->params.location), _imp->params.sync);

    return true;
}

void
ERepository::invalidate()
{
    _imp.reset(new Implementation<ERepository>(this, _imp->params));
}

void
ERepository::update_news() const
{
    Lock l(_imp->news_ptr_mutex);

    if (! _imp->news_ptr)
        _imp->news_ptr.reset(new ERepositoryNews(_imp->params.environment, this, _imp->params));

    _imp->news_ptr->update_news();
}

const tr1::shared_ptr<const erepository::Layout>
ERepository::layout() const
{
    return _imp->layout;
}

const tr1::shared_ptr<const ERepositoryProfile>
ERepository::profile() const
{
    _imp->need_profiles();
    return _imp->profile_ptr;
}

const tr1::shared_ptr<const erepository::ERepositoryEntries>
ERepository::entries() const
{
    return _imp->entries_ptr;
}

std::string
ERepository::get_environment_variable(
        const tr1::shared_ptr<const PackageID> & for_package,
        const std::string & var) const
{
    Context context("When fetching environment variable '" + var + "' from repository '"
            + stringify(name()) + "':");

    _imp->need_profiles();

    return _imp->entries_ptr->get_environment_variable(tr1::static_pointer_cast<const erepository::ERepositoryID>(for_package),
            var, _imp->profile_ptr);
}

tr1::shared_ptr<const RepositoryInfo>
ERepository::info(bool verbose) const
{
    tr1::shared_ptr<const RepositoryInfo> result_non_verbose(Repository::info(verbose));
    if (! verbose)
        return result_non_verbose;

    tr1::shared_ptr<RepositoryInfo> result(new RepositoryInfo);

    for (RepositoryInfo::SectionIterator s(result_non_verbose->begin_sections()),
            s_end(result_non_verbose->end_sections()) ; s != s_end ; ++s)
        result->add_section(*s);

    // don't inherit from master_repository, just causes clutter
    std::set<std::string> info_pkgs;
    if ((_imp->layout->info_packages_file(_imp->params.location / "profiles")).exists())
    {
        LineConfigFile vars(_imp->layout->info_packages_file(_imp->params.location / "profiles"), LineConfigFileOptions());
        info_pkgs.insert(vars.begin(), vars.end());
    }

    if (! info_pkgs.empty())
    {
        tr1::shared_ptr<RepositoryInfoSection> package_info(new RepositoryInfoSection("Package information"));
        for (std::set<std::string>::const_iterator i(info_pkgs.begin()),
                i_end(info_pkgs.end()) ; i != i_end ; ++i)
        {
            tr1::shared_ptr<const PackageIDSequence> q(
                    _imp->params.environment->package_database()->query(
                        query::Matches(PackageDepSpec(*i, pds_pm_eapi_0)) &
                        query::InstalledAtRoot(_imp->params.environment->root()),
                        qo_order_by_version));
            if (q->empty())
                package_info->add_kv(*i, "(none)");
            else
            {
                using namespace tr1::placeholders;
                std::set<VersionSpec> versions;
                std::copy(q->begin(), q->end(),
                        transform_inserter(std::inserter(versions, versions.begin()),
                            tr1::bind<const VersionSpec>(tr1::mem_fn(&PackageID::version), _1)));
                package_info->add_kv(*i, join(versions.begin(), versions.end(), ", "));
            }
        }

        result->add_section(package_info);
    }

#if 0
    // don't inherit from master_repository, just causes clutter
    std::set<std::string> info_vars;
    if (_imp->layout->info_variables_file(_imp->params.location / "profiles").exists())
    {
        LineConfigFile vars(_imp->layout->info_variables_file(_imp->params.location / "profiles"), LineConfigFileOptions());
        info_vars.insert(vars.begin(), vars.end());
    }

    if (! info_vars.empty() && ! info_pkgs.empty() &&
            ! package_ids(QualifiedPackageName(*info_pkgs.begin()))->empty())
    {
        PackageDatabaseEntry e(QualifiedPackageName(*info_pkgs.begin()),
                *package_ids(QualifiedPackageName(*info_pkgs.begin()))->last(),
                name());
        tr1::shared_ptr<RepositoryInfoSection> variable_info(new RepositoryInfoSection("Variable information"));
        for (std::set<std::string>::const_iterator i(info_vars.begin()),
                i_end(info_vars.end()) ; i != i_end ; ++i)
            variable_info->add_kv(*i, get_environment_variable(e, *i));

        result->add_section(variable_info);
    }
    else if (! info_vars.empty())
        Log::get_instance()->message(ll_warning, lc_no_context,
                "Skipping info_vars for '" + stringify(name()) +
                "' because info_pkgs is not usable");
#endif

    return result;
}

std::string
ERepository::profile_variable(const std::string & s) const
{
    _imp->need_profiles();

    return _imp->profile_ptr->environment_variable(s);
}

ERepository::MirrorsIterator
ERepository::begin_mirrors(const std::string & s) const
{
    need_mirrors();
    return MirrorsIterator(_imp->mirrors.equal_range(s).first);
}

ERepository::MirrorsIterator
ERepository::end_mirrors(const std::string & s) const
{
    need_mirrors();
    return MirrorsIterator(_imp->mirrors.equal_range(s).second);
}

tr1::shared_ptr<const ERepository::VirtualsSequence>
ERepository::virtual_packages() const
{
    Context context("When loading virtual packages for repository '" +
            stringify(name()) + "'");

    _imp->need_profiles();

    tr1::shared_ptr<VirtualsSequence> result(new VirtualsSequence);

    for (ERepositoryProfile::VirtualsIterator i(_imp->profile_ptr->begin_virtuals()),
            i_end(_imp->profile_ptr->end_virtuals()) ; i != i_end ; ++i)
        result->push_back(RepositoryVirtualsEntry::create()
                .provided_by_spec(i->second)
                .virtual_name(i->first));

    return result;
}

tr1::shared_ptr<const UseFlagNameSet>
ERepository::do_use_expand_flags() const
{
    _imp->need_profiles();

    std::string expand_sep(stringify(erepository::EAPIData::get_instance()->eapi_from_string(_imp->params.profile_eapi
                    )->supported->ebuild_options->use_expand_separator));
    tr1::shared_ptr<UseFlagNameSet> result(new UseFlagNameSet);
    for (ERepositoryProfile::UseExpandIterator i(_imp->profile_ptr->begin_use_expand()),
            i_end(_imp->profile_ptr->end_use_expand()) ; i != i_end ; ++i)
    {
        std::list<std::string> values;
        WhitespaceTokeniser::get_instance()->tokenise(_imp->profile_ptr->environment_variable(
                    stringify(*i)), std::back_inserter(values));
        for (std::list<std::string>::const_iterator j(values.begin()), j_end(values.end()) ;
                j != j_end ; ++j)
        {
            std::string f(stringify(*i) + expand_sep + *j), lower_f;
            std::transform(f.begin(), f.end(), std::back_inserter(lower_f), &::tolower);
            result->insert(UseFlagName(lower_f));
        }
    }

    return result;
}

tr1::shared_ptr<const UseFlagNameSet>
ERepository::do_use_expand_prefixes() const
{
    _imp->need_profiles();

    tr1::shared_ptr<UseFlagNameSet> result(new UseFlagNameSet);
    for (ERepositoryProfile::UseExpandIterator i(_imp->profile_ptr->begin_use_expand()),
            i_end(_imp->profile_ptr->end_use_expand()) ; i != i_end ; ++i)
    {
        std::string lower_i;
        std::transform(i->data().begin(), i->data().end(), std::back_inserter(lower_i), &::tolower);
        result->insert(UseFlagName(lower_i));
    }

    return result;
}

tr1::shared_ptr<const UseFlagNameSet>
ERepository::do_use_expand_hidden_prefixes() const
{
    _imp->need_profiles();

    tr1::shared_ptr<UseFlagNameSet> result(new UseFlagNameSet);
    for (ERepositoryProfile::UseExpandIterator i(_imp->profile_ptr->begin_use_expand_hidden()),
            i_end(_imp->profile_ptr->end_use_expand_hidden()) ; i != i_end ; ++i)
    {
        std::string lower_i;
        std::transform(i->data().begin(), i->data().end(), std::back_inserter(lower_i), &::tolower);
        result->insert(UseFlagName(lower_i));
    }

    return result;
}

void
ERepository::regenerate_cache() const
{
    _imp->names_cache->regenerate_cache();
}

tr1::shared_ptr<const CategoryNamePartSet>
ERepository::do_category_names_containing_package(const PackageNamePart & p) const
{
    if (! _imp->names_cache->usable())
        return Repository::do_category_names_containing_package(p);

    tr1::shared_ptr<const CategoryNamePartSet> result(
            _imp->names_cache->category_names_containing_package(p));

    return result ? result : Repository::do_category_names_containing_package(p);
}

ERepository::ProfilesIterator
ERepository::begin_profiles() const
{
    _imp->need_profiles_desc();
    return ProfilesIterator(_imp->profiles_desc.begin());
}

ERepository::ProfilesIterator
ERepository::end_profiles() const
{
    _imp->need_profiles_desc();
    return ProfilesIterator(_imp->profiles_desc.end());
}

ERepository::ProfilesIterator
ERepository::find_profile(const FSEntry & location) const
{
    _imp->need_profiles_desc();
    for (ProfilesDesc::const_iterator i(_imp->profiles_desc.begin()),
            i_end(_imp->profiles_desc.end()) ; i != i_end ; ++i)
        if (i->path == location)
            return ProfilesIterator(i);
    return ProfilesIterator(_imp->profiles_desc.end());
}

void
ERepository::set_profile(const ProfilesIterator & iter)
{
    Context context("When setting profile by iterator:");

    _imp->profile_ptr = iter->profile;

    if (DistributionData::get_instance()->distribution_from_string(_imp->params.environment->default_distribution())->support_old_style_virtuals)
        if (_imp->params.environment->package_database()->has_repository_named(RepositoryName("virtuals")))
            _imp->params.environment->package_database()->fetch_repository(
                    RepositoryName("virtuals"))->invalidate();
}

void
ERepository::set_profile_by_arch(const UseFlagName & arch)
{
    Context context("When setting profile by arch '" + stringify(arch) + "':");

    for (ProfilesIterator p(begin_profiles()), p_end(end_profiles()) ; p != p_end ; ++p)
        if (p->arch == stringify(arch) && p->status == "stable")
        {
            set_profile(p);
            return;
        }

    for (ProfilesIterator p(begin_profiles()), p_end(end_profiles()) ; p != p_end ; ++p)
        if (p->arch == stringify(arch))
        {
            set_profile(p);
            return;
        }

    throw ConfigurationError("Cannot find a profile appropriate for '" + stringify(arch) + "'");
}

std::string
ERepository::do_describe_use_flag(const UseFlagName & f,
        const PackageID & e) const
{
    Lock l(_imp->use_desc_mutex);

    if (_imp->use_desc.empty())
    {
        std::string expand_sep(stringify(erepository::EAPIData::get_instance()->eapi_from_string(
                        _imp->params.profile_eapi)->supported->ebuild_options->use_expand_separator));
        tr1::shared_ptr<const FSEntrySequence> use_desc_dirs(_imp->layout->use_desc_dirs());
        for (FSEntrySequence::Iterator p(use_desc_dirs->begin()), p_end(use_desc_dirs->end()) ;
                p != p_end ; ++p)
            _imp->use_desc.push_back(tr1::shared_ptr<UseDesc>(new UseDesc(*p, expand_sep)));
    }

    std::string result;
    for (std::list<tr1::shared_ptr<UseDesc> >::const_iterator i(_imp->use_desc.begin()),
            i_end(_imp->use_desc.end()) ; i != i_end ; ++i)
    {
        std::string new_result((*i)->describe(f, e));
        if (! new_result.empty())
            result = new_result;
    }
    return result;
}

const ERepositoryParams &
ERepository::params() const
{
    return _imp->params;
}

bool
ERepository::is_suitable_destination_for(const PackageID & e) const
{
    std::string f(e.repository()->format());
    return f == "ebuild" || f == "ebin";
}

bool
ERepository::is_default_destination() const
{
    return false;
}

bool
ERepository::want_pre_post_phases() const
{
    return false;
}

void
ERepository::merge(const MergeOptions & o)
{
    _imp->entries_ptr->merge(o);
}

HookResult
ERepository::perform_hook(const Hook & hook) const
{
    Context context("When performing hook '" + stringify(hook.name()) + "' for repository '"
            + stringify(name()) + "':");

    if (hook.name() == "sync_all_post"
            || hook.name() == "install_all_post"
            || hook.name() == "uninstall_all_post")
        update_news();

    return HookResult(0, "");
}

void
ERepository::check_qa(
        QAReporter & reporter,
        const QACheckProperties & ignore_if,
        const QACheckProperties & ignore_unless,
        const QAMessageLevel minimum_level,
        const FSEntry & dir
        ) const
{
#ifdef ENABLE_QA
    Context c("When performing QA checks for '" + stringify(dir) + "':");

    erepository::QAController controller(
            _imp->params.environment,
            shared_from_this(),
            ignore_if,
            ignore_unless,
            minimum_level,
            reporter);

    controller.run();

#endif
}

namespace
{
    struct SupportsActionQuery :
        ConstVisitor<SupportsActionTestVisitorTypes>
    {
        bool result;

        SupportsActionQuery() :
            result(false)
        {
        }

        void visit(const SupportsActionTest<InstalledAction> &)
        {
        }

        void visit(const SupportsActionTest<InstallAction> &)
        {
            result = true;
        }

        void visit(const SupportsActionTest<ConfigAction> &)
        {
        }

        void visit(const SupportsActionTest<PretendAction> &)
        {
            result = true;
        }

        void visit(const SupportsActionTest<UninstallAction> &)
        {
        }
    };
}

bool
ERepository::do_some_ids_might_support_action(const SupportsActionTestBase & a) const
{
    SupportsActionQuery q;
    a.accept(q);
    return q.result;
}

void
ERepository::make_manifest(const QualifiedPackageName & qpn)
{
    FSEntry package_dir = _imp->layout->package_directory(qpn);

    FSEntry(package_dir / "Manifest").unlink();
    std::ofstream manifest(stringify(FSEntry(package_dir 
                    / "Manifest")).c_str());
    if (! manifest)
        throw ERepositoryConfigurationError("Couldn't open Manifest for writing.");

    tr1::shared_ptr<Map<FSEntry, std::string> > files = _imp->layout->manifest_files(qpn);

    for (Map<FSEntry, std::string>::Iterator f(files->begin()) ;
            f != files->end() ; ++f)
    {
        FSEntry file(f->first);
        std::string filename = file.basename();
        std::string file_type(f->second);

        if ("AUX" == file_type)
        {
            filename = stringify(file).substr(stringify(package_dir / "files").length()+1);
        }

        std::ifstream file_stream(stringify(file).c_str());
        if (! file_stream)
            throw ERepositoryConfigurationError("Couldn't read " 
                    + stringify(file));

        RMD160 rmd160sum(file_stream);
        manifest << file_type << " " << filename << " "
            << file.file_size() << " RMD160 " << rmd160sum.hexsum();

        file_stream.clear();
        file_stream.seekg(0, std::ios::beg);
        SHA256 sha256sum(file_stream);
        manifest << " SHA256 " << sha256sum.hexsum() << std::endl;
    }

    tr1::shared_ptr<const PackageIDSequence> versions;
    versions = package_ids(qpn);

    std::set<std::string> done_files;

    for (PackageIDSequence::Iterator v(versions->begin()),
            v_end(versions->end()) ;
            v != v_end ; ++v)
    {
        tr1::shared_ptr<const PackageID> id = (*v);
        if (! id->src_uri_key())
            continue;
        paludis::erepository::AAVisitor aa;
        id->src_uri_key()->value()->accept(aa);

        for (paludis::erepository::AAVisitor::Iterator d(aa.begin()) ;
                d != aa.end() ; ++d)
        {
            if (done_files.count(*d))
                continue;
            done_files.insert(*d);

            FSEntry f(params().distdir / *d);

            std::ifstream file_stream(stringify(f).c_str());
            if (! file_stream)
                throw ERepositoryConfigurationError("Couldn't read "
                        + stringify(f));

            RMD160 rmd160sum(file_stream);
            manifest << "DIST " << f.basename() << " "
                << f.file_size()
                << " RMD160 " << rmd160sum.hexsum();

            file_stream.clear();
            file_stream.seekg(0, std::ios::beg);
            SHA256 sha256sum(file_stream);
            manifest << " SHA256 " << sha256sum.hexsum()
                << std::endl;
        }
    }
}

std::string
ERepository::accept_keywords_variable() const
{
    return erepository::EAPIData::get_instance()->eapi_from_string(
            params().profile_eapi)->supported->ebuild_environment_variables->env_accept_keywords;
}

std::string
ERepository::arch_variable() const
{
    return erepository::EAPIData::get_instance()->eapi_from_string(
            params().profile_eapi)->supported->ebuild_environment_variables->env_arch;
}