summaryrefslogtreecommitdiffstats
path: root/lib/MGA/DrakISO/BuildLoop.pm
blob: 5e840d4203ed404fef76b414bddf8d05a9cf7c04 (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
# Copyright (C) 2005 Mandriva
#                    Olivier Blin <oblin@mandriva.com>
# Copyright (C) 2017 Mageia
#                    Martin Whitaker <mageia@martin-whitaker.me.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program 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.

package MGA::DrakISO::BuildLoop;

use strict;

use MDK::Common;
use common;

use File::Temp qw(tmpnam);

use MGA::DrakISO::Live;
use MGA::DrakISO::Loopback;
use MGA::DrakISO::Utils;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(build_live_loopback_files list_loopback_modules);

sub build_live_loopback_files {
    my ($live) = @_;
    # make sure no external filesystems are mounted before creating the loopback
    umount_external_fs($live);

    my @excluded_files = expand_file_list($live, @{$live->{loopbacks}{exclude}{files} || []});
    my @modules_files = expand_file_list($live, @{$live->{loopbacks}{modules} || []});

    foreach (grep { exists $loop_types{$_->{type}}{build} } @{$live->{mount}{dirs} || []}) {
        local $_->{exclude} = [ @excluded_files, @modules_files ];
        $loop_types{$_->{type}}{build}->($live, $_);
    }

    foreach my $module (list_loopback_modules($live)) {
        my $copy_tree = $live->get_system_root . "/tmp/draklive/loop/$module->{name}";
        eval { rm_rf($copy_tree) };
        hardlink_filtered($live->get_system_root, $copy_tree, $module->{files});
        my $loop = $loop_types{$module->{type}};
        $loop->{build}->($live, { path => "$live->{prefix}{build}{modules}/$module->{name}", root => $copy_tree, exclude => \@excluded_files });
        eval { rm_rf($copy_tree) };
    }

    if (@excluded_files) {
        my $excluded_tree = $live->get_system_root . "/tmp/draklive/excluded/all";
        eval { rm_rf($excluded_tree) };
        hardlink_filtered($live->get_system_root, $excluded_tree, \@excluded_files);

        foreach my $module (list_loopback_modules($live)) {
            my $copy_tree = $live->get_system_root . "/tmp/draklive/excluded/$module->{name}";
            eval { rm_rf($copy_tree) };
            hardlink_filtered($excluded_tree, $copy_tree, $module->{files});
            my $loop = $loop_types{$module->{type}};
            $loop->{build}->($live, { path => "$live->{prefix}{build}{modules}/excluded-$module->{name}", root => $copy_tree });
            eval { rm_rf($copy_tree) };
        }

        my $loop = $loop_types{$live->{loopbacks}{exclude}{type}};
        $loop->{build}->($live, { path => "/excluded", root => $excluded_tree, exclude => \@modules_files });

        eval { rm_rf($excluded_tree) };
    }
}

sub expand_file_list {
    my ($live, @files) = @_;
    map {
        $_->{path} ?
          $_->{path} :
          chomp_(cat_(glob(($_->{rooted} && $live->get_system_root) . $_->{source})));
    } @files;
}

#- hardlink recursively file list to a directory
sub hardlink_filtered {
    my ($src, $dest, $files) = @_;
    mkdir_p($dest);
    my $pwd = $ENV{PWD};
    chdir($src);
    my $list_file = tmpnam();
    output_p($list_file, map { "$_\n" } grep { -e $src . $_ } @$files);
    #- cpio -pldm won't copy recursively, use rsync -r instead
    system('rsync', '-ar', '--files-from=' . $list_file, '--link-dest=' . $src, $src, $dest);
    unlink $list_file;
    chdir($pwd);
}

sub list_loopback_modules {
    my ($live) = @_;
    map {
        my $l = $_;
        map {
            my $list = $_;
            my $name = basename($list);
            $name =~ s/\.[^.]+$//;
            { type => $l->{type}, name => $name, files => [ expand_file_list($live, { source => $list }) ] };
        } glob(($_->{rooted} && $live->get_system_root) . $_->{source});
    } @{$live->{loopbacks}{modules}};
}

1;