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

/*
 * Copyright (c) 2010 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/merger.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/log.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/environment.hh>
#include <paludis/hook.hh>
#include <set>
#include <istream>
#include <ostream>

using namespace paludis;

MergerError::MergerError(const std::string & m) throw () :
    Exception(m)
{
}

namespace paludis
{
    template <>
    struct Imp<Merger>
    {
        MergerParams params;
        bool result;
        bool skip_dir;

        std::set<FSEntry> fixed_entries;

        Imp(const MergerParams & p) :
            params(p),
            result(true),
            skip_dir(false)
        {
        }
    };
}

#include <paludis/merger-se.cc>

Merger::Merger(const MergerParams & p) :
    Pimp<Merger>(p)
{
}

Merger::~Merger() = default;

Hook
Merger::extend_hook(const Hook & h)
{
    return h;
}

bool
Merger::check()
{
    Context context("When checking merge from '" + stringify(_imp->params.image()) + "' to '"
            + stringify(_imp->params.root()) + "':");

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_pre")
                         ("INSTALL_SOURCE", stringify(_imp->params.image()))
                         ("INSTALL_DESTINATION", stringify(_imp->params.root())))).max_exit_status())
        make_check_fail();

    do_dir_recursive(true, _imp->params.image(), _imp->params.root() / _imp->params.install_under());

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_post")
                         ("INSTALL_SOURCE", stringify(_imp->params.image()))
                         ("INSTALL_DESTINATION", stringify(_imp->params.root())))).max_exit_status())
        make_check_fail();

    return _imp->result;
}

void
Merger::merge()
{
    Context context("When performing merge from '" + stringify(_imp->params.image()) + "' to '"
            + stringify(_imp->params.root()) + "':");

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_install_pre")
                         ("INSTALL_SOURCE", stringify(_imp->params.image()))
                         ("INSTALL_DESTINATION", stringify(_imp->params.root())))).max_exit_status())
        Log::get_instance()->message("merger.pre_hooks.failure", ll_warning, lc_context) <<
            "Merge of '" << _imp->params.image() << "' to '" << _imp->params.root() << "' pre hooks returned non-zero";

    prepare_install_under();

    if (! _imp->params.no_chown())
        do_ownership_fixes_recursive(_imp->params.image());

    do_dir_recursive(false, _imp->params.image(), canonicalise_root_path(_imp->params.root() / _imp->params.install_under()));
    on_done_merge();

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_install_post")
                         ("INSTALL_SOURCE", stringify(_imp->params.image()))
                         ("INSTALL_DESTINATION", stringify(_imp->params.root())))).max_exit_status())
        Log::get_instance()->message("merger.post_hooks.failure", ll_warning, lc_context) <<
            "Merge of '" << _imp->params.image() << "' to '" << _imp->params.root() << "' post hooks returned non-zero";
}


void
Merger::make_check_fail()
{
    _imp->result = false;
}

void
Merger::do_dir_recursive(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    Context context("When " + stringify(is_check ? "checking" : "performing") + " merge from '" +
            stringify(src) + "' to '" + stringify(dst) + "':");

    if (! src.is_directory())
        throw MergerError("Source directory '" + stringify(src) + "' is not a directory");

    on_enter_dir(is_check, src);

    DirIterator d(src, { dio_include_dotfiles, dio_inode_sort }), d_end;

    if (is_check && d == d_end && dst != _imp->params.root().realpath())
    {
        if (_imp->params.options()[mo_allow_empty_dirs])
            Log::get_instance()->message("merger.empty_directory", ll_warning, lc_context) << "Installing empty directory '"
                << stringify(dst) << "'";
        else
            on_error(is_check, "Attempted to install empty directory '" + stringify(dst) + "'");
    }

    for ( ; d != d_end ; ++d)
    {
        EntryType m(entry_type(*d));
        switch (m)
        {
            case et_sym:
                on_sym(is_check, *d, dst);
                continue;

            case et_file:
                on_file(is_check, *d, dst);
                continue;

            case et_dir:
                on_dir(is_check, *d, dst);
                if (_imp->result)
                {
                    if (! _imp->skip_dir)
                        do_dir_recursive(is_check, *d,
                                is_check ? (dst / d->basename()) : canonicalise_root_path(dst / d->basename()));
                    else
                        _imp->skip_dir = false;
                }
                continue;

            case et_misc:
                on_misc(is_check, *d, dst);
                continue;

            case et_nothing:
            case last_et:
                ;
        }

        throw InternalError(PALUDIS_HERE, "Unexpected entry_type '" + stringify(m) + "'");
    }

    on_leave_dir(is_check, src);
}

void
Merger::on_enter_dir(bool, const FSEntry)
{
}

void
Merger::on_leave_dir(bool, const FSEntry)
{
}

EntryType
Merger::entry_type(const FSEntry & f)
{
    Context context("When checking type of '" + stringify(f) + "':");

    if (! f.exists())
        return et_nothing;

    if (f.is_symbolic_link())
        return et_sym;

    if (f.is_regular_file())
        return et_file;

    if (f.is_directory())
        return et_dir;

    return et_misc;
}

void
Merger::on_file(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    Context context("When handling file '" + stringify(src) + "' to '" + stringify(dst) + "':");

    if (is_check &&
        0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_file_pre")
                         ("INSTALL_SOURCE", stringify(src))
                         ("INSTALL_DESTINATION", stringify(dst / src.basename())))).max_exit_status())
        make_check_fail();

    if (! is_check)
    {
        HookResult hr(_imp->params.environment()->perform_hook(extend_hook(
                        Hook("merger_install_file_override")
                        ("INSTALL_SOURCE", stringify(src))
                        ("INSTALL_DESTINATION", stringify(dst / src.basename()))
                        .grab_output(Hook::AllowedOutputValues()("skip")))));

        if (hr.max_exit_status() != 0)
            Log::get_instance()->message("merger.file.skip_hooks.failure", ll_warning, lc_context) << "Merge of '"
                << stringify(src) << "' to '" << stringify(dst) << "' skip hooks returned non-zero";
        else if (hr.output() == "skip")
        {
            std::string tidy(stringify((dst / src.basename()).strip_leading(_imp->params.root().realpath())));
            display_override("--- [skp] " + tidy);
            return;
        }
    }

    on_file_main(is_check, src, dst);

    if (is_check &&
        0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_file_post")
                         ("INSTALL_SOURCE", stringify(src))
                         ("INSTALL_DESTINATION", stringify(dst / src.basename())))).max_exit_status())
        make_check_fail();
}

void
Merger::on_dir(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    Context context("When handling dir '" + stringify(src) + "' to '" + stringify(dst) + "':");

    if (is_check &&
        0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_dir_pre")
                         ("INSTALL_SOURCE", stringify(src))
                         ("INSTALL_DESTINATION", stringify(dst / src.basename())))).max_exit_status())
        make_check_fail();

    if (! is_check)
    {
        HookResult hr(_imp->params.environment()->perform_hook(extend_hook(
                        Hook("merger_install_dir_override")
                        ("INSTALL_SOURCE", stringify(src))
                        ("INSTALL_DESTINATION", stringify(dst / src.basename()))
                        .grab_output(Hook::AllowedOutputValues()("skip")))));

        if (hr.max_exit_status() != 0)
            Log::get_instance()->message("merger.dir.skip_hooks.failure", ll_warning, lc_context) << "Merge of '"
                << stringify(src) << "' to '" << stringify(dst) << "' skip hooks returned non-zero";
        else if (hr.output() == "skip")
        {
            std::string tidy(stringify((dst / src.basename()).strip_leading(_imp->params.root().realpath())));
            display_override("--- [skp] " + tidy);
            _imp->skip_dir = true;
            return;
        }
    }

    on_dir_main(is_check, src, dst);

    if (is_check &&
        0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_dir_post")
                         ("INSTALL_SOURCE", stringify(src))
                         ("INSTALL_DESTINATION", stringify(dst / src.basename())))).max_exit_status())
        make_check_fail();
}

void
Merger::on_sym(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    Context context("When handling sym '" + stringify(src) + "' to '" + stringify(dst) + "':");

    if (is_check &&
        0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_sym_pre")
                         ("INSTALL_SOURCE", stringify(src))
                         ("INSTALL_DESTINATION", stringify(dst / src.basename())))).max_exit_status())
        make_check_fail();

    if (! is_check)
    {
        HookResult hr(_imp->params.environment()->perform_hook(extend_hook(
                        Hook("merger_install_sym_override")
                        ("INSTALL_SOURCE", stringify(src))
                        ("INSTALL_DESTINATION", stringify(dst / src.basename()))
                        .grab_output(Hook::AllowedOutputValues()("skip")))));

        if (hr.max_exit_status() != 0)
            Log::get_instance()->message("merger.sym.skip_hooks.failure", ll_warning, lc_context) << "Merge of '"
                << stringify(src) << "' to '" << stringify(dst) << "' skip hooks returned non-zero";
        else if (hr.output() == "skip")
        {
            std::string tidy(stringify((dst / src.basename()).strip_leading(_imp->params.root().realpath())));
            display_override("--- [skp] " + tidy);
            return;
        }
    }
    else
    {
        if (symlink_needs_rewriting(src) && ! _imp->params.options()[mo_rewrite_symlinks])
            on_error(is_check, "Symlink to image detected at: " + stringify(src) + " (" + src.readlink() + ")");
    }

    on_sym_main(is_check, src, dst);

    if (is_check &&
        0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_check_sym_post")
                         ("INSTALL_SOURCE", stringify(src))
                         ("INSTALL_DESTINATION", stringify(dst / src.basename())))).max_exit_status())
        make_check_fail();
}

void
Merger::on_misc(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    Context context("When handling misc '" + stringify(src) + "' to '" + stringify(dst) + "':");

    on_error(is_check, "Cannot write '" + stringify(src) + "' to '" + stringify(dst) +
            "' because it is not a recognised file type");
}

bool
Merger::symlink_needs_rewriting(const FSEntry & sym)
{
    std::string target(sym.readlink());
    std::string real_image(stringify(_imp->params.image().realpath()));

    return (0 == target.compare(0, real_image.length(), real_image));
}

void
Merger::set_skipped_dir(const bool value)
{
    _imp->skip_dir = value;
}

void
Merger::do_ownership_fixes_recursive(const FSEntry & dir)
{
    for (DirIterator d(dir, { dio_include_dotfiles, dio_inode_sort }), d_end ; d != d_end ; ++d)
    {
        std::pair<uid_t, gid_t> new_ids(_imp->params.get_new_ids_or_minus_one()(*d));
        if (uid_t(-1) != new_ids.first || gid_t(-1) != new_ids.second)
        {
            FSEntry f(*d);
            f.lchown(new_ids.first, new_ids.second);

            if (et_sym != entry_type(*d))
            {
                mode_t mode(f.permissions());

                if (et_dir == entry_type(*d))
                {
                    if (uid_t(-1) != new_ids.first)
                        mode &= ~S_ISUID;
                    if (gid_t(-1) != new_ids.second)
                        mode &= ~S_ISGID;
                }

                f.chmod(mode); /* set*id */
            }

            _imp->fixed_entries.insert(f);
        }

        EntryType m(entry_type(*d));
        switch (m)
        {
            case et_sym:
            case et_file:
                continue;

            case et_dir:
                do_ownership_fixes_recursive(*d);
                continue;

            case et_misc:
                throw MergerError("Unexpected 'et_misc' entry found at: " + stringify(*d));
                continue;

            case et_nothing:
            case last_et:
                ;
        }

        throw InternalError(PALUDIS_HERE, "Unexpected entry_type '" + stringify(m) + "'");
    }
}

bool
Merger::fixed_ownership_for(const FSEntry & f)
{
    return _imp->fixed_entries.end() != _imp->fixed_entries.find(f);
}

void
Merger::on_done_merge()
{
}