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

/*
 * Copyright (c) 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/legacy/uninstall_task.hh>
#include <paludis/legacy/uninstall_list.hh>
#include <paludis/environment.hh>
#include <paludis/action.hh>
#include <paludis/dep_spec_flattener.hh>
#include <paludis/legacy/tasks_exceptions.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/selection.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/metadata_key.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/sequence-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/options.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/make_null_shared_ptr.hh>
#include <paludis/util/accept_visitor.hh>
#include <paludis/package_database.hh>
#include <paludis/hook.hh>
#include <paludis/dep_tag.hh>
#include <paludis/repository.hh>
#include <paludis/output_manager_from_environment.hh>
#include <paludis/output_manager.hh>
#include <paludis/partially_made_package_dep_spec.hh>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <functional>

using namespace paludis;

AmbiguousUnmergeTargetError::AmbiguousUnmergeTargetError(const std::string & t,
        const std::shared_ptr<const PackageIDSequence> m) throw () :
    Exception("Ambiguous unmerge target '" + t + "'"),
    _t(t),
    _p(m)
{
}

AmbiguousUnmergeTargetError::~AmbiguousUnmergeTargetError() throw ()
{
}

AmbiguousUnmergeTargetError::ConstIterator
AmbiguousUnmergeTargetError::begin() const
{
    return ConstIterator(_p->begin());
}

AmbiguousUnmergeTargetError::ConstIterator
AmbiguousUnmergeTargetError::end() const
{
    return ConstIterator(_p->end());
}

std::string
AmbiguousUnmergeTargetError::target() const
{
    return _t;
}

namespace paludis
{
    template<>
    struct Imp<UninstallTask>
    {
        Environment * const env;

        std::list<std::string> raw_targets;
        std::list<std::shared_ptr<const PackageDepSpec> > targets;

        bool pretend;
        bool preserve_world;
        bool all_versions;
        bool with_unused_dependencies;
        bool with_dependencies;
        bool unused;
        bool check_safety;

        bool had_set_targets;
        bool had_package_targets;

        Imp<UninstallTask>(Environment * const e) :
            env(e),
            pretend(false),
            preserve_world(false),
            all_versions(false),
            with_unused_dependencies(false),
            with_dependencies(false),
            unused(false),
            check_safety(false),
            had_set_targets(false),
            had_package_targets(false)
        {
        }
    };

    template <>
    struct WrappedForwardIteratorTraits<AmbiguousUnmergeTargetError::ConstIteratorTag>
    {
        typedef PackageIDSequence::ConstIterator UnderlyingIterator;
    };
}

UninstallTask::UninstallTask(Environment * const e) :
    Pimp<UninstallTask>(e)
{
}

UninstallTask::~UninstallTask()
{
}

void
UninstallTask::set_pretend(const bool v)
{
    _imp->pretend = v;
}

void
UninstallTask::set_preserve_world(const bool v)
{
    _imp->preserve_world = v;
}

void
UninstallTask::set_check_safety(const bool v)
{
    _imp->check_safety = v;
}

void
UninstallTask::add_target(const std::string & target)
{
    Context context("When adding uninstall target '" + target + "':");

    try
    {
        std::shared_ptr<PackageDepSpec> pds(std::make_shared<PackageDepSpec>(parse_user_package_dep_spec(
                        target, _imp->env, { updso_throw_if_set, updso_allow_wildcards },
                        filter::SupportsAction<UninstallAction>())));

        if (_imp->had_set_targets)
            throw HadBothPackageAndSetTargets();
        _imp->had_package_targets = true;

        if (pds->package_ptr())
        {
            /* don't need to dewildcard */
            pds->set_tag(std::shared_ptr<const DepTag>(std::make_shared<TargetDepTag>()));
            _imp->targets.push_back(pds);
        }
        else
        {
            /* blech. wildcards. */
            std::shared_ptr<const PackageIDSequence> names((*_imp->env)[selection::BestVersionOnly(
                        generator::Matches(*pds, { }) | filter::SupportsAction<UninstallAction>())]);
            if (names->empty())
            {
                /* no match. we'll get an error from this later anyway. */
                pds->set_tag(std::shared_ptr<const DepTag>(std::make_shared<TargetDepTag>()));
                _imp->targets.push_back(pds);
            }
            else
            {
                for (PackageIDSequence::ConstIterator i(names->begin()), i_end(names->end()) ;
                        i != i_end ; ++i)
                {
                    PartiallyMadePackageDepSpec p(*pds);
                    p.package((*i)->name());
                    std::shared_ptr<PackageDepSpec> pdsn(std::make_shared<PackageDepSpec>(p));
                    pdsn->set_tag(std::shared_ptr<const DepTag>(std::make_shared<TargetDepTag>()));
                    _imp->targets.push_back(pdsn);
                }
            }
        }
    }
    catch (const GotASetNotAPackageDepSpec &)
    {
        if (_imp->had_set_targets)
            throw MultipleSetTargetsSpecified();
        if (_imp->had_package_targets)
            throw HadBothPackageAndSetTargets();
        _imp->had_set_targets = true;

        std::shared_ptr<const SetSpecTree> spec(_imp->env->set(SetName(target)));
        DepSpecFlattener<SetSpecTree, PackageDepSpec> f(_imp->env);
        spec->top()->accept(f);
        std::copy(f.begin(), f.end(), std::back_inserter(_imp->targets));
    }

    _imp->raw_targets.push_back(target);

    if (_imp->unused)
        throw InternalError(PALUDIS_HERE, "Trying to mix unused and normal targets?");
}

