aboutsummaryrefslogtreecommitdiff
path: root/paludis/dep_list/uninstall_list.cc
blob: d5e4661e0600458125603713175888b60e64e224 (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) 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 "uninstall_list.hh"

using namespace paludis;

#include <paludis/dep_list/uninstall_list-sr.cc>
#include <paludis/dep_list/condition_tracker.hh>
#include <paludis/environment.hh>
#include <paludis/util/collection_concrete.hh>
#include <paludis/util/join.hh>
#include <paludis/util/log.hh>
#include <paludis/util/save.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/hashed_containers.hh>
#include <paludis/match_package.hh>
#include <paludis/package_database.hh>
#include <paludis/package_id.hh>
#include <paludis/query.hh>
#include <paludis/metadata_key.hh>
#include <paludis/dep_tag.hh>
#include <libwrapiter/libwrapiter_forward_iterator.hh>
#include <libwrapiter/libwrapiter_output_iterator.hh>
#include <list>
#include <algorithm>

typedef MakeHashedMap<tr1::shared_ptr<const PackageID>, tr1::shared_ptr<const DepListEntryTags> >::Type DepCollectorCache;

namespace paludis
{
    template<>
    struct Implementation<UninstallList>
    {
        const Environment * const env;
        UninstallListOptions options;
        std::list<UninstallListEntry> uninstall_list;

        mutable DepCollectorCache dep_collector_cache;

        Implementation(const Environment * const e, const UninstallListOptions & o) :
            env(e),
            options(o)
        {
        }
    };
}

namespace
{
    struct MatchUninstallListEntry
    {
        const tr1::shared_ptr<const PackageID> e;

        MatchUninstallListEntry(const tr1::shared_ptr<const PackageID> & ee) :
            e(ee)
        {
        }

        bool operator() (const UninstallListEntry & f) const
        {
            return *f.package_id == *e;
        }
    };
}

UninstallList::UninstallList(const Environment * const env, const UninstallListOptions & o) :
    PrivateImplementationPattern<UninstallList>(new Implementation<UninstallList>(env, o)),
    options(_imp->options)
{
}

UninstallList::~UninstallList()
{
}

void
UninstallList::add(const tr1::shared_ptr<const PackageID> & e, tr1::shared_ptr<DepTag> t)
{
    std::list<UninstallListEntry>::iterator i;
    if (_imp->uninstall_list.end() != ((i = std::find_if(_imp->uninstall_list.begin(),
                        _imp->uninstall_list.end(), MatchUninstallListEntry(e)))))
    {
        if (t)
            i->tags->insert(t);

        return;
    }

    Context context("When adding '" + stringify(*e) + "' to the uninstall list:");

    add_package(e, t);

    if (_imp->options.with_dependencies)
        add_dependencies(*e);

    move_package_to_end(e);

    if (_imp->options.with_unused_dependencies)
        add_unused_dependencies();
}

void
UninstallList::add_unused()
{
    Context context("When finding unused packages:");

    tr1::shared_ptr<const PackageIDSet> world(collect_world()),
        everything(collect_all_installed());

    tr1::shared_ptr<PackageIDSet>
        world_plus_deps(new PackageIDSet::Concrete),
        unused(new PackageIDSet::Concrete);

    world_plus_deps->insert(world->begin(), world->end());

    std::size_t old_size(0);
    while (old_size != world_plus_deps->size())
    {
        old_size = world_plus_deps->size();
        tr1::shared_ptr<const PackageIDSet> new_world_deps(collect_depped_upon(world_plus_deps));
        world_plus_deps->insert(new_world_deps->begin(), new_world_deps->end());
    }

    std::set_difference(everything->begin(), everything->end(),
            world_plus_deps->begin(), world_plus_deps->end(), unused->inserter(),
            PackageIDSetComparator());

    for (PackageIDSet::Iterator i(unused->begin()), i_end(unused->end()) ; i != i_end ; ++i)
        add_package(*i, tr1::shared_ptr<DepTag>());
}

UninstallList::Iterator
UninstallList::begin() const
{
    return Iterator(_imp->uninstall_list.begin());
}

