Search code examples
kshqnx

Difference between "test -a file" and "test file -ef file"


QNX (Neutrino 6.5.0) uses an open source implementation of ksh as its shell. A lot of the provided scripts, including the system startup scripts, use constructs such as

if ! test /dev/slog -ef /dev/slog; then
    # do something
fi

to check whether a resource manager exists or not in the filesystem. I've searched and could only find very dray explanations that -ef checks to see whether the two parameters are in fact the same file. Since the filename specified is the same it seems to just reduce to checking that the file exists.

I have checked the behaviour of test -a and test -e (both seem to check for file existance of any type of file according to the various docs I've read) and they seem to also work.

Is there any difference in the checks performed between -ef and -a/-e? Is using -ef some kind of attempt to protect against a race condition in the existence of the file?


Solution

  • Reviewing the strace on Ubuntu Linux's copy of ksh reveals no substantial differences. One call to stat vs two.

    $ strace test /tmp/tmp.geLaoPkXXC -ef /tmp/tmp.geLaoPkXXC
    

    showed this:

    mmap(NULL, 7220736, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f11dc80b000
    close(3)                                = 0
    stat("/tmp/tmp.geLaoPkXXC", {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
    stat("/tmp/tmp.geLaoPkXXC", {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
    close(1)                                = 0
    close(2)                                = 0
    

    ...whereas

    $  strace test -a /tmp/tmp.geLaoPkXXC
    

    showed this:

    fstat(3, {st_mode=S_IFREG|0644, st_size=7220736, ...}) = 0
    mmap(NULL, 7220736, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6b49e2b000
    close(3)                                = 0
    stat("/tmp/tmp.geLaoPkXXC", {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
    close(1)                                = 0
    close(2)                                = 0
    

    One stat vs two.

    $ ksh --version
      version         sh (AT&T Research) 93u 2011-02-08