Annotation of loncom/build/check-rpms, revision 1.4

1.1       harris41    1: #!/usr/bin/perl
                      2: #
1.3       harris41    3: # check-rpms, version 2.1.1
1.1       harris41    4: # Martin Siegert, SFU, siegert@sfu.ca, Feb 02
                      5: #
1.4     ! harris41    6: # documentation and minor patches,
        !             7: # Scott Harrison sharrison@users.sourceforge.net 2002
        !             8: 
1.2       harris41    9: =pod
                     10: 
                     11: =head1 NAME
                     12: 
                     13: check-rpms - compare installed rpms with up-to-date distribution
                     14: 
                     15: =head1 DESCRIPTION
                     16: 
                     17: I<check-rpms> compares installed RPM packages (listed by the command
                     18: "rpm -qa") on a Linux system with an up-to-date distribution. That
                     19: distribution may either reside in a local directory (possibly NFS
                     20: mounted) or on a ftp server.  If the B<-ftp> option is specified,
                     21: I<check-rpms> retrieves directory listings from the I<ftpserver>'s
                     22: I<directory>/<arch> directories, where <arch> is set to noarch, i386,
                     23: i586, i686, and athlon consecutively. If I<ftpserver/directory> is
                     24: not specified, $FTPSERVER/$FTPUPDATES is used. The $FTPSERVER and
                     25: $FTPUPDATES variables can be set in the configuration file. If
                     26: either of the two is not set, the default server "updates.redhat.com"
                     27: and the default directory "$RHversion/en/os" is used,
                     28: where $RHversion is obtained from the /etc/redhat-release file. If
                     29: run with the B<-ftp> option, all rpm packages that need to be downloaded
                     30: (see the B<--download>, B<--recheck>, and B<--update> options) will
                     31: be downloaded into the directory specified by the B<-d> directory
                     32: option. If that option is omitted the $RPMDIR directory is used.
                     33: The $RPMDIR variable that can be set in the configuration file. If
                     34: $RPMDIR variable is not set either, the default directory
                     35: "/mnt/redhat/RedHat/RPMS" is used.
                     36: 
                     37: If the B<-ftp> is omitted, it is assumed that B<-d> I<directory> specifies
                     38: a local directory that contains up-to-date rpm packages. If B<-d>
                     39: I<directory> is omitted as well, the $RPMDIR directory is used. If
                     40: $RPMDIR is not set, the default directory "/mnt/redhat/Red-
                     41: Hat/RPMS" is used.
                     42: 
                     43: I<check-rpms> uses a lexical sort on the version string and the
                     44: release string of the package in order to decide whether the
                     45: installed package or the package form the distribution is newer.
                     46: I<check-rpms> lists packages of the distribution that are found to be
                     47: newer than the installed packages or, if B<--update> is specified,
                     48: will update the packages using the "rpm -Fvh <list of packages>"
                     49: command. In the latter case I<check-rpms> must be run as root. Fur-
                     50: thermore, the $RPMUSER variable should be set to a non-root user-
                     51: name (see the B<-c> option below). I<check-rpms> will switch to that
                     52: user and run most of the script under that user id.Only the
                     53: final "rpm -Fvh ..." command will be run as root. If $RPMUSER is
                     54: not set, the "nobody" user id will be used. It is recommended to
                     55: set $RPMUSER to an ordinary username (such as yourself). Further-
                     56: more, if a ftp server is used, create the download directory
                     57: (which is specified in the B<-d> directory option or in the $RPMDIR
                     58: variable), change the owner ship of that directory to that user,
                     59: and set the permissions to 700 before running I<check-rpms> with the
                     60: B<--update> option. Note, that B<--update> implies the B<--no-kernel>
                     61: option, i.e., I<check-rpms> refuses to update the kernel directly.
                     62: 
                     63: =cut
                     64: 
1.1       harris41   65: # ************ WARNING *****************************************************
                     66: # THIS PROGRAM IS PROVIDED "AS IS" WITHOUT
                     67: # WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLICIT.
                     68: # IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     69: # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     70: # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     71: # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     72: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     73: # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     74: # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     75: # SUCH DAMAGE.
                     76: # **************************************************************************
                     77: 
                     78: # check-rpms.pl is free software; you can redistribute it and/or modify
                     79: # it under the terms of the GNU General Public License as published by
                     80: # the Free Software Foundation; either version 2 of the License, or
                     81: # (at your option) any later version.
                     82: #
                     83: # check-rpms.pl is distributed in the hope that it will be useful,
                     84: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     85: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     86: # GNU General Public License for more details:
                     87: # http://www.gnu.org/licenses/gpl.html
                     88: 
                     89: use Getopt::Long;
                     90: 
                     91: my $retval = &GetOptions("verbose|v","lm|list-missing","lq|list-questionable",
                     92:                          "dir|d=s","ftp:s","noftp","download|dl","recheck|r",
                     93:                          "nk|no-kernel","update","c=s");
                     94: 
