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

/*
 * Copyright (c) 2005, 2006, 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/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/package_mask_conf.hh>
#include <paludis/environments/paludis/licenses_conf.hh>
#include <paludis/environments/paludis/mirrors_conf.hh>
#include <paludis/environments/paludis/output_conf.hh>
#include <paludis/environments/paludis/world.hh>
#include <paludis/environments/paludis/suggestions_conf.hh>

#include <paludis/util/config_file.hh>
#include <paludis/hooker.hh>
#include <paludis/hook.hh>
#include <paludis/set_file.hh>
#include <paludis/distribution.hh>
#include <paludis/package_id.hh>
#include <paludis/mask.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/repository_factory.hh>
#include <paludis/standard_output_manager.hh>

#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/log.hh>
#include <paludis/util/system.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/save.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/set.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/map.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/options.hh>
#include <paludis/util/tribool.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/util/fs_iterator.hh>
#include <paludis/util/fs_error.hh>

#include <functional>
#include <algorithm>
#include <list>
#include <map>

using namespace paludis;
using namespace paludis::paludis_environment;

namespace paludis
{
    template<>
    struct Imp<PaludisEnvironment>
    {
        mutable Mutex hook_mutex;
        mutable bool done_hooks;
        mutable std::shared_ptr<Hooker> hooker;
        mutable std::list<std::pair<FSPath, bool> > hook_dirs;

        std::shared_ptr<PaludisConfig> config;
        std::string paludis_command;

        std::shared_ptr<PackageDatabase> package_database;

        mutable Mutex sets_mutex;
        mutable std::map<SetName, std::shared_ptr<const SetSpecTree> > sets;

        std::shared_ptr<LiteralMetadataValueKey<std::string> > format_key;
        std::shared_ptr<LiteralMetadataValueKey<FSPath> > config_location_key;
        std::shared_ptr<LiteralMetadataValueKey<FSPath> > world_file_key;
        std::shared_ptr<LiteralMetadataValueKey<FSPath> > preferred_root_key;
        std::shared_ptr<LiteralMetadataValueKey<FSPath> > system_root_key;

        Imp(PaludisEnvironment * const e, std::shared_ptr<PaludisConfig> c) :
            done_hooks(false),
            config(c),
            paludis_command("paludis"),
            package_database(std::make_shared<PackageDatabase>(e)),
            format_key(std::make_shared<LiteralMetadataValueKey<std::string>>("format", "Format", mkt_significant, "paludis")),
            config_location_key(std::make_shared<LiteralMetadataValueKey<FSPath>>("conf_dir", "Config dir", mkt_normal,
                        FSPath(config->config_dir()))),
            world_file_key(config->world()->location_if_set() ? std::make_shared<LiteralMetadataValueKey<FSPath>>("world_file", "World file", mkt_normal,
                            *config->world()->location_if_set())
                    : std::shared_ptr<LiteralMetadataValueKey<FSPath> >()),
            preferred_root_key(std::make_shared<LiteralMetadataValueKey<FSPath>>("root", "Root", mkt_normal,
                        FSPath(config->root()))),
            system_root_key(std::make_shared<LiteralMetadataValueKey<FSPath>>("system_root", "System Root", mkt_normal,
                        FSPath(config->system_root())))
        {
        }

        void add_one_hook(const FSPath & r, const bool v) const
        {
            try
            {
                if (r.stat().is_directory())
                {
                    Log::get_instance()->message("paludis_environment.hooks.add_dir", ll_debug, lc_no_context)
                        << "Adding hook directory '" << r << "'";
                    hook_dirs.push_back(std::make_pair(r, v));
                }
                else
                    Log::get_instance()->message("paludis_environment.hook.skipping", ll_debug, lc_no_context)
                        << "Skipping hook directory candidate '" << r << "'";
            }
            catch (const FSError & e)
            {
                Log::get_instance()->message("paludis_environment.hook.failure", ll_warning, lc_no_context)
                    << "Caught exception '" << e.message() << "' (" << e.what() << ") when checking hook "
                    "directory '" << r << "'";
            }
        }

        void need_hook_dirs(const FSPath & c) const
        {
            if (! done_hooks)
            {
                add_one_hook(c / "hooks", true);
                if (getenv_with_default("PALUDIS_NO_GLOBAL_HOOKS", "").empty())
                {
                    add_one_hook(FSPath(LIBEXECDIR) / "paludis" / "hooks", false);
                    add_one_hook(FSPath(DATADIR) / "paludis" / "hooks", true);
                    add_one_hook(FSPath(LIBDIR) / "paludis" / "hooks", true);
                }
                done_hooks = true;
            }
        }
    };
}

PaludisEnvironment::PaludisEnvironment(const std::string & s) :
    _imp(this, std::shared_ptr<PaludisConfig>(std::make_shared<PaludisConfig>(this, s)))
{
    Context context("When loading paludis environment:");

    for (PaludisConfig::RepositoryConstIterator r(_imp->config->begin_repositories()),
            r_end(_imp->config->end_repositories()) ; r != r_end ; ++r)
        _imp->package_database->add_repository(
                RepositoryFactory::get_instance()->importance(this, *r),
                RepositoryFactory::get_instance()->create(this, *r));

    add_metadata_key(_imp->format_key);
    add_metadata_key(_imp->config_location_key);
    if (_imp->world_file_key)
        add_metadata_key(_imp->world_file_key);
    add_metadata_key(_imp->preferred_root_key);
    add_metadata_key(_imp->system_root_key);
}

PaludisEnvironment::~PaludisEnvironment()
{
}

bool
PaludisEnvironment::accept_keywords(const std::shared_ptr<const KeywordNameSet> & k,
        const std::shared_ptr<const PackageID> & e) const
{
    return _imp->config->keywords_conf()->query(k, e);
}

bool
PaludisEnvironment::accept_license(const std::string & license, const std::shared_ptr<const PackageID> & d) const
{
    if (license == "*")
        return true;
    if (license == "-*")
        return false;

    Context context("When checking license of '" + license + "' for '" + stringify(*d) + "':");

    return _imp->config->licenses_conf()->query(license, d);
}

bool
PaludisEnvironment::unmasked_by_user(const std::shared_ptr<const PackageID> & d) const
{
    return _imp->config->package_unmask_conf()->query(d);
}

std::shared_ptr<const FSPathSequence>
PaludisEnvironment::bashrc_files() const
{
    return _imp->config->bashrc_files();
}

std::string
PaludisEnvironment::paludis_command() const
{
    return _imp->paludis_command;
}

void
PaludisEnvironment::set_paludis_command(const std::string & s)
{
    _imp->paludis_command = s;
}

HookResult
PaludisEnvironment::perform_hook(
        const Hook & hook,
        const std::shared_ptr<OutputManager> & optional_output_manager) const
{
    Lock lock(_imp->hook_mutex);

    if (! _imp->hooker)
    {
        _imp->need_hook_dirs(FSPath(_imp->config->config_dir()));
        _imp->hooker = std::make_shared<Hooker>(this);
        for (std::list<std::pair<FSPath, bool> >::const_iterator h(_imp->hook_dirs.begin()),
                h_end(_imp->hook_dirs.end()) ; h != h_end ; ++h)
            _imp->hooker->add_dir(h->first, h->second);
    }

    return _imp->hooker->perform_hook(hook, optional_output_manager);
}

std::shared_ptr<const FSPathSequence>
PaludisEnvironment::hook_dirs() const
{
    Lock lock(_imp->hook_mutex);

    _imp->need_hook_dirs(FSPath(_imp->config->config_dir()));

    std::shared_ptr<FSPathSequence> result(std::make_shared<FSPathSequence>());
    std::transform(_imp->hook_dirs.begin(), _imp->hook_dirs.end(), result->back_inserter(),
            std::mem_fn(&std::pair<FSPath, bool>::first));

    return result;
}

std::shared_ptr<const FSPathSequence>
PaludisEnvironment::fetchers_dirs() const
{
    std::shared_ptr<FSPathSequence> result(std::make_shared<FSPathSequence>());

    result->push_back(FSPath(_imp->config->config_dir()) / "fetchers");

    if (getenv_with_default("PALUDIS_NO_GLOBAL_FETCHERS", "").empty())
    {
        std::shared_ptr<const FSPathSequence> r(EnvironmentImplementation::fetchers_dirs());
        std::copy(r->begin(), r->end(), result->back_inserter());
    }

    return result;
}

std::shared_ptr<const FSPathSequence>
PaludisEnvironment::syncers_dirs() const
{
    std::shared_ptr<FSPathSequence> result(std::make_shared<FSPathSequence>());

    result->push_back(FSPath(_imp->config->config_dir()) / "syncers");

    if (getenv_with_default("PALUDIS_NO_GLOBAL_SYNCERS", "").empty())
    {
        std::shared_ptr<const FSPathSequence> r(EnvironmentImplementation::syncers_dirs());
        std::copy(r->begin(), r->end(), result->back_inserter());
    }

    return result;
}

bool
PaludisEnvironment::add_to_world(const QualifiedPackageName & q) const
{
    return _imp->config->world()->add_to_world(q);
}

bool
PaludisEnvironment::add_to_world(const SetName & s) const
{
    return _imp->config->world()->add_to_world(s);
}

bool
PaludisEnvironment::remove_from_world(const QualifiedPackageName & q) const
{
    return _imp->config->world()->remove_from_world(q);
}

bool
PaludisEnvironment::remove_from_world(const SetName & s) const
{
    return _imp->config->world()->remove_from_world(s);
}

std::shared_ptr<const MirrorsSequence>
PaludisEnvironment::mirrors(const std::string & m) const
{
    return _imp->config->mirrors_conf()->query(m);
}

uid_t
PaludisEnvironment::reduced_uid() const
{
    uid_t u(getuid());
    if (0 == u)
        return _imp->config->reduced_uid();
    else
        return u;
}

gid_t
PaludisEnvironment::reduced_gid() const
{
    gid_t g(getgid());
    if (0 == g)
        return _imp->config->reduced_gid();
    else
        return g;
}

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

std::shared_ptr<PackageDatabase>
PaludisEnvironment::package_database()
{
    return _imp->package_database;
}

std::shared_ptr<const PackageDatabase>
PaludisEnvironment::package_database() const
{
    return _imp->package_database;
}

std::string
PaludisEnvironment::distribution() const
{
    return _imp->config->distribution();
}

namespace
{
    class BreaksPortageMask :
        public UnsupportedMask
    {
        private:
            std::string breakages;

        public:
            BreaksPortageMask(const std::string & b) :
                breakages(b)
            {
            }

            char key() const
            {
                return 'B';
            }

            const std::string description() const
            {
                return "breaks Portage";
            }

            const std::string explanation() const
            {
                return breakages;
            }
    };

    class UserConfigMask :
        public UserMask
    {
        private:
        bool _overridden;

        public:
            UserConfigMask(const bool o) :
                _overridden(o)
            {
            }

            char key() const
            {
                return _overridden ? 'u' : 'U';
            }

            const std::string description() const
            {
                return _overridden ? "user (overridden)" : "user";
            }
    };
}

const std::shared_ptr<const Mask>
PaludisEnvironment::mask_for_breakage(const std::shared_ptr<const PackageID> & id) const
{
    if (! _imp->config->accept_all_breaks_portage())
    {
        std::shared_ptr<const Set<std::string> > breakages(id->breaks_portage());
        if (breakages)
        {
            std::list<std::string> bad_breakages;
            std::set_difference(breakages->begin(), breakages->end(),
                     _imp->config->accept_breaks_portage().begin(), _imp->config->accept_breaks_portage().end(),
                     std::back_inserter(bad_breakages));
            if (! bad_breakages.empty())
                return std::make_shared<BreaksPortageMask>(join(breakages->begin(), breakages->end(), " "));
        }
    }

    return std::shared_ptr<const Mask>();
}

const std::shared_ptr<const Mask>
PaludisEnvironment::mask_for_user(const std::shared_ptr<const PackageID> & d, const bool o) const
{
    if (_imp->config->package_mask_conf()->query(d))
        return std::make_shared<UserConfigMask>(o);

    return std::shared_ptr<const Mask>();
}

void
PaludisEnvironment::need_keys_added() const
{
}

const std::shared_ptr<const MetadataValueKey<std::string> >
PaludisEnvironment::format_key() const
{
    return _imp->format_key;
}

const std::shared_ptr<const MetadataValueKey<FSPath> >
PaludisEnvironment::config_location_key() const
{
    return _imp->config_location_key;
}

const std::shared_ptr<const MetadataValueKey<FSPath> >
PaludisEnvironment::preferred_root_key() const
{
    return _imp->preferred_root_key;
}

const std::shared_ptr<const MetadataValueKey<FSPath> >
PaludisEnvironment::system_root_key() const
{
    return _imp->system_root_key;
}

const Tribool
PaludisEnvironment::want_choice_enabled(
        const std::shared_ptr<const PackageID> & id,
        const std::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value
        ) const
{
    return _imp->config->use_conf()->want_choice_enabled(id, choice, value);
}

const std::string
PaludisEnvironment::value_for_choice_parameter(
        const std::shared_ptr<const PackageID> & id,
        const std::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value
        ) const
{
    return _imp->config->use_conf()->value_for_choice_parameter(id, choice, value);
}

std::shared_ptr<const Set<UnprefixedChoiceName> >
PaludisEnvironment::known_choice_value_names(
        const std::shared_ptr<const PackageID> & id,
        const std::shared_ptr<const Choice> & choice
        ) const
{
    return _imp->config->use_conf()->known_choice_value_names(id, choice);
}

Tribool
PaludisEnvironment::interest_in_suggestion(
        const std::shared_ptr<const PackageID> & i,
        const PackageDepSpec & s) const
{
    return _imp->config->suggestions_conf()->interest_in_suggestion(i, s);
}

const std::shared_ptr<OutputManager>
PaludisEnvironment::create_output_manager(const CreateOutputManagerInfo & i) const
{
    return _imp->config->output_conf()->create_output_manager(i);
}

namespace
{
    std::shared_ptr<const SetSpecTree> make_world_set(const std::shared_ptr<const World> & world)
    {
        return world->world_set();
    }

    std::shared_ptr<const SetSpecTree> make_set(
            const Environment * const env,
            const FSPath & f,
            const SetName & n,
            SetFileSetOperatorMode mode,
            SetFileType type)
    {
        Context context("When making set '" + stringify(f) + "':");

        SetFile s(make_named_values<SetFileParams>(
                    n::environment() = env,
                    n::file_name() = f,
                    n::parser() = std::bind(&parse_user_package_dep_spec, std::placeholders::_1, env,
                        UserPackageDepSpecOptions() + updso_allow_wildcards, filter::All()),
                    n::set_operator_mode() = mode,
                    n::type() = type
                    ));
        return s.contents();
    }
}

void
PaludisEnvironment::populate_sets() const
{
    Lock lock(_imp->sets_mutex);
    add_set(SetName("world"), SetName("world::environment"), std::bind(&make_world_set, _imp->config->world()), true);

    FSPath sets_dir(FSPath(_imp->config->config_dir()) / "sets");
    Context context("When looking in sets directory '" + stringify(sets_dir) + "':");

    if (! sets_dir.stat().exists())
        return;

    for (FSIterator d(sets_dir, { fsio_inode_sort }), d_end ; d != d_end ; ++d)
    {
        if (is_file_with_extension(*d, ".bash", { }))
        {
            SetName n(strip_trailing_string(d->basename(), ".bash"));
            add_set(n, n, std::bind(&make_set, this, *d, n, sfsmo_natural, sft_paludis_bash), false);

            SetName n_s(stringify(n) + "*");
            add_set(n_s, n_s, std::bind(&make_set, this, *d, n_s, sfsmo_star, sft_paludis_bash), false);
        }
        else if (is_file_with_extension(*d, ".conf", { }))
        {
            SetName n(strip_trailing_string(d->basename(), ".conf"));
            add_set(n, n, std::bind(&make_set, this, *d, n, sfsmo_natural, sft_paludis_conf), false);

            SetName n_s(stringify(n) + "*");
            add_set(n_s, n_s, std::bind(&make_set, this, *d, n_s, sfsmo_star, sft_paludis_conf), false);
        }
    }
}

const std::shared_ptr<Repository>
PaludisEnvironment::repository_from_new_config_file(const FSPath & f)
{
    const std::function<std::string (const std::string &)> repo_func(_imp->config->repo_func_from_file(f));
    if (! repo_func)
        throw PaludisConfigError("File '" + stringify(f) + "' does not describe a valid repository");
    return RepositoryFactory::get_instance()->create(this, repo_func);
}

void
PaludisEnvironment::update_config_files_for_package_move(const PackageDepSpec & s, const QualifiedPackageName & n) const
{
    _imp->config->world()->update_config_files_for_package_move(s, n);
}