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

/*
 * Copyright (c) 2008 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/repositories/e/exndbam_repository.hh>
#include <paludis/repositories/e/exndbam_id.hh>
#include <paludis/repositories/e/ebuild.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/repositories/e/eapi_phase.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/log.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/cookie.hh>
#include <paludis/util/set.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/system.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/distribution.hh>
#include <paludis/environment.hh>
#include <paludis/ndbam.hh>
#include <paludis/ndbam_merger.hh>
#include <paludis/ndbam_unmerger.hh>
#include <paludis/metadata_key.hh>
#include <paludis/package_id.hh>
#include <paludis/action.hh>
#include <paludis/literal_metadata_key.hh>
#include <tr1/functional>
#include <fstream>

using namespace paludis;
using namespace paludis::erepository;

#include <paludis/repositories/e/exndbam_repository-sr.cc>

namespace
{
    bool supported_exndbam(const std::string & s)
    {
        return s == "exndbam-1";
    }
}

namespace paludis
{
    template <>
    struct Implementation<ExndbamRepository>
    {
        ExndbamRepositoryParams params;
        mutable NDBAM ndbam;

        std::tr1::shared_ptr<const MetadataValueKey<FSEntry> > location_key;
        std::tr1::shared_ptr<const MetadataValueKey<FSEntry> > root_key;
        std::tr1::shared_ptr<const MetadataValueKey<std::string> > format_key;
        std::tr1::shared_ptr<const MetadataValueKey<FSEntry> > builddir_key;

        Implementation(const ExndbamRepositoryParams & p) :
            params(p),
            ndbam(params.location, &supported_exndbam, "exndbam-1"),
            location_key(new LiteralMetadataValueKey<FSEntry> ("location", "location",
                        mkt_significant, params.location)),
            root_key(new LiteralMetadataValueKey<FSEntry> ("root", "root",
                        mkt_normal, params.root)),
            format_key(new LiteralMetadataValueKey<std::string> ("format", "format",
                        mkt_significant, "vdb")),
            builddir_key(new LiteralMetadataValueKey<FSEntry> ("builddir", "builddir",
                        mkt_normal, params.builddir))
        {
        }
    };
}

ExndbamRepository::ExndbamRepository(const RepositoryName & n, const ExndbamRepositoryParams & p) :
    EInstalledRepository(
            EInstalledRepositoryParams::create()
            .deprecated_world(p.deprecated_world)
            .environment(p.environment)
            .builddir(p.builddir)
            .root(p.root),
            n,
            make_named_values<RepositoryCapabilities>(
                value_for<n::destination_interface>(this),
                value_for<n::e_interface>(static_cast<RepositoryEInterface *>(0)),
                value_for<n::environment_variable_interface>(this),
                value_for<n::hook_interface>(this),
                value_for<n::make_virtuals_interface>(static_cast<RepositoryMakeVirtualsInterface *>(0)),
                value_for<n::manifest_interface>(static_cast<RepositoryManifestInterface *>(0)),
                value_for<n::mirrors_interface>(static_cast<RepositoryMirrorsInterface *>(0)),
                value_for<n::provides_interface>(static_cast<RepositoryProvidesInterface *>(0)),
                value_for<n::qa_interface>(static_cast<RepositoryQAInterface *>(0)),
                value_for<n::sets_interface>(this),
                value_for<n::syncable_interface>(static_cast<RepositorySyncableInterface *>(0)),
                value_for<n::use_interface>(this),
                value_for<n::virtuals_interface>(static_cast<RepositoryVirtualsInterface *>(0))
            )),
    PrivateImplementationPattern<ExndbamRepository>(new Implementation<ExndbamRepository>(p)),
    _imp(PrivateImplementationPattern<ExndbamRepository>::_imp)
{
    _add_metadata_keys();
}

ExndbamRepository::~ExndbamRepository()
{
}

void
ExndbamRepository::_add_metadata_keys() const
{
    clear_metadata_keys();
    add_metadata_key(_imp->location_key);
    add_metadata_key(_imp->root_key);
    add_metadata_key(_imp->format_key);
    add_metadata_key(_imp->builddir_key);
}

std::tr1::shared_ptr<Repository>
ExndbamRepository::repository_factory_create(
        Environment * const env,
        const std::tr1::function<std::string (const std::string &)> & f)
{
    Context context("When making Exndbam repository from repo_file '" + f("repo_file") + "':");

    std::string location(f("location"));
    if (location.empty())
        throw ExndbamRepositoryConfigurationError("Key 'location' not specified or empty");

    std::string root(f("root"));
    if (root.empty())
        root = "/";

    std::string builddir(f("builddir"));
    if (builddir.empty())
    {
        builddir = f("buildroot");
        if (builddir.empty())
            builddir = (*DistributionData::get_instance()->distribution_from_string(
                        env->distribution())).default_ebuild_builddir();
        else
            Log::get_instance()->message("e.exndbam.configuration.deprecated", ll_warning, lc_context)
                << "Key 'buildroot' is deprecated, use 'builddir' instead";
    }

    std::string name(f("name"));
    if (name.empty())
        name = "installed";

    std::string deprecated_world(f("world"));
    if (deprecated_world.empty())
        deprecated_world = "/DOESNOTEXIST";
    else
        Log::get_instance()->message("e.exndbam.configuration.deprecated", ll_warning, lc_context) << "Specifying world location " <<
            "in repository configuration files is deprecated. File '" << deprecated_world << "' will be "
            "read but not updated. If you have recently upgraded from <paludis-0.26.0_alpha13, consult "
            "the FAQ Upgrades section.";

    return std::tr1::shared_ptr<Repository>(new ExndbamRepository(
                RepositoryName(name),
                ExndbamRepositoryParams::create()
                .environment(env)
                .location(location)
                .root(root)
                .deprecated_world(deprecated_world)
                .builddir(builddir)));
}

RepositoryName
ExndbamRepository::repository_factory_name(
        const Environment * const,
        const std::tr1::function<std::string (const std::string &)> & f)
{
    std::string name(f("name"));
    if (name.empty())
        return RepositoryName("installed");
    else
        return RepositoryName(name);
}

std::tr1::shared_ptr<const RepositoryNameSet>
ExndbamRepository::repository_factory_dependencies(
        const Environment * const,
        const std::tr1::function<std::string (const std::string &)> &)
{
    return make_shared_ptr(new RepositoryNameSet);
}

void
ExndbamRepository::invalidate()
{
    _imp.reset(new Implementation<ExndbamRepository>(_imp->params));
    _add_metadata_keys();
}

void
ExndbamRepository::invalidate_masks()
{
}

std::tr1::shared_ptr<const PackageIDSequence>
ExndbamRepository::package_ids(const QualifiedPackageName & q) const
{
    std::tr1::shared_ptr<NDBAMEntrySequence> entries(_imp->ndbam.entries(q));
    std::tr1::shared_ptr<PackageIDSequence> result(new PackageIDSequence);

    for (IndirectIterator<NDBAMEntrySequence::ConstIterator> e(entries->begin()), e_end(entries->end()) ;
            e != e_end ; ++e)
    {
        Lock l(*(*e).mutex());
        if (! (*e).package_id())
            (*e).package_id().reset(new ExndbamID((*e).name(), (*e).version(), _imp->params.environment,
                        shared_from_this(), (*e).fs_location(), &_imp->ndbam));
        result->push_back((*e).package_id());
    }

    return result;
}

std::tr1::shared_ptr<const QualifiedPackageNameSet>
ExndbamRepository::package_names(const CategoryNamePart & c) const
{
    return _imp->ndbam.package_names(c);
}

std::tr1::shared_ptr<const CategoryNamePartSet>
ExndbamRepository::category_names() const
{
    return _imp->ndbam.category_names();
}

std::tr1::shared_ptr<const CategoryNamePartSet>
ExndbamRepository::category_names_containing_package(
        const PackageNamePart & p) const
{
    return _imp->ndbam.category_names_containing_package(p);
}

bool
ExndbamRepository::has_package_named(const QualifiedPackageName & q) const
{
    return _imp->ndbam.has_package_named(q);
}

bool
ExndbamRepository::has_category_named(const CategoryNamePart & c) const
{
    return _imp->ndbam.has_category_named(c);
}

ExndbamRepositoryConfigurationError::ExndbamRepositoryConfigurationError(
        const std::string & msg) throw () :
    ConfigurationError("Exndbam repository configuration error: " + msg)
{
}

const std::tr1::shared_ptr<const MetadataValueKey<std::string> >
ExndbamRepository::format_key() const
{
    return _imp->format_key;
}

const std::tr1::shared_ptr<const MetadataValueKey<FSEntry> >
ExndbamRepository::location_key() const
{
    return _imp->location_key;
}

const std::tr1::shared_ptr<const MetadataValueKey<FSEntry> >
ExndbamRepository::installed_root_key() const
{
    return _imp->root_key;
}

void
ExndbamRepository::need_keys_added() const
{
}

namespace
{
    std::pair<uid_t, gid_t>
    get_new_ids_or_minus_one(const Environment * const env, const FSEntry & f)
    {
        uid_t uid = (f.owner() == env->reduced_uid()) ? 0 : -1;
        gid_t gid = (f.group() == env->reduced_gid()) ? 0 : -1;

        return std::make_pair(uid, gid);
    }
}

void
ExndbamRepository::merge(const MergeParams & m)
{
    Context context("When merging '" + stringify(*m.package_id()) + "' at '" + stringify(m.image_dir())
            + "' to Exndbam repository '" + stringify(name()) + "':");

    if (! is_suitable_destination_for(*m.package_id()))
        throw InstallActionError("Not a suitable destination for '" + stringify(*m.package_id()) + "'");

    std::tr1::shared_ptr<const PackageID> if_overwritten_id, if_same_name_id;
    {
        std::tr1::shared_ptr<const PackageIDSequence> ids(package_ids(m.package_id()->name()));
        for (PackageIDSequence::ConstIterator v(ids->begin()), v_end(ids->end()) ;
                v != v_end ; ++v)
        {
            if_same_name_id = *v;
            if ((*v)->version() == m.package_id()->version() && (*v)->slot() == m.package_id()->slot())
            {
                if_overwritten_id = *v;
                break;
            }
        }
    }

    FSEntry uid_dir(_imp->params.location);
    if (if_same_name_id)
        uid_dir = if_same_name_id->fs_location_key()->value().dirname();
    else
    {
        std::string uid(stringify(m.package_id()->name().category) + "---" + stringify(m.package_id()->name().package));
        uid_dir /= "data";
        uid_dir.mkdir();
        uid_dir /= uid;
        uid_dir.mkdir();
    }

    FSEntry target_ver_dir(uid_dir);
    target_ver_dir /= (stringify(m.package_id()->version()) + ":" + stringify(m.package_id()->slot()) + ":" + cookie());

    if (target_ver_dir.exists())
        throw InstallActionError("Temporary merge directory '" + stringify(target_ver_dir) + "' already exists, probably "
                "due to a previous failed install. If it is safe to do so, please remove this directory and try again.");
    target_ver_dir.mkdir();

    WriteVDBEntryCommand write_vdb_entry_command(
            make_named_values<WriteVDBEntryParams>(
            value_for<n::environment>(_imp->params.environment),
            value_for<n::environment_file>(m.environment_file()),
            value_for<n::output_directory>(target_ver_dir),
            value_for<n::package_id>(std::tr1::static_pointer_cast<const ERepositoryID>(m.package_id()))
            ));

    write_vdb_entry_command();

    /* load CONFIG_PROTECT, CONFIG_PROTECT_MASK back */
    std::string config_protect, config_protect_mask;
    {
        std::ifstream c(stringify(target_ver_dir / "CONFIG_PROTECT").c_str());
        config_protect = std::string((std::istreambuf_iterator<char>(c)), std::istreambuf_iterator<char>());

        std::ifstream c_m(stringify(target_ver_dir / "CONFIG_PROTECT_MASK").c_str());
        config_protect_mask = std::string((std::istreambuf_iterator<char>(c_m)), std::istreambuf_iterator<char>());
    }

    NDBAMMerger merger(
            make_named_values<NDBAMMergerParams>(
            value_for<n::config_protect>(config_protect),
            value_for<n::config_protect_mask>(config_protect_mask),
            value_for<n::contents_file>(target_ver_dir / "contents"),
            value_for<n::environment>(_imp->params.environment),
            value_for<n::get_new_ids_or_minus_one>(std::tr1::bind(&get_new_ids_or_minus_one, _imp->params.environment, std::tr1::placeholders::_1)),
            value_for<n::image>(m.image_dir()),
            value_for<n::install_under>(FSEntry("/")),
            value_for<n::options>(m.options()),
            value_for<n::package_id>(m.package_id()),
            value_for<n::root>(installed_root_key()->value())
            ));

    if (! merger.check())
    {
        for (DirIterator d(target_ver_dir, DirIteratorOptions() + dio_include_dotfiles), d_end
                ; d != d_end ; ++d)
            FSEntry(*d).unlink();
        target_ver_dir.rmdir();
        throw InstallActionError("Not proceeding with install due to merge sanity check failing");
    }

    merger.merge();

    _imp->ndbam.index(m.package_id()->name(), uid_dir.basename());

    if (if_overwritten_id)
    {
        perform_uninstall(std::tr1::static_pointer_cast<const ERepositoryID>(if_overwritten_id), true);
    }
    if (std::tr1::static_pointer_cast<const ERepositoryID>(m.package_id())
            ->eapi()->supported()->ebuild_phases()->ebuild_new_upgrade_phase_order())
    {
        const std::tr1::shared_ptr<const PackageIDSequence> & replace_candidates(package_ids(m.package_id()->name()));
        for (PackageIDSequence::ConstIterator it(replace_candidates->begin()),
                 it_end(replace_candidates->end()); it_end != it; ++it)
        {
            std::tr1::shared_ptr<const ERepositoryID> candidate(std::tr1::static_pointer_cast<const ERepositoryID>(*it));
            if (candidate != if_overwritten_id && candidate->slot() == m.package_id()->slot())
                perform_uninstall(candidate, false);
        }
    }

    VDBPostMergeCommand post_merge_command(
            make_named_values<VDBPostMergeCommandParams>(
                value_for<n::root>(installed_root_key()->value())
            ));

    post_merge_command();
}

