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

/*
 * Copyright (c) 2006, 2007, 2008, 2009 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/no_config/no_config_environment.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/log.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/map.hh>
#include <paludis/util/set.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/create_iterator-impl.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/tribool.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/graph-impl.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/member_iterator-impl.hh>
#include <paludis/standard_output_manager.hh>
#include <paludis/distribution.hh>
#include <paludis/package_database.hh>
#include <paludis/hook.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/repository_factory.hh>
#include <tr1/unordered_map>
#include <tr1/functional>
#include <algorithm>
#include <set>
#include <list>

#include "config.h"

using namespace paludis;
using namespace paludis::no_config_environment;

#include <paludis/environments/no_config/no_config_environment-se.cc>

namespace paludis
{
    template<>
    struct Implementation<NoConfigEnvironment>
    {
        const no_config_environment::Params params;

        const FSEntry top_level_dir;
        const FSEntry write_cache;
        bool accept_unstable;
        bool is_vdb;

        std::tr1::shared_ptr<Repository> main_repo;
        std::tr1::shared_ptr<Repository> master_repo;
        std::list<std::tr1::shared_ptr<Repository> > extra_repos;

        std::string paludis_command;

        std::tr1::shared_ptr<PackageDatabase> package_database;

        std::tr1::shared_ptr<LiteralMetadataValueKey<std::string> > format_key;
        std::tr1::shared_ptr<LiteralMetadataValueKey<FSEntry> > repository_dir_key;

        Implementation(NoConfigEnvironment * const env, const no_config_environment::Params & params);
        void initialise(NoConfigEnvironment * const env);
    };

    /* This goat is for Dave Wickham */
}

namespace
{
    bool is_vdb_repository(const FSEntry & location, no_config_environment::RepositoryType type)
    {
        switch (type)
        {
            case ncer_ebuild:
                return false;
            case ncer_vdb:
                return true;
            case ncer_auto:
            case last_ncer:
                ;
        }

        Context context("When determining repository type at '" + stringify(location) + "':");

        if (! location.is_directory())
            throw ConfigurationError("Location is not a directory");

        if ((location / "profiles").is_directory())
        {
            Log::get_instance()->message("no_config_environment.ebuild_detected", ll_debug, lc_context)
                << "Found profiles/, looks like Ebuild format";
            return false;
        }

        int outer_count(0);
        for (DirIterator d(location), d_end ; d != d_end ; ++d)
        {
            if (! d->is_directory())
                continue;

            int inner_count(0);
            for (DirIterator e(*d), e_end ; e != e_end ; ++e)
            {
                if (! e->is_directory())
                    continue;

                if ((*e / "CONTENTS").exists())
                {
                    Log::get_instance()->message("no_config_environment.vdb_detected", ll_debug, lc_context)
                        << "Found '" << stringify(*e) << "/CONTENTS', looks like VDB format";
                    return true;
                }

                if (inner_count++ >= 5)
                    break;
            }

            if (outer_count++ >= 5)
                break;
        }

        throw ConfigurationError("Can't work out what kind of repository this is");
    }

    std::string from_keys(const std::tr1::shared_ptr<const Map<std::string, std::string> > & m,
            const std::string & k)
    {
        Map<std::string, std::string>::ConstIterator mm(m->find(k));
        if (m->end() == mm)
            return "";
        else
            return mm->second;
    }
}

Implementation<NoConfigEnvironment>::Implementation(
        NoConfigEnvironment * const env, const no_config_environment::Params & p) :
    params(p),
    top_level_dir(p.repository_dir()),
    write_cache(p.write_cache()),
    accept_unstable(p.accept_unstable()),
    is_vdb(is_vdb_repository(p.repository_dir(), p.repository_type())),
    paludis_command("false"),
    package_database(new PackageDatabase(env)),
    format_key(new LiteralMetadataValueKey<std::string>("format", "Format", mkt_significant, "no_config")),
    repository_dir_key(new LiteralMetadataValueKey<FSEntry>("repository_dir", "Repository dir",
                mkt_normal, p.repository_dir()))
{
}

void
Implementation<NoConfigEnvironment>::initialise(NoConfigEnvironment * const env)
{
    Context context("When initialising NoConfigEnvironment at '" + stringify(params.repository_dir()) + "':");

    if (! is_vdb)
    {
        /* don't assume these're in initialisable order. */
        std::map<FSEntry, bool> repository_dirs;
        RepositoryName main_repository_name("x");
        bool ignored_one(false);

        repository_dirs.insert(std::make_pair(params.repository_dir(), true));
        for (FSEntrySequence::ConstIterator d(params.extra_repository_dirs()->begin()), d_end(params.extra_repository_dirs()->end()) ;
                d != d_end ; ++d)
        {
            if (params.repository_dir().realpath() == d->realpath())
            {
                Log::get_instance()->message("no_config_environment.extra_repository.ignoring", ll_warning, lc_context)
                    << "Ignoring extra_repository_dir '" << *d << "' because it is the same as repository_dir";
                ignored_one = true;
                continue;
            }

            repository_dirs.insert(std::make_pair(*d, false));
        }

        std::tr1::unordered_map<RepositoryName, std::tr1::function<std::string (const std::string &)>,
            Hash<RepositoryName> > repo_configs;

        for (std::map<FSEntry, bool>::const_iterator r(repository_dirs.begin()), r_end(repository_dirs.end()) ;
                r != r_end ; ++r)
        {
            Context local_context("When reading repository at location '" + stringify(r->first) + "':");

            std::tr1::shared_ptr<Map<std::string, std::string> > keys(new Map<std::string, std::string>);

            if (params.extra_params())
                std::copy(params.extra_params()->begin(), params.extra_params()->end(), keys->inserter());

            keys->insert("format", "ebuild");
            keys->insert("location", stringify(r->first));
            keys->insert("profiles", "/var/empty");
            keys->insert("ignore_deprecated_profiles", "true");
            keys->insert("write_cache", stringify(params.write_cache()));
            keys->insert("names_cache", "/var/empty");
            keys->insert("builddir", "/var/empty");

            if (params.disable_metadata_cache())
                keys->insert("cache", "/var/empty");

            if (r->second && ! params.master_repository_name().empty())
                keys->insert("master_repository", params.master_repository_name());

            if ((r->first / "metadata" / "profiles_desc.conf").exists())
                keys->insert("layout", "exheres");

            std::tr1::function<std::string (const std::string &)> repo_func(
                    std::tr1::bind(&from_keys, keys, std::tr1::placeholders::_1));

            RepositoryName name(RepositoryFactory::get_instance()->name(env, repo_func));
            if (ignored_one && r->second && stringify(name) == params.master_repository_name())
                keys->erase(std::string("master_repository"));
            if (! repo_configs.insert(std::make_pair(name, repo_func)).second)
            {
                Log::get_instance()->message("no_config_environment.repositories.duplicate", ll_warning, lc_context)
                    << "Duplicate repository name '" << name << "' from path '" << r->first << "', skipping";
                continue;
            }

            if (r->second)
                main_repository_name = name;
        }

        /* work out order for repository creation */
        DirectedGraph<RepositoryName, bool, RepositoryNameComparator> repository_deps;
        std::for_each(first_iterator(repo_configs.begin()), first_iterator(repo_configs.end()), std::tr1::bind(
                    std::tr1::mem_fn(&DirectedGraph<RepositoryName, bool, RepositoryNameComparator>::add_node),
                    &repository_deps, std::tr1::placeholders::_1));

        for (std::tr1::unordered_map<RepositoryName, std::tr1::function<std::string (const std::string &)>, Hash<RepositoryName> >::const_iterator
                r(repo_configs.begin()), r_end(repo_configs.end()) ; r != r_end ; ++r)
        {
            std::tr1::shared_ptr<const RepositoryNameSet> deps(RepositoryFactory::get_instance()->dependencies(
                        env, r->second));
            for (RepositoryNameSet::ConstIterator d(deps->begin()), d_end(deps->end()) ;
                    d != d_end ; ++d)
            {
                if (*d == r->first)
                    throw ConfigurationError("Repository '" + stringify(r->first) + "' depends upon itself");
                try
                {
                    repository_deps.add_edge(r->first, *d, true);
                }
                catch (const NoSuchGraphNodeError &)
                {
                    throw ConfigurationError("Repository '" + stringify(r->first) + "' depends upon '" +
                            stringify(*d) + "', which is not configured");
                }
            }
        }

        try
        {
            std::list<RepositoryName> ordered_repos;
            repository_deps.topological_sort(std::back_inserter(ordered_repos));

            for (std::list<RepositoryName>::const_iterator o(ordered_repos.begin()), o_end(ordered_repos.end()) ;
                    o != o_end ; ++o)
            {
                std::tr1::unordered_map<RepositoryName, std::tr1::function<std::string (const std::string &)>, Hash<RepositoryName> >::const_iterator
                    c(repo_configs.find(*o));
                if (c == repo_configs.end())
                    throw InternalError(PALUDIS_HERE, "*o not in repo_configs");

                std::tr1::shared_ptr<Repository> repo(RepositoryFactory::get_instance()->create(env, c->second));
                if (repo->name() == main_repository_name)
                {
                    main_repo = repo;
                    package_database->add_repository(3, repo);
                }
                else if (stringify(repo->name()) == params.master_repository_name())
                {
                    master_repo = repo;
                    package_database->add_repository(2, repo);
                }
                else
                    package_database->add_repository(1, repo);
            }
        }
        catch (const NoGraphTopologicalOrderExistsError & x)
        {
            throw ConfigurationError("Repositories have circular dependencies. Unresolvable repositories are '"
                    + join(x.remaining_nodes()->begin(), x.remaining_nodes()->end(), "', '") + "'");
        }

        if (! main_repo)
            throw ConfigurationError("Don't have a main repository");

        if ((! params.master_repository_name().empty()) && (! master_repo) &&
                (params.master_repository_name() != stringify(main_repo->name())))
            throw ConfigurationError("Can't find repository '" + params.master_repository_name() + "'");

#ifdef ENABLE_VIRTUALS_REPOSITORY
        std::tr1::shared_ptr<Map<std::string, std::string> > v_keys(new Map<std::string, std::string>);
        v_keys->insert("format", "virtuals");
        if ((*DistributionData::get_instance()->distribution_from_string(env->distribution())).support_old_style_virtuals())
            package_database->add_repository(-2, RepositoryFactory::get_instance()->create(env,
                        std::tr1::bind(from_keys, v_keys, std::tr1::placeholders::_1)));
#endif
    }
    else
    {
        Log::get_instance()->message("no_config_environment.vdb_detected", ll_debug, lc_context) << "VDB, using vdb_db";

        std::tr1::shared_ptr<Map<std::string, std::string> > keys(new Map<std::string, std::string>);
        if (params.extra_params())
            std::copy(params.extra_params()->begin(), params.extra_params()->end(), keys->inserter());

        keys->insert("format", "vdb");
        keys->insert("names_cache", "/var/empty");
        keys->insert("provides_cache", "/var/empty");
        keys->insert("location", stringify(top_level_dir));

        package_database->add_repository(1, RepositoryFactory::get_instance()->create(env,
                    std::tr1::bind(from_keys, keys, std::tr1::placeholders::_1)));

        std::tr1::shared_ptr<Map<std::string, std::string> > iv_keys(
                new Map<std::string, std::string>);
        iv_keys->insert("root", "/");
        iv_keys->insert("format", "installed_virtuals");

#ifdef ENABLE_VIRTUALS_REPOSITORY
        if ((*DistributionData::get_instance()->distribution_from_string(env->distribution())).support_old_style_virtuals())
            package_database->add_repository(-2, RepositoryFactory::get_instance()->create(env,
                        std::tr1::bind(from_keys, iv_keys, std::tr1::placeholders::_1)));
#endif
    }
}

NoConfigEnvironment::NoConfigEnvironment(const no_config_environment::Params & params) :
    PrivateImplementationPattern<NoConfigEnvironment>(
            new Implementation<NoConfigEnvironment>(this, params)),
    _imp(PrivateImplementationPattern<NoConfigEnvironment>::_imp)
{
    _imp->initialise(this);

    if (_imp->main_repo)
        if ((*_imp->main_repo).e_interface()->end_profiles() != (*_imp->main_repo).e_interface()->begin_profiles())
            (*_imp->main_repo).e_interface()->set_profile((*_imp->main_repo).e_interface()->begin_profiles());

    if (_imp->master_repo)
        if ((*_imp->master_repo).e_interface()->end_profiles() !=
                (*_imp->master_repo).e_interface()->begin_profiles())
            (*_imp->master_repo).e_interface()->set_profile(
                    (*_imp->master_repo).e_interface()->begin_profiles());

    add_metadata_key(_imp->format_key);
    add_metadata_key(_imp->repository_dir_key);
}

NoConfigEnvironment::~NoConfigEnvironment()
{
}

FSEntry
NoConfigEnvironment::main_repository_dir() const
{
    return _imp->top_level_dir;
}

void
NoConfigEnvironment::set_accept_unstable(const bool value)
{
    _imp->accept_unstable = value;
    for (PackageDatabase::RepositoryConstIterator it(_imp->package_database->begin_repositories()),
             it_end(_imp->package_database->end_repositories());
         it_end != it; ++it)
        (*it)->invalidate_masks();
}

