excess.org / back to filetosser ft download
#!/bin/sh
# File Tosser v0.1e-6
#
# Ian Ward '01, this script has been placed in the public domain

ACCESS_LIST="/usr/bin/cut -f 2- /home/tosser/access.list"
# ^^ this command must produce a list of file paths
#
# --> STDIN
#  [ :persist\n ]  toss more than one file
#  [ :seek\n
#    <offset>\n ]  skip <offset> bytes from next file
#  <file path>\n   toss file given
#  [ <EOF> ]       all done
#
# STDOUT -->
#  [ <file size>\n ]  when in :persist mode
#  <raw file data>
#  [ <EOF> ]          when not in :persist mode
#
# STDERR -->
#  <failure messages>


declare -i index  # this way the client can't send us anything nasty
persist=0

send() {
        if [ "$persist" == "1" ]; then
                # is there a better way to print file size?
                /usr/bin/perl -e'print ((stat("'$acc'"))[7]); print"\n";'
        fi
        index="$offset + 1"
        /usr/bin/tail -c +"$index" "$acc"
}

while read filename; do
        if [ "$filename" = ":persist" ]; then
                persist=1; continue
        elif [ "$filename" = ":seek" ]; then
                read offset; continue
        fi

        $ACCESS_LIST | while /bin/true; do
                if read acc; then
                        if [ "$filename" = "$acc" -a -f "$acc" ]; then
                                send; break
                        fi
                else
                        echo "404 Not Found" 1>&2
                        break
                fi
        done
        [ "$persist" = "1" ] || break
        offset=0
done