WTF did I change?

So, it’s been a while since I posted, I thought I’d share my latest heap of hacky shell up here.

Last week we were having a discussion following on from Chris Siebenmann’s post on restoring files from a package, to see what changes had been made. Anyway, we decided that such a feature could be useful, so I promised to hack one together.

It took a week to finish this, because I spent most of my time confused about how apt-get couldn’t just download package without being root. I now know that it defiantly can’t do this. I also know that aptitude can.

#!/bin/bash
#
# Check we have an argument
#
if [ -z "$1" ]; then
    echo "Usage $0 /path/to/file"
exit 1
fi
#
# Check this is actually a text file, if
# not we drop out straight away
#
if [ `file $1|grep -c text ` -lt 1 ]; then
    echo "The file $1 is not a text file"
    exit 2
fi
#
# Work out the package name
#
PACKAGE=`dpkg -S $1| awk -F: '{print $1}'`
#
# If there's no matching package then
# just exit with an error
#
if [ $? -gt 0 ]; then
    echo "No package provides this file"
    exit 3
fi
#
# Download the package
#
echo "File $1 is provided by $PACKAGE. Downloading to /tmp/$$"
mkdir /tmp/$$
cd /tmp/$$
aptitude download $PACKAGE > /dev/null
#
# Extract the package
#
PKGNAME=`find /tmp/$$ -maxdepth 1 -type f -name "*.deb"`
dpkg -x $PKGNAME /tmp/$$
#
# Diff shit
#
diff -us /tmp/$$/$1 $1
#
# If there's a difference then offer to save the diff
#
if [ `diff -qs $1 /tmp/$$/$1|grep -c identical` -ne 1 ]; then
    echo -n "Save this diff? [yes] "
    ANSWER=yes
    read answer
    if [ -n "$answer" ]; then
        ANSWER=$answer
    fi
    if [ "$ANSWER" = "yes" ]; then
        diff -us /tmp/$$/$1 $1 > /tmp/$PACKAGE.$$.diff
        File saved to /tmp/$PACKAGE.$$.diff
    fi
    echo
fi
#
# We should be nice and offer to clean up
#
echo -n "Delete the temporary package files from /tmp/$$? [yes] "
ANSWER=yes
read answer
if [ -n "$answer" ]; then
    ANSWER=$answer
fi
if [ "$ANSWER" = "yes" ]; then
    rm -fr /tmp/$$/
fi
echo

This script is also available from the RedBrick package repo

Posted August 23rd, 2009 in Linux, Scripts.

Leave a response: