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

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

#include <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/repositories/repository_maker.hh>

#include <paludis/config_file.hh>
#include <paludis/hooker.hh>
#include <paludis/hook.hh>
#include <paludis/set_file.hh>
#include <paludis/distribution.hh>
#include <paludis/dep_tag.hh>
#include <paludis/package_id.hh>

#include <paludis/util/collection_concrete.hh>
#include <paludis/util/log.hh>
#include <paludis/util/system.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/save.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/tr1_functional.hh>
#include <libwrapiter/libwrapiter_forward_iterator.hh>
#include <libwrapiter/libwrapiter_output_iterator.hh>
#include <functional>
#include <algorithm>
#include <list>

using namespace paludis;
using namespace paludis::paludis_environment;

namespace paludis
{
    template<>
    struct Implementation<PaludisEnvironment>
    {
        mutable bool done_hooks;
        mutable tr1::shared_ptr<Hooker> hooker;
        mutable std::list<std::pair<FSEntry, bool> > hook_dirs;

        tr1::shared_ptr<PaludisConfig> config;
        std::string paludis_command;
        std::list<UseConfigEntry> forced_use;

        tr1::shared_ptr<PackageDatabase> package_database;

        Implementation(PaludisEnvironment * const e, tr1::shared_ptr<PaludisConfig> c) :
            done_hooks(false),
            config(c),
            paludis_command("paludis"),
            package_database(new PackageDatabase(e))
        {
        }

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

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

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