UninstallList::Iterator
UninstallList::end() const
{
    return Iterator(_imp->uninstall_list.end());
}

UninstallListOptions::UninstallListOptions() :
    with_unused_dependencies(false),
    with_dependencies(false)
{
}

void
UninstallList::add_package(const tr1::shared_ptr<const PackageID> & e, tr1::shared_ptr<DepTag> t)
{
    Context context("When adding package '" + stringify(*e) + "' to the uninstall list:");

    std::list<UninstallListEntry>::iterator i(_imp->uninstall_list.insert(
                _imp->uninstall_list.end(), UninstallListEntry(
                    e, e->virtual_for_key(), tr1::shared_ptr<SortedCollection<tr1::shared_ptr<DepTag> > >(
                        new SortedCollection<tr1::shared_ptr<DepTag> >::Concrete))));
    if (t)
        i->tags->insert(t);
}

void
UninstallList::move_package_to_end(const tr1::shared_ptr<const PackageID> & e)
{
    Context context("When removing package '" + stringify(*e) + "' from the uninstall list:");

    std::list<UninstallListEntry>::iterator i(std::find_if(_imp->uninstall_list.begin(),
                _imp->uninstall_list.end(), MatchUninstallListEntry(e)));
    if (_imp->uninstall_list.end() != i)
        _imp->uninstall_list.splice(_imp->uninstall_list.end(), _imp->uninstall_list, i);
}

tr1::shared_ptr<const PackageIDSet>
UninstallList::collect_all_installed() const
{
    Context context("When collecting all installed packages:");

    tr1::shared_ptr<PackageIDSet> result(new PackageIDSet::Concrete);
    for (PackageDatabase::RepositoryIterator i(_imp->env->package_database()->begin_repositories()),
            i_end(_imp->env->package_database()->end_repositories()) ; i != i_end ; ++i)
    {
        if (! (*i)->installed_interface)
            continue;

        tr1::shared_ptr<const CategoryNamePartCollection> cats((*i)->category_names());
        for (CategoryNamePartCollection::Iterator c(cats->begin()), c_end(cats->end()) ;
                c != c_end ; ++c)
        {
            tr1::shared_ptr<const QualifiedPackageNameCollection> pkgs((*i)->package_names(*c));
            for (QualifiedPackageNameCollection::Iterator p(pkgs->begin()), p_end(pkgs->end()) ;
                    p != p_end ; ++p)
            {
                tr1::shared_ptr<const PackageIDSequence> ids((*i)->package_ids(*p));
                std::copy(ids->begin(), ids->end(), result->inserter());
            }
        }
    }

    return result;
}

namespace
{
    struct DepCollector :
        ConstVisitor<DependencySpecTree>,
        ConstVisitor<DependencySpecTree>::VisitConstSequence<DepCollector, AllDepSpec>,
        ConstVisitor<DependencySpecTree>::VisitConstSequence<DepCollector, AnyDepSpec>
    {
        using ConstVisitor<DependencySpecTree>::VisitConstSequence<DepCollector, AllDepSpec>::visit_sequence;

        const Environment * const env;
        const tr1::shared_ptr<const PackageID> pkg;
        tr1::shared_ptr<DepListEntryTags> matches;
        tr1::shared_ptr<ConstTreeSequence<DependencySpecTree, AllDepSpec> > conditions;


        DepCollector(const Environment * const ee, const tr1::shared_ptr<const PackageID> & e) :
            env(ee),
            pkg(e),
            matches(new DepListEntryTags::Concrete),
            conditions(tr1::shared_ptr<ConstTreeSequence<DependencySpecTree, AllDepSpec> >(
                           new ConstTreeSequence<DependencySpecTree, AllDepSpec>(
                               tr1::shared_ptr<AllDepSpec>(new AllDepSpec))))
        {
        }

        void visit_leaf(const PackageDepSpec & a)
        {
            Save<tr1::shared_ptr<ConstTreeSequence<DependencySpecTree, AllDepSpec> > > save_c(
                &conditions, ConditionTracker(conditions).add_condition(a));

            tr1::shared_ptr<const PackageIDSequence> m(env->package_database()->query(
                        query::Matches(a) & query::InstalledAtRoot(env->root()), qo_order_by_version));
            for (PackageIDSequence::Iterator it = m->begin(), it_end = m->end();
                 it_end != it; ++it)
                matches->insert(DepTagEntry(tr1::shared_ptr<const DepTag>(new DependencyDepTag(*it, a, conditions)), 0));
        }

        void visit_sequence(const AnyDepSpec & a,
                DependencySpecTree::ConstSequenceIterator cur,
                DependencySpecTree::ConstSequenceIterator end)
        {
            Save<tr1::shared_ptr<ConstTreeSequence<DependencySpecTree, AllDepSpec> > > save_c(
                &conditions, ConditionTracker(conditions).add_condition(a));

            std::for_each(cur, end, accept_visitor(*this));
        }

        void visit_sequence(const UseDepSpec & u,
                DependencySpecTree::ConstSequenceIterator cur,
                DependencySpecTree::ConstSequenceIterator end)
        {
            Save<tr1::shared_ptr<ConstTreeSequence<DependencySpecTree, AllDepSpec> > > save_c(
                &conditions, ConditionTracker(conditions).add_condition(u));

            if (env->query_use(UseFlagName(u.flag()), *pkg) ^ u.inverse())
                std::for_each(cur, end, accept_visitor(*this));
        }

        void visit_leaf(const BlockDepSpec &)
        {
        }
    };
}

