aboutsummaryrefslogtreecommitdiff
path: root/paludis/environments/paludis/paludis_config.cc
blob: 5617e311faa17581284afa963dda916dbb7f8594 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 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/environments/paludis/paludis_config.hh>
#include <paludis/environments/paludis/paludis_environment.hh>
#include <paludis/environments/paludis/keywords_conf.hh>
#include <paludis/environments/paludis/use_conf.hh>
#include <paludis/environments/paludis/mirrors_conf.hh>
#include <paludis/environments/paludis/licenses_conf.hh>
#include <paludis/environments/paludis/package_mask_conf.hh>

#include <paludis/util/config_file.hh>
#include <paludis/distribution.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/iterator.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/log.hh>
#include <paludis/util/pstream.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/sr.hh>
#include <paludis/util/set.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/map.hh>
#include <paludis/util/system.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/mutex.hh>

#include <paludis/util/tr1_functional.hh>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <list>
#include <map>
#include <vector>

#include <ctype.h>
#include <sys/types.h>
#include <pwd.h>

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

#include "config.h"

/** \file
 * Implementation of paludis_config.hh classes.
 *
 * \ingroup grppaludisconfig
 */

using namespace paludis;
using namespace paludis::paludis_environment;

#include <paludis/environments/paludis/use_config_entry-sr.cc>
#include <paludis/environments/paludis/repository_config_entry-sr.cc>

namespace paludis
{
    /**
     * Implementation data for PaludisConfig.
     *
     * \ingroup grppaludisconfig
     */
    template<>
    struct Implementation<PaludisConfig>
    {
        PaludisEnvironment * const env;

        std::string paludis_command;
        std::string root;
        std::string config_dir;
        mutable Mutex distribution_mutex;
        mutable std::string distribution;
        tr1::shared_ptr<FSEntrySequence> bashrc_files;

        std::list<RepositoryConfigEntry> repos;

        tr1::shared_ptr<KeywordsConf> keywords_conf;
        tr1::shared_ptr<UseConf> use_conf;
        tr1::shared_ptr<LicensesConf> licenses_conf;
        tr1::shared_ptr<PackageMaskConf> package_mask_conf;
        tr1::shared_ptr<PackageMaskConf> package_unmask_conf;
        tr1::shared_ptr<MirrorsConf> mirrors_conf;

        mutable Mutex reduced_mutex;
        mutable tr1::shared_ptr<uid_t> reduced_uid;
        mutable tr1::shared_ptr<gid_t> reduced_gid;

        mutable Mutex environment_conf_mutex;
        mutable bool has_environment_conf;
        mutable bool accept_breaks_portage;
        mutable std::string reduced_username;

        Implementation(PaludisEnvironment * const);

        void need_environment_conf() const;
    };

    Implementation<PaludisConfig>::Implementation(PaludisEnvironment * e) :
        env(e),
        paludis_command("paludis"),
        config_dir("(unset)"),
        bashrc_files(new FSEntrySequence),
        keywords_conf(new KeywordsConf(e)),
        use_conf(new UseConf(e)),
        licenses_conf(new LicensesConf(e)),
        package_mask_conf(new PackageMaskConf(e)),
        package_unmask_conf(new PackageMaskConf(e)),
        mirrors_conf(new MirrorsConf(e)),
        has_environment_conf(false),
        accept_breaks_portage(true),
        reduced_username(getenv_with_default("PALUDIS_REDUCED_USERNAME", "paludisbuild"))
    {
    }

