Ticket #329: trac_329.patch

File trac_329.patch, 4.4 KB (added by ddrake, 10 years ago)

apply to sage_scripts repo

  • new file sage-add-integrity-check-to-spkg

    # HG changeset patch
    # User Dan Drake <drake@kaist.edu>
    # Date 1268203512 -32400
    # Node ID 9213e8ab0c593d41273e7ac995b38bbb59105e1d
    # Parent  ff4bc22e7ae0f23e36e1355da78e3307cc27f75d
    integrity verification for spkgs; see trac #329
    
    diff --git a/sage-add-integrity-check-to-spkg b/sage-add-integrity-check-to-spkg
    new file mode 100755
    - +  
     1#!/usr/bin/env bash
     2
     3# Given a spkg file as $1, this adds the necessary checksum information.
     4# See http://trac.sagemath.org/sage_trac/ticket/329.
     5
     6SPKGNAME=$(basename $1 .spkg)
     7
     8if [ "x$1" = "x$SPKGNAME" ]
     9then
     10    echo "I don't think you gave me a spkg file."
     11    exit 1
     12fi
     13
     14echo "Adding integrity verification checksum to $1..."
     15
     16# is the spkg bzipped?
     17TYPE=$(file $1 | grep 'bzip2 compressed data')
     18if [ -z "$TYPE" ]
     19then
     20    CAT="cat"
     21    UNCAT="cat"
     22else
     23    CAT="bunzip2 -c"
     24    UNCAT="bzip2 -c"
     25fi
     26
     27$CAT $1 > $SPKGNAME.tar
     28
     29{ tar xf $SPKGNAME.tar --exclude $SPKGNAME.cksum -O; \
     30tar tf $SPKGNAME.tar --exclude $SPKGNAME.cksum; } | \
     31cksum | awk '{print $1, $2}' > $SPKGNAME.cksum
     32
     33echo "checksum of $1 is `cat $SPKGNAME.cksum`."
     34
     35tar rf $SPKGNAME.tar $SPKGNAME.cksum
     36
     37$UNCAT $SPKGNAME.tar > $1
     38rm $SPKGNAME.tar $SPKGNAME.cksum
  • sage-spkg

    diff --git a/sage-spkg b/sage-spkg
    a b  
    216216    cd "$CUR"
    217217fi
    218218
    219 # * The -i option below to ignore checksum errors, since
    220 #   I've had problems with this on Solaris.
    221 # * The m option avoids clock skew problems.   
     219sage-spkg-integrity-check $PKG_SRC
     220STATUS=$?
     221if [ $STATUS = 1 ]
     222then
     223    echo $0: integrity check of $PKG_SRC failed, exiting.
     224    exit 1
     225elif [ $STATUS = 2 ]
     226then
     227    echo no integrity checksum found for $PKG_SRC, continuing.
     228elif [ $STATUS = 3 ]
     229then
     230    echo $0: something strange happened while checking integrity of $PKG_SRC, exiting.
     231    exit 1
     232fi
     233# if we get here, we assume the spkg is okay.
    222234
    223235echo "Extracting package $PKG_SRC ..."
    224236ls -l "$PKG_SRC"
  • new file sage-spkg-integrity-check

    diff --git a/sage-spkg-integrity-check b/sage-spkg-integrity-check
    new file mode 100755
    - +  
     1#!/usr/bin/env bash
     2
     3# Given a spkg file as $1, this script will check the integrity of $1
     4# using the methods descrbed in
     5# http://trac.sagemath.org/sage_trac/ticket/329.
     6#
     7# This script exits with status...
     8#     0 if the verification passed;
     9#     1 if the checksum exits but verification failed;
     10#     2 if no cksum information was found or tar somehow had an error
     11#       extracting it;
     12#     3 if something else goes wrong.
     13
     14if [ -z "$1" ]
     15then
     16    echo "Error in $0: you need to specify a .spkg file."
     17    exit 3
     18fi
     19
     20if [ -z "$SAGE_ROOT" ]
     21then
     22    echo "Error in $0: \$SAGE_ROOT must be set."
     23    exit 3
     24fi
     25
     26echo -n "Verifying integrity of $1..."
     27
     28SPKGNAME=$(basename $1 .spkg)
     29
     30# is the spkg bzipped?
     31TYPE=$(file $1 | grep 'bzip2 compressed data')
     32if [ -z "$TYPE" ]
     33then
     34    J=""
     35else
     36    # we're assuming that "j" is the tar option for bzip2 compression
     37    J="j"
     38fi
     39
     40# extract existing checksum
     41tar ${J}xf $1 -O $SPKGNAME.cksum | awk 'NR==1 {print $1, $2}' > "$SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum"
     42
     43# the "NR==1" tells awk to only print line 1; if a checksum gets put
     44# into the spkg twice, the -O in tar will print it out twice. We just
     45# take the first line.
     46
     47# use the bash PIPESTATUS array to check exit code of tar (the usual $?
     48# returns the exit code of the final process in the pipe)
     49if [ ${PIPESTATUS[0]} != "0" ]
     50then
     51    # GNU tar gives different exit codes for "file not found" and other
     52    # errors, but BSD tar just returns 1, so we can't quite tell the
     53    # difference between no cksum file and some other error with
     54    # extraction
     55    echo "No integrity checksum stored in $1, skipping integrity check."
     56    exit 2
     57fi
     58
     59# now compute the checksum
     60{ tar ${J}xf $1 -O --exclude $SPKGNAME.cksum ; \
     61tar ${J}tf $1 --exclude $SPKGNAME.cksum 2>/dev/null; } | \
     62cksum | awk '{print $1, $2}' > "$SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum"
     63
     64cmp "$SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum" "$SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum" >/dev/null
     65if [ $? = "0" ]
     66then
     67    echo passed.
     68else
     69    echo FAILED!
     70    echo "$1 appears corrupted; expected checksum is \"$(cat "$SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum")\", but computed checksum is \"$(cat "$SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum")\"."
     71    exit 1
     72fi