View Single Post
  #1 (permalink)  
Old July 29th, 2002
Unregistered
Guest
 
Posts: n/a
Default Somewhat of a solution for resuming downloads

Well, I got sick and tired of watching a nearly finished download stall, then restart under a slightly different name, even when the file sizes were exactly the same. Jeez, this should just work.

So here's a bit of a hack solution: this is a simple shell script that will create symbolic links of all files that share the same prefix with the target file that you specify. In other words, you type:

combine.sh "T-98721-Some Thing.file"

and every file that has the same "T-XXXXX" will be deleted (or moved, if you want to play it safe) and a symbolic link will be created in its stead. All you need to do is supply the filename of your target.

I haven't fully tested this out to see if it works without introducing artifacts, so be forewarned. Oh, and this is for Linux, sorry to Windows users, but it should also work under OSX if you have bash. I'd appreciate comments and improvements to **EDIT EMAIL ADDRESS**
Good luck.

-----BEGIN SCRIPT-----
#!/bin/bash
# This file is used to link similar files to a target file. It is
# intended to be used with Limewire to facilitate downloads.

function print_usage {
echo "Usage: $( basename $0 ) [-s] filename"
echo -e "\t-s\tbackup files"
echo "Remember to surround files that include whitespace with quotes"
exit $E_WRONGARGS
}

TARGET=temp # Target file to link to
PREFIX=temp # Prefix of target file used for search
SAFEMODE=false # Indicate whether to backup files
E_WRONGARGS=65 # Improper arguments
E_NOTARGET=66 # Target file does not exist
E_NODELETE=67 # Cannot delete file
E_NOMOVE=68 # Cannot move file
E_NOLINK=69 # Cannot link files

# Save stdin
exec 6<&0

# Test for appropriate arguments
if [ -z "$1" ]; then
print_usage
elif [ "$1" = "-s" ]; then
if [ -z "$2" ]; then
print_usage
else
SAFEMODE=true
TARGET="$2"
fi
else
TARGET="$1"
fi

# Test for existence of target file
if [ ! -f "$TARGET" ]; then
echo "Target file does not exist"
exit $E_NOTARGET
fi

# Create backup directory if applicable
if [ "$SAFEMODE" = "true" -a ! -d "backup" ]; then
mkdir backup
if [ ! "$?" ]; then
echo "Cannot create backup directory"
echo "Check directory permissions"
exit $E_FAILBACKUP
fi
fi

# Strip prefix from target file
PREFIX=$( echo $TARGET | cut -d- -f1,2 - )

# Perform search for prefix and store in a temp file
ls | grep $PREFIX > files.temp

# Change stdin to files.temp and begin processing
exec < files.temp
read FILE
while [ ! -z "$FILE" ]; do
if [ "$FILE" != "$TARGET" ]; then
if [ "$SAFEMODE" = "false" ]; then
rm -f "$FILE"
if [ ! "$?" = "0" ]; then
echo "Unable to delete $FILE"
echo "Check permissions"
echo "Exiting"
exit E_NODELETE
fi
else
mv "$FILE" ./backup/
if [ ! "$?" = "0" ]; then
echo "Unable to move $FILE"
echo "Check permissions"
echo "Exiting"
exit E_NOMOVE
fi
fi
ln -s "$TARGET" "$FILE"
if [ ! "$?" = "0" ]; then
echo "Unable to link files"
echo "Check permissions"
echo "Exiting"
exit E_NOLINK
fi
fi
read FILE
done

# Restore stdin
exec 0<&6 6<&-

rm -f files.temp

echo "Done"

exit 0
-----END SCRIPT-----
Just copy and paste it into a file like:
combine.sh, make it executable with
chmod 777 combine.sh, and place it
in /usr/local/bin or whatever.

Last edited by Remoc; March 8th, 2009 at 09:35 AM.
Reply With Quote