views:

344

answers:

2

I have created a plain file which does not have execute permission but when I create a Java File object using this file's path/name and then call File.canExecute() I get true as the result, whereas I would expect this method call to return false. Can someone explain what I'm missing here?

Solaris:

$ touch /tmp/nonexecutable
$ ls -l /tmp/nonexecutable
-rw-r--r--   1 root     root           0 May 21 07:48 /tmp/nonexecutable

Java:

String pathName = "/tmp/nonexecutable";
File myFile = new File(pathName);
if (!myFile.canExecute())
{
    String errorMessage = "The file is not executable.";
    log.error(errorMessage);
    throw new RuntimeException(errorMessage);
}

Thanks in advance for your help.

--James

+2  A: 

Though I'm not an expert, and this will not answer your question properly, I'd like to add that this behavior is not specific to Java. From the find (GNU findutils) 4.4.0 manpage on my Ubuntu 8.10 install, regarding the -executable flag:

Matches files which are executable and directories which are searchable (in a file name resolution sense). This takes into account access control lists and other permissions artefacts which the -perm test ignores. This test makes use of the access(2) system call, and so can be fooled by NFS servers which do UID mapping (or root-squashing), since many systems implement access(2) in the client’s kernel and so cannot make use of the UID mapping information held on the server. Because this test is based only on the result of the access(2) system call, there is no guarantee that a file for which this test succeeds can actually be executed.

Stephan202
+3  A: 

Nothing to do with Java - you're running as root, and root is allowed everything, not matter what the permissions say.

Michael Borgwardt
Nothing in his post indicates that he's running as root.
JesperE
The file is owned by root.root and it's just been created, indicating the OP is running as root
MrWiggles