std::tr1::shared_ptr<Repository>
NoConfigEnvironment::main_repository()
{
    return _imp->main_repo;
}

std::tr1::shared_ptr<const Repository>
NoConfigEnvironment::main_repository() const
{
    return _imp->main_repo;
}

std::tr1::shared_ptr<Repository>
NoConfigEnvironment::master_repository()
{
    return _imp->master_repo;
}

std::tr1::shared_ptr<const Repository>
NoConfigEnvironment::master_repository() const
{
    return _imp->master_repo;
}

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

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

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

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

bool
NoConfigEnvironment::accept_keywords(const std::tr1::shared_ptr<const KeywordNameSet> & keywords,
        const PackageID &) const
{
    if (_imp->is_vdb)
        return true;

    std::string accept_keywords_var((*_imp->main_repo).e_interface()->accept_keywords_variable());
    std::string ak;
    if (! accept_keywords_var.empty())
        ak = (*_imp->main_repo).e_interface()->profile_variable(accept_keywords_var);

    if (ak.empty())
    {
        std::string arch_var((*_imp->main_repo).e_interface()->arch_variable());

        if (arch_var.empty())
        {
            if (_imp->params.extra_accept_keywords().empty())
                throw ConfigurationError("Don't know how to work out whether keywords are acceptable");
        }
        else
        {
            std::string arch((*_imp->main_repo).e_interface()->profile_variable(arch_var));

            if (keywords->end() != keywords->find(KeywordName(arch)))
                return true;

            if (_imp->accept_unstable && keywords->end() != keywords->find(KeywordName("~" + arch)))
                return true;
        }
    }
    else
    {
        std::list<KeywordName> accepted;
        tokenise_whitespace(ak, create_inserter<KeywordName>(std::back_inserter(accepted)));

        for (KeywordNameSet::ConstIterator k(keywords->begin()), k_end(keywords->end()) ;
                k != k_end ; ++k)
        {
            if (accepted.end() != std::find(accepted.begin(), accepted.end(), *k))
                return true;

            if (_imp->accept_unstable && stringify(*k).at(0) == '~')
                if (accepted.end() != std::find(accepted.begin(), accepted.end(), KeywordName(stringify(*k).substr(1))))
                    return true;
        }
    }

    {
        std::list<KeywordName> accepted;
        tokenise_whitespace(_imp->params.extra_accept_keywords(),
                create_inserter<KeywordName>(std::back_inserter(accepted)));

        for (KeywordNameSet::ConstIterator k(keywords->begin()), k_end(keywords->end()) ;
                k != k_end ; ++k)
        {
            if (accepted.end() != std::find(accepted.begin(), accepted.end(), *k))
                return true;

            if (_imp->accept_unstable && stringify(*k).at(0) == '~')
                if (accepted.end() != std::find(accepted.begin(), accepted.end(), KeywordName(stringify(*k).substr(1))))
                    return true;
        }
    }

    return false;
}

