This file is indexed.

/usr/share/perl5/XBase/SDBM.pm is in libdbd-xbase-perl 1:1.08-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
=head1 NAME

XBase::SDBM - SDBM index support for dbf

=head1 DESCRIPTION

When developing the XBase.pm/DBD::XBase module, I was trying to
support as many existing variants of file formats as possible. The
module thus accepts wide range of dbf files and their versions from
various producers. But with index files, the task is much, much
harder. First, there is little or no documentation of index files
formats, so the development is based on reverse engineering.

None if the index formats support is finalized. That made it hard to
integrate them into one consistent API. That is why I decided to write
my own index support, and as I wanted to avoid inventing yet another
way of storing records in pages and similar things, I used SDBM. It
comes with Perl, so you already have it, and it's proven and it
works.

Now, SDBM is a module that aims at other task than to do supporting
indexes for a dbf. But equality tests are fast with it and I have
creted a structure in each index file to enable "walk" though the
index file.

=head1 VERSION

1.02

=head1 AVAILABLE FROM

http://www.adelton.com/perl/DBD-XBase/

=head1 AUTHOR

(c) 2001--2011 Jan Pazdziora.

All rights reserved. This package is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=cut

package XBase::SDBM;
use SDBM_File;
use Fcntl;

sub fetch {
	my $self = shift;
	my $current = $self->{'current'};	# current pointer
	return unless defined $current;
	my $hash = $self->{'sdbmhash'};
	my $value = $hash->{$current};

	if (not defined $value) {
		delete $self->{'current'};
		return;
	}
	my ($key, $num) = ($current =~ /^(.*):(\d+)$/s);
	$num++;
	if (defined $hash->{"$key:$num"}) {	# next record for the same key
		$self->{'current'} = "$key:$num";
	} else {
		my $newkey = $hash->{"$key:next"};	# next key
		if (defined $newkey) {
			$self->{'current'} = "$newkey:1";
		} else {
			delete $self->{'current'};
		}
	}
	return ($key, $value);
}
sub fetch_current {
	my $self = shift;
	my $current = $self->{'current'};
	return unless defined $current;
	my $value = $self->{'sdbmhash'}{$current};
	return unless defined $value;
	my ($key) = ($current =~ /^(.*):\d+$/s);
	return ($key, $value);
}
sub tags {
	my $self = shift;
	return map { if (s/:file$//) { ( $_ ) } else { () } }
		keys %{$self->{'definition'}};
}

sub prepare_select {
	my $self = shift;
	$self->{'current'} = $self->{'sdbmhash'}{':first'};
	$self->{'current'} .= ':1' if defined $self->{'current'};
	1;
}
sub prepare_select_eq {
	my ($self, $eq, $recno) = @_;
	delete $self->{'current'};
	my $hash = $self->{'sdbmhash'};
	my $start = $eq;
	my $value = $hash->{"$start:1"};

	if (not defined $value) {
		# not exact match
		$start = $hash->{':first'};
		if (not defined $start) {
			# no records, jsut return
			return 1;
		}
		# move throught the chain
		while (defined $start and $start lt $eq) {
			$start = $hash->{"$start:next"};	
		}
		if (not defined $start) {
			return 1;
		}
		if ($start gt $eq) {
			$self->{'current'} = "$start:1";
			return 1;
		}
		# we shouldn't have never got here, but nevermind
		$value = $hash->{"$start:1"};
	}

	# here we've found exact match of the key
	if (not defined $recno) {
		# if not requested exact match of the recno, return
		$self->{'current'} = "$start:1";
		return 1;
	}

	my $num = 1;
	while (defined $value and $value != $recno) {
		$num++;
		$value = $hash->{"$start:$num"};
	}

	if (defined $value) {
		$self->{'current'} = "$start:$num";
	} else {
		$start = $hash->{"$start:next"};
		$self->{'current'} = "$start:1" if defined $start;
	}
	1;
}


# method new (open) will open the named SDBM index for given dbf
sub new {
	my ($class, $filename, %opts) = @_;
	my $dbf = $opts{'dbf'};
	my $tag = $opts{'tag'};
	
	# return immediatelly if the index file was already opened
	return $dbf->{'sdbm_definition'}{'tags'}{$tag}
		if defined $dbf->{'sdbm_definition'}
			and defined $dbf->{'sdbm_definition'}{'tags'}{$tag};

	my $dbffile = $dbf->{'filename'};
	my $file = $dbffile;
	$file =~ s/\.dbf$/.sdbmd/i;

	# some of the SDBM indexes were already touched
	# the definitionhash is a SDBM that lists the content of the
	# actual SDBM index files
	my $definitionhash = {};
	if (defined $dbf->{'sdbm_definition'}) {
		$definitionhash = $dbf->{'sdbm_definition'}{'definitionhash'};
	}
	else {
		# if it wasn't opened yet, open the definition file
		if (not tie(%$definitionhash, 'SDBM_File',
				$file, O_RDWR, 0666)) {
			die "SDBM index definition file `$file' not found for `$dbffile': $!.";
		}
		$dbf->{'sdbm_definition'} = { 'filename' => $file,
					'definitionhash' => $definitionhash };
	}

	# check the definition file for tag requested
	my $sdbmfile = $definitionhash->{"$tag:file"};
	if (not defined $sdbmfile) {
		# no such SDBM index exists, the definition SDBM says
		die "SDBM index `$tag' not known for `$dbffile'.";
	}

	# open the SDBM index file
	my $sdbmhash = {};
	unless (tie(%$sdbmhash, 'SDBM_File', $sdbmfile, O_RDWR, 0666)) {
		die "SDBM index file `$sdbmfile' not found for `$dbffile': $!.";
	}

	my $self = bless { 'dbf' => $dbf,
		'tag' => $tag, 'sdbmhash' => $sdbmhash,
		'definition' => $definitionhash }, $class;
	$dbf->{'sdbm_definition'}{'tags'}{$tag} = $self;
	return $self;
}
*open = \&new;

# method create will create SDBM index with given name and expression
# for the dbf table
sub create {
	my ($class, $dbf, $tag, $expression) = @_;
	my $dbffile = $dbf->{'filename'};
	my $file;

	my $definitionhash;
	if (defined $dbf->{'sdbm_definition'}) {
		# the definition SDBM was already opened
		$definitionhash = $dbf->{'sdbm_definition'}{'definitionhash'};
	} else {
		$file = $dbffile;
		$file =~ s/\.dbf$/.sdbmd/i;

		$definitionhash = {};
		# open or create the definition SDBM file
		if (not tie(%$definitionhash, 'SDBM_File',
				$file, O_RDWR|O_CREAT, 0666)) {
			die "SDBM index definition file `$file' not found/created for `$dbffile': $!.";
		}
		$dbf->{'sdbm_definition'} = { 'filename' => $file,
			'definitionhash' => $definitionhash };
	}

	if (defined $definitionhash->{"$tag:file"}) {
		die "SDBM index `$tag' already exists for `$dbfffile'.";
	}

	my $maxindexnumber = ++$definitionhash->{'tagnumber'};

	my $sdbmfile = $dbffile;
	$sdbmfile =~ s/\.dbf$/.sdbm$maxindexnumber/i;

	my $sdbmhash = {};
	if (not tie(%$sdbmhash, 'SDBM_File', $sdbmfile, O_CREAT|O_EXCL|O_RDWR, 0666)) {
		die "SDBM index file `$sdbmfile' couldn't be created for `$dbffile': $!."
	}

	my $self = bless { 'dbf' => $dbf, 'tag' => $tag,
		'sdbmhash' => $sdbmhash,
		'definition' => $definitionhash}, $class;
	$dbf->{'sdbm_definition'}{'tags'}{$tag} = $self;
	$definitionhash->{"$tag:file"} = $sdbmfile;

	if (defined $dbf->field_type(uc $expression)) {
		$expression = uc $expression;
	}
	if (not defined $dbf->field_type($expression)) {
		$self->drop;
		die "SDBM index `$expression' couldn't be created for `$dbffile': no such column name.";
	}
	$definitionhash->{"$tag:expression"} = $expression;

	my $i = 0;
	while ($i <= $dbf->last_record) {
		my ($deleted, $value) = $dbf->get_record($i);
		if (not $deleted) {
			$self->insert($value, $i + 1);		
		}
		$i++;
	}

	return $self;
}

# method drop will drop the SDBM index
sub drop {
	my ($self) = @_;
	my $tag = $self->{'tag'};
	my $definitionhash = $self->{'definition'};
	my $sdbmfile = $definitionhash->{"$tag:file"};
	delete $definitionhash->{"$tag:file"};
	delete $definitionhash->{"$tag:definition"};
	delete $self->{'dbf'}{'sdbm_definition'}{'tags'}{$tag};
	unlink "$sdbmfile.pag", "$sdbmfile.dir";
}

sub insert {
	my ($self, $key, $value) = @_;
	### print "Adding $key $value\n";
	my $hash = $self->{'sdbmhash'};
	my $key_maxid = $hash->{"$key:0"};
	$key_maxid++;

	$hash->{"$key:$key_maxid"} = $value;
	$hash->{"$key:0"} = $key_maxid;
	return 1 if $key_maxid > 1;	# no need to change the chain
	
	my $prev = undef;
	my $prev_next = ':first';
	my $next;
	while (defined($next = $hash->{$prev_next}) and $key gt $next) {
		$prev = $next;
		$prev_next = "$prev:next";
		$next = undef;
	}

	if (not defined $next) {
		$hash->{':last'} = $key;	# we reached the last record
	} else {
		$hash->{"$key:next"} = $next;
		$hash->{"$next:prev"} = $key;
	}
	if (not defined $prev) {
		$hash->{':first'} = $key;
	} else {
		$hash->{"$prev:next"} = $key;
		$hash->{"$key:prev"} = $prev;
	}
	return 1;
}

sub delete {
	my ($self, $key, $value) = @_;
	### print "Deleting $key $value\n";
	my $hash = $self->{'sdbmhash'};
	my $key_maxid = $hash->{"$key:0"};

	my $number = 1;
	while ($number <= $key_maxid) {
		if ($hash->{"$key:$number"} == $value) {
			last;
		}
		$number++;
	}
	if ($number > $key_maxid) {
		# such a record was not found
		return 0;
	}

	if ($key_maxid > 1) {
		$hash->{"$key:$number"} = $hash->{"$key:$key_maxid"}
			if $number != $key_maxid;
		delete $hash->{"$key:$key_maxid"};
		$hash->{"$key:0"} = $key_maxid - 1;
	} else {
		my $next = $hash->{"$key:next"};
		my $prev = $hash->{"$key:prev"};
		if (defined $next) {
			if (not defined $prev) {
				$hash->{':first'} = $next;
				delete $hash->{"$next:prev"};
			} else {
				$hash->{"$prev:next"} = $next;
				$hash->{"$next:prev"} = $prev;
				delete $hash->{"$key:prev"};
			}
			delete $hash->{"$key:next"};
		} else {
			if (not defined $prev) {
				delete $hash->{':first'};
				delete $hash->{':last'};
			} else {
				$hash->{':last'} = $prev;
				delete $hash->{"$prev:next"};
				delete $hash->{"$key:prev"};
			}
		}
		delete $hash->{"$key:0"};
		delete $hash->{"$key:1"};
	}
	return 1;
}
sub delete_current {
	my $self = shift;
	my ($key, $value) = $self->fetch_current;
	if (defined $value) {
		$self->delete($key, $value);
	}
}
sub insert_before_current {
	die "SDBM index doesn't support backward rolling yet.\n";
}

sub dump {
	my $self = shift;
	my $hash = $self->{'sdbmhash'};

	for (sort keys %$hash) {
		print "$_ $hash->{$_}\n";
	}
}

1;