1.2       harris41   95: =pod
                     96: 
                     97: =head1 OPTIONS
                     98: 
                     99: =over 4
                    100: 
                    101: =item B<-v> B<--verbose>
                    102: 
                    103: verbose  mode:  prints  additional  progress information on
                    104: standard output
                    105: 
                    106: =item B<-ftp> [I<ftpserver/directory>]
                    107: 
                    108: compare the installed packages with the rpm packages found
                    109: on the ftp server I<ftpserver> in the directories I<directory>/<arch>,
                    110: where arch is set to noarch, i386, i586, i686,
                    111: and athlon consecutively. If I<ftpserver/directory> is not
                    112: specified, the $FTPSERVER and $FTPUPDATES variables are
                    113: checked. These variables can be set in the configuration
                    114: file (see the B<-c> option below). If those variables are not
                    115: set either, the default server "updates.redhat.com" and the
                    116: default directory "$RHversion/en/os" is used, where $RHversion
                    117: is obtained from the I</etc/redhat-release> file.
                    118: 
                    119: =item B<-noftp>
                    120: 
                    121: use  a  local  directory as the source for new rpm packages
                    122: even if the $FTP veriable is set to 1 in the  configuration
                    123: file.
                    124: 
                    125: =item B<-d> I<directory> B<--rpm-directory> I<directory>
                    126: 
                    127: if B<-ftp> is specified download all rpm packages that need to
                    128: be downloaded into I<directory>. If B<-ftp> is not specified,
                    129: regard the rpm packages found in I<directory> as an up-to-date
                    130: distribution against which the installed packages are
                    131: compared to.
                    132: 
                    133: =item B<-lm> B<--list-missing>
                    134: 
                    135: list installed packages that do not have an equivalent in
                    136: the up-to-date distribution. This will generate lots of
                    137: output when the comparison is made with the updates directory
                    138: of a ftp server.
                    139: 
                    140: =item B<-lq> B<--list-questionable>
                    141: 
                    142: list packages for which the lexical sort algorithm does not
                    143: give a conclusive result on whether the installed package
                    144: is older than the package in the distribution. These are
                    145: packages that have version and/or release strings that contain
                    146: letters. For example, it is not absolutely clear
                    147: whether the version 1.2.3b is actually newer or older than
                    148: 1.2.3. The lexical sort would classify 1.2.3b to be newer
                    149: than 1.2.3; with B<-lq> specified the package would be listed
                    150: in any case. See also B<--recheck> below.
                    151: 
                    152: =item B<-dl> B<--download>
                    153: 
                    154: download packages from the remote ftp server that are found
                    155: to be newer than installed packages into the directory that
                    156: is specified in the B<-d> I<directory> option or in the $RPMDIR
                    157: variable or, if neither of the two are specified, into
                    158: "/mnt/redhat/RedHat/RPMS". If the download directory does
                    159: not exist, I<check-rpms> will create it.
                    160: 
                    161: =item B<-r> B<--recheck>
                    162: 
                    163: Use the "rpm -Uvh --test --nodeps <package>" command to
                    164: check all packages that have letters in their version
                    165: and/or release string; B<--recheck> implies B<--list-questionable>
                    166: (see above). At the time of writing (Feb. 2002) there
                    167: is one known case for which the lexical sort algorithm
                    168: fails to detect a new package: mutt-1.2.5.1 was released to
                    169: replace mutt-1.2.5i, however, the lexical sort algorithm
                    170: incorrectly classifies mutt-1.2.5i to be  newer  than
                    171: mutt-1.2.5.1. In this case using the B<--recheck> option is
                    172: essential. In all other cases it is not. It is nevertheless
                    173: probably a good idea to use B<--recheck> at least once in a
                    174: while. B<--recheck> can increase the run-time of I<check-rpms>
                    175: substantially, particularly if a ftp server is used. In
                    176: that case the questionable packages must be downloaded from
                    177: the server into a directory I<directory> (as specified in the
                    178: -d option or the $RPMDIR variable) which will be created,
                    179: if it does not exist.
                    180: 
                    181: =item B<-nk> B<--no-kernel>
                    182: 
                    183: do not list kernel packages. That is, kernel, kernel-smp,
                    184: kernel-enterprise, kernel-BOOT, and kernel-debug will not
                    185: be checked and listed. However, kernel-headers and kernel-source
                    186: will be checked. The B<--update> option (see below)
                    187: implies B<--no-kernel>.
                    188: 
                    189: =item B<--update>
                    190: 
                    191: update all packages that were found to have newer versions.
                    192: For this to work I<check-rpms> must be run as root and a suitable
                    193: $RPMUSER must exist (see DESCRIPTION above). It is
                    194: strongly advisable to do a dry run B<check-rpms -v -lq> before
                    195: running B<check-rpms --update>.
                    196: 
                    197: =item B<-c> I<configurationfile>
                    198: 
                    199: The optional configuration file to use. This file can be
                    200: used to specify the $RPMDIR variable, the $FTP, $FTPSERVER,
                    201: and $FTPUPDATES, variables, and the $RPMUSER variable. An
                    202: example configuration file is given below. If the B<-c> option
                    203: is omitted, I<check-rpms> will use the default configuration
                    204: file I</usr/local/etc/check-rpms.conf>, if it exists.
                    205: 
                    206: =back
                    207: 
                    208: =head1 EXAMPLES
                    209: 
                    210: =over 4
                    211: 
                    212: =item check-rpms
                    213: 
                    214: will 1) check whether /usr/local/etc/check-rpms.conf exists; 2) if
                    215: it does it will read the variables specified in that file, if it
                    216: doesn't exist, $RPMDIR is set to /mnt/redhat/RedHat/RPMS; 3) if
                    217: $RPMDIR is set, this directory will be regarded as the source of
                    218: the up-to-date distribution, unless $FTP is set to 1. In that latter
                    219: case the $FTPSERVER and $FTPUPDATES are used, if those variables are
                    220: set. Otherwise "updates.redhat.com" and "<RHversion>/en/os"
                    221: will be used; 4) the installed packages are compared
                    222: 
                    223: =item check-rpms -v -lq -d /mnt/redhat/7.1/RedHat/RPMS
                    224: 
                    225: will use the distribution in the directory /mnt/redhat/7.1/RedHat/RPMS
                    226: for comparison with the installed packages. The command
                    227: will give more detailed information on its progress and will list
                    228: the packages that need upgrading and in another section it will
                    229: list packages they may need to be upgraded.
                    230: 
                    231: =item check-rpms -v -lq -ftp updates.redhat.com/7.1/en/os
                    232: 
                    233: same as above, but the directories 7.1/en/os/noarch,
                    234: 7.1/en/os/i386, 7.1/en/os/i586, 7.1/en/os/i686, and
                    235: 7.1/en/os/athlon on updates.redhat.com will be searched for new
                    236: packages.
                    237: 
                    238: =item check-rpms -v -r --updates
                    239: 
                    240: will use the default location for updated packages (determined as
                    241: indicated in the first example); if a ftp server is used, it will
                    242: download all newer and all packages with letters in the version
                    243: and/or release strings (i.e., "questionable" packages) from that
                    244: ftp server, recheck the questionable packages, and finally update
                    245: all packages that need to be updated.
                    246: 
                    247: =back
                    248: 
                    249: =cut
                    250: 