void
ExndbamRepository::perform_uninstall(const std::tr1::shared_ptr<const ERepositoryID> & id, bool replace) const
{
    Context context("When uninstalling '" + stringify(*id) + (replace ? "' for a reinstall:" : "':"));

    if (! _imp->params.root.is_directory())
        throw InstallActionError("Couldn't uninstall '" + stringify(*id) +
                "' because root ('" + stringify(_imp->params.root) + "') is not a directory");

    FSEntry ver_dir(id->fs_location_key()->value());
    std::tr1::shared_ptr<FSEntry> load_env(new FSEntry(ver_dir / "environment.bz2"));

    std::tr1::shared_ptr<FSEntrySequence> eclassdirs(new FSEntrySequence);
    eclassdirs->push_back(ver_dir);

    EAPIPhases phases(id->eapi()->supported()->ebuild_phases()->ebuild_uninstall());
    for (EAPIPhases::ConstIterator phase(phases.begin_phases()), phase_end(phases.end_phases()) ;
            phase != phase_end ; ++phase)
    {
        if (phase->option("unmerge"))
        {
            /* load CONFIG_PROTECT, CONFIG_PROTECT_MASK from vdb, supplement with env */
            std::string config_protect, config_protect_mask;
            {
                std::ifstream c(stringify(ver_dir / "CONFIG_PROTECT").c_str());
                config_protect = std::string((std::istreambuf_iterator<char>(c)), std::istreambuf_iterator<char>()) +
                    " " + getenv_with_default("CONFIG_PROTECT", "");

                std::ifstream c_m(stringify(ver_dir / "CONFIG_PROTECT_MASK").c_str());
                config_protect_mask = std::string((std::istreambuf_iterator<char>(c_m)), std::istreambuf_iterator<char>()) +
                    " " + getenv_with_default("CONFIG_PROTECT_MASK", "");
            }

            /* unmerge */
            NDBAMUnmerger unmerger(
                    make_named_values<NDBAMUnmergerOptions>(
                    value_for<n::config_protect>(config_protect),
                    value_for<n::config_protect_mask>(config_protect_mask),
                    value_for<n::contents_file>(ver_dir / "contents"),
                    value_for<n::environment>(_imp->params.environment),
                    value_for<n::ndbam>(&_imp->ndbam),
                    value_for<n::package_id>(id),
                    value_for<n::root>(installed_root_key()->value())
                    ));

            unmerger.unmerge();
        }
        else
        {
            EbuildCommandParams params(
                    make_named_values<EbuildCommandParams>(
                        value_for<n::builddir>(_imp->params.builddir),
                        value_for<n::commands>(join(phase->begin_commands(), phase->end_commands(), " ")),
                        value_for<n::distdir>(ver_dir),
                        value_for<n::ebuild_dir>(ver_dir),
                        value_for<n::ebuild_file>(ver_dir / (stringify(id->name().package) + "-" + stringify(id->version()) + ".ebuild")),
                        value_for<n::eclassdirs>(eclassdirs),
                        value_for<n::environment>(_imp->params.environment),
                        value_for<n::exlibsdirs>(make_shared_ptr(new FSEntrySequence)),
                        value_for<n::files_dir>(ver_dir),
                        value_for<n::package_id>(id),
                        value_for<n::portdir>(_imp->params.location),
                        value_for<n::sandbox>(phase->option("sandbox")),
                        value_for<n::userpriv>(phase->option("userpriv"))
                    ));

            EbuildUninstallCommandParams uninstall_params(
                    make_named_values<EbuildUninstallCommandParams>(
                        value_for<n::load_environment>(load_env.get()),
                        value_for<n::loadsaveenv_dir>(ver_dir),
                        value_for<n::root>(stringify(_imp->params.root)),
                        value_for<n::unmerge_only>(false)
                    ));

            EbuildUninstallCommand uninstall_cmd_pre(params, uninstall_params);
            uninstall_cmd_pre();
        }
    }

    for (DirIterator d(ver_dir, DirIteratorOptions() + dio_include_dotfiles), d_end ; d != d_end ; ++d)
        FSEntry(*d).unlink();
    ver_dir.rmdir();

    FSEntry pkg_dir(ver_dir.dirname());
    if (DirIterator() == DirIterator(pkg_dir, DirIteratorOptions() + dio_include_dotfiles + dio_inode_sort + dio_first_only))
    {
        pkg_dir.rmdir();

        _imp->ndbam.deindex(id->name());
    }
}

void
ExndbamRepository::regenerate_cache() const
{
}