From 42e551c708aed332e1e76a5de7cccdb7362b3c58 Mon Sep 17 00:00:00 2001 From: Rafael Garcia-Suarez Date: Thu, 10 Mar 2005 12:43:16 +0000 Subject: Comprehensive system error reporting. --- urpm.pm | 117 ++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 47 deletions(-) diff --git a/urpm.pm b/urpm.pm index 890b053b..ae8710f2 100644 --- a/urpm.pm +++ b/urpm.pm @@ -224,7 +224,7 @@ sub read_config { } #- read MD5 sums (usually not in urpmi.cfg but in a separate file) - open my $md5sum, "$urpm->{statedir}/MD5SUM"; + my $md5sum = $urpm->open_safe("<", "$urpm->{statedir}/MD5SUM"); while (<$md5sum>) { my ($md5sum, $file) = /(\S*)\s+(.*)/; foreach (@{$urpm->{media}}) { @@ -285,7 +285,7 @@ sub probe_medium { unless ($medium->{url}) { my %probe; if (-r "$urpm->{statedir}/$medium->{list}") { - open my $listfile, "$urpm->{statedir}/$medium->{list}"; + my $listfile = $urpm->open_safe("<", "$urpm->{statedir}/$medium->{list}"); while (<$listfile>) { #- /./ is end of url marker in list file (typically generated by a #- find . -name "*.rpm" > list @@ -390,8 +390,7 @@ sub write_config { or $urpm->{fatal}(6, N("unable to write config file [%s]", $urpm->{config})); #- write MD5SUM file - open my $md5sum, '>', "$urpm->{statedir}/MD5SUM" - or $urpm->{error}(N("unable to write file [%s]", "$urpm->{statedir}/MD5SUM")), return 0; + my $md5sum = $urpm->open_safe('>', "$urpm->{statedir}/MD5SUM") or return 0; foreach my $medium (@{$urpm->{media}}) { $medium->{md5sum} and print $md5sum "$medium->{md5sum} " . ($medium->{synthesis} && "synthesis.") . $medium->{hdlist} . "\n"; @@ -416,7 +415,7 @@ sub configure { my ($parallel_options, $parallel_handler); #- read parallel configuration local $_; - open my $parallel, "/etc/urpmi/parallel.cfg"; + my $parallel = $urpm->open_safe("<", "/etc/urpmi/parallel.cfg"); while (<$parallel>) { chomp; s/#.*$//; s/^\s*//; s/\s*$//; /\s*([^:]*):(.*)/ or $urpm->{error}(N("unable to parse \"%s\" in file [%s]", $_, "/etc/urpmi/parallel.cfg")), next; @@ -426,14 +425,16 @@ sub configure { #- if a configuration option has been found, use it; else fatal error. if ($parallel_options) { foreach my $dir (grep { -d $_ } map { "$_/urpm" } @INC) { - opendir my $dh, $dir or die $!; - while (defined ($_ = readdir $dh)) { #- load parallel modules - /parallel.*\.pm$/ && -f "$dir/$_" or next; - $urpm->{log}->(N("examining parallel handler in file [%s]", "$dir/$_")); - eval { require "$dir/$_"; $parallel_handler = $urpm->handle_parallel_options($parallel_options) }; - $parallel_handler and last; + my $dh = $urpm->opendir_safe($dir); + if ($dh) { + while (defined ($_ = readdir $dh)) { #- load parallel modules + /parallel.*\.pm$/ && -f "$dir/$_" or next; + $urpm->{log}->(N("examining parallel handler in file [%s]", "$dir/$_")); + eval { require "$dir/$_"; $parallel_handler = $urpm->handle_parallel_options($parallel_options) }; + $parallel_handler and last; + } + closedir $dh; } - closedir $dh; $parallel_handler and last; } } @@ -617,7 +618,8 @@ sub configure { local $SIG{QUIT} = $sig_handler; $db or $urpm->{fatal}(9, N("unable to open rpmdb")); - open my $rpmdb, "| " . ($ENV{LD_LOADER} || '') . " gzip -9 >'$options{bug}/rpmdb.cz'"; + open my $rpmdb, "| " . ($ENV{LD_LOADER} || '') . " gzip -9 >'$options{bug}/rpmdb.cz'" + or $urpm->syserror("Can't fork", "gzip"); $db->traverse(sub { my ($p) = @_; #- this is not right but may be enough. @@ -776,7 +778,8 @@ sub add_distrib_media { my @newnames; #- at this point, we have found an hdlists file, so parse it #- and create all necessary media according to it. - if (open my $hdlistsfh, $hdlists_file) { + my $hdlistsfh; + if ($hdlistsfh = $urpm->open_safe("<", $hdlists_file)) { my $medium = $options{initial_number} || 1; foreach (<$hdlistsfh>) { chomp; @@ -908,7 +911,7 @@ sub reconfig_urpmi { my @replacements; my @reconfigurable = qw(url with_hdlist clear_url); my $reconfigured = 0; - open my $fh, $rfile or return undef; + my $fh = $urpm->open_safe("<", $rfile) or return undef; $urpm->{log}(N("reconfiguring urpmi for media \"%s\"", $name)); #- the first line of reconfig.urpmi must be magic, to be sure it's not an error file my $magic = <$fh>; @@ -1931,11 +1934,13 @@ this could happen if you mounted manually the directory when creating the medium if ($options{noclean}) { local $_; my %headers; - opendir my $dh, "$urpm->{cachedir}/headers"; - while (defined($_ = readdir $dh)) { - m|^([^/]*-[^-]*-[^-]*\.[^\.]*)(?::\S*)?$| and $headers{$1} = $_; + my $dh = $urpm->opendir_safe("$urpm->{cachedir}/headers"); + if ($dh) { + while (defined($_ = readdir $dh)) { + m|^([^/]*-[^-]*-[^-]*\.[^\.]*)(?::\S*)?$| and $headers{$1} = $_; + } + closedir $dh; } - closedir $dh; $urpm->{log}(N("found %d headers in cache", scalar(keys %headers))); foreach (@{$urpm->{depslist}}) { delete $headers{$_->fullname}; @@ -1955,11 +1960,13 @@ this could happen if you mounted manually the directory when creating the medium foreach (@{$urpm->{media}}) { unlink "$urpm->{statedir}/names.$_->{name}"; if (defined $_->{start} && defined $_->{end}) { - open my $fh, ">$urpm->{statedir}/names.$_->{name}"; - foreach ($_->{start} .. $_->{end}) { - print $fh $urpm->{depslist}[$_]->name . "\n"; + my $fh = $urpm->open_safe(">", "$urpm->{statedir}/names.$_->{name}"); + if ($fh) { + foreach ($_->{start} .. $_->{end}) { + print $fh $urpm->{depslist}[$_]->name . "\n"; + } + close $fh; } - close $fh; } } @@ -2359,31 +2366,32 @@ sub get_source_packages { } #- examine the local repository, which is trusted (no gpg or pgp signature check but md5 is now done). - opendir my $dh, "$urpm->{cachedir}/rpms"; - while (defined($_ = readdir $dh)) { - if (my ($filename) = m|^([^/]*\.rpm)$|) { - my $filepath = "$urpm->{cachedir}/rpms/$filename"; - if (!$options{clean_all} && -s $filepath) { - if (keys(%{$file2fullnames{$filename} || {}}) > 1) { - $urpm->{error}(N("there are multiple packages with the same rpm filename \"%s\"", $filename)); - next; - } elsif (keys(%{$file2fullnames{$filename} || {}}) == 1) { - my ($fullname) = keys(%{$file2fullnames{$filename} || {}}); - if (defined($id = delete $fullname2id{$fullname})) { - $local_sources{$id} = $filepath; + my $dh = $urpm->opendir_safe("$urpm->{cachedir}/rpms"); + if ($dh) { + while (defined($_ = readdir $dh)) { + if (my ($filename) = m|^([^/]*\.rpm)$|) { + my $filepath = "$urpm->{cachedir}/rpms/$filename"; + if (!$options{clean_all} && -s $filepath) { + if (keys(%{$file2fullnames{$filename} || {}}) > 1) { + $urpm->{error}(N("there are multiple packages with the same rpm filename \"%s\"", $filename)); + next; + } elsif (keys(%{$file2fullnames{$filename} || {}}) == 1) { + my ($fullname) = keys(%{$file2fullnames{$filename} || {}}); + if (defined($id = delete $fullname2id{$fullname})) { + $local_sources{$id} = $filepath; + } else { + $options{clean_other} && ! exists $protected_files{$filepath} and unlink $filepath; + } } else { $options{clean_other} && ! exists $protected_files{$filepath} and unlink $filepath; } } else { - $options{clean_other} && ! exists $protected_files{$filepath} and unlink $filepath; + unlink $filepath; #- this file should be removed or is already empty. } - } else { - #- this file should be removed or is already empty. - unlink $filepath; - } - } #- no error on unknown filename located in cache (because .listing) inherited from old urpmi + } #- no error on unknown filename located in cache (because .listing) inherited from old urpmi + } + closedir $dh; } - closedir $dh; #- clean download directory, do it here even if this is not the best moment. if ($options{clean_all}) { @@ -2405,7 +2413,7 @@ sub get_source_packages { -s $file or $file = "$dir/list"; } if ($file && -r $file) { - open my $fh, $file or die $!; + my $fh = $urpm->open_safe('<', $file); while (<$fh>) { if (my ($filename) = m|/([^/]*\.rpm)$|) { if (keys(%{$file2fullnames{$filename} || {}}) > 1) { @@ -2917,8 +2925,7 @@ sub install { $options{delta} ||= 1000; $options{callback_open} ||= sub { my ($data, $type, $id) = @_; - open $fh, $install->{$id} || $upgrade->{$id} or - $urpm->{error}(N("unable to access rpm file [%s]", $install->{$id} || $upgrade->{$id})); + $fh = $urpm->open_safe('<', $install->{$id} || $upgrade->{$id}); return fileno $fh; }; $options{callback_close} ||= sub { @@ -3262,7 +3269,7 @@ sub parse_md5sum { my ($urpm, $path, $basename) = @_; my $retrieved_md5sum; $urpm->{log}(N("examining MD5SUM file")); - open my $fh, $path or return undef; + my $fh = $urpm->open_safe('<', $path) or return undef; local $_; while (<$fh>) { my ($md5sum, $file) = m|(\S+)\s+(?:\./)?(\S+)| or next; @@ -3274,7 +3281,23 @@ sub parse_md5sum { return $retrieved_md5sum; } -sub DESTROY { } +sub syserror { my ($urpm, $msg, $info) = @_; $urpm->{error}("$msg [$info] [$!]") } + +sub open_safe { + my ($urpm, $sense, $filename) = @_; + open my $f, $sense, $filename + or $urpm->syserror($sense eq '>' ? "Can't write file" : "Can't open file", $filename), return undef; + return $f; +} + +sub opendir_safe { + my ($urpm, $dirname) = @_; + opendir my $d, $dirname + or $urpm->syserror("Can't open directory", $dirname), return undef; + return $d; +} + +sub DESTROY {} 1; -- cgit v1.2.1