portable tests for symlinks (was patch for autoconf manual)

Martin Pool mbp at samba.org
Mon Apr 8 18:47:01 EST 2002


On 14 Mar 2002, Akim Demaille <akim at epita.fr> wrote:
> Would you contribute the text itself?  TIA!

Yes, I'd be happy to help.

I can't promise that the solution outlined below works everywhere, but
it has been tested on a number of unix-like platforms and seems to be
OK.  I suppose at least making people aware of the problem is better
than nothing.

Basically, we ended up using this code, in

  <http://cvs.samba.org/cgi-bin/cvsweb/rsync/testsuite/rsync.fns?rev=1.49&content-type=text/x-cvsweb-markup>

    # Determine what shell command will appropriately test for links.
    ln -s foo "$scratchdir/testlink"
    for cmd in test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test
    do
        for switch in -h -L
        do
            if $cmd $switch "$scratchdir/testlink" 2>/dev/null
    	then
    	    # how nice
    	    TEST_SYMLINK_CMD="$cmd $switch"
    	    # i wonder if break 2 is portable?
    	    break 2
    	fi
       done
    done
    # ok, now get rid of it
    rm "$scratchdir/testlink"
    
    
    if [ "x$TEST_SYMLINK_CMD" = 'x' ]
    then
        test_fail "Couldn't determine how to test for symlinks"
    else
        echo "Testing for symlinks using '$TEST_SYMLINK_CMD'"
    fi
   
Obviously some of the code is specific to our test framework, but you
get the idea.  To handle various platforms it seemed necessary to try
both the builtin and external test commands.  Neither -h nor -L
reliably works on all platforms.

Rather than just documenting this restriction, it might be best to add
an autoconf macro that performs this determination.  Otherwise,
perhaps just add this paragraph in the "limitations of builtins"
section:

  `test' (symlinks)

  On different platforms, the `-h' or `-L' options to `test' may be
  used to determine if a file is a symbolic link.  GNU `test' supports
  both, but on some platforms only one or the other will work.  On
  some systems such as Solaris, the builtin and external versions of
  `test' may support different option.  (On Solaris, using the wrong
  option produces the misleading message "argument expected".)

  It seems that the best solution is to try both `-h' and `-L', and
  each one of 

    test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test 

  until you find one that correctly detects symbolic links; then use
  that command for the rest of your script.

Regards,
-- 
Martin 





More information about the rsync mailing list