summaryrefslogtreecommitdiffstats
path: root/move/tree/mdk_move_loop
blob: b98c4a417742832f70e6b19df52f9c7754e7d342 (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
#!/usr/bin/perl

sub name2file { 
    my ($name) = @_;
    "live_tree_$name.clp";
}

sub get_file2loop {
    my %file2loop;
    foreach (0 .. 9) {
	my ($file) = `sudo losetup /dev/loop$_ 2>/dev/null` =~ m!\([^)]*/([^)]*)\)! or last;
	$file2loop{$file} = "loop$_";
    }
    %file2loop;
}

sub to_memory {
    my ($loop, $file) = @_;

    return if -e "/tmp/$file";

    if (-e "/$file") {
	system("sudo mv /$file /tmp");
    } else {
	system("sudo cp /cdrom/$file /tmp");
    }
    system("sudo losetup /dev/$loop /tmp/$file");
}

sub to_cdrom {
    my ($loop, $file) = @_;
    my $f = -e "/$file" ? "/$file" : -e "/tmp/$file" ? "/tmp/$file" : return;
    system("sudo losetup /dev/$loop /cdrom/$file");
    system("sudo rm $f");
}

sub usage() { die "usage: mdk_move_loop <to_cdrom | to_memory> [names ...]\n" }

my ($direction, @names) = @ARGV;
my $f = $direction eq 'to_memory' && \&to_memory || $direction eq 'to_cdrom' && \&to_cdrom;
$f && @names or usage();

my %file2loop = get_file2loop(@names);

foreach my $name (@names) {
    if ($name eq 'always_i18n') {
	/(always_i18n.*)\.clp/ and $name = $1 foreach keys %file2loop;
    }
    my $file = name2file($name);
    my $loop = $file2loop{$file} or next;
    $f->($loop, $file);
}

if ($ENV{GIVE_LOOP}) {
    print "$_=$file2loop{$_}\n" foreach keys %file2loop;
}