aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/e_key.cc
blob: e8fbacefdd9175b83f28dd41c1102d2e03a887fb (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
 *
 * 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 <paludis/repositories/e/e_key.hh>
#include <paludis/repositories/e/ebuild_id.hh>
#include <paludis/repositories/e/dep_parser.hh>
#include <paludis/repositories/e/parse_uri_label.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/repositories/e/vdb_contents_tokeniser.hh>
#include <paludis/repositories/e/e_repository.hh>
#include <paludis/repositories/e/myoption.hh>
#include <paludis/repositories/e/spec_tree_pretty_printer.hh>

#include <paludis/util/pretty_print.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/log.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/join.hh>
#include <paludis/util/create_iterator-impl.hh>
#include <paludis/util/tribool.hh>
#include <paludis/util/member_iterator-impl.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/timestamp.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/util/fs_error.hh>

#include <paludis/contents.hh>
#include <paludis/repository.hh>
#include <paludis/environment.hh>
#include <paludis/dep_spec_flattener.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/call_pretty_printer.hh>

#include <algorithm>
#include <functional>
#include <list>
#include <vector>

using namespace paludis;
using namespace paludis::erepository;

namespace paludis
{
    template <>
    struct Imp<EDependenciesKey>
    {
        const Environment * const env;
        const std::shared_ptr<const ERepositoryID> id;
        const std::string string_value;
        const std::shared_ptr<const DependenciesLabelSequence> labels;

        const std::string raw_name;
        const std::string human_name;
        const MetadataKeyType type;

        Imp(
                const Environment * const e,
                const std::shared_ptr<const ERepositoryID> & i, const std::string & v,
                const std::shared_ptr<const DependenciesLabelSequence> & s,
                const std::string & r, const std::string & h, const MetadataKeyType & t) :
            env(e),
            id(i),
            string_value(v),
            labels(s),
            raw_name(r),
            human_name(h),
            type(t)
        {
        }
    };
}

EDependenciesKey::EDependenciesKey(
        const Environment * const e,
        const std::shared_ptr<const ERepositoryID> & id,
        const std::string & r, const std::string & h, const std::string & v,
        const std::shared_ptr<const DependenciesLabelSequence> & l, const MetadataKeyType t) :
    _imp(e, id, v, l, r, h, t)
{
}

EDependenciesKey::~EDependenciesKey()
{
}

const std::shared_ptr<const DependencySpecTree>
EDependenciesKey::parse_value() const
{
    Context context("When parsing metadata key '" + raw_name() + "' from '" + stringify(*_imp->id) + "':");
    return parse_depend(_imp->string_value, _imp->env, *_imp->id->eapi(), _imp->id->is_installed());
}

const std::shared_ptr<const DependenciesLabelSequence>
EDependenciesKey::initial_labels() const
{
    return _imp->labels;
}

const std::string
EDependenciesKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::string
EDependenciesKey::raw_name() const
{
    return _imp->raw_name;
}

const std::string
EDependenciesKey::human_name() const
{
    return _imp->human_name;
}

MetadataKeyType
EDependenciesKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<ELicenseKey>
    {
        const Environment * const env;
        const std::string string_value;

        const std::shared_ptr<const EAPIMetadataVariable> variable;
        const std::shared_ptr<const EAPI> eapi;
        const MetadataKeyType type;
        const bool is_installed;

        Imp(const Environment * const e,
                const std::string & v,
                const std::shared_ptr<const EAPIMetadataVariable> & m,
                const std::shared_ptr<const EAPI> & p,
                const MetadataKeyType t,
                bool i) :
            env(e),
            string_value(v),
            variable(m),
            eapi(p),
            type(t),
            is_installed(i)
        {
        }
    };
}

ELicenseKey::ELicenseKey(
        const Environment * const e,
        const std::shared_ptr<const EAPIMetadataVariable> & m,
        const std::shared_ptr<const EAPI> & p,
        const std::string & v, const MetadataKeyType t,
        const bool i) :
    _imp(e, v, m, p, t, i)
{
}

ELicenseKey::~ELicenseKey()
{
}

const std::shared_ptr<const LicenseSpecTree>
ELicenseKey::parse_value() const
{
    Context context("When parsing metadata key '" + raw_name() + "':");
    return parse_license(_imp->string_value, _imp->env, *_imp->eapi, _imp->is_installed);
}

const std::string
ELicenseKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::string
ELicenseKey::raw_name() const
{
    return _imp->variable->name();
}