void
UninstallTask::add_unused()
{
    Context context("When adding unused packages:");
    _imp->unused = true;

    if (! _imp->raw_targets.empty())
        throw InternalError(PALUDIS_HERE, "Trying to mix unused and normal targets?");
}

namespace
{
    bool ignore_nothing(const FSPath &)
    {
        return false;
    }
}

void
UninstallTask::execute()
{
    using namespace std::placeholders;

    Context context("When executing uninstall task:");

    on_build_unmergelist_pre();

    UninstallList list(_imp->env, make_named_values<UninstallListOptions>(
            n::with_dependencies_as_errors() = _imp->check_safety,
            n::with_dependencies_included() = _imp->with_dependencies,
            n::with_unused_dependencies() = _imp->with_unused_dependencies
            ));

    if (_imp->unused)
        list.add_unused();
    else
    {
        for (std::list<std::shared_ptr<const PackageDepSpec> >::const_iterator t(_imp->targets.begin()),
                t_end(_imp->targets.end()) ; t != t_end ; ++t)
        {
            Context local_context("When looking for target '" + stringify(**t) + "':");

            std::shared_ptr<const PackageIDSequence> r((*_imp->env)[selection::AllVersionsSorted(
                        generator::Matches(**t, { }) |
                        filter::SupportsAction<UninstallAction>())]);
            if (r->empty())
            {
                if (! _imp->had_set_targets)
                    throw NoSuchPackageError(stringify(**t));
            }
            else if (next(r->begin()) != r->end())
            {
                if (_imp->all_versions)
                {
                    /* all_versions, not all_packages. */
                    for (PackageIDSequence::ConstIterator i_start(r->begin()), i(r->begin()),
                            i_end(r->end()) ; i != i_end ; ++i)
                        if ((*i)->name() != (*i_start)->name())
                            throw AmbiguousUnmergeTargetError(stringify(**t), r);

                    for (PackageIDSequence::ConstIterator i(r->begin()), i_end(r->end()) ;
                            i != i_end ; ++i)
                        list.add(*i);
                }
                else
                    throw AmbiguousUnmergeTargetError(stringify(**t), r);
            }
            else
                list.add(*r->begin());
        }

        if (_imp->check_safety)
            list.add_errors_for_system();
    }

    on_build_unmergelist_post();

    on_display_unmerge_list_pre();

    for (UninstallList::ConstIterator i(list.begin()), i_end(list.end()) ; i != i_end ; ++i)
        on_display_unmerge_list_entry(*i);

    on_display_unmerge_list_post();

    if (_imp->pretend)
        return;

    if (list.has_errors())
    {
        on_not_continuing_due_to_errors();
        return;
    }

    if (_imp->preserve_world)
        on_preserve_world();
    else
    {
        on_update_world_pre();

        std::shared_ptr<SetSpecTree> all(std::make_shared<SetSpecTree>(std::make_shared<AllDepSpec>()));

        std::map<QualifiedPackageName, std::set<VersionSpec> > being_removed;
        for (UninstallList::ConstIterator i(list.begin()), i_end(list.end()) ; i != i_end ; ++i)
            if (i->kind() != ulk_virtual)
                being_removed[i->package_id()->name()].insert(i->package_id()->version());

        for (std::map<QualifiedPackageName, std::set<VersionSpec> >::const_iterator
                i(being_removed.begin()), i_end(being_removed.end()) ; i != i_end ; ++i)
        {
            bool remove(true);
            std::shared_ptr<const PackageIDSequence> installed((*_imp->env)[selection::AllVersionsUnsorted(
                        generator::Matches(make_package_dep_spec({ }).package(i->first), { }) |
                        filter::InstalledAtRoot(_imp->env->preferred_root_key()->value())
                        )]);
            for (PackageIDSequence::ConstIterator r(installed->begin()), r_end(installed->end()) ;
                    r != r_end && remove ; ++r)
                if (i->second.end() == i->second.find((*r)->version()))
                    remove = false;

            if (remove)
                all->top()->append(std::make_shared<PackageDepSpec>(make_package_dep_spec(
                                    { }).package(i->first)));
        }

        world_remove_packages(all);

        if (_imp->had_set_targets)
            for (std::list<std::string>::const_iterator t(_imp->raw_targets.begin()),
                    t_end(_imp->raw_targets.end()) ; t != t_end ; ++t)
                world_remove_set(SetName(*t));

        on_update_world_post();
    }

    if (0 !=
        _imp->env->perform_hook(Hook("uninstall_all_pre")("TARGETS", join(_imp->raw_targets.begin(),
                         _imp->raw_targets.end(), " ")),
            make_null_shared_ptr()).max_exit_status())
        throw ActionAbortedError("Uninstall aborted by hook");
    on_uninstall_all_pre();

    int x(0), y(0);
    for (UninstallList::ConstIterator i(list.begin()), i_end(list.end()) ; i != i_end ; ++i)
        if (i->kind() != ulk_virtual)
            ++y;

    for (UninstallList::ConstIterator i(list.begin()), i_end(list.end()) ; i != i_end ; ++i)
    {
        if (i->kind() == ulk_virtual)
            continue;
        ++x;

        std::string cpvr(stringify(*i->package_id()));

        if (0 !=
            _imp->env->perform_hook(Hook("uninstall_pre")("TARGET", cpvr)
                     ("X_OF_Y", stringify(x) + " of " + stringify(y)),
                     make_null_shared_ptr()).max_exit_status())
            throw ActionAbortedError("Uninstall of '" + cpvr + "' aborted by hook");
        on_uninstall_pre(*i);

        try
        {
            OutputManagerFromEnvironment output_manager_holder(_imp->env, i->package_id(), oe_exclusive,
                    ClientOutputFeatures());
            UninstallAction uninstall_action(
                    make_named_values<UninstallActionOptions>(
                        n::config_protect() = "",
                        n::if_for_install_id() = make_null_shared_ptr(),
                        n::ignore_for_unmerge() = &ignore_nothing,
                        n::is_overwrite() = false,
                        n::make_output_manager() = std::ref(output_manager_holder)
                        ));
            i->package_id()->perform_action(uninstall_action);

            if (output_manager_holder.output_manager_if_constructed())
                output_manager_holder.output_manager_if_constructed()->succeeded();
        }
        catch (const ActionFailedError & e)
        {
            HookResult PALUDIS_ATTRIBUTE((unused)) dummy(_imp->env->perform_hook(Hook("uninstall_fail")("TARGET", cpvr)("MESSAGE", e.message()),
                        make_null_shared_ptr()));
            throw;
        }

        on_uninstall_post(*i);
        if (0 !=
            _imp->env->perform_hook(Hook("uninstall_post")("TARGET", cpvr)
                     ("X_OF_Y", stringify(x) + " of " + stringify(y)),
                     make_null_shared_ptr()).max_exit_status())
            throw ActionAbortedError("Uninstall of '" + cpvr + "' aborted by hook");
    }

    on_uninstall_all_post();
    if (0 !=
        _imp->env->perform_hook(Hook("uninstall_all_post")("TARGETS", join(_imp->raw_targets.begin(),
                         _imp->raw_targets.end(), " ")),
            make_null_shared_ptr()).max_exit_status())
        throw ActionAbortedError("Uninstall aborted by hook");
}

