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

/*
 * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010 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/environment_implementation.hh>
#include <paludis/package_id.hh>
#include <paludis/metadata_key.hh>
#include <paludis/package_database.hh>
#include <paludis/util/log.hh>
#include <paludis/util/save.hh>
#include <paludis/util/set.hh>
#include <paludis/util/system.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/member_iterator-impl.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/make_null_shared_ptr.hh>
#include <paludis/hook.hh>
#include <paludis/distribution.hh>
#include <paludis/selection.hh>
#include <paludis/selection_cache.hh>
#include <algorithm>
#include <map>
#include <list>
#include "config.h"

using namespace paludis;

namespace
{
    typedef std::function<void (const SetName &) > CombiningFunction;

    struct CombineSets
    {
        std::shared_ptr<AllDepSpec> top;
        std::shared_ptr<SetSpecTree> tree;

        void add(const SetName & s)
        {
            tree->top()->append(std::make_shared<NamedSetDepSpec>(s));
        }

        CombineSets() :
            top(std::make_shared<AllDepSpec>()),
            tree(std::make_shared<SetSpecTree>(top))
        {
        }

        CombineSets(const CombineSets & other) :
            top(other.top),
            tree(other.tree)
        {
        }

        std::shared_ptr<const SetSpecTree> result() const
        {
            return tree;
        }
    };

    typedef std::map<SetName, std::pair<std::function<std::shared_ptr<const SetSpecTree> ()>, CombiningFunction> > SetsStore;

    template <typename F_>
    struct Cache
    {
        F_ func;
        std::shared_ptr<typename std::remove_reference<typename F_::result_type>::type> result;

        Cache(const F_ & f) :
            func(f)
        {
        }

        typename F_::result_type operator() ()
        {
            if (! result)
                result = std::make_shared<typename std::remove_reference<typename F_::result_type>::type>(func());
            return *result;
        }
    };

    template <typename F_>
    F_ cache(const F_ & f)
    {
        return Cache<F_>(f);
    }
}

namespace paludis
{
    template <>
    struct Imp<EnvironmentImplementation>
    {
        std::map<unsigned, NotifierCallbackFunction> notifier_callbacks;
        std::list<std::shared_ptr<const SelectionCache> > selection_caches;

        mutable Mutex sets_mutex;
        mutable bool loaded_sets;
        std::shared_ptr<SetNameSet> set_names;
        SetsStore sets;

        Imp() :
            loaded_sets(false)
        {
        }
    };
}

EnvironmentImplementation::EnvironmentImplementation() :
    Pimp<EnvironmentImplementation>(),
    _imp(Pimp<EnvironmentImplementation>::_imp)
{
}

EnvironmentImplementation::~EnvironmentImplementation()
{
}


std::shared_ptr<const FSPathSequence>
EnvironmentImplementation::bashrc_files() const
{
    return std::make_shared<FSPathSequence>();
}

std::shared_ptr<const FSPathSequence>
EnvironmentImplementation::syncers_dirs() const
{
    std::shared_ptr<FSPathSequence> result(std::make_shared<FSPathSequence>());
    result->push_back(FSPath(DATADIR "/paludis/syncers"));
    result->push_back(FSPath(LIBEXECDIR "/paludis/syncers"));
    return result;
}

std::shared_ptr<const FSPathSequence>
EnvironmentImplementation::fetchers_dirs() const
{
    std::shared_ptr<FSPathSequence> result(std::make_shared<FSPathSequence>());
    std::string fetchers_dir(getenv_with_default("PALUDIS_FETCHERS_DIR", ""));
    if (fetchers_dir.empty())
    {
        result->push_back(FSPath(DATADIR "/paludis/fetchers"));
        result->push_back(FSPath(LIBEXECDIR "/paludis/fetchers"));
    }
    else
        result->push_back(FSPath(fetchers_dir));
    return result;
}

std::shared_ptr<const DestinationsSet>
EnvironmentImplementation::default_destinations() const
{
    std::shared_ptr<DestinationsSet> result(std::make_shared<DestinationsSet>());

    for (PackageDatabase::RepositoryConstIterator r(package_database()->begin_repositories()),
            r_end(package_database()->end_repositories()) ;
            r != r_end ; ++r)
        if ((**r).destination_interface())
            if ((**r).destination_interface()->is_default_destination())
                result->insert(*r);

    return result;
}

std::string
EnvironmentImplementation::distribution() const
{
    static const std::string result(getenv_with_default("PALUDIS_DISTRIBUTION", DEFAULT_DISTRIBUTION));
    return result;
}

bool
EnvironmentImplementation::is_paludis_package(const QualifiedPackageName & n) const
{
    return stringify(n) == (*DistributionData::get_instance()->distribution_from_string(distribution())).paludis_package();
}

std::shared_ptr<PackageIDSequence>
EnvironmentImplementation::operator[] (const Selection & selection) const
{
    if (_imp->selection_caches.empty())
        return selection.perform_select(this);
    else
        return _imp->selection_caches.back()->perform_select(this, selection);
}

NotifierCallbackID
EnvironmentImplementation::add_notifier_callback(const NotifierCallbackFunction & f)
{
    unsigned idx(0);
    if (! _imp->notifier_callbacks.empty())
        idx = _imp->notifier_callbacks.rbegin()->first + 1;

    _imp->notifier_callbacks.insert(std::make_pair(idx, f));
    return idx;
}

void
EnvironmentImplementation::remove_notifier_callback(const NotifierCallbackID i)
{
    _imp->notifier_callbacks.erase(i);
}

void
EnvironmentImplementation::trigger_notifier_callback(const NotifierCallbackEvent & e) const
{
    for (std::map<unsigned, NotifierCallbackFunction>::const_iterator i(_imp->notifier_callbacks.begin()),
            i_end(_imp->notifier_callbacks.end()) ;
            i != i_end ; ++i)
        (i->second)(e);
}

void
EnvironmentImplementation::add_selection_cache(const std::shared_ptr<const SelectionCache> & c)
{
    _imp->selection_caches.push_back(c);
}

void
EnvironmentImplementation::remove_selection_cache(const std::shared_ptr<const SelectionCache> & c)
{
    _imp->selection_caches.remove(c);
}

void
EnvironmentImplementation::add_set(
        const SetName & name,
        const SetName & combined_name,
        const std::function<std::shared_ptr<const SetSpecTree> ()> & func,
        const bool combine) const
{
    Lock lock(_imp->sets_mutex);
    Context context("When adding set named '" + stringify(name) + ":");

    if (combine)
    {
        if (! _imp->sets.insert(std::make_pair(combined_name, std::make_pair(cache(func), CombiningFunction()))).second)
            throw DuplicateSetError(combined_name);

        std::shared_ptr<CombineSets> c_s(std::make_shared<CombineSets>());
        CombiningFunction c_func(_imp->sets.insert(std::make_pair(name, std::make_pair(
                            std::bind(&CombineSets::result, c_s),
                            std::bind(&CombineSets::add, c_s, std::placeholders::_1)
                            ))).first->second.second);
        if (! c_func)
            throw DuplicateSetError(name);
        c_func(combined_name);
    }
    else
    {
        if (! _imp->sets.insert(std::make_pair(name, std::make_pair(cache(func), CombiningFunction()))).second)
            throw DuplicateSetError(name);
    }
}

std::shared_ptr<const SetNameSet>
EnvironmentImplementation::set_names() const
{
    Lock lock(_imp->sets_mutex);
    _need_sets();

    return _imp->set_names;
}

const std::shared_ptr<const SetSpecTree>
EnvironmentImplementation::set(const SetName & s) const
{
    Lock lock(_imp->sets_mutex);
    _need_sets();

    SetsStore::const_iterator i(_imp->sets.find(s));
    if (_imp->sets.end() != i)
        return i->second.first();
    else
        return make_null_shared_ptr();
}

void
EnvironmentImplementation::_need_sets() const
{
    if (_imp->loaded_sets)
        return;

    for (PackageDatabase::RepositoryConstIterator r(package_database()->begin_repositories()),
            r_end(package_database()->end_repositories()) ;
            r != r_end ; ++r)
        (*r)->populate_sets();

    populate_sets();
    populate_standard_sets();

    _imp->set_names = std::make_shared<SetNameSet>();
    std::copy(first_iterator(_imp->sets.begin()), first_iterator(_imp->sets.end()), _imp->set_names->inserter());

    _imp->loaded_sets = true;
}

namespace
{
    std::shared_ptr<const SetSpecTree> make_everything_set()
    {
        Log::get_instance()->message("environment_implementation.everything_deprecated", ll_warning, lc_context)
            << "The 'everything' set is deprecated. Use either 'installed-packages' or 'installed-slots' instead";

        std::shared_ptr<SetSpecTree> result(std::make_shared<SetSpecTree>(std::make_shared<AllDepSpec>()));
        result->top()->append(std::make_shared<NamedSetDepSpec>(SetName("installed-packages")));
        return result;
    }
}

void
EnvironmentImplementation::populate_standard_sets() const
{
    set_always_exists(SetName("system"));
    set_always_exists(SetName("world"));

    set_always_exists(SetName("installed-packages"));
    set_always_exists(SetName("installed-slots"));

    SetsStore::iterator i(_imp->sets.find(SetName("world")));
    /* some test cases build world through evil haxx. don't inject system in
     * then. */
    if (i->second.second)
        i->second.second(SetName("system"));

    /* nothing should define 'everything' any more */
    if (_imp->sets.end() != _imp->sets.find(SetName("everything")))
        throw InternalError(PALUDIS_HERE, "something's still defining the 'everything' set");
    add_set(SetName("everything"), SetName("everything"), make_everything_set, false);
}

namespace
{
    std::shared_ptr<const SetSpecTree> make_empty_set()
    {
        return std::make_shared<SetSpecTree>(std::make_shared<AllDepSpec>());
    }
}

void
EnvironmentImplementation::set_always_exists(const SetName & s) const
{
    SetsStore::const_iterator i(_imp->sets.find(s));
    if (_imp->sets.end() == i)
        add_set(s, SetName(stringify(s) + "::default"), make_empty_set, true);
}

DuplicateSetError::DuplicateSetError(const SetName & s) throw () :
    Exception("A set named '" + stringify(s) + "' already exists")
{
}