package interactive::http; # $Id$ use diagnostics; use strict; use vars qw(@ISA); @ISA = qw(interactive); use CGI; use interactive; use common; use log; my $script_name = $ENV{INTERACTIVE_HTTP}; my $no_header; my $pipe_r = "/tmp/interactive_http_r"; my $pipe_w = "/tmp/interactive_http_w"; sub open_stdout() { open STDOUT, ">$pipe_w" or die; $| = 1; print CGI::header(); $no_header = 1; } # cont_stdout must be called after open_stdout and before the first print sub cont_stdout { my ($o_title) = @_; print CGI::start_html('-title' => $o_title) if $no_header; $no_header = 0; } sub new_uid() { my ($s, $ms) = gettimeofday(); $s * 256 + $ms % 256; } sub new { open_stdout(); bless {}, $_[0]; } sub end { -e $pipe_r or return; # do not run this twice my $q = CGI->new; cont_stdout("Exit"); print "It's done, thanks for playing", $q->end_html; close STDOUT; unlink $pipe_r, $pipe_w; } sub exit { end(); exit($_[1]) } END { end() } sub ask_fromW { my ($o, $common, $l, $_l2) = @_; redisplay: my $uid = new_uid(); my $q = CGI->new; $q->param(state => 'next_step'); $q->param(uid => $uid); cont_stdout($common->{title}); # print $q->img({ -src => "/icons/$o->{icon}" }) if $o->{icon}; print @{$common->{messages}}; print $q->start_form('-name' => 'form', '-action' => $script_name, '-method' => 'post'); print "\n"; each_index { my $e = $_; print "\n"; } @$l; print "
$e->{label}\n"; $e->{type} = 'list' if $e->{type} =~ /(icon|tree)list/; #- combo does not exist, fallback to a sensible default $e->{type} = $e->{not_edit} ? 'list' : 'entry' if $e->{type} eq 'combo'; if ($e->{type} eq 'bool') { print $q->checkbox('-name' => "w$::i", '-checked' => ${$e->{val}} && 'on', '-label' => $e->{text} || " "); } elsif ($e->{type} eq 'button') { print "nobuttonyet"; } elsif ($e->{type} =~ /list/) { my %t; $t{$_} = may_apply($e->{format}, $_) foreach @{$e->{list}}; print $q->scrolling_list('-name' => "w$::i", '-values' => $e->{list}, '-default' => [ ${$e->{val}} ], '-size' => 5, '-multiple' => '', '-labels' => \%t); } else { print $e->{hidden} ? $q->password_field('-name' => "w$::i", '-default' => ${$e->{val}}) : $q->textfield('-name' => "w$::i", '-default' => ${$e->{val}}); } print "
\n"; print $q->p; print $q->submit('-name' => 'ok_submit', '-value' => $common->{ok} || N("Ok")); print $q->submit('-name' => 'cancel_submit', '-value' => $common->{cancel} || N("Cancel")) if $common->{cancel} || !exists $common->{ok}; print $q->hidden('state'), $q->hidden('uid'); print $q->end_form, $q->end_html; close STDOUT; # page terminated while (1) { open(my $F, "<$pipe_r") or die; $q = CGI->new($F); $q->param('force_exit_dead_prog') and $o->exit; last if $q->param('uid') == $uid; open_stdout(); # re-open for writing cont_stdout(N("Error")); print $q->h1(N("Error")), $q->p("Sorry, you can not go back"); goto redisplay; } each_index { my $e = $_; my $v = $q->param("w$::i"); if ($e->{type} eq 'bool') { $v = $v eq 'on'; } ${$e->{val}} = $v; } @$l; open_stdout(); # re-open for writing $q->param('ok_submit'); } sub p { print "\n" . CGI::br($_) foreach @_; } sub wait_messageW { my ($_o, $_title, $messages) = @_; cont_stdout(); print "\n" . CGI::p(); p(@$messages); } sub wait_message_nextW { my ($_o, $messages, $_w) = @_; p(@$messages); } sub wait_message_endW { my ($_o, $_w) = @_; p(N("Done")); print "\n" . CGI::p(); } sub ok { N("Ok"); } sub cancel { N("Cancel"); } 1; ion> Mageia Installer and base platform for many utilitiesThierry Vignaud [tv]
summaryrefslogtreecommitdiffstats
path: root/perl-install/devices.pm
blob: b2b44aec727e54f794bb68c3fa05ee6901861d39 (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
package devices; # $Id$

use diagnostics;
use strict;

use common;
use run_program;
use log;
use c;

sub size {
    my ($dev) = @_;
    sysopen(my $F, $dev, 0) or log::l("open $dev: $!"), return 0;

    my $valid_offset = sub { sysseek($F, $_[0], 0) && sysread($F, my $_a, 1) };

    #- first try getting the size nicely
    if (my $size = c::total_sectors(fileno $F)) {
	return $size * $common::SECTORSIZE;
    }

    #- sad it didn't work, well searching the size using the dichotomy algorithm!
    my $low = 0;
    my ($high, $mid);

    #- first find n where 2^n < size <= 2^n+1
    for ($high = 1; $high > 0 && $valid_offset->($high); $high *= 2) { $low = $high }

    while ($low < $high - 1) {
	$mid = int(($low + $high) / 2);
	$valid_offset->($mid) ? $low : $high = $mid;
    }
    $low + 1;
}

sub del_loop {
    my ($dev) = @_;
    run_program::run("losetup", "-d", $dev);
}
sub find_free_loop() {
    foreach (0..7) {
	my $dev = make("loop$_");
	sysopen(my $F, $dev, 2) or next;
	!ioctl($F, c::LOOP_GET_STATUS(), my $_tmp) && $! == 6 or next; #- 6 == ENXIO
	return $dev;
    }
    die "no free loop found";
}
sub set_loop {
    my ($file, $o_encrypt_key, $o_encryption) = @_;
    my $dev = find_free_loop();

    if ($o_encrypt_key && $o_encryption) {
	my $cmd = "losetup -p 0 -e $o_encryption $dev $file";
	log::l("calling $cmd");
	open(my $F, "|$cmd");
	print $F $o_encrypt_key;
	close $F or die "losetup failed";
    } else {
	run_program::run("losetup", $dev, $file) or return;
    }
    $dev;
}

sub entry {
    my ($type, $major, $minor);
    local ($_) = @_;

    if (/^0x([\da-f]{3,4})$/i) {
	$type = c::S_IFBLK();
	($major, $minor) = unmakedev(hex $1);
    } elsif (/^sd(.)(\d{0,2})/) {
	$type = c::S_IFBLK();
	$major = 8;
	$minor = 16 * (ord($1) - ord('a')) + ($2 || 0);
    } elsif (/^hd(.)(\d{0,2})/) {
	$type = c::S_IFBLK();
	($major, $minor) =
	    @{ ${{'a' => [3, 0], 'b' => [3, 64],
		   'c' => [22,0], 'd' => [22,64],
		   'e' => [33,0], 'f' => [33,64],
		   'g' => [34,0], 'h' => [34,64],
		   'i' => [56,0], 'j' => [56,64],
		   'k' => [57,0], 'l' => [57,64],
                   'm' => [88,0], 'n' => [88,64],
                   'o' => [89,0], 'p' => [89,64],
                   'q' => [90,0], 'r' => [90,64],
                   's' => [91,0], 't' => [91,64],
	       }}{$1} or die "unknown device $_" };
	$minor += $2 || 0;
    } elsif (/^ram(.*)/) {
	$type = c::S_IFBLK();
	$major = 1;
	$minor = $1 eq '' ? 1 : $1;
    } elsif (m|^rd/c(\d+)d(\d+)(p(\d+))?|) {
	# dac 960 "rd/cXdXXpX"
        $type = c::S_IFBLK();
	$major = 48 + $1;
	$minor = 8 * $2 + $4;
    } elsif (m,(ida|cciss)/c(\d+)d(\d+)(?:p(\d+))?,) {
	# Compaq Smart Array "ida/c0d0{p1}"
	$type = c::S_IFBLK();
	$major = ($1 eq 'ida' ? 72 : 104) + $2;
	$minor = 16 * $3 + ($4 || 0);
    } elsif (m,(ataraid)/d(\d+)(?:p(\d+))?,) {
	# ATA raid "ataraid/d0{p1}"
	$type = c::S_IFBLK();
	$major = 114;
	$minor = 16 * $1 + ($2 || 0);
    } elsif (/(.*?)(\d+)$/) {
	    ($type, $major, $minor) =
	     @{ ${{"fd"          => [ c::S_IFBLK(), 2,  0  ],