const std::string
ELicenseKey::human_name() const
{
    return _imp->variable->description();
}

MetadataKeyType
ELicenseKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<EFetchableURIKey>
    {
        const Environment * const env;
        const std::shared_ptr<const ERepositoryID> id;
        const std::shared_ptr<const EAPIMetadataVariable> variable;
        const std::string string_value;
        const MetadataKeyType type;

        Imp(const Environment * const e, const std::shared_ptr<const ERepositoryID> & i,
                const std::shared_ptr<const EAPIMetadataVariable> & m, const std::string & v,
                const MetadataKeyType t) :
            env(e),
            id(i),
            variable(m),
            string_value(v),
            type(t)
        {
        }
    };
}

EFetchableURIKey::EFetchableURIKey(const Environment * const e,
        const std::shared_ptr<const ERepositoryID> & id,
        const std::shared_ptr<const EAPIMetadataVariable> & m, const std::string & v, const MetadataKeyType t) :
    _imp(e, id, m, v, t)
{
}

EFetchableURIKey::~EFetchableURIKey()
{
}

const std::shared_ptr<const FetchableURISpecTree>
EFetchableURIKey::parse_value() const
{
    Context context("When parsing metadata key '" + raw_name() + "' from '" + stringify(*_imp->id) + "':");
    return parse_fetchable_uri(_imp->string_value, _imp->env, *_imp->id->eapi(), _imp->id->is_installed());
}

const std::string
EFetchableURIKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::shared_ptr<const URILabel>
EFetchableURIKey::initial_label() const
{
    std::shared_ptr<const URILabel> result;

    DepSpecFlattener<PlainTextSpecTree, PlainTextDepSpec> f(_imp->env, _imp->id);
    if (_imp->id->restrict_key())
        _imp->id->restrict_key()->parse_value()->top()->accept(f);

    for (DepSpecFlattener<PlainTextSpecTree, PlainTextDepSpec>::ConstIterator i(f.begin()), i_end(f.end()) ;
            i != i_end ; ++i)
    {
        if (_imp->id->eapi()->supported()->ebuild_options()->restrict_fetch()->end() !=
                std::find(_imp->id->eapi()->supported()->ebuild_options()->restrict_fetch()->begin(),
                    _imp->id->eapi()->supported()->ebuild_options()->restrict_fetch()->end(), (*i)->text()))
            result = *parse_uri_label("default-restrict-fetch:", *_imp->id->eapi())->begin();

        else if (_imp->id->eapi()->supported()->ebuild_options()->restrict_mirror()->end() !=
                std::find(_imp->id->eapi()->supported()->ebuild_options()->restrict_mirror()->begin(),
                    _imp->id->eapi()->supported()->ebuild_options()->restrict_mirror()->end(), (*i)->text()))
            result = *parse_uri_label("default-restrict-mirror:", *_imp->id->eapi())->begin();

        else if (_imp->id->eapi()->supported()->ebuild_options()->restrict_primaryuri()->end() !=
                std::find(_imp->id->eapi()->supported()->ebuild_options()->restrict_primaryuri()->begin(),
                    _imp->id->eapi()->supported()->ebuild_options()->restrict_primaryuri()->end(), (*i)->text()))
            result = *parse_uri_label("default-restrict-primaryuri:", *_imp->id->eapi())->begin();
    }

    if (! result)
        result = *parse_uri_label("default:", *_imp->id->eapi())->begin();

    return result;
}

const std::string
EFetchableURIKey::raw_name() const
{
    return _imp->variable->name();
}

const std::string
EFetchableURIKey::human_name() const
{
    return _imp->variable->description();
}

MetadataKeyType
EFetchableURIKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<ESimpleURIKey>
    {
        const Environment * const env;
        const std::string string_value;
        mutable Mutex value_mutex;
        mutable std::shared_ptr<const SimpleURISpecTree> value;

        const std::shared_ptr<const EAPIMetadataVariable> variable;
        const std::shared_ptr<const EAPI> eapi;
        const MetadataKeyType type;
        const bool is_installed;

        Imp(const Environment * const e, const std::string & v,
                const std::shared_ptr<const EAPIMetadataVariable> & m,
                const std::shared_ptr<const EAPI> & p,
                const MetadataKeyType t, bool i) :
            env(e),
            string_value(v),
            variable(m),
            eapi(p),
            type(t),
            is_installed(i)
        {
        }
    };
}

