aboutsummaryrefslogtreecommitdiff
path: root/src/clients/reconcilio/fix_linkage.cc
blob: 09780a339a15009d9b776e316b8b60049ec9f7f1 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 David Leverton
 *
 * 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 "command_line.hh"
#include "fix_linkage.hh"
#include "install.hh"

#include <src/clients/reconcilio/broken_linkage_finder/broken_linkage_finder.hh>

#include <paludis/util/fs_entry.hh>
#include <paludis/util/join.hh>
#include <paludis/util/log.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/stringify.hh>

#include <paludis/dep_spec.hh>
#include <paludis/name.hh>
#include <paludis/package_id.hh>
#include <paludis/version_requirements.hh>

#include <src/output/colour.hh>

#include <libwrapiter/libwrapiter_forward_iterator-decl.hh>

#include <iostream>

using namespace paludis;

int
do_fix_linkage(const tr1::shared_ptr<Environment> & env)
{
    Context ctx("When performing the Fix Linkage action:");

    bool verbose(CommandLine::get_instance()->a_verbose.specified());
    std::string library(CommandLine::get_instance()->a_library.argument());

    if (library.empty())
        std::cout << "Searching for broken packages... " << std::flush;
    else
        std::cout << "Searching for packages that depend on " << library << "... " << std::flush;
    BrokenLinkageFinder finder(env.get(), library);
    std::cout << std::endl;

    if (finder.begin_broken_packages() == finder.end_broken_packages())
    {
        if (library.empty())
            std::cout << "No broken packages found" << std::endl;
        else
            std::cout << "No packages that depend on " << library << " found" << std::endl;
    }
    else
    {
        if (library.empty())
            std::cout << std::endl << colour(cl_heading, "Broken packages:") << std::endl;
        else
            std::cout << std::endl << colour(cl_heading, "Packages that depend on " +  library + ":") << std::endl;
        if (! verbose)
            std::cout << std::endl;
    }

    tr1::shared_ptr<Sequence<std::string> > targets(new Sequence<std::string>);
    for (BrokenLinkageFinder::BrokenPackageConstIterator pkg_it(finder.begin_broken_packages()),
             pkg_it_end(finder.end_broken_packages()); pkg_it_end != pkg_it; ++pkg_it)
    {
        if (verbose)
            std::cout << std::endl;

        std::string pkgname(stringify((*pkg_it)->name()));
        std::string fullname((*pkg_it)->canonical_form(idcf_full));
        std::string::size_type pos(fullname.find(pkgname));
        if (std::string::npos != pos)
            fullname.replace(pos, pkgname.length(), colour(cl_package_name, pkgname));
        std::cout << "* " << fullname << std::endl;

        if (verbose)
            for (BrokenLinkageFinder::BrokenFileConstIterator file_it(finder.begin_broken_files(*pkg_it)),
                     file_it_end(finder.end_broken_files(*pkg_it)); file_it_end != file_it; ++file_it)
            {
                std::cout << "    " << *file_it;
                if (library.empty())
                    std::cout << " (requires "
                              << join(finder.begin_missing_requirements(*pkg_it, *file_it),
                                      finder.end_missing_requirements(*pkg_it, *file_it),
                                      " ") << ")";
                std::cout << std::endl;
            }

        targets->push_back(
            stringify(
                PackageDepSpec(
                    make_shared_ptr(new QualifiedPackageName((*pkg_it)->name())),
                    tr1::shared_ptr<CategoryNamePart>(),
                    tr1::shared_ptr<PackageNamePart>(),
                    CommandLine::get_instance()->a_exact.specified()
                        ? make_equal_to_version_requirements((*pkg_it)->version())
                        : tr1::shared_ptr<VersionRequirements>(),
                    vr_and,
                    make_shared_ptr(new SlotName((*pkg_it)->slot())))));
    }

    tr1::shared_ptr<const PackageID> orphans;
    if (verbose && finder.begin_broken_files(orphans) != finder.end_broken_files(orphans))
    {
        if (library.empty())
            std::cout << std::endl << "The following broken files are not owned by any installed package:" << std::endl;
        else
            std::cout << std::endl << "The following files that depend on " << library << " are not owned by any installed package:" << std::endl;

        for (BrokenLinkageFinder::BrokenFileConstIterator file_it(finder.begin_broken_files(orphans)),
                 file_it_end(finder.end_broken_files(orphans)); file_it_end != file_it; ++file_it)
        {
            std::cout << "    " << *file_it;
            if (library.empty())
                std::cout << " (requires "
                          << join(finder.begin_missing_requirements(orphans, *file_it),
                                  finder.end_missing_requirements(orphans, *file_it),
                                  " ") << ")";
            std::cout << std::endl;
        }
    }

    if (! targets->empty())
        return do_install(env, targets);
    return 0;
}