summaryrefslogtreecommitdiffstats
path: root/perl-install/resize_fat/c_rewritten.xs
blob: 89be89826df3173e4c63d43aa83ee67319894059 (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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

/* set by scan_fat, used by next */
short *fat = NULL;
char *fat_flag_map = NULL;
unsigned int *fat_remap = NULL;
int fat_remap_size;
int type_size, nb_clusters, bad_cluster_value;

void free_all() {
#define FREE(p) if (p) free(p), p = NULL;
  FREE(fat);
  FREE(fat_flag_map);
  FREE(fat_remap);
#undef FREE
}

unsigned int next(unsigned int cluster) {
  short *p = fat + type_size * cluster;
  if (!fat) {
    free_all();
    croak("fat::next: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("fat::next: cluster %d outside filesystem", cluster);
  }
  return type_size == 1 ? *p : *((unsigned int *) p);
}

void set_next(unsigned int cluster, unsigned int val) {
  short *p = fat + type_size * cluster;
  if (!fat) {
    free_all();
    croak("fat::set_next: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("fat::set_next: cluster %d outside filesystem", cluster);
  }
  type_size == 1 ? *p : *((unsigned int *) p) = val;
}

MODULE = resize_fat::c_rewritten PACKAGE = resize_fat::c_rewritten

PROTOTYPES: DISABLE


void 
read_fat(fd, offset, size, magic)
  int fd
  int offset
  int size
  unsigned char magic
  PPCODE:
{
  fat = (short *) malloc(size);
  if (!fat) {
    free_all();
    croak("read_fat: not enough memory");
  }
  if (lseek(fd, offset, SEEK_SET) != offset ||
      read(fd, fat, size) != size) {
    free_all();
    croak("read_fat: reading FAT failed");
  }
  if (magic != *(unsigned char *) fat) {
    free_all();
    croak("read_fat: FAT has invalid signature");
  }
}

void
write_fat(fd, size)
  int fd
  int size
  PPCODE:
{
  if (write(fd, fat, size) != size) {
    free_all();
    croak("write_fat: write failed");
  }
}

void
free_all()
  PPCODE:
  free_all();

void
scan_fat(nb_clusters_, type_size_)
  int nb_clusters_
  int type_size_
  PPCODE:
{
  unsigned int v;  
  int free = 0, bad = 0, used = 0;
  short *p;
  
  type_size = type_size_; nb_clusters = nb_clusters_;
  bad_cluster_value = type_size == 32 ? 0x0ffffff7 : 0xfff7;

  if (type_size % 16) {
    free_all();
    croak("scan_fat: unable to handle FAT%d", type_size);
  }
  type_size /= 16;

  for (p = fat + 2 * type_size; p < fat + type_size * (nb_clusters + 2); p += type_size) {
    v = type_size == 1 ? *p : *((unsigned int *) p);

    if (v == 0) free++;
    else if (v == bad_cluster_value) bad++;
  }
  used = nb_clusters - free - bad;
  EXTEND(SP, 3);
  PUSHs(sv_2mortal(newSViv(free)));
  PUSHs(sv_2mortal(newSViv(bad)));
  PUSHs(sv_2mortal(newSViv(used)));
}

unsigned int
next(unused, cluster)
  void *unused
  unsigned int cluster
  CODE:
  RETVAL = next(cluster);
  OUTPUT:
  RETVAL

void
set_next(unused, cluster, val)
  void *unused
  unsigned int cluster
  unsigned int val
  CODE:
  set_next(cluster, val);

void
allocate_fat_flag(size)
  int size
  CODE:
  fat_flag_map = calloc(size, 1);
  if (!fat_flag_map) {
    free_all();
    croak("allocate_fat_flag: not enough memory");
  }

int
checkFat(cluster, type, name)
  unsigned int cluster
  int type
  char *name
  CODE:
  int nb = 0;

  if (!fat_flag_map) {
    free_all();
    croak("Bad FAT: trying to use null pointer");
  }
  for (; cluster < bad_cluster_value; cluster = next(cluster)) {
    if (cluster == 0) {
      free_all();
      croak("Bad FAT: unterminated chain for %s\n", name);
    }
    if (cluster >= nb_clusters + 2) {
      free_all();
      croak("Bad FAT: chain outside filesystem for %s\n", name);
    }
    if (fat_flag_map[cluster]) {
      free_all();
      croak("Bad FAT: cluster %d is cross-linked for %s\n", cluster, name);
    }
    fat_flag_map[cluster] = type;
    nb++;
  }
  RETVAL = nb;
  OUTPUT:
  RETVAL

unsigned int
flag(cluster)
  unsigned int cluster
  CODE:
  if (!fat_flag_map) {
    free_all();
    croak("Bad FAT: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("Bad FAT: going outside filesystem");
  }
  RETVAL = fat_flag_map[cluster];
  OUTPUT:
  RETVAL

void
set_flag(cluster, flag)
  unsigned int cluster
  int flag
  CODE:
  if (!fat_flag_map) {
    free_all();
    croak("Bad FAT: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("Bad FAT: going outside filesystem");
  }
  fat_flag_map[cluster] = flag;

void
allocate_fat_remap(size)
  int size
  CODE:
  fat_remap_size = size;
  fat_remap = (unsigned int *) calloc(size, sizeof(unsigned int *));
  if (!fat_remap) {
    free_all();
    croak("allocate_fat_remap: not enough memory");
  }

unsigned int
fat_remap(cluster)
  unsigned int cluster
  CODE:
  if (!fat_remap) {
    free_all();
    croak("fat_remap: trying to use null pointer");
  }
  if (cluster >= bad_cluster_value) {
    RETVAL = cluster; /* special cases */
  } else {
    if (cluster >= fat_remap_size) {
      free_all();
      croak("fat_remap: cluster %d >= %d in fat_remap", cluster, fat_remap_size);
    }
    RETVAL = fat_remap[cluster];
  }
  OUTPUT:
  RETVAL

void
set_fat_remap(cluster, val)
  unsigned int cluster
  unsigned int val
  CODE:
  if (!fat_remap) {
    free_all();
    croak("set_fat_remap: trying to use null pointer");
  }
  if (cluster >= fat_remap_size) {
    free_all();
    croak("set_fat_remap: cluster %d >= %d in set_fat_remap", cluster, fat_remap_size);
  }
  if (val < bad_cluster_value && val >= fat_remap_size) {
    free_all();
    croak("set_fat_remap: remapping cluster %d to cluster %d >= %d in set_fat_remap", cluster, val, fat_remap_size);
  }
  fat_remap[cluster] = val;
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
package interactive; # $Id$

use diagnostics;
use strict;

#-######################################################################################
#- misc imports
#-######################################################################################
use MDK::Common::Func;
use common;

#- minimal example using interactive:
#
#- > use lib qw(/usr/lib/libDrakX);
#- > use interactive;
#- > my $in = interactive->vnew;
#- > $in->ask_okcancel('title', 'question');
#- > $in->exit;

#- ask_from_ takes global options ($common):
#-  title                => window title
#-  messages             => message displayed in the upper part of the window
#-  advanced_messages    => message displayed when "Advanced" is pressed
#-  ok                   => force the name of the "Ok"/"Next" button
#-  cancel               => force the name of the "Cancel"/"Previous" button
#-  advanced_label       => force the name of the "Advanced" button
#-  advanced_label_close => force the name of the "Basic" button
#-  advanced_state       => if set to 1, force the "Advanced" part of the dialog to be opened initially
#-  focus_cancel         => force focus on the "Cancel" button
#-  focus_first          => force focus on the first entry
#-  callbacks            => functions called when something happen: complete canceled advanced changed focus_out ok_disabled

#- ask_from_ takes a list of entries with fields:
#-  val      => reference to the value
#-  label    => description
#-  icon     => icon to put before the description
#-  help     => tooltip
#-  advanced => wether it is shown in by default or only in advanced mode
#-  disabled => function returning wether it should be disabled (grayed)
#-  gtk      => gtk preferences
#-  type     => 
#-     button => (with clicked or clicked_may_quit)
#-               (type defaults to button if clicked or clicked_may_quit is there)
#-               (val need not be a reference) (if clicked_may_quit return true, it's as if "Ok" was pressed)
#-     label => (val need not be a reference) (type defaults to label if val is not a reference) 
#-     bool (with "text" or "image" (which overrides text) giving an image filename)
#-     range (with min, max)
#-     combo (with list, not_edit, format)
#-     list (with list, icon2f (aka icon), separator (aka tree), format (aka pre_format function),
#-           help can be a hash or a function,
#-           tree_expanded boolean telling wether the tree should be wide open by default
#-           quit_if_double_click boolean
#-           allow_empty_list disables the special cases for 0 and 1 element lists
#-           image2f is a subroutine which takes a value of the list as parameter, and returns an array (text, image_file_name))
#-     entry (the default) (with hidden)
#
#- heritate from this class and you'll get all made interactivity for same steps.
#- for this you need to provide
#- - ask_from_listW(o, title, messages, arrayref, default) returns one string of arrayref
#-
#- where
#- - o is the object
#- - title is a string
#- - messages is an refarray of strings
#- - default is an optional string (default is in arrayref)
#- - arrayref is an arrayref of strings
#- - arrayref2 contains booleans telling the default state,
#-
#- ask_from_list and ask_from_list_ are wrappers around ask_from_biglist and ask_from_smalllist
#-
#- ask_from_list_ just translate arrayref before calling ask_from_list and untranslate the result
#-
#- ask_from_listW should handle differently small lists and big ones.
#-


#-######################################################################################
#- OO Stuff
#-######################################################################################
sub new($) {
    my ($type) = @_;

    bless {}, ref($type) || $type;
}

sub vnew {
    my ($_type, $su, $icon) = @_;
    $su = $su eq "su";
    if ($ENV{INTERACTIVE_HTTP}) {
	require interactive::http;
	return interactive::http->new;
    }
    require c;
    if ($su) {
	$ENV{PATH} = "/sbin:/usr/sbin:$ENV{PATH}";
	$su = '' if $::testing || $ENV{TESTING};
    }
    require_root_capability() if $su;
    if (check_for_xserver()) {
	eval { require interactive::gtk };
	if (!$@) {
	    my $o = interactive::gtk->new;
	    if ($icon && $icon ne 'default' && !$::isWizard) { $o->{icon} = $icon } else { undef $o->{icon} }
	    return $o;
	}
    }

    require 'log.pm'; #- "require log" causes some pb, perl thinking that "log" is the log() function
    undef *log::l;
    *log::l = sub {}; # otherwise, it will bother us :(
    require interactive::newt;
    interactive::newt->new;
}

sub enter_console {}
sub leave_console {}
sub suspend {}
sub resume {}
sub end {}
sub exit { exit($_[0]) }

#-######################################################################################
#- Interactive functions
#-######################################################################################
sub ask_warn {
    my ($o, $title, $message) = @_;
    ask_warn_($o, { title => $title, messages => $message });
}
sub ask_yesorno {
    my ($o, $title, $message, $def) = @_;
    ask_yesorno_($o, { title => $title, messages => $message }, $def);
}
sub ask_okcancel {
    my ($o, $title, $message, $def) = @_;
    ask_okcancel_($o, { title => $title, messages => $message }, $def);
}

sub ask_warn_ {
    my ($o, $common) = @_;
    ask_from_listf_raw_no_check($o, $common, undef, [ N("Ok") ]);
}

sub ask_yesorno_ {
    my ($o, $common, $def) = @_;
    $common->{cancel} = '';