ESimpleURIKey::ESimpleURIKey(const Environment * const e,
        const std::shared_ptr<const EAPIMetadataVariable> & m,
        const std::shared_ptr<const EAPI> & p,
        const std::string & v, const MetadataKeyType t,
        const bool i) :
    _imp(e, v, m, p, t, i)
{
}

ESimpleURIKey::~ESimpleURIKey()
{
}

const std::shared_ptr<const SimpleURISpecTree>
ESimpleURIKey::parse_value() const
{
    Lock l(_imp->value_mutex);

    if (_imp->value)
        return _imp->value;

    Context context("When parsing metadata key '" + raw_name() + "':");
    _imp->value = parse_simple_uri(_imp->string_value, _imp->env, *_imp->eapi, _imp->is_installed);
    return _imp->value;
}

const std::string
ESimpleURIKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::string
ESimpleURIKey::raw_name() const
{
    return _imp->variable->name();
}

const std::string
ESimpleURIKey::human_name() const
{
    return _imp->variable->description();
}

MetadataKeyType
ESimpleURIKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<EPlainTextSpecKey>
    {
        const Environment * const env;
        const std::string string_value;
        mutable Mutex value_mutex;
        mutable std::shared_ptr<const PlainTextSpecTree> value;

        const std::shared_ptr<const EAPIMetadataVariable> variable;
        const std::shared_ptr<const EAPI> eapi;
        const MetadataKeyType type;
        const bool is_installed;

        Imp(const Environment * const e, const std::string & v,
                const std::shared_ptr<const EAPIMetadataVariable> & m,
                const std::shared_ptr<const EAPI> & p,
                const MetadataKeyType t, bool i) :
            env(e),
            string_value(v),
            variable(m),
            eapi(p),
            type(t),
            is_installed(i)
        {
        }
    };
}

EPlainTextSpecKey::EPlainTextSpecKey(const Environment * const e,
        const std::shared_ptr<const EAPIMetadataVariable> & m,
        const std::shared_ptr<const EAPI> & p,
        const std::string & v, const MetadataKeyType t, bool i) :
    _imp(e, v, m, p, t, i)
{
}

EPlainTextSpecKey::~EPlainTextSpecKey()
{
}

const std::shared_ptr<const PlainTextSpecTree>
EPlainTextSpecKey::parse_value() const
{
    Lock l(_imp->value_mutex);

    if (_imp->value)
        return _imp->value;

    Context context("When parsing metadata key '" + raw_name() + "':");
    _imp->value = parse_plain_text(_imp->string_value, _imp->env, *_imp->eapi, _imp->is_installed);
    return _imp->value;
}

const std::string
EPlainTextSpecKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::string
EPlainTextSpecKey::raw_name() const
{
    return _imp->variable->name();
}

const std::string
EPlainTextSpecKey::human_name() const
{
    return _imp->variable->description();
}

MetadataKeyType
EPlainTextSpecKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<EMyOptionsKey>
    {
        const Environment * const env;
        const std::string string_value;
        mutable Mutex value_mutex;
        mutable std::shared_ptr<const PlainTextSpecTree> value;

        const std::shared_ptr<const EAPIMetadataVariable> variable;
        const std::shared_ptr<const EAPI> eapi;
        const MetadataKeyType type;
        const bool is_installed;

        Imp(const Environment * const e,
                const std::string & v,
                const std::shared_ptr<const EAPIMetadataVariable> & m,
                const std::shared_ptr<const EAPI> & p,
                const MetadataKeyType t, const bool i) :
            env(e),
            string_value(v),
            variable(m),
            eapi(p),
            type(t),
            is_installed(i)
        {
        }
    };
}

EMyOptionsKey::EMyOptionsKey(
        const Environment * const e,
        const std::shared_ptr<const EAPIMetadataVariable> & m,
        const std::shared_ptr<const EAPI> & p,
        const std::string & v, const MetadataKeyType t,
        const bool i) :
    _imp(e, v, m, p, t, i)
{
}

EMyOptionsKey::~EMyOptionsKey()
{
}

const std::shared_ptr<const PlainTextSpecTree>
EMyOptionsKey::parse_value() const
{
    Lock l(_imp->value_mutex);
    if (_imp->value)
        return _imp->value;

    Context context("When parsing metadata key '" + raw_name() + "':");
    _imp->value = parse_myoptions(_imp->string_value, _imp->env, *_imp->eapi, _imp->is_installed);
    return _imp->value;
}

const std::string
EMyOptionsKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::string
EMyOptionsKey::raw_name() const
{
    return _imp->variable->name();
}