    void
    Implementation<PaludisConfig>::need_environment_conf() const
    {
        Lock lock(environment_conf_mutex);

        if (has_environment_conf)
            return;

        Context context("When loading environment.conf:");

        tr1::shared_ptr<KeyValueConfigFile> kv;

        if ((FSEntry(config_dir) / "environment.conf").exists())
            kv.reset(new KeyValueConfigFile(FSEntry(config_dir) / "environment.conf", KeyValueConfigFileOptions()));
        else if ((FSEntry(config_dir) / "environment.bash").exists())
        {
            Command cmd(Command("bash '" + stringify(FSEntry(config_dir) / "environment.bash") + "'")
                    .with_setenv("PALUDIS_LOG_LEVEL", stringify(Log::get_instance()->log_level()))
                    .with_setenv("PALUDIS_EBUILD_DIR", getenv_with_default("PALUDIS_EBUILD_DIR", LIBEXECDIR "/paludis"))
                    .with_stderr_prefix("environment.bash> "));
            PStream s(cmd);
            kv.reset(new KeyValueConfigFile(s, KeyValueConfigFileOptions()));

            if (s.exit_status() != 0)
            {
                Log::get_instance()->message(ll_warning, lc_context, "Script '" + stringify(FSEntry(config_dir) / "environment.bash")
                        + "' returned non-zero exit status '" + stringify(s.exit_status()) + "'");
                kv.reset();
            }
        }
        else
            Log::get_instance()->message(ll_debug, lc_context, "No environment.conf or environment.bash in '"
                    + config_dir + "'");

        if (kv)
        {
            if (! kv->get("reduced_username").empty())
            {
                reduced_username = kv->get("reduced_username");
                Log::get_instance()->message(ll_debug, lc_context,
                        "loaded key 'reduced_username' = '" + reduced_username + "'");
            }
            else
                Log::get_instance()->message(ll_debug, lc_context,
                        "Key 'reduced_username' is unset, using '" + reduced_username + "'");

            accept_breaks_portage = kv->get("portage_compatible").empty();
            distribution = kv->get("distribution");
        }

        has_environment_conf = true;
    }
}

PaludisConfigError::PaludisConfigError(const std::string & msg) throw () :
    ConfigurationError("Paludis configuration error: " + msg)
{
}

PaludisConfigNoDirectoryError::PaludisConfigNoDirectoryError(const std::string & msg) throw () :
    PaludisConfigError("Paludis configuration error: " + msg)
{
}

