fileExists() problem

49 views
Skip to first unread message

Steve

unread,
Nov 27, 2012, 12:38:21 AM11/27/12
to xad...@googlegroups.com
Hello,

I'm evaluating XADisk and have run into behavior where the fileExists() method throws an unexpected exception. The problem seems to be that the fileExists() implementation in NativeSession is not looking to see whether the parent directory exists before checking if the child directory has read permissions.

I'm running OS X 10.7.5 with the default JVM for my system, 1.6.0 with XADisk release 1.2.1.

Thanks in advance for any advice.

-steve


Here is code to reproduce:

import java.io.File;

import org.xadisk.bridge.proxies.interfaces.*;
import org.xadisk.filesystem.standalone.StandaloneFileSystemConfiguration;

public class TestXADisk
{
public static void main(String[] args)
{
File pwd = new File(".").getAbsoluteFile();
String systemDir = new File(pwd, "test-xa-disk").getPath();
StandaloneFileSystemConfiguration cfg = new StandaloneFileSystemConfiguration(systemDir, "test-xa-disk");
XAFileSystem xaDisk = XAFileSystemProxy.bootNativeXAFileSystem(cfg);
try
{
File tmp = File.createTempFile("test-xa-disk", null);
tmp.delete();
tmp.mkdir();
assertTrue(tmp.exists());
System.out.println("Files directory: " + tmp);
File child = new File(tmp, "child");
File grandchild = new File(child, "grandchild");
assertFalse(child.exists());
assertFalse(grandchild.exists());
Session session = xaDisk.createSessionForLocalTransaction();
assertTrue(session.fileExists(tmp));
assertFalse(session.fileExists(child));
assertFalse(session.fileExists(grandchild));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
xaDisk.shutdown();
}
catch (Exception e2)
{
e2.printStackTrace();
System.exit(-1);
}
System.out.println("done");
}
}
static void assertTrue(boolean condition) throws Exception
{
if ( !condition )
throw new Exception();
}
static void assertFalse(boolean condition) throws Exception
{
assertTrue(!condition);
}
}

Steve

unread,
Nov 27, 2012, 10:41:08 AM11/27/12
to xad...@googlegroups.com
Realized that I forgot the call to xaDisk.waitForBootup(-1) after booting, but it doesn't change matters. The exception still gets thrown on session.fileExists(grandchild).

Thanks,
-steve

Nitin Verma

unread,
Nov 27, 2012, 12:52:21 PM11/27/12
to xad...@googlegroups.com
Hello Steve,

I ran the above code and see InsufficientPermissionOnFileException for "child" during fileExists(grandchild).

The except is misleading since it reports permission not being available on the "child" directory even if the "child" directory itself does not exist. To explain better, this is the flow for fileExists(grandchild)...

1. see if read permission available on grandchild.parent, that is, "child".

2. to check if child is "readable", the parent directory of child, that is, tmp is consulted. See TransactionVirtualView.isDirectoryReadable (last line).

3. since the tmp directory exists, and the nature of parentVVD.isDirReadable (parentVVD refers to tmp here) is to return false even in case of non-existent children files, we see not a FileNotExists exception, but a resulting permission exception in the caller, that is, in NativeSession.checkPermission (last line).

So, the only problem to deal here with is a better exception type, is not it?

Thanks,
Nitin

Steve

unread,
Nov 27, 2012, 4:47:45 PM11/27/12
to xad...@googlegroups.com
Hello, Nitin. Thank you for the fast response.

The issue that I'm concerned with is not so much the exception type as that an exception is thrown at all. I was hoping that fileExists() would simply return false in the way that the standard java.io.File.exists() behaves. 

I could imagine having a File and not knowing whether it or any of its ancestors exist, and calling fileExists(). Is it true that a session will be rolled back when an exception is thrown? If so, this is problematic in my use case. And even if not, it means having to catch the exception and resume processing in a way that is somewhat inconvenient. So, I am not sure whether you would consider this a bug, per se, but I am instead opting to use the following to avoid it:

String[] parts = file.getPath().split(File.separator);
file = new File(parts[0]);
if ( !session.fileExists(file) )
return false;
for ( int i = 1; i < parts.length; i++ )
{
file = new File(file, parts[i]);
if ( !session.fileExists(file) )
return false;
}
return true;


Thanks again for replying so quickly.

Regards,
-steve

Nitin Verma

unread,
Nov 28, 2012, 1:14:58 AM11/28/12
to XADisk
Hi Steve,

A session does not get rolled-back on throwing this exception.

Just a quick thought. How about simply writing a wrapper method around
fileExists which would catch the exception thrown and return false in
that case.

I would consider fixing it soon. Let me file a tracking bug.

Thanks,
Nitin

Nitin Verma

unread,
Nov 28, 2012, 1:41:47 AM11/28/12
to XADisk
Hello Steve. Logged a tracking bug here: http://java.net/jira/browse/XADISK-127.

Nitin

Steve

unread,
Nov 28, 2012, 10:26:30 AM11/28/12
to xad...@googlegroups.com
Hi Nitin,

I'm new to XADisk, so that's good to know about not rolling back on this exception. I suppose it's time for me to look at the docs ;-)

In that case I agree that your wrapper approach should do nicely. Thanks for the help.

Cheers,
-steve
Reply all
Reply to author
Forward
0 new messages