system.pathForFile bug ?

Why is this not failing / working :

  1. Assume there is no bla file
  2. Then one of the following should be false

print ( system.DocumentsDirectory )
print ( system.ResourceDirectory)

local dd = system.pathForFile( “bla.cfg”, system.DocumentsDirectory )
local rd = system.pathForFile( “bla.cfg”, system.ResourceDirectory )

assert ( ( dd and rd ) or ( not dd and not rd) , “Inconsistent api behavior”)

Thanks
Frank

@frank76,

I modified your code a bit:

print ( system.DocumentsDirectory ) print ( system.ResourceDirectory) local dd = system.pathForFile( "bla.cfg", system.DocumentsDirectory ) local rd = system.pathForFile( "bla.cfg", system.ResourceDirectory ) print( "dd == ", tostring( dd ) ) print( "rd == ", tostring( rd ) ) 

The last two statements will print:

  • dd == A very long path ending in bla.cfg
  • rd == nil

Why?

In the case of the DocumentsDirectory, Corona assumes you want to either open the file or create it.  It doesn’t care and it has full access to that directory.

In the case of ResourceDirectory,  Corona does not allow you to create files there so if the file doesn’t exist it just returns nil.

Thanks, that is exactly what i wanted to point out. Same call, different behaviour.

@frank76,

I modified your code a bit:

print ( system.DocumentsDirectory ) print ( system.ResourceDirectory) local dd = system.pathForFile( "bla.cfg", system.DocumentsDirectory ) local rd = system.pathForFile( "bla.cfg", system.ResourceDirectory ) print( "dd == ", tostring( dd ) ) print( "rd == ", tostring( rd ) ) 

The last two statements will print:

  • dd == A very long path ending in bla.cfg
  • rd == nil

Why?

In the case of the DocumentsDirectory, Corona assumes you want to either open the file or create it.  It doesn’t care and it has full access to that directory.

In the case of ResourceDirectory,  Corona does not allow you to create files there so if the file doesn’t exist it just returns nil.

Thanks, that is exactly what i wanted to point out. Same call, different behaviour.