const std::string
EMyOptionsKey::human_name() const
{
    return _imp->variable->description();
}

MetadataKeyType
EMyOptionsKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<ERequiredUseKey>
    {
        const Environment * const env;
        const std::string string_value;
        mutable Mutex value_mutex;
        mutable std::shared_ptr<const RequiredUseSpecTree> value;

        const std::shared_ptr<const EAPIMetadataVariable> variable;
        const std::shared_ptr<const EAPI> eapi;
        const MetadataKeyType type;
        const bool is_installed;

        Imp(const Environment * const e,
                const std::string & v,
                const std::shared_ptr<const EAPIMetadataVariable> & m,
                const std::shared_ptr<const EAPI> & p,
                const MetadataKeyType t,
                bool i) :
            env(e),
            string_value(v),
            variable(m),
            eapi(p),
            type(t),
            is_installed(i)
        {
        }
    };
}

ERequiredUseKey::ERequiredUseKey(
        const Environment * const e,
        const std::shared_ptr<const EAPIMetadataVariable> & m,
        const std::shared_ptr<const EAPI> & p,
        const std::string & v, const MetadataKeyType t,
        const bool i) :
    _imp(e, v, m, p, t, i)
{
}

ERequiredUseKey::~ERequiredUseKey()
{
}

const std::shared_ptr<const RequiredUseSpecTree>
ERequiredUseKey::parse_value() const
{
    Lock l(_imp->value_mutex);
    if (_imp->value)
        return _imp->value;

    Context context("When parsing metadata key '" + raw_name() + "':");
    _imp->value = parse_required_use(_imp->string_value, _imp->env, *_imp->eapi, _imp->is_installed);
    return _imp->value;
}

const std::string
ERequiredUseKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::string
ERequiredUseKey::raw_name() const
{
    return _imp->variable->name();
}

const std::string
ERequiredUseKey::human_name() const
{
    return _imp->variable->description();
}

MetadataKeyType
ERequiredUseKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<EProvideKey>
    {
        const Environment * const env;
        const std::string string_value;
        mutable Mutex value_mutex;
        mutable std::shared_ptr<const ProvideSpecTree> value;

        const std::shared_ptr<const EAPIMetadataVariable> variable;
        const std::shared_ptr<const EAPI> eapi;
        const MetadataKeyType type;
        const bool is_installed;

        Imp(const Environment * const e,
                const std::string & v,
                const std::shared_ptr<const EAPIMetadataVariable> & m,
                const std::shared_ptr<const EAPI> & p,
                const MetadataKeyType t, bool i) :
            env(e),
            string_value(v),
            variable(m),
            eapi(p),
            type(t),
            is_installed(i)
        {
        }
    };
}

EProvideKey::EProvideKey(
        const Environment * const e,
        const std::shared_ptr<const EAPIMetadataVariable> & m,
        const std::shared_ptr<const EAPI> & p,
        const std::string & v, const MetadataKeyType t, const bool i) :
    _imp(e, v, m, p, t, i)
{
}

EProvideKey::~EProvideKey()
{
}

const std::shared_ptr<const ProvideSpecTree>
EProvideKey::parse_value() const
{
    Lock l(_imp->value_mutex);
    if (_imp->value)
        return _imp->value;

    Context context("When parsing metadata key '" + raw_name() + "':");
    _imp->value = parse_provide(_imp->string_value, _imp->env, *_imp->eapi, _imp->is_installed);
    return _imp->value;
}

const std::string
EProvideKey::pretty_print_value(
        const PrettyPrinter & pretty_printer,
        const PrettyPrintOptions & options) const
{
    SpecTreePrettyPrinter p(pretty_printer, options);
    parse_value()->top()->accept(p);
    return stringify(p);
}

const std::string
EProvideKey::raw_name() const
{
    return _imp->variable->name();
}

const std::string
EProvideKey::human_name() const
{
    return _imp->variable->description();
}

MetadataKeyType
EProvideKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<EContentsKey>
    {
        const FSPath filename;

        const std::string raw_name;
        const std::string human_name;
        const MetadataKeyType type;

        Imp(const FSPath & v, const std::string & r, const std::string & h, const MetadataKeyType & t) :
            filename(v),
            raw_name(r),
            human_name(h),
            type(t)
        {
        }
    };
}

EContentsKey::EContentsKey(const std::string & r, const std::string & h, const FSPath & v, const MetadataKeyType t) :
    _imp(v, r, h, t)
{
}

EContentsKey::~EContentsKey()
{
}

