#!/bin/bash # # $Id: safedir,v 1.6 2005/02/01 23:47:34 jpeek Exp $ # # safedir - make a temporary directory populated with sanely-named # symlinks to entries in another directory remdir. Outputs name # of temporary directory, or /CANNOT-MAKE-DIRECTORY-* on error. # Makes filename translation table in destination directory. # # Usage: safedir remdir # or: cd `safedir remdir` # # NO WARRANTY. YOU'RE RESPONSIBLE TO DECIDE IF THIS MEETS YOUR NEEDS. # # Placed in the public domain by Jerry Peek. If you make changes or # have suggestions, please tell me; see www.jpeek.com/contact.html. myname="${0##*/}" # name of this script destdir="/tmp/SAFEDIR-$USER-$$" # where we make the symlinks usage="Usage: ${0##*/} remote-directory" srcdir="$1" # directory we make links to ttable=.safedir_translation_table # holds newold filenames pwdcmd=/bin/pwd # gets hard path to current dir fix=_ # replace $xlate with this # Characters to replace with $fix. # Note embedded single quote and the space, tab and newline at end: xlate='=!#$%^&*()|\\'"'"';/<>?~`[]{} ' giveup() { # Output a bogus directory name so "cd 'safedir'" will fail: echo "DID-NOT-MAKE-DIRECTORY-$destdir" exit 1 } if [ ! -d "$srcdir" -o $# -ne 1 ] then echo "$myname: $srcdir isn't a directory, or command line has error." 1>&2 echo "$usage" 1>&2 giveup fi # Change current directory to $srcdir, directory to which we make links. # Get absolute pathname of this directory with symlinks resolved: if ! cd "$srcdir" then echo "$myname: can't change to directory '$srcdir'" 1>&2 echo "$usage" 1>&2 giveup else curdir=$($pwdcmd) fi # Be sure current directory is readable ([ -r ] doesn't seem to work) lsout=$(/bin/ls) if [ $? -ne 0 -o -z "$lsout" ] then echo "$myname: can't list directory '$srcdir'?" 1>&2 giveup fi # Be sure we won't clobber an existing translation table file: if [ -e $ttable ] then echo "$myname: aborting: $ttable exists in $srcdir, so copy I generate in $destdir might be incorrect." 1>&2 giveup fi if mkdir "$destdir" then chmod 700 "$destdir" || giveup else echo "$myname: can't create temporary directory $destdir" 1>&2 giveup fi # Loop through current directory and make symlinks in $destdir. # NOTE that standard output of loop is redirected to $ttable file; # this collects standard output from the "echo -E" command: for f in * do fnew=$(echo -E -n "$f" | tr "$xlate" "[${fix}*]") # new filename if [ -d "$f" ] then echo "$myname: caution: $f ($fnew) is a subdirectory. cd'ing through it will take you to non-converted filenames" 1>&2 fi # Be sure some earlier name wasn't already converted to $fnew. # We could do some fancy renaming, but we simply punt: if [ -L "$destdir/$fnew" ] then echo "$myname: WARNING: not linking to $f because safe-named $fnew already exists in $destdir. Continuing..." 1>&2 else if ln -s "$curdir/$f" "$destdir/$fnew" then # Write to stdout, redirected at end of loop to $ttable file. # This string has an embedded TAB (we can't use \t because # "echo -e" could mistakenly interpret an escape sequence in $f): echo -E "$fnew $f" else echo "$myname: ABORTING: can't create symlink $destdir/$fnew?" 1>&2 giveup fi fi done > "$destdir/$ttable" # Output links-directory name ("cd 'safedir'" will work if it's used): echo "$destdir" exit 0