/usr/share/perl5/Debian/DocBase/DocBaseFile.pm is in doc-base 0.10.8.
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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | # vim:cindent:ts=2:sw=2:et:fdm=marker:cms=\ #\ %s
#
# $Id: DocBaseFile.pm 228 2011-03-06 17:51:46Z robert $
#
package Debian::DocBase::DocBaseFile;
use strict;
use warnings;
use File::Glob ':glob';
use Debian::DocBase::Common;
use Debian::DocBase::Utils;
use Debian::DocBase::Gettext;
use Scalar::Util qw(weaken);
our %CONTROLFILES = ();
# constants for _PrsErr function
use constant PRS_FATAL_ERR => 1; # fatal error, marks documents as invalid
use constant PRS_ERR_IGN => 2; # error, marks documents as invalid
use constant PRS_WARN => 3; # warning, marks document as invalid
my %valid_sections = ();
my $global_errors_cnt = 0;
#################################################
### PUBLIC STATIC FUNCTIONS ###
#################################################
sub GetDocIdFromRegisteredFile($) { # {{{
my $file = shift;
my $data = Debian::DocBase::DB::GetFilesDB()->GetData($file);
return undef unless $data;
return $data->{'ID'};
} # }}}
sub GetAllDocBaseFiles() { # {{{
my @result;;
my @dirs = ($CONTROL_DIR, $LOCAL_CONTROL_DIR);
foreach my $idx (0 .. $#dirs)
{
if (opendir(DIR, $dirs[$idx])) {
push @result, grep { $_ = "$dirs[$idx]/$_"
if $_ ne "README"
and $_ !~ /(\.(bak|swp|dpkg-tmp|dpkg-new|dpkg-old)|~)$/o
and -f "$dirs[$idx]/$_" } readdir(DIR);
closedir DIR;
}
}
return @result;
} # }}}
sub GetChangedDocBaseFiles($$){ # {{{
my ($toremove, $toinstall) = @_;
my @changed_reg = ();
my @changed_unreg = ();
my %files = map { $_ => (stat $_)[$CTIME_FIELDNO] } GetAllDocBaseFiles();
my $filesdb = Debian::DocBase::DB::GetFilesDB();
my @dbkeys = $filesdb->GetDBKeys();
foreach my $realfile ( @dbkeys ) {
my $dbdata = $filesdb->GetData($realfile);
my $registered = defined $dbdata->{'ID'};
if ($files{$realfile} ) {
my $changed = $files{$realfile} != $dbdata->{'CT'};
push @changed_reg, $realfile if $changed and $registered;
push @changed_unreg, $realfile if $changed and ! $registered;
delete $files{$realfile}
} elsif ($registered) {
push @$toremove, $realfile;
} else {
# file no longer exists at file system and ID was not defined,
# so it was never registered and can't be unregistered. Don't
# try to unregister it, just remove entry from files.db
$filesdb->RemoveData($realfile);
}
}
@$toinstall = keys %files;
my @retval = ($#{$toremove} + 1, $#changed_reg + $#changed_unreg + 2, $#{$toinstall} + 1);
push @$toinstall, @changed_reg;
push @$toinstall, @changed_unreg;
push @$toremove, @changed_reg;
undef @changed_reg;
undef @changed_unreg;
return @retval;
} # }}}
sub DisplayErrorNote()
{
return unless $global_errors_cnt;
return if $opt_verbose;
Inform(_ng(
"Note: `install-docs --verbose --check file_name' may give more details about the above error.",
"Note: `install-docs --verbose --check file_name' may give more details about the above errors.",
$global_errors_cnt));
}
sub new { # {{{
my $class = shift;
my $filename = shift;
my $do_add_checks = shift;
if (defined $CONTROLFILES{$filename}) {
return $CONTROLFILES{$filename}
}
my $self = {
MAIN_DATA => {}, # hash of main_fld=>value
FORMAT_LIST => {}, # array of format data hashes
FILE_NAME => $filename,
CTIME => 0,
DO_ADD_CHECKS => $do_add_checks ? 1 : 0,
WARNERR_CNT => 0, # errors/warnings count
INVALID => 1,
PARSED => 0
};
bless($self, $class);
$self->_ReadStatusDB();
$CONTROLFILES{$filename} = $self;
weaken $CONTROLFILES{$filename};
return $self;
} # }}}
#################################################
### PUBLIC FUNCTIONS ###
#################################################
sub DESTROY { # {{{
my $self = shift;
delete $CONTROLFILES{$self->GetSourceFileName()};
} # }}}
sub GetDocumentID() { # {{{
my $self = shift;
$self->_CheckParsed();
return $self->{'MAIN_DATA'}->{$FLD_DOCUMENT};
} # }}}
sub GetFldValue($$) { # {{{
my $self = shift;
my $fld = shift;
$self->_CheckParsed();
return $self->{'MAIN_DATA'}->{$fld};
} # }}}
sub GetFormat($$) { # {{{
my $self = shift;
my $format_name = shift;
$self->_CheckParsed();
return $self->{'FORMAT_LIST'}->{$format_name};
} # }}}
# returns list of all format names defined in control file
sub GetFormatNames($$) { # {{{
my $self = shift;
my @fnames = sort keys %{$self->{'FORMAT_LIST'}};
return @fnames;
} # }}}
sub GetSourceFileName() { # {{{
my $self = shift;
return $self->{'FILE_NAME'};
} # }}}
sub Invalid() { # {{{
my $self = shift;
return $self->{'INVALID'};
} # }}}
sub GetWarnErrCount() { # {{{
my $self = shift;
return $self->{'WARNERR_CNT'};
} # }}}
sub OnRegistered($$) { # {{{
my ($self, $valid) = @_;
my $docid = $valid ? $self->GetDocumentID() : undef;
my $data = { CT => $self->{'CTIME'},
ID => $docid,
};
Debug("OnRegistered (".$self->GetSourceFileName().", $valid)");
Debian::DocBase::DB::GetFilesDB()->PutData($self->GetSourceFileName(), $data);
} # }}}
sub OnUnregistered() { # {{{
my $self = shift;
Debug("OnUnregistered (".$self->GetSourceFileName().")");
Debian::DocBase::DB::GetFilesDB()->RemoveData($self->GetSourceFileName());
} # }}}
sub GetLastChangeTime($) { # {{{
my $self = shift;
return $self->{'CTIME'};
} # }}}
sub _ReadStatusDB($) { # {{{
my $self = shift;
my $data = Debian::DocBase::DB::GetFilesDB()->GetData($self->GetSourceFileName());
return unless $data;
$self->{'MAIN_DATA'}->{$FLD_DOCUMENT} = $data->{'ID'};
$self->{'CTIME'} = $data->{'CT'}
} # }}}
sub Parse { # {{{
my $self = shift;
my $file = $self->{'FILE_NAME'};
my $fh = undef;
my $docid = undef;
# is file already parsed
return if $self->{'PARSED'};
open($fh, "<", $file) or
Fatal($ERR_PROCESSING, _g("Cannot open file `%s' for reading: %s."), $file, $!);
$self->{'CTIME'} = (stat $fh)[$CTIME_FIELDNO];
$self->_ReadControlFile($fh);
$self->{'PARSED'} = 1;
close($fh);
# if document doesn't contain valid document id,
# mark that document file exists in our files.db
$self->OnRegistered(0) unless defined $self->GetDocumentID();
} # }}}
#################################################
### PRIVATE FUNCTIONS ###
#################################################
# Parsing errors routine
# The first argument should be
# PRS_FATAL_ERR, which sets global exit status to 1 and {'INVALID'} to 1
# or PRS_ERR , INVALID to 1
# or PRS_WARN , does not change INVALID
# The second argument should be the message
sub _PrsErr($$) { # {{{
my $self = shift;
my $flag = shift;
my $fmt = shift;
my $msg = sprintf ($fmt, @_);
my $filepos = $. ? sprintf _g("`%s', line %d"), $self->GetSourceFileName(), $.
: sprintf "`%s'", $self->GetSourceFileName();
$self->{'WARNERR_CNT'}++;
$self->{'INVALID'} = 1 if $flag != PRS_WARN;
if ($flag == PRS_FATAL_ERR) {
++$global_errors_cnt;
Error(_g("Error in %s: %s."), $filepos, $msg);
} elsif ($flag == PRS_ERR_IGN) {
++$global_errors_cnt;
ErrorNF(_g("Error in %s: %s."), $filepos, $msg);
} elsif ($flag == PRS_WARN) {
Warn(_g("Warning in %s: %s."), $filepos, $msg);
} else {
Fatal($ERR_INTERNAL, _g("Unknown flag (%s, %s)."), $flag, $msg);
}
return undef;
} # }}}
# Check if input is UTF-8 encoded. If it's not recode and warn
# Parameters: $line- input line
# $fld - original field name
sub _CheckUTF8($$) { # {{{
my ($self, $line, $fld) = @_;
my $is_utf8_expr= '^(?:[\x{00}-\x{7f}]|[\x{80}-\x{255}]{2,})*$';
return $line if length($line) > 512;
if ($line !~ /$is_utf8_expr/o) {
$self->_PrsErr(PRS_WARN, _g("line in field `%s' seems not to be UTF-8 encoded, recoding"), $fld);
utf8::encode($line);
}
return $line;
} # }}}
##
## assuming filehandle IN is the control file, read a section (or
## "stanza") of the doc-base control file and adds data in that
## section to the hash reference passed as an argument. Returns 1 if
## there is data, 0 if it was empty or undef in case of parse error
##
sub _ReadControlFileSection($$$) { # {{{
my $self = shift;
my $fh = shift; # file handle
my $pfields = shift; # read fields
my $fldstype = shift; # $FLDTYPE_MAIN or $FLDTYPE_FORMAT
my $empty = 1;
my ($origcf, $cf,$v);
while (<$fh>) {
chomp;
s/\s*$//o; # trim trailing whitespace
# empty line?
if (/^\s*$/o) {
$empty ? next : last;
}
$empty = 0;
# new field?
if (/^(\S+)\s*:\s*(.*)$/o) {
($origcf, $cf, $v) = ($1, lc $1, $2);
if (exists $pfields->{$cf}) {
$self->_PrsErr(PRS_WARN, _g("control field `%s' already defined"), $origcf);
next;
} elsif (not defined $FIELDS_DEF{$cf}) {
$self->_PrsErr(PRS_WARN, _g("unrecognised control field `%s'"), $origcf);
next;
} elsif ($FIELDS_DEF{$cf}->{$FLDDEF_TYPE} != $fldstype) {
$self->_PrsErr(PRS_WARN, _g("field `%s' in incorrect section (missing empty line before the field?)"), $origcf);
next;
}
$pfields->{$cf} = $self->_CheckUTF8($v, $origcf);
} elsif (/^\s+(\S.*)$/o) {
$v = $&;
defined($cf) or return $self->_PrsErr(PRS_FATAL_ERR, _g("syntax error - no field specified"));
not defined($FIELDS_DEF{$cf}) or $FIELDS_DEF{$cf}->{$FLDDEF_MULTILINE} or return $self->_PrsErr(PRS_FATAL_ERR, _g("field `%s' cannot consist of multi lines"), $origcf);
#print STDERR "$cf -> $v (continued)\n";
$$pfields{$cf} .= "\n" . $self->_CheckUTF8($v, $origcf);
} else {
return $self->_PrsErr(PRS_FATAL_ERR, _g("syntax error in control file: %s"), $_);
}
}
return $self->_CheckRequiredFields($pfields, $fldstype) unless $empty and $fldstype == $FLDTYPE_FORMAT;
return not $empty;
} # }}}
sub _CheckParsed() { # {{{
my $self = shift;
my $filename = $self->GetSourceFileName();
Fatal($ERR_INTERNAL, _g("File `%s' not yet parsed."), (defined $filename ? $filename : ""))
unless $self->{'PARSED'};
} # }}}
sub _CheckSection($$) { # {{{
my $self = shift;
my $orig_section = shift;
ReadMap($DOCBASE_VALID_SECTIONS_LIST, \%valid_sections, 1) unless %valid_sections;
my $section = lc $orig_section;
$section =~ s/[\/\s]+$//g;
$section =~ s/^[\/\s]+//g;
while ($section) {
return if $valid_sections{$section};
last unless $section =~ s/\/[^\/]+$//;
}
$self->_PrsErr(PRS_WARN, _g("unknown section: `%s'"), $orig_section);
} # }}}
sub _CheckRequiredFields($$$) { # {{{
my $self = shift;
my $pfields = shift;
my $fldstype = shift; # $FLDTYPE_MAIN or $FLDTYPE_FORMAT
foreach my $fldname (sort keys (%FIELDS_DEF)) {
if (
$FIELDS_DEF{$fldname} -> {$FLDDEF_TYPE} == $fldstype
and $FIELDS_DEF{$fldname} -> {$FLDDEF_REQUIRED}
and not exists $pfields->{$fldname}
) {
return $self -> _PrsErr(PRS_FATAL_ERR, _g("value of `%s' not specified"), ucfirst($fldname));
}
}
return 1;
} # }}}
# reads control file specified as argument
# output:
# sets $docid
# sets $doc_data to point to a hash containing the document data
# sets @format_list, a list of pointers to hashes containing the format data
sub _ReadControlFile { # {{{
my $self = shift;
my $fh = shift;
my ($tmp, $tmpnam);
# first find doc id
$_ = <$fh>;
return $self->_PrsErr(PRS_FATAL_ERR, _g("the first line does not contain valid `Document' field"))
unless defined $_ and /^\s*Document\s*:\s*(\S+)\s*$/i;
$self->{'MAIN_DATA'} = { $FLD_DOCUMENT => ($tmp = $1) };
$self->_PrsErr(PRS_WARN, _g("invalid value of `Document' field"))
unless $tmp =~ /^[a-z0-9\.\+\-]+$/;
my $optdirmsg = ($opt_rootdir eq "") ? "" : " " . sprintf ( _g("(using `%s' as the root directory)"),
$opt_rootdir);
my $doc_data = $self->{'MAIN_DATA'};
# parse rest of the file
$self->_ReadControlFileSection($fh, $doc_data, $FLDTYPE_MAIN)
or return undef;
return $self->_PrsErr(PRS_WARN, _g("unsupported doc-base file version: %s"), $$doc_data{'version'})
if defined $$doc_data{'version'};
$self->_CheckSection($doc_data->{$FLD_SECTION}) if $self->{'DO_ADD_CHECKS'};
$self->{'MAIN_SECTION'} = $doc_data;
undef $doc_data;
my $format_data = {};
my $status = 0;
my $fmts_count = 0;
while ($status = $self->_ReadControlFileSection($fh, $format_data, $FLDTYPE_FORMAT)) {
my $format = $$format_data{'format'};
++$fmts_count;
# adjust control fields
$format =~ tr/A-Z/a-z/;
if (defined $self->{FORMAT_LIST}->{$format}) {
return $self->_PrsErr(PRS_ERR_IGN, _g("format `%s' already defined"), $format);
}
if (not grep { $_ eq $format } @SUPPORTED_FORMATS) {
$self->_PrsErr(PRS_WARN, _g("format `%s' is not supported"), $$format_data{'format'});
next;
}
my $index_value = undef;
# Check `Index' field
if (grep { $_ eq $format } @NEED_INDEX_FORMATS) {
$index_value = $tmp = $$format_data{'index'};
$tmpnam = "Index";
# a) does the field exist?
defined $tmp
or return $self->_PrsErr(PRS_FATAL_ERR, _g("`%s' value missing for format `%s'"), $tmpnam, $format);
# b) does it start with / ?
if ($$format_data{'index'} !~ /^\//) {
$self->_PrsErr(PRS_WARN, _g("`%s' value has to be specified with absolute path: %s"), $tmpnam, $tmp);
next;
}
# c) does the index file exist?
if (not -e $opt_rootdir.$tmp) {
$self->_PrsErr(PRS_WARN, _g("file `%s' does not exist").$optdirmsg, $tmp) ;
next;
}
}
# `Files' fields checks
# a) is field defined?
$tmp = $$format_data{'files'};
$tmpnam = "Files";
if (not defined $tmp) {
$self->_PrsErr(PRS_WARN, _g("`%s' value not specified for format `%s'"), $tmpnam, $format);
next;
}
if (not defined $index_value or $tmp ne $index_value) {
my @masks = split /\s+/, $tmp;
# b) do values start with / ?
my @invalid = grep { /^[^\/]/ } @masks;
if ($#invalid > -1) {
$self->_PrsErr(PRS_WARN, _g("`%s' value has to be specified with absolute path: %s"), $tmpnam, join (' ', @invalid));
next;
}
# c) do files exist ?
if (not grep { &bsd_glob($opt_rootdir.$_, GLOB_NOSORT) } @masks) {
$self->_PrsErr(PRS_WARN, _g("file mask `%s' does not match any files") . $optdirmsg,
join (" ", @masks));
next;
}
}
$self->{FORMAT_LIST}->{$format} = $format_data;
} continue {
$format_data = {};
}
unless (keys %{$self->{FORMAT_LIST}})
{
return $self->_PrsErr(PRS_ERR_IGN, _g("no `Format' section found")) unless $fmts_count;
return $self->_PrsErr(PRS_ERR_IGN, _g("all `Format' sections are invalid"));
}
$self->{'INVALID'} = 0;
} # }}}
1;
|