aboutsummaryrefslogtreecommitdiff
path: root/paludis/fs_merger.cc
blob: 659948ac2f1c28d95be8e674b827c715ce76e725 (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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 2008, 2009, 2010 Ciaran McCreesh
 * Copyright (c) 2008 Fernando J. Pereda
 *
 * 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/fs_merger.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/fd_holder.hh>
#include <paludis/util/log.hh>
#include <paludis/util/options.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/set.hh>
#include <paludis/util/timestamp.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/selinux/security_context.hh>
#include <paludis/environment.hh>
#include <paludis/hook.hh>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <cstring>
#include <cstdio>
#include <list>
#include <set>
#include <unordered_map>

#include "config.h"
#ifdef HAVE_XATTRS
#  include <sys/xattr.h>
#endif

using namespace paludis;

#include <paludis/fs_merger-se.cc>

typedef std::unordered_map<std::pair<dev_t, ino_t>, std::string, Hash<std::pair<dev_t, ino_t> > > MergedMap;

namespace paludis
{
    template <>
    struct Imp<FSMerger>
    {
        MergedMap merged_ids;
        FSMergerParams params;

        Imp(const FSMergerParams & p) :
            params(p)
        {
        }
    };
}

FSMergerError::FSMergerError(const std::string & s) throw () :
    MergerError(s)
{
}

FSMerger::FSMerger(const FSMergerParams & p) :
    Pimp<FSMerger>(p),
    Merger(make_named_values<MergerParams>(
                n::environment() = p.environment(),
                n::get_new_ids_or_minus_one() = p.get_new_ids_or_minus_one(),
                n::image() = p.image(),
                n::install_under() = p.install_under(),
                n::merged_entries() = p.merged_entries(),
                n::no_chown() = p.no_chown(),
                n::options() = p.options(),
                n::root() = p.root()
                )),
    _imp(Pimp<FSMerger>::_imp)
{
}

FSMerger::~FSMerger()
{
}

void
FSMerger::merge()
{
    struct SaveUmask
    {
        mode_t m;

        SaveUmask(mode_t mm) :
            m(mm)
        {
        }

        ~SaveUmask()
        {
            ::umask(m);
        }
    } old_umask(::umask(0000));

    Merger::merge();
}

void
FSMerger::prepare_install_under()
{
    Context context("When preparing install_under directory '" + stringify(_imp->params.install_under()) + "' under root '"
            + stringify(_imp->params.root()) + "':");

    std::list<FSEntry> dd;
    for (FSEntry d(_imp->params.root().realpath() / _imp->params.install_under()), d_end(_imp->params.root().realpath()) ;
            d != d_end ; d = d.dirname())
        dd.push_front(d);
    for (std::list<FSEntry>::iterator d(dd.begin()), d_end(dd.end()) ; d != d_end ; ++d)
        if (! d->exists())
        {
            d->mkdir();
            track_install_under_dir(*d, FSMergerStatusFlags());
        }
        else
            track_install_under_dir(*d, FSMergerStatusFlags() + msi_used_existing);
}

void
FSMerger::on_file_over_nothing(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    track_install_file(src, dst, src.basename(), install_file(src, dst, src.basename()));
}

void
FSMerger::on_file_over_file(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    if (config_protected(src, dst))
    {
        std::string cfgpro_name(make_config_protect_name(src, dst));
        track_install_file(src, dst, cfgpro_name, install_file(src, dst, cfgpro_name));
    }
    else
        track_install_file(src, dst, src.basename(), install_file(src, dst, src.basename()) + msi_unlinked_first);
}

void
FSMerger::on_file_over_dir(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    on_error(is_check, "Cannot overwrite directory '" + stringify(dst / src.basename()) + "' with file '"
            + stringify(src) + "'");
}

void
FSMerger::on_file_over_sym(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    track_install_file(src, dst, src.basename(), install_file(src, dst, src.basename()) + msi_unlinked_first);
}

void
FSMerger::on_file_over_misc(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    track_install_file(src, dst, src.basename(), install_file(src, dst, src.basename()) + msi_unlinked_first);
}

void
FSMerger::on_dir_over_nothing(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    track_install_dir(src, dst, install_dir(src, dst));
}

void
FSMerger::on_dir_over_file(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    on_error(is_check, "Cannot overwrite file '" + stringify(dst / src.basename()) + "' with directory '"
            + stringify(src) + "'");
}

void
FSMerger::on_dir_over_dir(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    track_install_dir(src, dst, FSMergerStatusFlags() + msi_used_existing);
}

void
FSMerger::on_dir_over_sym(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    EntryType m;
    try
    {
        m = entry_type((dst / src.basename()).realpath());
    }
    catch (const FSError &)
    {
        m = et_nothing;
    }

    if (m == et_dir)
    {
        on_warn(is_check, "Expected '" + stringify(dst / src.basename()) +
                "' to be a directory but found a symlink to a directory");
        if (! is_check)
            track_install_dir(src, dst, FSMergerStatusFlags() + msi_used_existing);
    }
    else
        on_error(is_check, "Expected '" + stringify(dst / src.basename()) +
                "' to be a directory but found a symlink to a non-directory");
}

void
FSMerger::on_dir_over_misc(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    unlink_misc(dst / src.basename());
    track_install_dir(src, dst, install_dir(src, dst) + msi_unlinked_first);
}

void
FSMerger::on_sym_over_nothing(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    track_install_sym(src, dst, install_sym(src, dst));
}

void
FSMerger::on_sym_over_file(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    unlink_file(dst / src.basename());
    track_install_sym(src, dst, install_sym(src, dst) + msi_unlinked_first);
}

void
FSMerger::on_sym_over_dir(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    on_error(is_check, "Cannot overwrite directory '" + stringify(dst / src.basename()) + "' with symlink '"
            + stringify(src) + "'");
}

void
FSMerger::on_sym_over_sym(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    unlink_sym(dst / src.basename());
    track_install_sym(src, dst, install_sym(src, dst) + msi_unlinked_first);
}

void
FSMerger::on_sym_over_misc(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if (is_check)
        return;

    unlink_misc(dst / src.basename());
    track_install_sym(src, dst, install_sym(src, dst) + msi_unlinked_first);
}

FSMergerStatusFlags
FSMerger::install_file(const FSEntry & src, const FSEntry & dst_dir, const std::string & dst_name)
{
    Context context("When installing file '" + stringify(src) + "' to '" + stringify(dst_dir) + "' with protection '"
            + stringify(dst_name) + "':");
    FSMergerStatusFlags result;

    FSEntry dst(dst_dir / (stringify(dst_name) + "|paludis-midmerge"));
    FSEntry dst_real(dst_dir / dst_name);

    if (dst_real.is_regular_file())
        dst_real.chmod(0);

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_install_file_pre")
                        ("INSTALL_SOURCE", stringify(src))
                        ("INSTALL_DESTINATION", stringify(dst_dir / src.basename()))
                        ("REAL_DESTINATION", stringify(dst_real)))).max_exit_status())
        Log::get_instance()->message("merger.file.pre_hooks.failure", ll_warning, lc_context) <<
                "Merge of '" << src << "' to '" << dst_dir << "' pre hooks returned non-zero";

    std::shared_ptr<const SecurityContext> secctx(MatchPathCon::get_instance()->match(stringify(dst_real), src.permissions()));
    FSCreateCon createcon(secctx);
    if (0 != paludis::setfilecon(src, secctx))
        throw FSMergerError("Could not set SELinux context on '"
                + stringify(src) + "': " + stringify(::strerror(errno)));

    mode_t src_perms(src.permissions());
    if (0 != (src_perms & (S_ISVTX | S_ISUID | S_ISGID)))
        result += msi_setid_bits;


    bool do_copy(false);

    if ((! _imp->params.options()[mo_nondestructive]) &&
            0 == std::rename(stringify(src).c_str(), stringify(dst_real).c_str()))
    {
        result += msi_rename;

        bool touch(_imp->merged_ids.end() == _imp->merged_ids.find(src.lowlevel_id()));
        _imp->merged_ids.insert(make_pair(src.lowlevel_id(), stringify(dst_real)));

        FSEntry d(stringify(dst_real));
        if (touch && (
                    (! _imp->params.options()[mo_preserve_mtimes]) ||
                    (d.mtim() < _imp->params.fix_mtimes_before())
                    ))
        {
            bool ok(false);
            if (d.mtim() < _imp->params.fix_mtimes_before())
                ok = d.utime(_imp->params.fix_mtimes_before());
            else
                ok = d.utime(Timestamp::now());
            if (! ok)
                throw FSMergerError("utime(" + stringify(dst_real) + ", 0) failed: " + stringify(::strerror(errno)));
        }

        /* set*id bits get partially clobbered on a rename on linux */
        dst_real.chmod(src_perms);
    }
    else
    {
        do_copy = true;
        std::pair<MergedMap::const_iterator, MergedMap::const_iterator> ii(
                _imp->merged_ids.equal_range(src.lowlevel_id()));
        for (MergedMap::const_iterator i = ii.first ; i != ii.second ; ++i)
        {
            if (0 == ::link(i->second.c_str(), stringify(dst).c_str()))
            {
                if (0 != std::rename(stringify(dst).c_str(), stringify(dst_real).c_str()))
                    throw FSMergerError("rename(" + stringify(dst) + ", " + stringify(dst_real) + ") failed: " + stringify(::strerror(errno)));
                do_copy = false;
                result += msi_as_hardlink;
                break;
            }
            Log::get_instance()->message("merger.file.link_failed", ll_debug, lc_context)
                    << "link(" << i->second << ", " << dst_real << ") failed: "
                    << ::strerror(errno);
        }
    }

    if (do_copy)
    {
        Log::get_instance()->message("merger.file.will_copy", ll_debug, lc_context) <<
            "rename/link failed: " << ::strerror(errno) << ". Falling back to regular read/write copy";

        FDHolder input_fd(::open(stringify(src).c_str(), O_RDONLY), false);
        if (-1 == input_fd)
            throw FSMergerError("Cannot read '" + stringify(src) + "': " + stringify(::strerror(errno)));

        FDHolder output_fd(::open(stringify(dst).c_str(), O_WRONLY | O_CREAT, src_perms), false);
        if (-1 == output_fd)
            throw FSMergerError("Cannot write '" + stringify(dst) + "': " + stringify(::strerror(errno)));

        if (! _imp->params.no_chown())
            if (0 != ::fchown(output_fd, src.owner(), src.group()))
                throw FSMergerError("Cannot fchown '" + stringify(dst) + "': " + stringify(::strerror(errno)));

        /* set*id bits */
        if (0 != ::fchmod(output_fd, src_perms))
            throw FSMergerError("Cannot fchmod '" + stringify(dst) + "': " + stringify(::strerror(errno)));
        try_to_copy_xattrs(src, output_fd, result);

        char buf[4096];
        ssize_t count;
        while ((count = read(input_fd, buf, 4096)) > 0)
            if (-1 == write(output_fd, buf, count))
                throw FSMergerError("write failed: " + stringify(::strerror(errno)));
        if (-1 == count)
            throw FSMergerError("read failed: " + stringify(::strerror(errno)));

        /* might need to copy mtime */
        if (_imp->params.options()[mo_preserve_mtimes])
        {
            Timestamp timestamp(src.mtim());
            if (timestamp < _imp->params.fix_mtimes_before())
                timestamp = _imp->params.fix_mtimes_before();

            struct timespec ts[2];
            ts[0] = ts[1] = timestamp.as_timespec();
            if (0 != ::futimens(output_fd, ts))
                throw FSMergerError("Cannot futimens '" + stringify(dst) + "': " + stringify(::strerror(errno)));
        }

        if (0 != std::rename(stringify(dst).c_str(), stringify(dst_real).c_str()))
            throw FSMergerError(
                    "rename(" + stringify(dst) + ", " + stringify(dst_real) + ") failed: " + stringify(::strerror(errno)));

        _imp->merged_ids.insert(make_pair(src.lowlevel_id(), stringify(dst_real)));
    }

    if (fixed_ownership_for(src))
        result += msi_fixed_ownership;

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_install_file_post")
                         ("INSTALL_SOURCE", stringify(src))
                         ("INSTALL_DESTINATION", stringify(dst_dir / src.basename()))
                         ("REAL_DESTINATION", stringify(dst_real)))).max_exit_status())
        Log::get_instance()->message("merger.file.post_hooks.failed", ll_warning, lc_context) <<
            "Merge of '" << src << "' to '" << dst_dir << "' post hooks returned non-zero";

    return result;
}