tr1::shared_ptr<const PackageIDSet>
UninstallList::collect_depped_upon(tr1::shared_ptr<const PackageIDSet> targets) const
{
    Context context("When collecting depended upon packages:");

    tr1::shared_ptr<PackageIDSet> result(new PackageIDSet::Concrete);

    for (PackageIDSet::Iterator i(targets->begin()), i_end(targets->end()) ;
            i != i_end ; ++i)
    {
        Context local_context("When collecting depended upon packages for '" + stringify(**i) + "':");

        DepCollectorCache::const_iterator cache(_imp->dep_collector_cache.find(*i));

        if (cache == _imp->dep_collector_cache.end())
        {
            DepCollector c(_imp->env, *i);
            if ((*i)->build_dependencies_key())
                (*i)->build_dependencies_key()->value()->accept(c);
            if ((*i)->run_dependencies_key())
                (*i)->run_dependencies_key()->value()->accept(c);
            if ((*i)->post_dependencies_key())
                (*i)->post_dependencies_key()->value()->accept(c);
            if ((*i)->suggested_dependencies_key())
                (*i)->suggested_dependencies_key()->value()->accept(c);

            cache = _imp->dep_collector_cache.insert(std::make_pair(*i,
                        tr1::shared_ptr<const DepListEntryTags>(c.matches))).first;
        }

        for (DepListEntryTags::Iterator it(cache->second->begin()), it_end(cache->second->end());
             it_end != it; ++it)
            result->insert(tr1::static_pointer_cast<const DependencyDepTag>(it->tag)->package_id());
    }

    return result;
}