1.1       harris41  251: if ( $retval == 0 ) {
                    252:     usage();
                    253: }
                    254: 
                    255: # executables
                    256: $FTPLS = "ncftpls";
                    257: $FTPGET = "ncftpget";
                    258: $GREP = "grep";
                    259: 
                    260: # default values
                    261: $RHversion = (split /\s/, `cat /etc/redhat-release`)[4];
                    262: $DEFCONF = "/usr/local/etc/check-rpms.conf";
                    263: $DEFRPMDIR = "/mnt/redhat/RedHat/RPMS";
                    264: $DEFFTPSERVER = "updates.redhat.com";
                    265: $DEFFTPUPDATES = "$RHversion/en/os";
1.4     ! harris41  266: $DEFRPMUSER = "harris41";
1.1       harris41  267: 
                    268: $RPMDIR=$DEFRPMDIR;
                    269: 
                    270: # configuration
                    271: # the configuration file should set the $RPMDIR variable and/or $FTPSERVER,
                    272: # $FTPUPDATES and $DOWNLOADDIR variables, and the $RPMUSER variable.
                    273: if ($opt_c) {
                    274:    $CONF = $opt_c;
                    275: } else {
                    276:    $CONF = $DEFCONF;
                    277: }
                    278: 
1.2       harris41  279: =pod
                    280: 
                    281: =head1 The Configuration File
                    282: 
                    283: All variables must be defined using perl syntax, i.e., in the form
                    284: 
                    285: $variable = value;
                    286: 
                    287: (do not forget the semicolon at the  end  of  a  line).   Comments
                    288: start with "#" and blank lines may be included as well.
                    289: 
                    290: Example configuration file:
                    291: 
                    292:  # check-rpms configuration file
                    293: 
                    294:  # $RPMDIR is the directory where up-to-date RPMs can be found and/or
                    295:  # rpm packages are downloaded into.
                    296:  $RPMDIR = "/mnt/redhat/RedHat/RPMS";
                    297: 
                    298:  # $RPMUSER is the user name that check-rpms switches to for most of
                    299:  # the script when run as root
1.4     ! harris41  300:  $RPMUSER = "harris41";
1.2       harris41  301: 
                    302:  # $FTPSERVER and $FTPUPDATES are the hostname of a ftp server and the
                    303:  # directory where RPM updates can be found without the <arch> directory.
                    304:  # I.e., $FTPUPDATES should be set to something like pub/7.2, if the RPMs
                    305:  # are located in pub/7.2/i386, pub/7.2/i686, etc.
                    306:  # $FTPSERVER and $FTPUPDATES are used if -ftp is specified or if the following
                    307:  # line is uncommented.
                    308:  # $FTP = 1;
                    309:  $FTPSERVER = "updates.redhat.com";
                    310:  $FTPUPDATES = "7.2/en/os";
                    311: 
                    312: =cut
                    313: 
