7 |
BEGIN { |
BEGIN { |
8 |
use Exporter (); |
use Exporter (); |
9 |
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
10 |
$VERSION = 0.02; |
$VERSION = 0.03; |
11 |
@ISA = qw (Exporter); |
@ISA = qw (Exporter); |
12 |
#Give a hoot don't pollute, do not export more than needed by default |
#Give a hoot don't pollute, do not export more than needed by default |
13 |
@EXPORT = qw (); |
@EXPORT = qw (); |
22 |
|
|
23 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
24 |
|
|
25 |
use IsisDB |
use IsisDB; |
26 |
|
|
27 |
my $isis = new IsisDB( |
my $isis = new IsisDB( |
28 |
isisdb => './cds/cds', |
isisdb => './cds/cds', |
29 |
); |
); |
30 |
|
|
31 |
|
for(my $mfn = 1; $mfn <= $isis->{'maxmfn'}; $mfn++) { |
32 |
|
print $isis->to_ascii($mfn),"\n"; |
33 |
|
} |
34 |
|
|
35 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
36 |
|
|
37 |
This module will read CDS/ISIS databases and create hash values out of it. |
This module will read CDS/ISIS databases and create hash values out of it. |
38 |
It can be used as perl-only alternative to OpenIsis module. |
It can be used as perl-only alternative to OpenIsis module. |
39 |
|
|
40 |
|
This will module will always be slower that OpenIsis module which use C |
41 |
|
library. However, since it's written in perl, it's platform independent (so |
42 |
|
you don't need C compiler), and can be easily modified. |
43 |
|
|
44 |
|
Unique feature of this module is ability to C<include_deleted> records. |
45 |
|
It will also skip zero sized fields (OpenIsis has a bug in XS bindings, so |
46 |
|
fields which are zero sized will be filled with random junk from memory). |
47 |
|
|
48 |
=head1 METHODS |
=head1 METHODS |
49 |
|
|
50 |
=cut |
=cut |
63 |
# some binary reads |
# some binary reads |
64 |
# |
# |
65 |
|
|
|
sub Read32 { |
|
|
my $self = shift; |
|
|
|
|
|
my $f = shift || die "Read32 needs file handle"; |
|
|
read($$f,$b,4) || die "can't read 4 bytes from $$f from position ".tell($f); |
|
|
return unpack("l",$b); |
|
|
} |
|
|
|
|
66 |
=head2 new |
=head2 new |
67 |
|
|
68 |
Open CDS/ISIS database |
Open CDS/ISIS database |
71 |
isisdb => './cds/cds', |
isisdb => './cds/cds', |
72 |
read_fdt => 1, |
read_fdt => 1, |
73 |
debug => 1, |
debug => 1, |
74 |
|
include_deleted => 1, |
75 |
); |
); |
76 |
|
|
77 |
Options are described below: |
Options are described below: |
92 |
|
|
93 |
Dump a C<lot> of debugging output. |
Dump a C<lot> of debugging output. |
94 |
|
|
95 |
|
=item include_deleted |
96 |
|
|
97 |
|
Don't skip logically deleted records in ISIS. |
98 |
|
|
99 |
=back |
=back |
100 |
|
|
101 |
It will also set C<$isis-E<gt>{'maxmfn'}> which is maximum MFN stored in database. |
It will also set C<$isis-E<gt>{'maxmfn'}> which is maximum MFN stored in database. |
107 |
my $self = {}; |
my $self = {}; |
108 |
bless($self, $class); |
bless($self, $class); |
109 |
|
|
110 |
$self->{isisdb} = {@_}->{isisdb} || croak "new needs database name as argument!"; |
croak "new needs database name (isisdb) as argument!" unless ({@_}->{isisdb}); |
111 |
|
|
112 |
$self->{debug} = {@_}->{debug}; |
foreach my $v (qw{isisdb debug include_deleted}) { |
113 |
|
$self->{$v} = {@_}->{$v}; |
114 |
|
} |
115 |
|
|
116 |
# if you want to read .FDT file use read_fdt argument when creating class! |
# if you want to read .FDT file use read_fdt argument when creating class! |
117 |
if ({@_}->{read_fdt} && -e $self->{isisdb}.".FDT") { |
if ({@_}->{read_fdt} && -e $self->{isisdb}.".FDT") { |
152 |
# NXTMFP offset to next available position in last block |
# NXTMFP offset to next available position in last block |
153 |
# MFTYPE always 0 for user db file (1 for system) |
# MFTYPE always 0 for user db file (1 for system) |
154 |
seek(fileMST,4,0); |
seek(fileMST,4,0); |
155 |
$self->{'NXTMFN'}=$self->Read32(\*fileMST) || carp "NXTNFN is zero"; |
|
156 |
|
my $buff; |
157 |
|
|
158 |
|
read(fileMST, $buff, 4); |
159 |
|
$self->{'NXTMFN'}=unpack("l",$buff) || carp "NXTNFN is zero"; |
160 |
|
|
161 |
# save maximum MFN |
# save maximum MFN |
162 |
$self->{'maxmfn'} = $self->{'NXTMFN'} - 1; |
$self->{'maxmfn'} = $self->{'NXTMFN'} - 1; |
189 |
my $buff = shift || return; |
my $buff = shift || return; |
190 |
my @arr = unpack("ssssssllls", $buff); |
my @arr = unpack("ssssssllls", $buff); |
191 |
|
|
192 |
|
print "unpack_cnt: ",join(" ",@arr),"\n" if ($self->{'debug'}); |
193 |
|
|
194 |
my $IDTYPE = shift @arr; |
my $IDTYPE = shift @arr; |
195 |
foreach (@flds) { |
foreach (@flds) { |
196 |
$self->{$IDTYPE}->{$_} = abs(shift @arr); |
$self->{$IDTYPE}->{$_} = abs(shift @arr); |
197 |
} |
} |
198 |
} |
} |
199 |
|
|
|
my $buff; |
|
200 |
read(fileCNT, $buff, 26); |
read(fileCNT, $buff, 26); |
201 |
$self->unpack_cnt($buff); |
$self->unpack_cnt($buff); |
202 |
|
|
240 |
print "seeking to $mfnpos in file '$self->{isisdb}.XRF'\n" if ($self->{debug}); |
print "seeking to $mfnpos in file '$self->{isisdb}.XRF'\n" if ($self->{debug}); |
241 |
seek($self->{'fileXRF'},$mfnpos,0); |
seek($self->{'fileXRF'},$mfnpos,0); |
242 |
|
|
243 |
|
my $buff; |
244 |
|
|
245 |
# read XRFMFB abd XRFMFP |
# read XRFMFB abd XRFMFP |
246 |
my $pointer=$self->Read32(\*{$self->{'fileXRF'}}); |
read($self->{'fileXRF'}, $buff, 4); |
247 |
|
my $pointer=unpack("l",$buff) || carp "pointer is null"; |
248 |
|
|
249 |
my $XRFMFB = int($pointer/2048); |
my $XRFMFB = int($pointer/2048); |
250 |
my $XRFMFP = $pointer - ($XRFMFB*2048); |
my $XRFMFP = $pointer - ($XRFMFB*2048); |
269 |
|
|
270 |
seek($self->{'fileMST'},$offset4,0); |
seek($self->{'fileMST'},$offset4,0); |
271 |
|
|
272 |
my $value=$self->Read32(\*{$self->{'fileMST'}}); |
read($self->{'fileMST'}, $buff, 4); |
273 |
|
my $value=unpack("l",$buff); |
274 |
|
|
275 |
if ($value!=$mfn) { |
if ($value!=$mfn) { |
276 |
print ("Error: The MFN:".$mfn." is not found in MST(".$value.")"); |
print ("Error: The MFN:".$mfn." is not found in MST(".$value.")"); |
284 |
# $NVF=$self->Read16($fileMST); |
# $NVF=$self->Read16($fileMST); |
285 |
# $STATUS=$self->Read16($fileMST); |
# $STATUS=$self->Read16($fileMST); |
286 |
|
|
|
my $buff; |
|
287 |
read($self->{'fileMST'}, $buff, 14); |
read($self->{'fileMST'}, $buff, 14); |
288 |
|
|
289 |
my ($MFRL,$MFBWB,$MFBWP,$BASE,$NVF,$STATUS) = unpack("slssss", $buff); |
my ($MFRL,$MFBWB,$MFBWP,$BASE,$NVF,$STATUS) = unpack("slssss", $buff); |
290 |
|
|
291 |
print "MFRL: $MFRL MFBWB: $MFBWB MFBWP: $MFBWP BASE: $BASE NVF: $NVF STATUS: $STATUS\n" if ($self->{debug}); |
print "MFRL: $MFRL MFBWB: $MFBWB MFBWP: $MFBWP BASE: $BASE NVF: $NVF STATUS: $STATUS\n" if ($self->{debug}); |
292 |
|
|
293 |
|
# delete old record |
294 |
|
delete $self->{record}; |
295 |
|
|
296 |
|
if (! $self->{'include_deleted'} && $MFRL < 0) { |
297 |
|
print "## logically deleted record $mfn, skipping...\n" if ($self->{debug}); |
298 |
|
return; |
299 |
|
} |
300 |
|
|
301 |
# Get Directory Format |
# Get Directory Format |
302 |
|
|
303 |
my @FieldPOS; |
my @FieldPOS; |
337 |
|
|
338 |
# Get Variable Fields |
# Get Variable Fields |
339 |
|
|
|
delete $self->{record}; |
|
|
|
|
340 |
read($self->{'fileMST'},$buff,$fld_len); |
read($self->{'fileMST'},$buff,$fld_len); |
341 |
|
|
342 |
for (my $i = 0 ; $i < $NVF ; $i++) { |
for (my $i = 0 ; $i < $NVF ; $i++) { |
343 |
|
# skip zero-sized fields |
344 |
|
next if ($FieldLEN[$i] == 0); |
345 |
|
|
346 |
push @{$self->{record}->{$FieldTAG[$i]}}, substr($buff,$FieldPOS[$i],$FieldLEN[$i]); |
push @{$self->{record}->{$FieldTAG[$i]}}, substr($buff,$FieldPOS[$i],$FieldLEN[$i]); |
347 |
} |
} |
348 |
close(fileMST); |
close(fileMST); |
349 |
|
|
|
# The record is marked for deletion |
|
|
if ($STATUS==1) { |
|
|
return -1; |
|
|
} |
|
|
|
|
350 |
print Dumper($self) if ($self->{debug}); |
print Dumper($self) if ($self->{debug}); |
351 |
|
|
352 |
return $self->{'record'}; |
return $self->{'record'}; |