PaludisConfig::PaludisConfig(PaludisEnvironment * const e, const std::string & suffix) :
    PrivateImplementationPattern<PaludisConfig>(new Implementation<PaludisConfig>(e))
{
    using namespace tr1::placeholders;

    Context context("When loading paludis configuration:");

    /* indirection */
    std::string root_prefix;
    std::string local_config_suffix;
    if (! suffix.empty())
        local_config_suffix = "-" + suffix;

    FSEntry local_config_dir(FSEntry(getenv_with_default("PALUDIS_HOME", getenv_or_error("HOME"))) /
            (".paludis" + local_config_suffix)), old_config_dir(local_config_dir);
    try
    {
        if (! local_config_dir.exists())
            local_config_dir = (FSEntry(SYSCONFDIR) / ("paludis" + local_config_suffix));
    }
    catch (const FSError &)
    {
        local_config_dir = (FSEntry(SYSCONFDIR) / ("paludis" + local_config_suffix));
    }

    if (! local_config_dir.exists())
        throw PaludisConfigNoDirectoryError("Can't find configuration directory (tried '"
                + stringify(old_config_dir) + "', '" + stringify(local_config_dir) + "')");

    Log::get_instance()->message(ll_debug, lc_no_context, "PaludisConfig initial directory is '"
            + stringify(local_config_dir) + "'");

    // Prefer specpath.conf over specpath. Warn if specpath is used.
    if ((local_config_dir / "specpath.conf").exists() || (local_config_dir / "specpath").exists())
    {
        KeyValueConfigFile* specpath;
        if ((local_config_dir / "specpath.conf").exists())
            specpath = new KeyValueConfigFile(local_config_dir / "specpath.conf", KeyValueConfigFileOptions());
        else
        {
            specpath = new KeyValueConfigFile(local_config_dir / "specpath", KeyValueConfigFileOptions());
            Log::get_instance()->message(ll_warning, lc_no_context, "Using specpath is deprecated, use specpath.conf instead");
        }
        root_prefix = specpath->get("root");
        local_config_suffix = specpath->get("config-suffix");

        if (! root_prefix.empty() && stringify(FSEntry(root_prefix).realpath()) != "/")
        {
            local_config_dir = FSEntry(root_prefix) / SYSCONFDIR / ("paludis" + local_config_suffix);
            if (! local_config_dir.exists())
                throw PaludisConfigError("Can't find configuration directory under root ("
                        "tried '" + stringify(local_config_dir) + "'");
        }
    }

    _imp->root = root_prefix;
    _imp->config_dir = stringify(local_config_dir);
    /* check that we can safely use userpriv */
    {
        Command cmd(Command("ls -ld '" + stringify(local_config_dir) + "'/* >/dev/null 2>/dev/null")
                .with_uid_gid(reduced_uid(), reduced_gid()));
        if (0 != run_command(cmd))
        {
            Log::get_instance()->message(ll_warning, lc_context, "Cannot access configuration directory '"
                    + stringify(local_config_dir) + "' using userpriv, so userpriv will be disabled");
            _imp->reduced_uid.reset(new uid_t(getuid()));
            _imp->reduced_gid.reset(new gid_t(getgid()));
        }
    }

    tr1::shared_ptr<Map<std::string, std::string> > conf_vars(
            new Map<std::string, std::string>);
    conf_vars->insert("ROOT", root_prefix);
    conf_vars->insert("root", root_prefix);

    Log::get_instance()->message(ll_debug, lc_no_context, "PaludisConfig real directory is '"
            + stringify(local_config_dir) + "', root prefix is '" + root_prefix +
            "', config suffix is '" + local_config_suffix + "'");

    /* repositories */
    {
        /* add virtuals repositories */

        if (DistributionData::get_instance()->distribution_from_string(distribution())->support_old_style_virtuals)
        {
            tr1::shared_ptr<Map<std::string, std::string> > iv_keys(
                    new Map<std::string, std::string>);
            iv_keys->insert("root", root_prefix.empty() ? "/" : root_prefix);
            _imp->repos.push_back(RepositoryConfigEntry("installed_virtuals", -1, iv_keys));

            _imp->repos.push_back(RepositoryConfigEntry("virtuals", -2,
                        tr1::shared_ptr<Map<std::string, std::string> >()));
        }

        /* add normal repositories */

        if ((local_config_dir / "repository_defaults.conf").exists())
        {
            KeyValueConfigFile defaults_file(local_config_dir / "repository_defaults.conf", KeyValueConfigFileOptions(),
                    KeyValueConfigFile::Defaults(conf_vars));
            std::copy(defaults_file.begin(), defaults_file.end(), conf_vars->inserter());
        }
        else if ((local_config_dir / "repository_defaults.bash").exists())
        {
            Command cmd(Command("bash '" + stringify(local_config_dir / "repository_defaults.bash") + "'")
                    .with_setenv("PALUDIS_LOG_LEVEL", stringify(Log::get_instance()->log_level()))
                    .with_setenv("PALUDIS_EBUILD_DIR", getenv_with_default("PALUDIS_EBUILD_DIR", LIBEXECDIR "/paludis"))
                    .with_stderr_prefix("repository_defaults.bash> "));
            PStream s(cmd);
            KeyValueConfigFile defaults_file(s, KeyValueConfigFileOptions(), KeyValueConfigFile::Defaults(conf_vars));
            std::copy(defaults_file.begin(), defaults_file.end(), conf_vars->inserter());
        }

        std::list<FSEntry> dirs;
        dirs.push_back(local_config_dir / "repositories");

        std::list<FSEntry> repo_files;
        for (std::list<FSEntry>::const_iterator dir(dirs.begin()), dir_end(dirs.end()) ;
                dir != dir_end ; ++dir)
        {
            if (! dir->exists())
                continue;

            std::copy(DirIterator(*dir), DirIterator(),
                    filter_inserter(std::back_inserter(repo_files), tr1::bind(&is_file_with_extension, _1, ".conf", IsFileWithOptions())));
            std::copy(DirIterator(*dir), DirIterator(),
                    filter_inserter(std::back_inserter(repo_files), tr1::bind(&is_file_with_extension, _1, ".bash", IsFileWithOptions())));
        }

        std::list<tr1::shared_ptr<Map<std::string, std::string> > > later_keys;
        for (std::list<FSEntry>::const_iterator repo_file(repo_files.begin()), repo_file_end(repo_files.end()) ;
                repo_file != repo_file_end ; ++repo_file)
        {
            Context local_context("When reading repository file '" + stringify(*repo_file) + "':");

            tr1::shared_ptr<KeyValueConfigFile> kv;
            if (is_file_with_extension(*repo_file, ".bash", IsFileWithOptions()))
            {
                Command cmd(Command("bash '" + stringify(*repo_file) + "'")
                        .with_setenv("PALUDIS_LOG_LEVEL", stringify(Log::get_instance()->log_level()))
                        .with_setenv("PALUDIS_EBUILD_DIR", getenv_with_default("PALUDIS_EBUILD_DIR", LIBEXECDIR "/paludis"))
                        .with_stderr_prefix(repo_file->basename() + "> "));
                PStream s(cmd);
                kv.reset(new KeyValueConfigFile(s, KeyValueConfigFileOptions(), KeyValueConfigFile::Defaults(conf_vars)));

                if (s.exit_status() != 0)
                {
                    Log::get_instance()->message(ll_warning, lc_context, "Script '" + stringify(*repo_file)
                            + "' returned non-zero exit status '" + stringify(s.exit_status()) + "'");
                    kv.reset();
                }
            }
            else
                kv.reset(new KeyValueConfigFile(*repo_file, KeyValueConfigFileOptions(), KeyValueConfigFile::Defaults(conf_vars)));

            if (! kv)
                continue;

            std::string format(kv->get("format"));
            if (format.empty())
                throw PaludisConfigError("Key 'format' not specified or empty");

            int importance(kv->get("master_repository").empty() ? 0 : 10);
            if (! kv->get("importance").empty())
                importance = destringify<int>(kv->get("importance"));

            tr1::shared_ptr<Map<std::string, std::string> > keys(new Map<std::string, std::string>);
            std::copy(kv->begin(), kv->end(), keys->inserter());

            keys->erase("importance");
            keys->insert("importance", stringify(importance));

            keys->erase("repo_file");
            keys->insert("repo_file", stringify(*repo_file));

            keys->erase("root");
            keys->insert("root", root_prefix.empty() ? "/" : root_prefix);

            if (! kv->get("master_repository").empty())
            {
                Log::get_instance()->message(ll_debug, lc_context, "Delaying '" + stringify(*repo_file) +
                        "' because it uses master_repository");
                later_keys.push_back(keys);
            }
            else
            {
                Log::get_instance()->message(ll_debug, lc_context, "Not delaying '" + stringify(*repo_file) + "'");
                _imp->repos.push_back(RepositoryConfigEntry(format, importance, keys));
            }
        }

        for (std::list<tr1::shared_ptr<Map<std::string, std::string> > >::const_iterator
                k(later_keys.begin()), k_end(later_keys.end()) ; k != k_end ; ++k)
            _imp->repos.push_back(RepositoryConfigEntry((*k)->find("format")->second,
                        destringify<int>((*k)->find("importance")->second), *k));

        if (_imp->repos.empty())
            throw PaludisConfigError("No repositories specified");
    }

    /* keywords */
    {
        std::list<FSEntry> files;
        files.push_back(local_config_dir / "keywords.conf");
        files.push_back(local_config_dir / "keywords.bash");
        if ((local_config_dir / "keywords.conf.d").exists())
        {
            std::copy(DirIterator(local_config_dir / "keywords.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".conf", IsFileWithOptions())));
            std::copy(DirIterator(local_config_dir / "keywords.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".bash", IsFileWithOptions())));
        }

        for (std::list<FSEntry>::const_iterator file(files.begin()), file_end(files.end()) ;
                file != file_end ; ++file)
        {
            Context local_context("When reading keywords file '" + stringify(*file) + "':");

            if (! file->exists())
                continue;

            _imp->keywords_conf->add(*file);
        }
    }

    /* use */
    {
        std::list<FSEntry> files;
        files.push_back(local_config_dir / "use.conf");
        files.push_back(local_config_dir / "use.bash");
        if ((local_config_dir / "use.conf.d").exists())
        {
            std::copy(DirIterator(local_config_dir / "use.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".conf", IsFileWithOptions())));
            std::copy(DirIterator(local_config_dir / "use.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".bash", IsFileWithOptions())));
        }

        for (std::list<FSEntry>::const_iterator file(files.begin()), file_end(files.end()) ;
                file != file_end ; ++file)
        {
            Context local_context("When reading use file '" + stringify(*file) + "':");

            if (! file->exists())
                continue;

            _imp->use_conf->add(*file);
        }
    }

    /* licenses */
    {
        std::list<FSEntry> files;
        files.push_back(local_config_dir / "licenses.conf");
        files.push_back(local_config_dir / "licenses.bash");
        if ((local_config_dir / "licenses.conf.d").exists())
        {
            std::copy(DirIterator(local_config_dir / "licenses.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".conf", IsFileWithOptions())));
            std::copy(DirIterator(local_config_dir / "licenses.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".bash", IsFileWithOptions())));
        }

        for (std::list<FSEntry>::const_iterator file(files.begin()), file_end(files.end()) ;
                file != file_end ; ++file)
        {
            Context local_context("When reading licenses file '" + stringify(*file) + "':");

            if (! file->exists())
                continue;

            _imp->licenses_conf->add(*file);
        }
    }

    /* user mask */
    {
        std::list<FSEntry> files;
        files.push_back(local_config_dir / "package_mask.conf");
        files.push_back(local_config_dir / "package_mask.bash");
        if ((local_config_dir / "package_mask.conf.d").exists())
        {
            std::copy(DirIterator(local_config_dir / "package_mask.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".conf", IsFileWithOptions())));
            std::copy(DirIterator(local_config_dir / "package_mask.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".bash", IsFileWithOptions())));
        }

        for (std::list<FSEntry>::const_iterator file(files.begin()), file_end(files.end()) ;
                file != file_end ; ++file)
        {
            Context local_context("When reading package_mask file '" + stringify(*file) + "':");

            if (! file->exists())
                continue;

            _imp->package_mask_conf->add(*file);
        }
    }

    /* user unmask */
    {
        std::list<FSEntry> files;
        files.push_back(local_config_dir / "package_unmask.conf");
        files.push_back(local_config_dir / "package_unmask.bash");
        if ((local_config_dir / "package_unmask.conf.d").exists())
        {
            std::copy(DirIterator(local_config_dir / "package_unmask.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".conf", IsFileWithOptions())));
            std::copy(DirIterator(local_config_dir / "package_unmask.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".bash", IsFileWithOptions())));
        }

        for (std::list<FSEntry>::const_iterator file(files.begin()), file_end(files.end()) ;
                file != file_end ; ++file)
        {
            Context local_context("When reading package_unmask file '" + stringify(*file) + "':");

            if (! file->exists())
                continue;

            _imp->package_unmask_conf->add(*file);
        }
    }

    /* mirrors */
    {
        std::list<FSEntry> files;
        files.push_back(local_config_dir / "mirrors.conf");
        files.push_back(local_config_dir / "mirrors.bash");
        if ((local_config_dir / "mirrors.conf.d").exists())
        {
            std::copy(DirIterator(local_config_dir / "mirrors.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".conf", IsFileWithOptions())));
            std::copy(DirIterator(local_config_dir / "mirrors.conf.d"), DirIterator(),
                    filter_inserter(std::back_inserter(files), tr1::bind(&is_file_with_extension, _1, ".bash", IsFileWithOptions())));
        }

        for (std::list<FSEntry>::const_iterator file(files.begin()), file_end(files.end()) ;
                file != file_end ; ++file)
        {
            Context local_context("When reading mirrors file '" + stringify(*file) + "':");

            if (! file->exists())
                continue;

            _imp->mirrors_conf->add(*file);
        }
    }

    _imp->bashrc_files->push_back(local_config_dir / "bashrc");
}

PaludisConfig::~PaludisConfig()
{
}

tr1::shared_ptr<const FSEntrySequence>
PaludisConfig::bashrc_files() const
{
    return _imp->bashrc_files;
}

PaludisConfig::RepositoryConstIterator
PaludisConfig::begin_repositories() const
{
    return RepositoryConstIterator(_imp->repos.begin());
}

PaludisConfig::RepositoryConstIterator
PaludisConfig::end_repositories() const
{
    return RepositoryConstIterator(_imp->repos.end());
}

std::string
PaludisConfig::root() const
{
    return _imp->root;
}

std::string
PaludisConfig::config_dir() const
{
    return _imp->config_dir;
}

uid_t
PaludisConfig::reduced_uid() const
{
    Lock lock(_imp->reduced_mutex);

    if (! _imp->reduced_uid)
    {
        Context context("When determining reduced UID:");

        if (0 != getuid())
            _imp->reduced_uid.reset(new uid_t(getuid()));
        else
        {
            struct passwd * p(getpwnam(reduced_username().c_str()));
            if (! p)
            {
                Log::get_instance()->message(ll_warning, lc_no_context,
                        "Couldn't determine uid for user '" + reduced_username() + "'");
                _imp->reduced_uid.reset(new uid_t(getuid()));
            }
            else
                _imp->reduced_uid.reset(new uid_t(p->pw_uid));
        }

        Log::get_instance()->message(ll_debug, lc_context, "Reduced uid is '"
                + stringify(*_imp->reduced_uid) + "'");
    }

    return *_imp->reduced_uid;
}

gid_t
PaludisConfig::reduced_gid() const
{
    Lock lock(_imp->reduced_mutex);

    if (! _imp->reduced_gid)
    {
        if (0 != getuid())
            _imp->reduced_gid.reset(new gid_t(getgid()));
        else
        {
            struct passwd * p(getpwnam(reduced_username().c_str()));
            if (! p)
            {
                Log::get_instance()->message(ll_warning, lc_no_context,
                        "Couldn't determine gid for user '" + reduced_username() + "'");
                _imp->reduced_gid.reset(new gid_t(getgid()));
            }
            else
                _imp->reduced_gid.reset(new gid_t(p->pw_gid));
        }
    }

    return *_imp->reduced_gid;
}

std::string
PaludisConfig::reduced_username() const
{
    Context context("When determining reduced username:");
    _imp->need_environment_conf();

    Log::get_instance()->message(ll_debug, lc_context,
            "Reduced username is '" + _imp->reduced_username + "'");

    return _imp->reduced_username;
}

bool
PaludisConfig::accept_breaks_portage() const
{
    _imp->need_environment_conf();

    return _imp->accept_breaks_portage;
}

tr1::shared_ptr<const KeywordsConf>
PaludisConfig::keywords_conf() const
{
    return _imp->keywords_conf;
}

tr1::shared_ptr<const UseConf>
PaludisConfig::use_conf() const
{
    return _imp->use_conf;
}

tr1::shared_ptr<const LicensesConf>
PaludisConfig::licenses_conf() const
{
    return _imp->licenses_conf;
}

tr1::shared_ptr<const PackageMaskConf>
PaludisConfig::package_mask_conf() const
{
    return _imp->package_mask_conf;
}

tr1::shared_ptr<const PackageMaskConf>
PaludisConfig::package_unmask_conf() const
{
    return _imp->package_unmask_conf;
}

tr1::shared_ptr<const MirrorsConf>
PaludisConfig::mirrors_conf() const
{
    return _imp->mirrors_conf;
}

std::string
PaludisConfig::distribution() const
{
    Lock lock(_imp->distribution_mutex);

    if (! _imp->distribution.empty())
        return _imp->distribution;

    _imp->need_environment_conf();

    if (_imp->distribution.empty())
        _imp->distribution = getenv_with_default("PALUDIS_DISTRIBUTION", DEFAULT_DISTRIBUTION);

    return _imp->distribution;
}