1.1       harris41  314: if ( -f $CONF) {
                    315:    require($CONF);
                    316: } else {
                    317:    $FTPSERVER = $DEFFTPSERVER;
                    318:    $FTPUPDATES = $DEFFTPUPDATES;
                    319: }
                    320: 
                    321: # check whether we are running as root
                    322: if ($< == 0){
                    323:    if (! $RPMUSER) {
                    324:       $RPMUSER = $DEFRPMUSER;
                    325:    }
                    326:    $RPMUID = getpwnam($RPMUSER);
                    327:    if (! $RPMUID) {
                    328:       die "You do not seem to have a $RPMUSER user on your system.\nSet the \$RPMUSER variable in the $CONF configuration file to a non-root user.\n";
                    329:    }
                    330:    if ($RPMUID == 0) {
                    331:       die "You must set the \$RPMUSER variable in $CONF to a non-root user.\n";
                    332:    }
                    333:    # switch to $RPMUID
                    334:    $> = $RPMUID;
                    335:    if ($> != $RPMUID) { die "switching to $RPMUID uid failed.\n" }
                    336: }
                    337: 
                    338: # command-line arguments
                    339: $verbose         = $opt_verbose;
                    340: $list_missing    = $opt_lm;
                    341: $questionable    = $opt_lq;
                    342: $no_kernel       = $opt_nk;
                    343: $download        = $opt_download;
                    344: $recheck         = $opt_recheck;
                    345: $update          = $opt_update;
                    346: 
                    347: if (defined $opt_update && $< != 0) {
                    348:     die "You must be root in order to update rpms.\n";
                    349: }
                    350: 
                    351: if ( defined $opt_dir ){
                    352:    $RPMDIR = $opt_dir;
                    353: }
                    354: 
                    355: if (defined $opt_ftp && defined $opt_noftp) {
                    356:    die "Setting -ftp and -noftp does not make sense, does it?\n";
                    357: }
                    358: 
                    359: if (defined $opt_noftp) { $FTP = 0; }
                    360: 
                    361: if (defined $opt_ftp || $FTP) {
                    362:    $ftp = 1;
                    363:    if ( $opt_ftp ) {
                    364:       $_ = $opt_ftp;
                    365:       ($FTPSERVER, $FTPUPDATES) = m/^([^\/]+)\/(.*)$/;
                    366:    } elsif ( ! ($FTPSERVER && $FTPUPDATES)) {
                    367:       $FTPSERVER = $DEFFTPSERVER;
                    368:       $FTPUPDATES = $DEFFTPUPDATES;
                    369:    }
                    370: 
                    371:    if (defined $opt_update){
                    372:       $download=1;
                    373:    }
                    374: 
                    375:    if ($download || $recheck) {
                    376:        if ( ! -d $RPMDIR) {
1.3       harris41  377:           if ($verbose) { print "Creating $RPMDIR ...\n"; }
                    378:           if ($< == 0) {
                    379:              $retval = system("su $RPMUSER -c \'mkdir -p $RPMDIR\'; chmod 700 $RPMDIR");
                    380:           } else {
                    381:              $retval = system("mkdir -p $RPMDIR; chmod 700 $RPMDIR");
                    382:           }
1.1       harris41  383:           if ($retval) { die "error: could not create $RPMDIR\n"; }
                    384:       }
                    385:    }
                    386: } elsif ( (! -d $RPMDIR) || system("ls $RPMDIR/*.rpm > /dev/null 2>&1")) {
                    387:    die "Either $RPMDIR does not exist or it does not contain any packages.\n";
                    388: }
                    389: 
                    390: if ($recheck) {
                    391:    $questionable=1;
                    392: }
                    393: 
                    394: if (defined $opt_update || defined $opt_nk) {
                    395:     $no_kernel=1;
                    396: }
                    397: 