    for (PaludisConfig::RepositoryIterator r(_imp->config->begin_repositories()),
            r_end(_imp->config->end_repositories()) ; r != r_end ; ++r)
    {
        std::string keys;
        if (Log::get_instance()->log_level() <= ll_debug)
        {
            if (r->keys)
                for (AssociativeCollection<std::string, std::string>::Iterator
                        i(r->keys->begin()), i_end(r->keys->end()) ; i != i_end ; ++i)
                {
                    if (! keys.empty())
                        keys.append(", ");
                    keys.append("'" + i->first + "'='" + i->second + "'");
                }
            else
                keys = "empty";

            Log::get_instance()->message(ll_debug, lc_context,
                    "Creating repository with format='" + r->format + "', importance='"
                    + stringify(r->importance) + "', keys " + keys);
        }

        _imp->package_database->add_repository(r->importance,
                RepositoryMaker::get_instance()->find_maker(r->format)(this, r->keys));
    }
}

PaludisEnvironment::~PaludisEnvironment()
{
}

bool
PaludisEnvironment::query_use(const UseFlagName & f, const PackageID & e) const
{
    Context context("When querying use flag '" + stringify(f) + "' for '" + stringify(e) +
            "' in Paludis environment:");

    static bool recursive(false);
    if (recursive)
    {
        Log::get_instance()->message(ll_warning, lc_context) <<
            "use flag state is defined recursively, forcing it to disabled instead";
        return false;
    }
    Save<bool> save_recursive(&recursive, true);

    /* first check package database use masks... */
    if (e.repository()->use_interface)
    {
        if (e.repository()->use_interface->query_use_mask(f, e))
            return false;
        if (e.repository()->use_interface->query_use_force(f, e))
            return true;
    }

    /* check configs */
    do
    {
        switch (_imp->config->use_conf()->query(f, e))
        {
            case use_disabled:
                return false;

            case use_enabled:
                return true;

            case use_unspecified:
                continue;

            case last_use:
                ;
        }
        throw InternalError(PALUDIS_HERE, "bad state");
    } while (false);

    /* check use: package database config */
    if (e.repository()->use_interface)
    {
        switch (e.repository()->use_interface->query_use(f, e))
        {
            case use_disabled:
            case use_unspecified:
                return false;

            case use_enabled:
                return true;

            case last_use:
                ;
        }

        throw InternalError(PALUDIS_HERE, "bad state");
    }

    return false;
}

bool
PaludisEnvironment::accept_breaks_portage(const PackageID &) const
{
    return _imp->config->accept_breaks_portage();
}

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

bool
PaludisEnvironment::accept_license(const std::string & license, 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::masked_by_user(const PackageID & d) const
{
    return _imp->config->package_mask_conf()->query(d);
}

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

tr1::shared_ptr<const FSEntryCollection>
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
{
    if (! _imp->hooker)
    {
        _imp->need_hook_dirs(_imp->config->config_dir());
        _imp->hooker.reset(new Hooker(this));
        for (std::list<std::pair<FSEntry, 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);
}

tr1::shared_ptr<const FSEntryCollection>
PaludisEnvironment::hook_dirs() const
{
    _imp->need_hook_dirs(_imp->config->config_dir());

    tr1::shared_ptr<FSEntryCollection> result(new FSEntryCollection::Concrete);
    std::copy(_imp->hook_dirs.begin(), _imp->hook_dirs.end(),
            transform_inserter(result->inserter(), tr1::mem_fn(&std::pair<FSEntry, bool>::first)));

    return result;
}

tr1::shared_ptr<const FSEntryCollection>
PaludisEnvironment::fetchers_dirs() const
{
    tr1::shared_ptr<FSEntryCollection> result(new FSEntryCollection::Concrete);

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

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

    return result;
}

tr1::shared_ptr<const FSEntryCollection>
PaludisEnvironment::syncers_dirs() const
{
    tr1::shared_ptr<FSEntryCollection> result(new FSEntryCollection::Concrete);

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

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

    return result;
}

tr1::shared_ptr<SetSpecTree::ConstItem>
PaludisEnvironment::local_set(const SetName & s) const
{
    Context context("When looking for package set '" + stringify(s) + "' in paludis environment:");

    FSEntry dir(FSEntry(_imp->config->config_dir()) / "sets");
    tr1::shared_ptr<GeneralSetDepTag> tag(new GeneralSetDepTag(s, stringify(s) + ".conf"));

    if ((dir / (stringify(s) + ".bash")).exists())
    {
        SetFile f(SetFileParams::create()
                .file_name(dir / (stringify(s) + ".bash"))
                .type(sft_paludis_bash)
                .parse_mode(pds_pm_unspecific)
                .tag(tag)
                .environment(this));
        return f.contents();
    }
    else if ((dir / (stringify(s) + ".conf")).exists())
    {
        SetFile f(SetFileParams::create()
                .file_name(dir / (stringify(s) + ".conf"))
                .type(sft_paludis_conf)
                .parse_mode(pds_pm_unspecific)
                .tag(tag)
                .environment(this));
        return f.contents();
    }
    else
        return tr1::shared_ptr<SetSpecTree::ConstItem>();
}

tr1::shared_ptr<const SetNameCollection>
PaludisEnvironment::set_names() const
{
    tr1::shared_ptr<SetNameCollection> result(new SetNameCollection::Concrete);

    if ((FSEntry(_imp->config->config_dir()) / "sets").exists())
        for (DirIterator d(FSEntry(_imp->config->config_dir()) / "sets"), d_end ;
                d != d_end ; ++d)
        {
            if (is_file_with_extension(*d, ".conf", IsFileWithOptions()))
                result->insert(SetName(strip_trailing_string(d->basename(), ".conf")));
            else if (is_file_with_extension(*d, ".bash", IsFileWithOptions()))
                result->insert(SetName(strip_trailing_string(d->basename(), ".bash")));
        }

    return result;
}

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

tr1::shared_ptr<const UseFlagNameCollection>
PaludisEnvironment::known_use_expand_names(const UseFlagName & prefix, const PackageID & e) const
{
    return _imp->config->use_conf()->known_use_expand_names(prefix, e);
}

const FSEntry
PaludisEnvironment::root() const
{
    return _imp->config->root();
}

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();
}

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

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

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