const std::tr1::shared_ptr<const SetSpecTree>
NoConfigEnvironment::local_set(const SetName &) const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const SetSpecTree>
NoConfigEnvironment::world_set() const
{
    return make_null_shared_ptr();
}

void
NoConfigEnvironment::add_to_world(const QualifiedPackageName &) const
{
}

void
NoConfigEnvironment::remove_from_world(const QualifiedPackageName &) const
{
}

void
NoConfigEnvironment::add_to_world(const SetName &) const
{
}

void
NoConfigEnvironment::remove_from_world(const SetName &) const
{
}

bool
NoConfigEnvironment::unmasked_by_user(const PackageID &) const
{
    return false;
}

const std::tr1::shared_ptr<const Mask>
NoConfigEnvironment::mask_for_breakage(const PackageID &) const
{
    return std::tr1::shared_ptr<const Mask>();
}

const std::tr1::shared_ptr<const Mask>
NoConfigEnvironment::mask_for_user(const PackageID &, const bool) const
{
    return std::tr1::shared_ptr<const Mask>();
}

uid_t
NoConfigEnvironment::reduced_uid() const
{
    return getuid();
}

gid_t
NoConfigEnvironment::reduced_gid() const
{
    return getgid();
}

std::tr1::shared_ptr<const MirrorsSequence>
NoConfigEnvironment::mirrors(const std::string &) const
{
    return make_shared_ptr(new MirrorsSequence);
}

bool
NoConfigEnvironment::accept_license(const std::string &, const PackageID &) const
{
    return true;
}

const FSEntry
NoConfigEnvironment::root() const
{
    return FSEntry("/");
}

HookResult
NoConfigEnvironment::perform_hook(const Hook &) const
{
    return make_named_values<HookResult>(value_for<n::max_exit_status>(0), value_for<n::output>(""));
}

std::tr1::shared_ptr<const FSEntrySequence>
NoConfigEnvironment::hook_dirs() const
{
    return make_shared_ptr(new FSEntrySequence);
}

void
NoConfigEnvironment::need_keys_added() const
{
}

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

const std::tr1::shared_ptr<const MetadataValueKey<FSEntry> >
NoConfigEnvironment::config_location_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<FSEntry> >();
}

const Tribool
NoConfigEnvironment::want_choice_enabled(
        const std::tr1::shared_ptr<const PackageID> &,
        const std::tr1::shared_ptr<const Choice> &,
        const UnprefixedChoiceName &
        ) const
{
    return Tribool(indeterminate);
}

const std::string
NoConfigEnvironment::value_for_choice_parameter(
        const std::tr1::shared_ptr<const PackageID> &,
        const std::tr1::shared_ptr<const Choice> &,
        const UnprefixedChoiceName &
        ) const
{
    return "";
}

std::tr1::shared_ptr<const Set<UnprefixedChoiceName> >
NoConfigEnvironment::known_choice_value_names(
        const std::tr1::shared_ptr<const PackageID> &,
        const std::tr1::shared_ptr<const Choice> &
        ) const
{
    return make_shared_ptr(new Set<UnprefixedChoiceName>);
}

const std::tr1::shared_ptr<OutputManager>
NoConfigEnvironment::create_output_manager(const CreateOutputManagerInfo &) const
{
    return make_shared_ptr(new StandardOutputManager);
}