aboutsummaryrefslogtreecommitdiff
path: root/src/uninstall.cc
blob: 47c40bfc82e77cb6aed370aa0a5c76be13486d55 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaran.mccreesh@blueyonder.co.uk>
 *
 * 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 "src/colour.hh"
#include "src/uninstall.hh"
#include <iostream>
#include <paludis/paludis.hh>

/** \file
 * Handle the --uninstall action for the main paludis program.
 */

namespace p = paludis;

using std::cerr;
using std::cout;
using std::endl;

int
do_uninstall()
{
    int return_code(0);

    p::Context context("When performing uninstall action from command line:");
    p::Environment * const env(p::DefaultEnvironment::get_instance());

    cout << colour(cl_heading, "These packages will be uninstalled:") << endl << endl;

    std::list<p::PackageDepAtom::Pointer> targets;

    CommandLine::ParametersIterator q(CommandLine::get_instance()->begin_parameters()),
        q_end(CommandLine::get_instance()->end_parameters());

    for ( ; q != q_end ; ++q)
    {
        /* we might have a dep atom, but we might just have a simple package name
         * without a category. either should work. */
        if (std::string::npos != q->find('/'))
            targets.push_back(p::PackageDepAtom::Pointer(new p::PackageDepAtom(*q)));
        else
            targets.push_back(p::PackageDepAtom::Pointer(new p::PackageDepAtom(
                            env->package_database()->fetch_unique_qualified_package_name(
                                p::PackageNamePart(*q)))));
    }

    bool ok(true);
    p::PackageDatabaseEntryCollection::Pointer unmerge(new p::PackageDatabaseEntryCollection);
    for (std::list<p::PackageDepAtom::Pointer>::iterator t(targets.begin()), t_end(targets.end()) ;
            t != t_end ; ++t)
    {
        p::PackageDatabaseEntryCollection::ConstPointer r(
                env->package_database()->query(*t, p::is_installed_only));
        if (r->empty())
        {
            cout << "* No match for " << colour(cl_red, **t) << endl;
            ok = false;
        }
        else if (r->size() > 1)
        {
            cout << "* Multiple matches for " << colour(cl_red, **t) << ":" << endl;
            for (p::PackageDatabaseEntryCollection::Iterator p(r->begin()),
                    p_end(r->end()) ; p != p_end ; ++p)
                cout << "  * " << *p << endl;
            ok = false;
        }
        else
        {
            cout << "* " << colour(cl_package_name, *r->begin()) << endl;
            unmerge->insert(*r->begin());
        }
    }

    int current_count = 0, max_count = std::distance(unmerge->begin(), unmerge->end());

    cout << endl << "Total: " << max_count <<
        (max_count == 1 ? " package" : " packages") << endl << endl;

    if (! ok)
        return 1 | return_code;

    if (CommandLine::get_instance()->a_pretend.specified())
        return return_code;

    if (! CommandLine::get_instance()->a_preserve_world.specified())
    {
        p::AllDepAtom::Pointer all(new p::AllDepAtom);
        for (std::list<p::PackageDepAtom::Pointer>::const_iterator t(targets.begin()),
                t_end(targets.end()) ; t != t_end ; ++t)
            all->add_child(*t);

        env->remove_appropriate_from_world(all);
    }

    p::InstallOptions opts(false, false);
    if (CommandLine::get_instance()->a_no_config_protection.specified())
        opts.set<p::io_noconfigprotect>(true);

    for (p::PackageDatabaseEntryCollection::Iterator pkg(unmerge->begin()), pkg_end(unmerge->end()) ;
            pkg != pkg_end ; ++pkg)
    {
        std::string cpv = p::stringify(pkg->get<p::pde_name>()) + "-" +
            p::stringify(pkg->get<p::pde_version>());

        cout << endl << colour(cl_heading,
                "Uninstalling " + cpv) << endl << endl;

        // TODO: some way to reset this properly would be nice.
        cerr << xterm_title("(" + p::stringify(++current_count) + " of " +
                p::stringify(max_count) + ") Uninstalling " + cpv);

        env->package_database()->fetch_repository(pkg->get<p::pde_repository>())->
            uninstall(pkg->get<p::pde_name>(), pkg->get<p::pde_version>(), opts);
    }

    return return_code;
}