void
FSMerger::track_renamed_dir_recursive(const FSEntry & dst)
{
    for (DirIterator d(dst, { dio_include_dotfiles, dio_inode_sort }), d_end ; d != d_end ; ++d)
    {
        FSMergerStatusFlags merged_how;
        if (fixed_ownership_for(_imp->params.image() / *d))
            merged_how += msi_fixed_ownership;
        EntryType m(entry_type(*d));
        switch (m)
        {
            case et_sym:
                rewrite_symlink_as_needed(*d, dst);
                track_install_sym(*d, dst, merged_how + msi_parent_rename);
                _imp->merged_ids.insert(make_pair(d->lowlevel_id(), stringify(*d)));
                continue;

            case et_file:
                {
                    bool touch(_imp->merged_ids.end() == _imp->merged_ids.find(d->lowlevel_id()));
                    _imp->merged_ids.insert(make_pair(d->lowlevel_id(), stringify(*d)));

                    if (touch && (
                                (! _imp->params.options()[mo_preserve_mtimes]) ||
                                (d->mtim() < _imp->params.fix_mtimes_before())
                                ))
                    {
                        bool ok(false);
                        if (d->mtim() < _imp->params.fix_mtimes_before())
                            ok = FSEntry(*d).utime(_imp->params.fix_mtimes_before());
                        else
                            ok = FSEntry(*d).utime(Timestamp::now());
                        if (! ok)
                            throw FSMergerError("utime(" + stringify(*d) + ", 0) failed: " + stringify(::strerror(errno)));
                    }
                    track_install_file(*d, dst, stringify(d->basename()), merged_how + msi_parent_rename);
                }
                continue;

            case et_dir:
                track_install_dir(*d, d->dirname(), merged_how + msi_parent_rename);
                track_renamed_dir_recursive(*d);
                continue;

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

            case et_nothing:
            case last_et:
                ;
        }

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

void
FSMerger::relabel_dir_recursive(const FSEntry & src, const FSEntry & dst)
{
    for (DirIterator d(src, { dio_include_dotfiles, dio_inode_sort }), d_end ; d != d_end ; ++d)
    {
        mode_t mode(d->permissions());
        std::shared_ptr<const SecurityContext> secctx(
                MatchPathCon::get_instance()->match(stringify(dst / d->basename()), mode));
        if (0 != paludis::setfilecon(*d, secctx))
            throw FSMergerError("Could not set SELinux context on '"
                    + stringify(*d) + "' : " + stringify(::strerror(errno)));
        if (d->is_directory())
            relabel_dir_recursive(*d, dst / d->basename());
    }
}

FSMergerStatusFlags
FSMerger::install_dir(const FSEntry & src, const FSEntry & dst_dir)
{
    Context context("When installing dir '" + stringify(src) + "' to '" + stringify(dst_dir) + "':");

    FSMergerStatusFlags result;

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

    mode_t mode(src.permissions());
    if (0 != (mode & (S_ISVTX | S_ISUID | S_ISGID)))
        result += msi_setid_bits;

    FSEntry dst(dst_dir / src.basename());
    std::shared_ptr<const SecurityContext> secctx(MatchPathCon::get_instance()->match(stringify(dst), mode));
    FSCreateCon createcon(secctx);
    if (0 != paludis::setfilecon(src, secctx))
        throw FSMergerError("Could not set SELinux context on '"
                + stringify(src) + "': " + stringify(::strerror(errno)));

    if (is_selinux_enabled())
        relabel_dir_recursive(src, dst);

    if ((! _imp->params.options()[mo_nondestructive]) &&
            0 == std::rename(stringify(src).c_str(), stringify(dst).c_str()))
    {
        result += msi_rename;
        track_renamed_dir_recursive(dst);
        set_skipped_dir(true);
    }
    else
    {
        Log::get_instance()->message("merger.dir.rename_failed", ll_debug, lc_context) <<
            "rename failed. Falling back to recursive copy.";

        dst.mkdir(mode);
        FDHolder dst_fd(::open(stringify(dst).c_str(), O_RDONLY));
        struct stat sb;
        if (-1 == dst_fd)
            throw FSMergerError("Could not get an FD for the directory '"
                    + stringify(dst) + "' that we just created: " + stringify(::strerror(errno)));
        if (-1 == ::fstat(dst_fd, &sb))
            throw FSMergerError("Could not fstat the directory '"
                    + stringify(dst) + "' that we just created: " + stringify(::strerror(errno)));
        if ( !S_ISDIR(sb.st_mode))
            throw FSMergerError("The directory that we just created is not a directory anymore");
        if (! _imp->params.no_chown())
            if (-1 == ::fchown(dst_fd, src.owner(), src.group()))
                throw FSMergerError("Could not fchown the directory '" + stringify(dst) + "' that we just created: "
                        + stringify(::strerror(errno)));
        /* pick up set*id bits */
        ::fchmod(dst_fd, mode);
        try_to_copy_xattrs(src, dst_fd, result);
    }

    if (fixed_ownership_for(src))
        result += msi_fixed_ownership;

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

    return result;
}

FSMergerStatusFlags
FSMerger::install_sym(const FSEntry & src, const FSEntry & dst_dir)
{
    Context context("When installing sym '" + stringify(src) + "' to '" + stringify(dst_dir) + "':");

    FSMergerStatusFlags result;

    FSEntry dst(dst_dir / src.basename());

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

    if (0 != (src.permissions() & (S_ISVTX | S_ISUID | S_ISGID)))
        result += msi_setid_bits;

    bool do_sym(true);

    FSCreateCon createcon(MatchPathCon::get_instance()->match(stringify(dst), S_IFLNK));
    std::pair<MergedMap::const_iterator, MergedMap::const_iterator> ii(
            _imp->merged_ids.equal_range(src.lowlevel_id()));
    for (MergedMap::const_iterator i = ii.first ; i != ii.second ; ++i)
    {
        if (0 == ::link(i->second.c_str(), stringify(dst).c_str()))
        {
            do_sym = false;
            result += msi_as_hardlink;
            break;
        }
        Log::get_instance()->message("merger.sym.link_failed", ll_debug, lc_context)
            << "link(" << i->second + ", " << stringify(dst) << ") failed: "
            << ::strerror(errno);
    }

    if (do_sym)
    {
        if (0 != ::symlink(stringify(src.readlink()).c_str(), stringify(dst).c_str()))
            throw FSMergerError("Couldn't create symlink at '" + stringify(dst) + "': "
                    + stringify(::strerror(errno)));
        _imp->merged_ids.insert(make_pair(src.lowlevel_id(), stringify(dst)));
    }

    if (! _imp->params.no_chown())
    {
        dst.lchown(src.owner(), src.group());
        if (fixed_ownership_for(src))
            result += msi_fixed_ownership;
    }

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

    return result;
}

void
FSMerger::unlink_file(FSEntry d)
{
    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_file_pre")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_file.pre_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' pre hooks returned non-zero";

    d.chmod(0);
    d.unlink();

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_file_post")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_file.post_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' post hooks returned non-zero";
}

void
FSMerger::unlink_sym(FSEntry d)
{
    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_sym_pre")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_sym.pre_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' pre hooks returned non-zero";

    d.unlink();

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_sym_post")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_sym.post_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' post hooks returned non-zero";
}

void
FSMerger::unlink_dir(FSEntry d)
{
    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_dir_pre")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_dir.pre_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' pre hooks returned non-zero";

    d.rmdir();

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_dir_post")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_dir.post_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' post hooks returned non-zero";
}

void
FSMerger::unlink_misc(FSEntry d)
{
    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_misc_pre")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_misc.pre_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' pre hooks returned non-zero";

    d.unlink();

    if (0 != _imp->params.environment()->perform_hook(extend_hook(
                         Hook("merger_unlink_misc_post")
                         ("UNLINK_TARGET", stringify(d)))).max_exit_status())
        Log::get_instance()->message("merger.unlink_misc.post_hooks.failure", ll_warning, lc_context) <<
            "Unmerge of '" << d << "' post hooks returned non-zero";
}

Hook
FSMerger::extend_hook(const Hook & h)
{
    return h
        ("ROOT", stringify(_imp->params.root()))
        ("IMAGE", stringify(_imp->params.image()));
}

#ifdef HAVE_XATTRS

void
FSMerger::try_to_copy_xattrs(const FSEntry & src, int dst_fd, FSMergerStatusFlags & flags)
{
    FDHolder src_fd(::open(stringify(src).c_str(), O_RDONLY));

    ssize_t list_sz(flistxattr(src_fd, 0, 0));
    if (-1 == list_sz)
    {
        if (ENOTSUP != errno)
            Log::get_instance()->message("merger.xattrs.failure", ll_warning, lc_context) <<
                "Got error '" << ::strerror(errno) << "' when trying to find extended attributes size for '" << src << "'";
        return;
    }
    else if (list_sz > (2 << 29))
    {
        Log::get_instance()->message("merger.xattrs.your_fs_sucks", ll_warning, lc_context) <<
            "flistxattr returned " << list_sz << ", which clearly isn't right. Are you using some crazy "
            "ricer filesystem or kernel?";
        return;
    }

    std::shared_ptr<char> list_holder(static_cast<char *>(::operator new(list_sz)));
    list_sz = flistxattr(src_fd, list_holder.get(), list_sz);
    if (-1 == list_sz)
    {
        Log::get_instance()->message("merger.xattrs.failure", ll_warning, lc_context) <<
            "Got error '" << ::strerror(errno) << "' when trying to find extended attributes for '" << src << "'";
        return;
    }

    for (int offset(0) ; list_sz > 0 ; )
    {
        std::string key(list_holder.get() + offset);
        do
        {
            ssize_t value_sz(fgetxattr(src_fd, key.c_str(), 0, 0));
            if (-1 == value_sz)
            {
                Log::get_instance()->message("merger.xattrs.failure", ll_warning, lc_context) <<
                    "Got error '" << ::strerror(errno) << "' when trying to read size of extended attribute '" <<
                    key << "' for '" << src << "'";
                break;
            }

            std::shared_ptr<char> value_holder(static_cast<char *>(::operator new(value_sz)));
            value_sz = fgetxattr(src_fd, key.c_str(), value_holder.get(), value_sz);
            if (-1 == value_sz)
            {
                Log::get_instance()->message("merger.xattrs.failure", ll_warning, lc_context) <<
                    "Got error '" << ::strerror(errno) << "' when trying to read extended attribute '" <<
                    key << "' for '" << src << "'";
            }

            if (-1 == fsetxattr(dst_fd, key.c_str(), value_holder.get(), value_sz, 0))
            {
                if (ENOTSUP == errno)
                {
                    Log::get_instance()->message("merger.xattrs.failure", ll_warning, lc_context) <<
                        "Could not copy extended attributes from source file '" << src << "'";
                    return;
                }
                else
                    Log::get_instance()->message("merger.xattrs.failure", ll_warning, lc_context) <<
                        "Got error '" << ::strerror(errno) << "' when trying to set extended attribute '" <<
                        key << "' taken from source file '" << src << "'";
            }

            flags += msi_xattr;

        } while (false);

        list_sz -= (key.length() + 1);
        offset += (key.length() + 1);
    }
}

#else

void
FSMerger::try_to_copy_xattrs(const FSEntry &, int, FSMergerStatusFlags &)
{
}

#endif

void
FSMerger::track_install_file(const FSEntry & src, const FSEntry & dst_dir, const std::string & dst_name, const FSMergerStatusFlags & flags)
{
    _imp->params.merged_entries()->insert(dst_dir / dst_name);
    record_install_file(src, dst_dir, dst_name, flags);
}

void
FSMerger::track_install_dir(const FSEntry & src, const FSEntry & dst_dir, const FSMergerStatusFlags & flags)
{
    _imp->params.merged_entries()->insert(dst_dir / src.basename());
    record_install_dir(src, dst_dir, flags);
}

void
FSMerger::track_install_under_dir(const FSEntry & dst, const FSMergerStatusFlags & flags)
{
    _imp->params.merged_entries()->insert(dst);
    record_install_under_dir(dst, flags);
}

void
FSMerger::track_install_sym(const FSEntry & src, const FSEntry & dst_dir, const FSMergerStatusFlags & flags)
{
    _imp->params.merged_entries()->insert(dst_dir / src.basename());
    record_install_sym(src, dst_dir, flags);
}

void
FSMerger::on_file_main(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    EntryType m(entry_type(dst / src.basename()));

    do
    {
        switch (m)
        {
            case et_nothing:
                on_file_over_nothing(is_check, src, dst);
                continue;

            case et_sym:
                on_file_over_sym(is_check, src, dst);
                continue;

            case et_dir:
                on_file_over_dir(is_check, src, dst);
                continue;

            case et_misc:
                on_file_over_misc(is_check, src, dst);
                continue;

            case et_file:
                on_file_over_file(is_check, src, dst);
                continue;

            case last_et:
                ;
        }

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

void
FSMerger::on_dir_main(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    EntryType m(entry_type(dst / src.basename()));

    do
    {
        switch (m)
        {
            case et_nothing:
                on_dir_over_nothing(is_check, src, dst);
                continue;

            case et_sym:
                on_dir_over_sym(is_check, src, dst);
                continue;

            case et_dir:
                on_dir_over_dir(is_check, src, dst);
                continue;

            case et_misc:
                on_dir_over_misc(is_check, src, dst);
                continue;

            case et_file:
                on_dir_over_file(is_check, src, dst);
                continue;

            case last_et:
                ;
        }

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

    } while (false);
}

void
FSMerger::on_sym_main(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    EntryType m(entry_type(dst / src.basename()));

    do
    {
        switch (m)
        {
            case et_nothing:
                on_sym_over_nothing(is_check, src, dst);
                continue;

            case et_sym:
                on_sym_over_sym(is_check, src, dst);
                continue;

            case et_dir:
                on_sym_over_dir(is_check, src, dst);
                continue;

            case et_misc:
                on_sym_over_misc(is_check, src, dst);
                continue;

            case et_file:
                on_sym_over_file(is_check, src, dst);
                continue;

            case last_et:
                ;
        }

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

FSEntry
FSMerger::canonicalise_root_path(const FSEntry & f)
{
    return f.realpath();
}

void
FSMerger::do_dir_recursive(bool is_check, const FSEntry & src, const FSEntry & dst)
{
    if ((! is_check) && (! dst.is_directory()))
        throw MergerError("Destination directory '" + stringify(dst) + "' is not a directory");

    Merger::do_dir_recursive(is_check, src, dst);
}