void
UninstallList::add_unused_dependencies()
{
    Context context("When adding unused dependencies:");

    bool added(true);
    tr1::shared_ptr<const PackageIDSet> everything(collect_all_installed());
    while (added)
    {
        added = false;

        /* find packages that're depped upon by anything in our uninstall list */
        tr1::shared_ptr<PackageIDSet> uninstall_list_targets(new PackageIDSet::Concrete);
        for (std::list<UninstallListEntry>::const_iterator i(_imp->uninstall_list.begin()),
                i_end(_imp->uninstall_list.end()) ; i != i_end ; ++i)
            uninstall_list_targets->insert(i->package_id);

        tr1::shared_ptr<const PackageIDSet> depped_upon_list(collect_depped_upon(uninstall_list_targets));

        /* find packages that're depped upon by anything not in our uninstall list */
        tr1::shared_ptr<PackageIDSet> everything_except_uninstall_list_targets(
                new PackageIDSet::Concrete);
        std::set_difference(everything->begin(), everything->end(),
                uninstall_list_targets->begin(), uninstall_list_targets->end(),
                everything_except_uninstall_list_targets->inserter(),
                PackageIDSetComparator());

        Log::get_instance()->message(ll_debug, lc_context, "everything_except_uninstall_list_targets is '"
                + join(indirect_iterator(everything_except_uninstall_list_targets->begin()),
                    indirect_iterator(everything_except_uninstall_list_targets->end()), " ") + "'");

        tr1::shared_ptr<const PackageIDSet> depped_upon_not_list(
                collect_depped_upon(everything_except_uninstall_list_targets));

        /* find unused dependencies */
        tr1::shared_ptr<PackageIDSet> unused_dependencies(new PackageIDSet::Concrete);
        std::set_difference(depped_upon_list->begin(), depped_upon_list->end(),
                depped_upon_not_list->begin(), depped_upon_not_list->end(), unused_dependencies->inserter(),
                PackageIDSetComparator());

        /* if any of them aren't already on the list, and aren't in world, add them and recurse */
        tr1::shared_ptr<SetSpecTree::ConstItem> world(_imp->env->set(SetName("world")));
        for (PackageIDSet::Iterator i(unused_dependencies->begin()),
                i_end(unused_dependencies->end()) ; i != i_end ; ++i)
        {
            if (match_package_in_set(*_imp->env, *world, **i))
                continue;

            if (_imp->uninstall_list.end() != std::find_if(_imp->uninstall_list.begin(),
                        _imp->uninstall_list.end(), MatchUninstallListEntry(*i)))
                continue;

            add_package(*i, tr1::shared_ptr<DepTag>());
            added = true;
        }
    }
}

void
UninstallList::add_dependencies(const PackageID & e)
{
    Context context("When adding things that depend upon '" + stringify(e) + "':");

    tr1::shared_ptr<const PackageIDSet> everything(collect_all_installed());
    for (PackageIDSet::Iterator i(everything->begin()),
            i_end(everything->end()) ; i != i_end ; ++i)
    {
        Context local_context("When seeing whether '" + stringify(**i) + "' has a dep:");

        DepCollectorCache::const_iterator cache(_imp->dep_collector_cache.find(*i));

        if (cache == _imp->dep_collector_cache.end())
        {
            DepCollector c(_imp->env, *i);
            if ((*i)->build_dependencies_key())
                (*i)->build_dependencies_key()->value()->accept(c);
            if ((*i)->run_dependencies_key())
                (*i)->run_dependencies_key()->value()->accept(c);
            if ((*i)->post_dependencies_key())
                (*i)->post_dependencies_key()->value()->accept(c);
            if ((*i)->suggested_dependencies_key())
                (*i)->suggested_dependencies_key()->value()->accept(c);
            cache = _imp->dep_collector_cache.insert(std::make_pair(*i,
                        tr1::shared_ptr<const DepListEntryTags>(c.matches))).first;
        }

        bool logged(false);
        for (DepListEntryTags::Iterator it(cache->second->begin()), it_end(cache->second->end());
             it_end != it; ++it)
        {
            tr1::shared_ptr<const DependencyDepTag> tag(tr1::static_pointer_cast<const DependencyDepTag>(it->tag));
            if (*tag->package_id() == e)
            {
                if (! logged)
                {
                    Log::get_instance()->message(ll_debug, lc_context) << "Adding '" << **i <<
                            "' because it depends upon '" << e << "'";
                    logged = true;
                }
                add(*i, tr1::shared_ptr<DependencyDepTag>(
                        new DependencyDepTag(tag->package_id(), *tag->dependency(), tag->conditions())));
            }
        }

    }
}

tr1::shared_ptr<const PackageIDSet>
UninstallList::collect_world() const
{
    Context local_context("When collecting world packages:");

    tr1::shared_ptr<PackageIDSet> result(new PackageIDSet::Concrete);
    tr1::shared_ptr<const PackageIDSet> everything(collect_all_installed());

    tr1::shared_ptr<SetSpecTree::ConstItem> world(_imp->env->set(SetName("world")));
    for (PackageIDSet::Iterator i(everything->begin()),
            i_end(everything->end()) ; i != i_end ; ++i)
        if (match_package_in_set(*_imp->env, *world, **i))
            result->insert(*i);

    return result;
}