void
UninstallTask::set_with_unused_dependencies(const bool value)
{
    _imp->with_unused_dependencies = value;
}

void
UninstallTask::set_with_dependencies(const bool value)
{
    _imp->with_dependencies = value;
}

void
UninstallTask::set_all_versions(const bool value)
{
    _imp->all_versions = value;
}

void
UninstallTask::world_remove_set(const SetName & s)
{
    _imp->env->remove_from_world(s);
    on_update_world(s);
}

namespace
{
    struct WorldTargetFinder
    {
        Environment * const env;
        UninstallTask * const task;

        WorldTargetFinder(Environment * const e, UninstallTask * const t) :
            env(e),
            task(t)
        {
        }

        void visit(const SetSpecTree::NodeType<AllDepSpec>::Type & node)
        {
            std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
        }

        void visit(const SetSpecTree::NodeType<PackageDepSpec>::Type & node)
        {
            if (! (node.spec()->slot_requirement_ptr() || (node.spec()->version_requirements_ptr() && ! node.spec()->version_requirements_ptr()->empty())))
            {
                if (node.spec()->package_ptr())
                    env->remove_from_world(*node.spec()->package_ptr());

                task->on_update_world(*node.spec());
            }
        }

        void visit(const SetSpecTree::NodeType<NamedSetDepSpec>::Type &)
        {
        }
    };
}

void
UninstallTask::world_remove_packages(const std::shared_ptr<const SetSpecTree> & a)
{
    WorldTargetFinder w(_imp->env, this);
    a->top()->accept(w);
}

template class WrappedForwardIterator<AmbiguousUnmergeTargetError::ConstIteratorTag, const std::shared_ptr<const PackageID> >;