Ticket #329: trac_329_sage_scripts.patch

File trac_329_sage_scripts.patch, 5.1 KB (added by ddrake, 8 years ago)

apply only this patch to the scripts repo.

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

    # HG changeset patch
    # User Dan Drake <drake@kaist.edu>
    # Date 1268203512 -32400
    # Node ID c76d11fa22ffac17818998751b4a7ee47521d619
    # Parent  26abf552ceaa2a69e7cf50ad4826a64406284cf2
    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  
    239239    cd "$CUR"
    240240fi
    241241
    242 # * The -i option below to ignore checksum errors, since
    243 #   I've had problems with this on Solaris.
    244 # * The m option avoids clock skew problems.   
     242sage-spkg-integrity-check $PKG_SRC
     243STATUS=$?
     244if [ $STATUS = 1 ]
     245then
     246    echo $0: integrity check of $PKG_SRC failed, exiting.
     247    exit 1
     248elif [ $STATUS = 2 ]
     249then
     250    echo No integrity checksum found for $PKG_SRC, continuing.
     251elif [ $STATUS = 3 ]
     252then
     253    echo $0: something strange happened while checking integrity of $PKG_SRC, exiting.
     254    exit 1
     255fi
     256# if we get here, we assume the spkg is okay.
    245257
    246258echo "Extracting package $PKG_SRC ..."
    247259ls -l "$PKG_SRC"
     
    407419            cd "$SAGE_PACKAGES/build/"
    408420            rm -rf "$SAGE_PACKAGES/build/$PKG_NAME"
    409421        fi
     422        rm -f "$SAGE_PACKAGES/build/$PKG_NAME.cksum"
    410423    else
    411424        echo "You can safely delete the temporary build directory"
    412425        echo "$SAGE_PACKAGES/build/$PKG_NAME"
     426        if [ -f "$SAGE_PACKAGES/build/$PKG_NAME.cksum" ]; then
     427            echo "and the checksum file"
     428            echo "$SAGE_PACKAGES/build/$PKG_NAME.cksum"
     429        fi
    413430    fi
    414431
    415432else
  • 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 "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
     40if [ ! -d $SAGE_ROOT/spkg/cksum ]
     41then
     42    mkdir $SAGE_ROOT/spkg/cksum
     43    if [ $? != 0 ]
     44    then
     45        echo Something went wrong trying to create the \$SAGE_ROOT/cksum directory.
     46        echo $0 exiting.
     47        exit 3
     48    fi
     49fi
     50
     51# extract existing checksum
     52$SAGE_ROOT/spkg/pipestatus "tar ${J}xf $1 -O $SPKGNAME.cksum" "awk 'NR==1 {print $1, $2}'" > "$SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum"
     53
     54# the "NR==1" tells awk to only print line 1; if a checksum gets put
     55# into the spkg twice, the -O in tar will print it out twice. We just
     56# take the first line.
     57
     58# use our pipestatus script to check exit code of tar (the usual $?
     59# returns the exit code of the final process in the pipe)
     60if [ $? != "0" ]
     61then
     62    # GNU tar gives different exit codes for "file not found" and other
     63    # errors, but BSD tar just returns 1, so we can't quite tell the
     64    # difference between no cksum file and some other error with
     65    # extraction
     66    echo "No integrity checksum stored in $1, skipping integrity check."
     67    exit 2
     68fi
     69
     70# now compute the checksum
     71{ tar ${J}xf $1 -O --exclude $SPKGNAME.cksum ; \
     72tar ${J}tf $1 --exclude $SPKGNAME.cksum 2>/dev/null; } | \
     73cksum | awk '{print $1, $2}' > "$SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum"
     74
     75cmp "$SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum" "$SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum" >/dev/null
     76if [ $? = "0" ]
     77then
     78    echo Integrity check for $1 passed.
     79else
     80    echo Integrity check for $1 FAILED!
     81    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")\"."
     82    exit 1
     83fi