1.4     ! harris41  398: $PROC = `grep -i "athlon\|amd" /proc/cpuinfo`;
1.1       harris41  399: if ( ! "$PROC" ) {
                    400:     $PROC = `uname -m`;
                    401:     chomp($PROC);
                    402: } else {
                    403:     $PROC = "athlon";
                    404: }
                    405: 
                    406: @ARCHITECTURES = ("noarch", "i386", "i586", "i686");
                    407: if ( $RHversion > 7.0 ){ 
                    408:     push(@ARCHITECTURES, "athlon");
                    409: }
                    410: 
                    411: # get the local list of installed packages
                    412: 
                    413: if ($verbose) {
                    414:    print "updates for $PROC processor, RH $RHversion\n";
                    415:    print "Getting list of installed packages\n";
                    416: }
                    417: 
                    418: if ($< == 0) {
                    419:    @local_rpm_list = `su $RPMUSER -c 'rpm -qa'`;
                    420: } else {
                    421:    @local_rpm_list = `rpm -qa`;
                    422: }
                    423: chop(@local_rpm_list);
                    424: 
                    425: %local_rpm = %remote_rpm = ();
                    426: 
                    427: for (@local_rpm_list) {
                    428: #    good place to test the regular expressions...
                    429: #    ($pkg, $ver, $release) = m/^(.*)-([^-]*)-([^-]+)/;
                    430: #    print "$_\t->$pkg, $ver, $release\n";
                    431: 
                    432:     my ($pkg, $pver) = m/([^ ]*)-([^-]+-[^-]+)/;
                    433:     $local_rpm{$pkg} = $pver;
                    434: }
                    435: 
                    436: # now connect to the remote host
                    437: 
                    438: my @templist;
                    439: if ($ftp) {
                    440:    if ( `rpm -q ncftp --pipe "grep 'not installed'"` ) {
                    441:       die "you must have the ncftp package installed in order to use a\n",
                    442:           "ftp server with check-rpms.\n";
                    443:    }
                    444:    $SOURCE = $FTPSERVER;
                    445:    for (@ARCHITECTURES) {
                    446:       my $FTPDIR = "$FTPUPDATES/$_";
                    447:       if ($verbose) {
                    448:          print ("Getting package lists from $FTPSERVER/$FTPDIR ...\n");
                    449:       }
                    450:       push(@templist, grep(/\.rpm$/, `$FTPLS -x "-1a" "ftp://$FTPSERVER/$FTPDIR/"`));
                    451:       if ($?) { print STDERR "$FTPLS failed with status ",$?/256,".\n"; }
                    452:    }
                    453: } else {
                    454:    $SOURCE = $RPMDIR;
                    455:    if ($verbose) {
                    456:        print ("Getting package lists from $RPMDIR ...\n");
                    457:    }
                    458:    @templist = grep(/\.rpm$/, `(cd $RPMDIR;ls -1)`);
                    459: }
                    460: 
                    461: #
                    462: # If two versions of the same RPM appear with different architectures
                    463: # and/or different versions, the right one must be found.
                    464: #
                    465: 
                    466: $giveup = 0;
                    467: for (@templist) {
                    468:    ($rpm, $pkg, $pver, $arch) = m/(([^ ]*)-([^- ]+-[^-]+\.(\w+)\.rpm))/;
1.3       harris41  469:    if (! defined $local_rpm{$pkg}) { next; }
1.1       harris41  470:    if ($remote_rpm{$pkg}) {
                    471:       # problem: there are several versions of the same package.
                    472:       # this means that the package exists for different architectures
                    473:       # (e.g., kernel, glibc, etc.) and/or that the remote server
                    474:       # has several versions of the same package in which case the
                    475:       # latest version must be picked.
                    476:       my ($pkg1) = ($remote_rpm{$pkg} =~ m/([^-]+-[^-]+)\.\w+.rpm/);
                    477:       my ($pkg2) = ($pver =~ m/([^-]+-[^-]+)\.\w+.rpm/);
                    478:       my ($vcmp, $qflag) = cmp_versions($pkg1, $pkg2);
                    479:       if ($qflag && $questionable) {
                    480:          # cannot decide which of the two is newer - what should we do?
                    481:          # print a warning that lists the two rpms.
                    482:          # If running with --update, both packages must be rechecked with 
                    483:          # rpm -qp --queryformat '%{SERIAL}' <pkg>
                    484:          if ($recheck || $update) {
                    485:             my $decision = pkg_compare("$pkg-$remote_rpm{$pkg}",$rpm, $vcmp);
                    486:             if ($decision < 0) {
                    487:                # an error in the ftp download routine accured: giveup
                    488:                $remote_rpm{$pkg} = undef;
                    489:                $giveup = 1;
                    490:             } elsif ($decision > 0) {
                    491:                # second package is newer
                    492:                $remote_rpm{$pkg} = $pver;
                    493:             }
                    494:             next;
                    495:          } else {
                    496:             mulpkg_msg("$pkg-$remote_rpm{$pkg}", $rpm, $vcmp);
                    497:             print "** check whether this is correct or rerun with --recheck option.\n";
                    498:             if ($vcmp < 0) {
                    499:                $remote_rpm{$pkg} = $pver;
                    500:             }
                    501:          }
                    502:       }
                    503:       if ($vcmp == 0) {        
                    504:          # versions are equal: must be different architecture
                    505:          # procedure to select the correct architecture:
                    506:          # if $PROC = athlon: if available use $arch = athlon (exist for
                    507:          # RH 7.1 or newer) otherwise use i686
                    508:          # if $PROC = ix86: choose pkg with $PROC cmp $arch >= 0 and
                    509:          # $arch cmp $prev_arch = 1
                    510:          $_ = $remote_rpm{$pkg};
                    511:          ($prev_arch) =  m/.*\.(\w+)\.rpm$/;
                    512:          if (cmp_arch($arch,$prev_arch)) { $remote_rpm{$pkg} = $pver };
                    513:       } elsif ($vcmp < 0) {    # second rpm is newer
                    514:          $remote_rpm{$pkg} = $pver;
                    515:       }
                    516:    } else {
                    517:       $remote_rpm{$pkg} = $pver;
                    518:    }
                    519: }
                    520: 
                    521: if ($giveup && defined $opt_update) {
                    522:    die "Multiple versions of the same package were found on the server.\n",
                    523:        "However, due to ftp download problems it could not be verified\n",
                    524:        "which of the packages are the most recent ones.\n",
                    525:        "If the choices specified above appear to be correct, rerun check-rpms\n",
                    526:        "without the -lq (or --list-questionable) option. Otherwise, fix the download\n",
                    527:        "problems or install those packages separately first.\n";
                    528: }
                    529: 
                    530: #
                    531: # check for UPDated and DIFferent packages...
                    532: #
                    533: 
                    534: for (@local_rpm_list) {
                    535:     my ($pkg,  $version) = m/^([^ ]*)-([^-]+-[^-]+)$/;
                    536:     if (! $pkg) { print "Couldn't parse $_\n"; next; }
                    537:     if ($no_kernel) {
                    538:        if ($pkg eq 'kernel' || $pkg eq 'kernel-smp'
                    539:            || $pkg eq 'kernel-enterprise' || $pkg eq 'kernel-BOOT'
                    540:            || $pkg eq 'kernel-debug') { next; }
                    541:     }
                    542:     if (defined $remote_rpm{$pkg}) { 
                    543:         # this package has an update
                    544: 	my ($rversion) = ($remote_rpm{$pkg} =~ m/([^-]+-[^-]+)\.\w+.rpm/);
                    545: 	my $rpm = ($pkg . '-' . $remote_rpm{$pkg});
                    546: 	my ($vcmp,$qflag) = cmp_versions($version, $rversion);
                    547: 	if ( $qflag && $questionable ) {
                    548:             # at least one of the version strings contains letters
                    549:             push(@q_updates, $rpm);
                    550:         } elsif ( $vcmp < 0 ) {
                    551:             # local version is lower
1.3       harris41  552:             push(@updates, $rpm);
1.1       harris41  553: 	}
                    554:     } elsif ($list_missing) {
                    555: 	print "Package '$pkg' missing from remote repository\n";
                    556:     }
                    557: }
                    558: 
                    559: if ($recheck && @q_updates) {
                    560:    if ($ftp) {    
                    561:       for (@q_updates) {
                    562:          ($arch) = m/[^ ]*-[^- ]+-[^-]+\.(\w+)\.rpm/;
                    563:          push(@ftp_files, "$FTPUPDATES/$arch/$_");
                    564:       }
                    565:       if ($verbose) {
                    566:          print "Getting questionable packages form $FTPSERVER ...\n";
                    567:       }
                    568:       my $status = system("$FTPGET $FTPSERVER $RPMDIR @ftp_files");
                    569:       if ($status) {
                    570:          if ($< == 0) {
                    571:             # if we are running as root exit to avoid symlink attacks, etc.
                    572:             die "$FTPGET failed with status ", $status/256, ".\n";
                    573: 	 } else {
                    574:             print STDERR "warning: $FTPGET failed with status ", $status/256, ".\n";
                    575:          } 
                    576:       }
                    577:    }
                    578:    for (@q_updates) {
                    579:       if ($verbose) {print "** rechecking $_ ... ";}
                    580:       my $errmsg = `rpm -Uvh --test --nodeps --pipe 'grep -v ^Preparing' $RPMDIR/$_ 2>&1`;
                    581:       if (! $errmsg) {
                    582:          # no error message, i.e., the rpm is needed.
                    583:           push(@updates,$_);
                    584:           if ($verbose) {print "needed!\n";}
                    585:       } elsif ($verbose) {
                    586:           print "not needed:\n$errmsg\n";
                    587:       }
                    588:    }
                    589:    @q_updates=();
                    590: }
                    591:        
                    592: #
                    593: # print list of new files and download ...
                    594: #
                    595: 
                    596: @updates = sort @updates;
                    597: if (@updates) {
                    598:     if ($verbose) {
                    599:        print "\nRPM files to be updated:\n\n";
                    600:     }
                    601:     for (@updates) {
                    602:        print "$_\n";
                    603:     }
                    604:     if ($download) {
                    605:        @ftp_files=();
                    606:        for (@updates) {
                    607:           ($arch) = m/[^ ]*-[^- ]+-[^-]+\.(\w+)\.rpm/;
                    608:           push(@ftp_files, "$FTPUPDATES/$arch/$_");
                    609:        }
                    610:        if ($verbose) {
                    611:           print "starting downloads ... \n";
                    612:        }
                    613:        my $status = system("$FTPGET $FTPSERVER $RPMDIR @ftp_files");
                    614:        if ($status) {
                    615:           if ($< == 0) {
                    616:              # if we are running as root exit to avoid symlink attacks, etc.
                    617:              die "$FTPGET failed with status ", $status/256, ".\n";
                    618: 	  } else {
                    619:              print STDERR "warning: $FTPGET failed with status ", $status/256, ".\n";
                    620:           } 
                    621:        } elsif ($verbose) {
                    622:           print "... done.\n";
                    623:        }
                    624:     }           
                    625: }
                    626: 
                    627: @q_updates = sort @q_updates;
                    628: if (@q_updates && $questionable) {
                    629:     if ($verbose) {
                    630:        print "\nRPM files that may need to be updated:\n\n";
                    631:        for (@q_updates) {
                    632:           my ($old) = m/^([^ ]*)-[^-]+-[^-]+\.\w+\.rpm$/;
                    633:           $old = `rpm -q $old`;
                    634:           chomp($old);
                    635:           print "upgrade ", $old, " to ", $_, " ?\n";
                    636:        }
                    637:     } else {
                    638:        for (@q_updates) {
                    639:           print "$_\n";
                    640:        }
                    641:     }
                    642:     if ($download) {
                    643:        @ftp_files=();
                    644:        for (@updates) {
                    645:           ($arch) = m/[^ ]*-[^- ]+-[^-]+\.(\w+)\.rpm/;
                    646:           push(@ftp_files, $FTPUPDATES/$arch/$_);
                    647:        }
                    648:        if ($verbose) {
                    649:           print "starting downloads ... \n";
                    650:           system("$FTPGET $FTPSERVER $$RPMDIR @ftp_files");
                    651:           print "... done.\n";
                    652:        } else {
                    653:           system("$FTPGET $FTPSERVER $$RPMDIR @ftp_files");
                    654:        }
                    655:     }           
                    656: }
                    657: 
                    658: if ($verbose && !(@updates || @q_updates)) {
                    659:     print "No new updates are available in $SOURCE\n";
                    660: }
                    661: 
                    662: if ($opt_update) {
                    663:     if (@q_updates){
                    664:        push(@updates,@q_updates);
                    665:     }
                    666:     if (@updates) {
                    667:        if ($verbose) {
                    668:           print "Running rpm -Fvh ...\n";
                    669:        }
                    670:        # switch to UID=0
                    671:        $> = $<;
                    672:        system("(cd $RPMDIR;rpm -Fvh @updates)");
                    673:    }
                    674: }
                    675: 
                    676: # download routine
                    677: sub ftp_download {
                    678:    my ($FTPSERVER, $FTPDIR, $downloaddir, @packages) = @_;
                    679:    my @ftp_packages=();
                    680:    for (@packages) {
                    681:        my ($arch) = m/[^ ]*-[^-]+-[^-]*\.(\w+)\.rpm$/;
                    682:        push(@ftp_packages,"$FTPDIR/$arch/$_");
                    683:     }
                    684:     my $status = system("$FTPGET $FTPSERVER $downloaddir @ftp_packages");
                    685:     return $status;
                    686: }
                    687: 
                    688: sub pkg_compare($$$) {
                    689:    my ($pkg1, $pkg2, $cmp) = @_;
                    690:    if (defined $opt_ftp) {
                    691:       if ($verbose) {
                    692:          my ($pkg) = ($pkg1 =~ /([^ ]*)-[^-]+-[^-]+\.\w+\.rpm/);
                    693:          print "The ftp server provides multiple versions of the $pkg package.\n",
                    694:                "Downloading $pkg1 and $pkg2 in order to find out which is newer.\n";
                    695:       }
                    696:       my $status = ftp_download($FTPSERVER, $FTPUPDATES, $RPMDIR, ($pkg1, $pkg2));
                    697:       if ($status) {
                    698:          # at this point just give up ...
                    699:          print STDERR "** $FTPGET failed with status ", $status/256, ".\n";
                    700:          mulpkg_msg($pkg1, $pkg2, $cmp);
                    701:          return -1;
                    702:       }
                    703:    }
                    704:    my $serial1 = `rpm -qp --queryformat '%{SERIAL}' $RPMDIR/$pkg1`;
                    705:    my $serial2 = `rpm -qp --queryformat '%{SERIAL}' $RPMDIR/$pkg2`;
1.3       harris41  706:    if ($serial2 > $serial1) {
                    707:       remove_pkg("$RPMDIR/$pkg1");
                    708:       return 1;
                    709:    } else {
                    710:       remove_pkg("$RPMDIR/$pkg2");
                    711:       return 0;
                    712:    }
                    713: }
                    714: 
                    715: sub remove_pkg($) {
                    716:    my ($pkg) = @_;
                    717:    if ($verbose) {
                    718:       print "Removing $pkg ...\n";
                    719:    }
                    720:    my $status = system("rm -f $pkg");
                    721:    if ($status) {
                    722:       printf STDERR "error: could not remove $pkg. You must remove this file before updating.\n";
                    723:       if ($update) { $giveup = 1; }
                    724:    }
1.1       harris41  725: }
                    726: 
                    727: sub mulpkg_msg($$$) {
                    728:    my ($pkg1, $pkg2, $cmp) = @_;
                    729:    print "** The server provides two versions of the same package:\n",
                    730:          "** $pkg1 and $pkg2.\n";
                    731:    if ($cmp > 0) {
                    732:        print "** It appears that $pkg-$remote_rpm{$pkg} is newer.\n"
                    733:    } else {
                    734:        print "** It appears that $pkg-$pver is newer.\n";
                    735:    }
                    736: }
                    737: 
                    738: #############################################################################
                    739: #
                    740: # Version comparison utilities
                    741: #
                    742: 
                    743: sub hack_version($) {
                    744:     my ($pver) = @_;
                    745:     $pver =~ s/(\d+)/sprintf("%08d", $1)/eg; # pad numbers with leading zeros to make alphabetical sort do the right thing
                    746:     $pver =  (sprintf "%-80s", $pver);	     # pad with spaces so that "3.2.1" is greater than "3.2"
                    747:     return $pver;
                    748: }
                    749: 
                    750: sub cmp_versions($$) {
                    751:     my ($pkg1, $pkg2) = @_;
                    752: 
                    753:     # shortcut if they're obviously the same.
                    754:     return (0,0) if ($pkg1 eq $pkg2);
                    755: 
                    756:     # split into version and release
                    757:     my ($ver1, $rel1) = ($pkg1 =~ m/([^-]+)-([^-]+)/);
                    758:     my ($ver2, $rel2) = ($pkg2 =~ m/([^-]+)-([^-]+)/);
                    759: 
                    760:     if ($ver1 ne $ver2) {
                    761:        my $qflag = ((grep /[A-z]/, $ver1) || (grep /[A-z]/, $ver2));
                    762:        $ver1 = hack_version($ver1);
                    763:        $ver2 = hack_version($ver2);
                    764:        return ($ver1 cmp $ver2, $qflag);
                    765:     } else {
                    766:        my $qflag = ((grep /[A-z]/, $rel1) || (grep /[A-z]/, $rel2));
                    767:        $rel1 = hack_version($rel1);
                    768:        $rel2 = hack_version($rel2);
                    769:        return ($rel1 cmp $rel2, $qflag);
                    770:     }
                    771: }
                    772: 
                    773: sub cmp_arch($$) {
                    774:     my ($arch1, $arch2) = @_;
                    775:     my $retval = 0;
                    776:     $archcmp = ($arch1 cmp $arch2) > 0;
                    777:     if ( "$PROC" eq "athlon" ) {
                    778:        if ( "$arch2" ne "athlon" 
                    779:               && ( "$arch1" eq "athlon" || $archcmp )){
                    780: 	   $retval = 1;
                    781:        }
                    782:     } elsif ( $archcmp && ($PROC cmp $arch1) >= 0 ) {
                    783:        $retval = 1;
                    784:     }
                    785:     return $retval;
                    786: }
                    787: 
                    788: # @tests = ('3.2', '3.2',
                    789: #           '3.2a', '3.2a',
                    790: #           '3.2', '3.2a',
                    791: #           '3.2', '3.3',
                    792: #           '3.2', '3.2.1',
                    793: #           '1.2.5i', '1.2.5.1',
                    794: #           '1.6.3p6', '1.6.4');
                    795: # 
                    796: # while (@tests) {
                    797: #     $a = shift(@tests);
                    798: #     $b = shift(@tests);
                    799: #     printf "%-10s < %-10s = %d\n", $a, $b, cmp_versions($a, $b);
                    800: # }
                    801: #
                    802: # And the correct output is...
                    803: #
                    804: #     3.2        < 3.2        = 0
                    805: #     3.2a       < 3.2a       = 0
                    806: #     3.2        < 3.2a       = -1
                    807: #     3.2        < 3.3        = -1
                    808: #     3.2        < 3.2.1      = -1
                    809: #     1.2.5i     < 1.2.5.1    = -1
                    810: #     1.6.3p6    < 1.6.4      = -1
                    811: #
                    812: # the lexical sort does not give the correct result in the second to last case.
                    813: 
                    814: 
                    815: sub usage(){
                    816:    die "usage: check-rpms [-v | --verbose]  [-d directory | --dir directory]\n",
                    817:        "                  [-ftp [server/directory]] [-noftp] [-lm | --list-missing]\n",
                    818:        "                  [-lq | --list-questionable] [-r | --recheck ]\n",
                    819:        "                  [-nk | --no-kernel] [--update] [-c configurationfile]\n";
                    820: }
1.2       harris41  821: 
                    822: =pod
                    823: 
                    824: =head1 SEE ALSO
                    825: 
                    826: rpm(8), ncftpls(1), ncftpget(1)
                    827: 
                    828: =head1 AUTHOR
                    829: 
                    830: Martin Siegert, Simon Fraser University, siegert@sfu.ca
                    831: 
                    832: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>