aboutsummaryrefslogtreecommitdiff
path: root/paludis/filter.cc
blob: e165692316a7b98ddec65b318de22e07184cc931 (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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2009, 2010, 2011 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/filter.hh>
#include <paludis/filter_handler.hh>
#include <paludis/name.hh>
#include <paludis/action.hh>
#include <paludis/environment.hh>
#include <paludis/package_id.hh>
#include <paludis/package_database.hh>
#include <paludis/metadata_key.hh>
#include <paludis/match_package.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/set.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/action_names.hh>

using namespace paludis;

namespace paludis
{
    template <>
    struct Imp<Filter>
    {
        std::shared_ptr<const FilterHandler> handler;

        Imp(const std::shared_ptr<const FilterHandler> & h) :
            handler(h)
        {
        }
    };
}

Filter::Filter(const std::shared_ptr<const FilterHandler> & h) :
    _imp(h)
{
}

Filter::Filter(const Filter & other) :
    _imp(other._imp->handler)
{
}

Filter &
Filter::operator= (const Filter & other)
{
    if (this != &other)
        _imp->handler = other._imp->handler;
    return *this;
}

Filter::~Filter()
{
}

std::shared_ptr<const RepositoryNameSet>
Filter::repositories(
        const Environment * const env,
        const std::shared_ptr<const RepositoryNameSet> & r) const
{
    return _imp->handler->repositories(env, r);
}

std::shared_ptr<const CategoryNamePartSet>
Filter::categories(
        const Environment * const env,
        const std::shared_ptr<const RepositoryNameSet> & r,
        const std::shared_ptr<const CategoryNamePartSet> & c) const
{
    return _imp->handler->categories(env, r, c);
}

std::shared_ptr<const QualifiedPackageNameSet>
Filter::packages(
        const Environment * const env,
        const std::shared_ptr<const RepositoryNameSet> & r,
        const std::shared_ptr<const QualifiedPackageNameSet> & c) const
{
    return _imp->handler->packages(env, r, c);
}

std::shared_ptr<const PackageIDSet>
Filter::ids(
        const Environment * const env,
        const std::shared_ptr<const PackageIDSet> & i) const
{
    return _imp->handler->ids(env, i);
}

const RepositoryContentMayExcludes
Filter::may_excludes() const
{
    return _imp->handler->may_excludes();
}

std::string
Filter::as_string() const
{
    return _imp->handler->as_string();
}

namespace
{
    struct AllFilterHandler :
        AllFilterHandlerBase
    {
        virtual std::string as_string() const
        {
            return "all matches";
        }
    };

    template <typename A_>
    struct SupportsActionFilterHandler :
        AllFilterHandlerBase
    {
        virtual std::shared_ptr<const RepositoryNameSet> repositories(
                const Environment * const env,
                const std::shared_ptr<const RepositoryNameSet> & repos) const
        {
            std::shared_ptr<RepositoryNameSet> result(std::make_shared<RepositoryNameSet>());

            for (RepositoryNameSet::ConstIterator r(repos->begin()), r_end(repos->end()) ;
                    r != r_end ; ++r)
            {
                if (env->package_database()->fetch_repository(*r)->some_ids_might_support_action(SupportsActionTest<A_>()))
                    result->insert(*r);
            }

            return result;
        }

        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const,
                const std::shared_ptr<const PackageIDSet> & id) const
        {
            std::shared_ptr<PackageIDSet> result(std::make_shared<PackageIDSet>());

            for (PackageIDSet::ConstIterator i(id->begin()), i_end(id->end()) ;
                    i != i_end ; ++i)
            {
                if ((*i)->supports_action(SupportsActionTest<A_>()))
                    result->insert(*i);
            }

            return result;
        }

        virtual std::string as_string() const
        {
            return "supports action " + stringify(ActionNames<A_>::value);
        }
    };

    struct NotMaskedFilterHandler :
        AllFilterHandlerBase
    {
        virtual const RepositoryContentMayExcludes may_excludes() const
        {
            return { rcme_masked };
        }

        virtual std::shared_ptr<const RepositoryNameSet> repositories(
                const Environment * const env,
                const std::shared_ptr<const RepositoryNameSet> & repos) const
        {
            std::shared_ptr<RepositoryNameSet> result(std::make_shared<RepositoryNameSet>());

            for (RepositoryNameSet::ConstIterator r(repos->begin()), r_end(repos->end()) ;
                    r != r_end ; ++r)
            {
                if (env->package_database()->fetch_repository(*r)->some_ids_might_not_be_masked())
                    result->insert(*r);
            }

            return result;
        }

        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const,
                const std::shared_ptr<const PackageIDSet> & id) const
        {
            std::shared_ptr<PackageIDSet> result(std::make_shared<PackageIDSet>());

            for (PackageIDSet::ConstIterator i(id->begin()), i_end(id->end()) ;
                    i != i_end ; ++i)
                if (! (*i)->masked())
                    result->insert(*i);

            return result;
        }

        virtual std::string as_string() const
        {
            return "not masked";
        }
    };

    struct InstalledAtFilterHandler :
        AllFilterHandlerBase
    {
        const FSPath root;
        const bool equal;

        InstalledAtFilterHandler(const FSPath & r, const bool e) :
            root(r),
            equal(e)
        {
        }

        virtual std::shared_ptr<const RepositoryNameSet> repositories(
                const Environment * const env,
                const std::shared_ptr<const RepositoryNameSet> & repos) const
        {
            std::shared_ptr<RepositoryNameSet> result(std::make_shared<RepositoryNameSet>());

            for (RepositoryNameSet::ConstIterator r(repos->begin()), r_end(repos->end()) ;
                    r != r_end ; ++r)
            {
                const std::shared_ptr<const Repository> repo(env->package_database()->fetch_repository(*r));
                if (repo->installed_root_key() && (equal == (root == repo->installed_root_key()->value())))
                    result->insert(*r);
            }

            return result;
        }

        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const,
                const std::shared_ptr<const PackageIDSet> & id) const
        {
            std::shared_ptr<PackageIDSet> result(std::make_shared<PackageIDSet>());

            for (PackageIDSet::ConstIterator i(id->begin()), i_end(id->end()) ;
                    i != i_end ; ++i)
                result->insert(*i);

            return result;
        }

        virtual std::string as_string() const
        {
            return "installed " + std::string(equal ? "" : "not ") + "at root " + stringify(root);
        }
    };

    struct AndFilterHandler :
        FilterHandler
    {
        const Filter f1;
        const Filter f2;

        AndFilterHandler(const Filter & a1, const Filter & a2) :
            f1(a1),
            f2(a2)
        {
        }

        virtual const RepositoryContentMayExcludes may_excludes() const
        {
            /* we can exclude anything either filter would reject */
            return f1.may_excludes() | f2.may_excludes();
        }

        virtual std::shared_ptr<const RepositoryNameSet> repositories(
                const Environment * const env,
                const std::shared_ptr<const RepositoryNameSet> & s) const
        {
            return f2.repositories(env, f1.repositories(env, s));
        }

        virtual std::shared_ptr<const CategoryNamePartSet> categories(
                const Environment * const env,
                const std::shared_ptr<const RepositoryNameSet> & r,
                const std::shared_ptr<const CategoryNamePartSet> & c) const
        {
            return f2.categories(env, r, f1.categories(env, r, c));
        }

        virtual std::shared_ptr<const QualifiedPackageNameSet> packages(
                const Environment * const env,
                const std::shared_ptr<const RepositoryNameSet> & r,
                const std::shared_ptr<const QualifiedPackageNameSet> & q) const
        {
            return f2.packages(env, r, f1.packages(env, r, q));
        }

        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const env,
                const std::shared_ptr<const PackageIDSet> & s) const
        {
            return f2.ids(env, f1.ids(env, s));
        }

        virtual std::string as_string() const
        {
            return stringify(f1) + " filtered through " + stringify(f2);
        }
    };

    struct SameSlotHandler :
        AllFilterHandlerBase
    {
        const std::shared_ptr<const PackageID> as_id;

        SameSlotHandler(const std::shared_ptr<const PackageID> & i) :
            as_id(i)
        {
        }

        virtual const RepositoryContentMayExcludes may_excludes() const
        {
            return { };
        }

        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const,
                const std::shared_ptr<const PackageIDSet> & id) const
        {
            std::shared_ptr<PackageIDSet> result(std::make_shared<PackageIDSet>());

            for (PackageIDSet::ConstIterator i(id->begin()), i_end(id->end()) ;
                    i != i_end ; ++i)
                if (as_id->slot_key())
                {
                    if ((*i)->slot_key() && (*i)->slot_key()->value() == as_id->slot_key()->value())
                        result->insert(*i);
                }
                else
                {
                    if (! (*i)->slot_key())
                        result->insert(*i);
                }

            return result;
        }

        virtual std::string as_string() const
        {
            return "same slot as " + stringify(*as_id);
        }
    };

    struct SlotHandler :
        AllFilterHandlerBase
    {
        const SlotName slot;

        SlotHandler(const SlotName & s) :
            slot(s)
        {
        }

        virtual const RepositoryContentMayExcludes may_excludes() const
        {
            return { };
        }

        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const,
                const std::shared_ptr<const PackageIDSet> & id) const
        {
            std::shared_ptr<PackageIDSet> result(std::make_shared<PackageIDSet>());

            for (PackageIDSet::ConstIterator i(id->begin()), i_end(id->end()) ;
                    i != i_end ; ++i)
                if ((*i)->slot_key() && (*i)->slot_key()->value() == slot)
                    result->insert(*i);

            return result;
        }

        virtual std::string as_string() const
        {
            return "slot is " + stringify(slot);
        }
    };

    struct NoSlotHandler :
        AllFilterHandlerBase
    {
        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const,
                const std::shared_ptr<const PackageIDSet> & id) const
        {
            std::shared_ptr<PackageIDSet> result(std::make_shared<PackageIDSet>());

            for (PackageIDSet::ConstIterator i(id->begin()), i_end(id->end()) ;
                    i != i_end ; ++i)
                if (! (*i)->slot_key())
                    result->insert(*i);

            return result;
        }

        virtual const RepositoryContentMayExcludes may_excludes() const
        {
            return { };
        }

        virtual std::string as_string() const
        {
            return "has no slot";
        }
    };

    struct MatchesHandler :
        AllFilterHandlerBase
    {
        const PackageDepSpec spec;
        const std::shared_ptr<const PackageID> from_id;
        const MatchPackageOptions options;

        MatchesHandler(const PackageDepSpec & s, const std::shared_ptr<const PackageID> & f, const MatchPackageOptions & o) :
            spec(s),
            from_id(f),
            options(o)
        {
        }

        virtual const RepositoryContentMayExcludes may_excludes() const
        {
            return { };
        }

        virtual std::shared_ptr<const PackageIDSet> ids(
                const Environment * const env,
                const std::shared_ptr<const PackageIDSet> & id) const
        {
            std::shared_ptr<PackageIDSet> result(std::make_shared<PackageIDSet>());

            for (PackageIDSet::ConstIterator i(id->begin()), i_end(id->end()) ;
                    i != i_end ; ++i)
            {
                if (match_package(*env, spec, *i, from_id, options))
                    result->insert(*i);
            }

            return result;
        }

        virtual std::string as_string() const
        {
            std::string suffix;
            if (options[mpo_ignore_additional_requirements])
                suffix = " (ignoring additional requirements)";
            return "packages matching " + stringify(spec) + suffix;
        }
    };
}

