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

/*
 * Copyright (c) 2005, 2006 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 <paludis/dep_atom.hh>
#include <paludis/match_package.hh>
#include <paludis/package_database.hh>
#include <paludis/util/iterator.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/collection_concrete.hh>
#include <paludis/util/compare.hh>

#include <list>
#include <map>
#include <set>

/** \file
 * Implementation of PackageDatabase.
 */

using namespace paludis;

#include "package_database_entry-sr.cc"

std::ostream &
paludis::operator<< (std::ostream & s, const PackageDatabaseEntry & v)
{
    s << v.name << "-" << v.version << "::" << v.repository;
    return s;
}

PackageDatabaseError::PackageDatabaseError(const std::string & our_message) throw () :
    Exception(our_message)
{
}

PackageDatabaseLookupError::PackageDatabaseLookupError(const std::string & our_message) throw () :
    PackageDatabaseError(our_message)
{
}

DuplicateRepositoryError::DuplicateRepositoryError(const std::string & name) throw () :
    PackageDatabaseError("A repository named '" + name + "' already exists")
{
}

NoSuchPackageError::NoSuchPackageError(const std::string & our_name) throw () :
    PackageDatabaseLookupError("Could not find '" + our_name + "'"),
    _name(our_name)
{
}

NoSuchRepositoryError::NoSuchRepositoryError(const std::string & name) throw () :
    PackageDatabaseLookupError("Could not find repository '" + name + "'")
{
}

/**
 * Name data for an AmbiguousPackageNameError.
 *
 * \ingroup grpexceptions
 */
struct AmbiguousPackageNameError::NameData
{
    /// Our query name
    std::string name;

    /// Our match names
    std::list<std::string> names;
};

template <typename I_>
AmbiguousPackageNameError::AmbiguousPackageNameError(const std::string & our_name,
        I_ begin, const I_ end) throw () :
    PackageDatabaseLookupError("Ambiguous package name '" + our_name + "' (candidates are " +
            join(begin, end, ", ") + ")"),
    _name_data(new NameData)
{
    _name_data->name = our_name;
    std::transform(begin, end, std::back_inserter(_name_data->names),
            &stringify<typename std::iterator_traits<I_>::value_type>);
}

AmbiguousPackageNameError::AmbiguousPackageNameError(const AmbiguousPackageNameError & other) :
    PackageDatabaseLookupError(other),
    _name_data(new NameData)
{
    _name_data->name = other._name_data->name;
    _name_data->names = other._name_data->names;
}

AmbiguousPackageNameError::~AmbiguousPackageNameError() throw ()
{
    delete _name_data;
}

AmbiguousPackageNameError::OptionsIterator
AmbiguousPackageNameError::begin_options() const
{
    return OptionsIterator(_name_data->names.begin());
}

AmbiguousPackageNameError::OptionsIterator
AmbiguousPackageNameError::end_options() const
{
    return OptionsIterator(_name_data->names.end());
}

const std::string &
AmbiguousPackageNameError::name() const
{
    return _name_data->name;
}

namespace paludis
{
    /**
     * Implementation data for a PackageDatabase.
     *
     * \ingroup grppackagedatabase
     */
    template<>
    struct Implementation<PackageDatabase> :
        InternalCounted<Implementation<PackageDatabase> >
    {
        /**
         * Our Repository instances.
         */
        std::list<Repository::ConstPointer> repositories;

        /// Our environment.
        const Environment * environment;
    };
}

PackageDatabase::PackageDatabase(const Environment * const e) :
    PrivateImplementationPattern<PackageDatabase>(new Implementation<PackageDatabase>)
{
    _imp->environment = e;
}

PackageDatabase::~PackageDatabase()
{
}

void
PackageDatabase::add_repository(const Repository::ConstPointer r)
{
    Context c("When adding a repository named '" + stringify(r->name()) + "':");

    IndirectIterator<std::list<Repository::ConstPointer>::const_iterator, const Repository>
        r_c(_imp->repositories.begin()),
        r_end(_imp->repositories.end());
    for ( ; r_c != r_end ; ++r_c)
        if (r_c->name() == r->name())
            throw DuplicateRepositoryError(stringify(r->name()));

    _imp->repositories.push_back(r);
}