const std::shared_ptr<const Contents>
EContentsKey::parse_value() const
{
    Context context("When creating contents from '" + stringify(_imp->filename) + "':");

    auto value(std::make_shared<Contents>());

    FSPath f(_imp->filename);
    if (! f.stat().is_regular_file_or_symlink_to_regular_file())
    {
        Log::get_instance()->message("e.contents.not_a_file", ll_warning, lc_context) << "Could not read CONTENTS file '" <<
            _imp->filename << "'";
        return value;
    }

    SafeIFStream ff(f);

    std::string line;
    unsigned line_number(0);
    while (std::getline(ff, line))
    {
        ++line_number;

        std::vector<std::string> tokens;
        if (! VDBContentsTokeniser::tokenise(line, std::back_inserter(tokens)))
        {
            Log::get_instance()->message("e.contents.broken", ll_warning, lc_context) << "CONTENTS has broken line '" <<
                line_number << "', skipping";
            continue;
        }

        if ("obj" == tokens.at(0))
        {
            std::shared_ptr<ContentsEntry> e(std::make_shared<ContentsFileEntry>(FSPath(tokens.at(1))));
            e->add_metadata_key(std::make_shared<LiteralMetadataTimeKey>("mtime", "mtime", mkt_normal,
                            Timestamp(destringify<time_t>(tokens.at(3)), 0)));
            e->add_metadata_key(std::make_shared<LiteralMetadataValueKey<std::string>>("md5", "md5", mkt_normal, tokens.at(2)));
            value->add(e);
        }
        else if ("dir" == tokens.at(0))
        {
            std::shared_ptr<ContentsEntry> e(std::make_shared<ContentsDirEntry>(FSPath(tokens.at(1))));
            value->add(e);
        }
        else if ("sym" == tokens.at(0))
        {
            std::shared_ptr<ContentsEntry> e(std::make_shared<ContentsSymEntry>(FSPath(tokens.at(1)), tokens.at(2)));
            e->add_metadata_key(std::make_shared<LiteralMetadataTimeKey>("mtime", "mtime", mkt_normal,
                            Timestamp(destringify<time_t>(tokens.at(3)), 0)));
            value->add(e);
        }
        else if ("misc" == tokens.at(0) || "fif" == tokens.at(0) || "dev" == tokens.at(0))
            value->add(std::shared_ptr<ContentsEntry>(std::make_shared<ContentsOtherEntry>(FSPath(tokens.at(1)))));
        else
            Log::get_instance()->message("e.contents.unknown", ll_warning, lc_context) << "CONTENTS has unsupported entry type '" <<
                tokens.at(0) << "', skipping";
    }

    return value;
}

const std::string
EContentsKey::raw_name() const
{
    return _imp->raw_name;
}

const std::string
EContentsKey::human_name() const
{
    return _imp->human_name;
}

MetadataKeyType
EContentsKey::type() const
{
    return _imp->type;
}

namespace paludis
{
    template <>
    struct Imp<EMTimeKey>
    {
        const FSPath filename;
        mutable Mutex value_mutex;
        mutable std::shared_ptr<Timestamp> value;

        const std::string raw_name;
        const std::string human_name;
        const MetadataKeyType type;

        Imp(const FSPath & v,
                const std::string & r, const std::string & h, const MetadataKeyType t) :
            filename(v),
            raw_name(r),
            human_name(h),
            type(t)
        {
        }
    };
}

EMTimeKey::EMTimeKey(const std::string & r, const std::string & h, const FSPath & v, const MetadataKeyType t) :
    _imp(v, r, h, t)
{
}

EMTimeKey::~EMTimeKey()
{
}

Timestamp
EMTimeKey::parse_value() const
{
    Lock l(_imp->value_mutex);

    if (_imp->value)
        return *_imp->value;

    _imp->value = std::make_shared<Timestamp>(Timestamp::now());

    try
    {
        *_imp->value = _imp->filename.stat().mtim();
    }
    catch (const FSError & e)
    {
        Log::get_instance()->message("e.contents.mtime_failure", ll_warning, lc_context) << "Couldn't get mtime for '"
            << _imp->filename << "' due to exception '" << e.message() << "' (" << e.what() << ")";
    }

    return *_imp->value;
}

const std::string
EMTimeKey::raw_name() const
{
    return _imp->raw_name;
}

const std::string
EMTimeKey::human_name() const
{
    return _imp->human_name;
}

MetadataKeyType
EMTimeKey::type() const
{
    return _imp->type;
}