filter::All::All() :
    Filter(std::make_shared<AllFilterHandler>())
{
}

template <typename A_>
filter::SupportsAction<A_>::SupportsAction() :
    Filter(std::make_shared<SupportsActionFilterHandler<A_>>())
{
}

filter::NotMasked::NotMasked() :
    Filter(std::make_shared<NotMaskedFilterHandler>())
{
}

filter::InstalledAtRoot::InstalledAtRoot(const FSPath & r) :
    Filter(std::make_shared<InstalledAtFilterHandler>(r, true))
{
}

filter::InstalledNotAtRoot::InstalledNotAtRoot(const FSPath & r) :
    Filter(std::make_shared<InstalledAtFilterHandler>(r, false))
{
}

filter::InstalledAtSlash::InstalledAtSlash() :
    Filter(std::make_shared<InstalledAtFilterHandler>(FSPath("/"), true))
{
}

filter::InstalledAtNotSlash::InstalledAtNotSlash() :
    Filter(std::make_shared<InstalledAtFilterHandler>(FSPath("/"), false))
{
}

filter::And::And(const Filter & f1, const Filter & f2) :
    Filter(std::make_shared<AndFilterHandler>(f1, f2))
{
}

filter::SameSlot::SameSlot(const std::shared_ptr<const PackageID> & i) :
    Filter(std::make_shared<SameSlotHandler>(i))
{
}

filter::Slot::Slot(const SlotName & s) :
    Filter(std::make_shared<SlotHandler>(s))
{
}

filter::NoSlot::NoSlot() :
    Filter(std::make_shared<NoSlotHandler>())
{
}

filter::Matches::Matches(const PackageDepSpec & spec, const std::shared_ptr<const PackageID> & f, const MatchPackageOptions & o) :
    Filter(std::make_shared<MatchesHandler>(spec, f, o))
{
}

std::ostream &
paludis::operator<< (std::ostream & s, const Filter & f)
{
    s << f.as_string();
    return s;
}

template class Pimp<Filter>;
template class filter::SupportsAction<InstallAction>;
template class filter::SupportsAction<UninstallAction>;
template class filter::SupportsAction<PretendAction>;
template class filter::SupportsAction<ConfigAction>;
template class filter::SupportsAction<FetchAction>;
template class filter::SupportsAction<InfoAction>;
template class filter::SupportsAction<PretendFetchAction>;