QualifiedPackageName
PackageDatabase::fetch_unique_qualified_package_name(
        const PackageNamePart & p) const
{
    Context context("When disambiguating package name '" + stringify(p) + "':");

    QualifiedPackageNameCollection::Pointer result(new QualifiedPackageNameCollection::Concrete);

    IndirectIterator<std::list<Repository::ConstPointer>::const_iterator, const Repository>
        r(_imp->repositories.begin()),
        r_end(_imp->repositories.end());
    for ( ; r != r_end ; ++r)
    {
        Context local_context("When looking in repository '" + stringify(r->name()) + "':");

        CategoryNamePartCollection::ConstPointer cats(r->category_names());
        CategoryNamePartCollection::Iterator c(cats->begin()), c_end(cats->end());
        for ( ; c != c_end ; ++c)
        {
            Context very_local_context("When looking in category '" + stringify(*c) + "':");
            if (r->has_package_named(*c + p))
                    result->insert(*c + p);
        }
    }

    if (result->empty())
        throw NoSuchPackageError(stringify(p));
    if (result->size() > 1)
        throw AmbiguousPackageNameError(stringify(p), result->begin(), result->end());

    return *(result->begin());
}

PackageDatabaseEntryCollection::Pointer
PackageDatabase::_do_query(const PackageDepAtom & a, const InstallState installed_state) const
{
    PackageDatabaseEntryCollection::Pointer result(new PackageDatabaseEntryCollection::Concrete);

    IndirectIterator<std::list<Repository::ConstPointer>::const_iterator, const Repository>
        r(_imp->repositories.begin()),
        r_end(_imp->repositories.end());
    for ( ; r != r_end ; ++r)
    {
        if ((installed_state == is_installed_only) && ! r->installed_interface)
            continue;

        if ((installed_state == is_uninstalled_only) && r->installed_interface)
            continue;

        if (! r->has_category_named(a.package().category))
            continue;

        if (! r->has_package_named(a.package()))
            continue;

        VersionSpecCollection::ConstPointer versions(r->version_specs(a.package()));
        VersionSpecCollection::Iterator v(versions->begin()), v_end(versions->end());
        for ( ; v != v_end ; ++v)
        {
            PackageDatabaseEntry e(a.package(), *v, r->name());
            if (! match_package(_imp->environment, a, e))
                continue;

            result->insert(e);
        }
    }

    return result;
}

PackageDatabaseEntryCollection::Pointer
PackageDatabase::query(const PackageDepAtom & a, const InstallState s) const
{
    return _do_query(a, s);
}

Repository::ConstPointer
PackageDatabase::fetch_repository(const RepositoryName & n) const
{
    std::list<Repository::ConstPointer>::const_iterator
        r(_imp->repositories.begin()),
        r_end(_imp->repositories.end());
    for ( ; r != r_end ; ++r)
        if ((*r)->name() == n)
            return *r;

    throw NoSuchRepositoryError(stringify(n));
}

const RepositoryName &
PackageDatabase::better_repository(const RepositoryName & r1,
        const RepositoryName & r2) const
{
    IndirectIterator<std::list<Repository::ConstPointer>::const_iterator, const Repository>
        r(_imp->repositories.begin()),
        r_end(_imp->repositories.end());
    for ( ; r != r_end ; ++r)
    {
        if (r->name() == r1)
            return r2;
        else if (r->name() == r2)
            return r1;
    }

    throw InternalError(PALUDIS_HERE, "better_repository called on non-owned repositories");
}

RepositoryName
PackageDatabase::favourite_repository() const
{
    for (RepositoryIterator r(_imp->repositories.begin()), r_end(_imp->repositories.end()) ;
            r != r_end ; ++r)
        if ((*r)->can_be_favourite_repository())
            return (*r)->name();

    return RepositoryName("unnamed");
}

PackageDatabaseEntryCollection::Pointer
PackageDatabase::query(PackageDepAtom::ConstPointer a, const InstallState s) const
{
    return _do_query(*a.raw_pointer(), s);
}

PackageDatabase::RepositoryIterator
PackageDatabase::begin_repositories() const
{
    return RepositoryIterator(_imp->repositories.begin());
}

PackageDatabase::RepositoryIterator
PackageDatabase::end_repositories() const
{
    return RepositoryIterator(_imp->repositories.end());
}

std::ostream &
paludis::operator<< (std::ostream & o, const InstallState & s)
{
    do
    {
        switch (s)
        {
            case is_installed_only:
                o << "installed_only";
                continue;

            case is_uninstalled_only:
                o << "uninstalled_only";
                continue;

            case is_either:
                o << "either";
                continue;

            case last_install_state:
                ;
        }

        throw InternalError(PALUDIS_HERE, "Bad InstallState");
    } while (false);

    return o;
}