Bug in lfs

Hi,

it is possible to read single files from the resourceDirectory according to this: http://docs.coronalabs.com/api/library/system/ResourceDirectory.html

so why isn’t it possible to read the contents of a directory in the resourceDirectory?

WORKS

=======

local path = system.pathForFile(“myFolder/myFile.json”, system.ResourceDirectory)

local file = io.open(path, “r”)

DOES NOT WORK

===============

local path = system.pathForFile(“myFolder”, system.ResourceDirectory)

local files = lfs.chdir(path) – This throws an exception.

As an example, this bug prevents a game from loading up for instance a bunch of json levels from a directory. And that is exactly what we need to do in our game right now.

I’m not the only one with this problem, check the end of the comments here: http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/

Corona staff - what is your comment? 

Hi Haakon.  Are you seeing this in the simulator or on device?  If device are you building for Android or iOS?

Hey Haakon,

I have the following snippet working in the simulator, and on my iPhone 5:

[lua]

local lfs = require “lfs”

local path = system.pathForFile("", system.ResourceDirectory)
print (“Path:”,path)   --nil means path does not exist
local new_folder_path
local success = lfs.chdir( path ) – returns true on success  , if path is nil, you will get an error

if success then
   lfs.mkdir( “myFolder” ) --not ‘really’ used, I created the folder in my project, and put files in it…
   new_folder_path = lfs.currentdir() … “/myFolder”
end

local files = lfs.dir(new_folder_path) – This throws an exception.

local f

print(“Files in new folder”)
for myFile in  lfs.dir(new_folder_path) do
   --file is the current file or directory name
   print( "Found file: " … myFile )
end

print(“Files in root path”)
for myFile in  lfs.dir(path) do
   --file is the current file or directory name
   print( "Found file: " … myFile )
end

[/lua]

This lists the following in xcode:

Dec  3 16:48:14 iPhone SpringBoard[33] <Warning>: LICreateIconForImage passed NULL CGImageRef image
Dec  3 16:48:15 iPhone kernel[0] <Debug>: launchd[2247] Container: /private/var/mobile/Applications/89194237-9E68-4B71-AA5F-9E628F8D6D34 (sandbox)
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Path:    /var/mobile/Applications/89194237-9E68-4B71-AA5F-9E628F8D6D34/folderaccess.app
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Files in new folder
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: .
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: …
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxEmpty.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxExplosive.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxExplosiveAlt.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxExplosive_disabled.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItem.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItemAlt.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItemAlt_disabled.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItem_disabled.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxWarning.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: brickWall.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: bridge.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: bridgeLogs.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: castle.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: castleCenter.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Files in root path
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: .
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: …
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: CoronaResources.bundle
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: CoronaSDK3rdPartyLicenses.txt
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: FacebookSDKResources.bundle
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: Info.plist
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: MainWindow-iPad.nib
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: MainWindow.nib
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: PkgInfo
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: ResourceRules.plist
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: _CodeSignature
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: embedded.mobileprovision
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: folderaccess
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: myFolder
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: resource.car
 

Just tried the same code on my Nexus 7, and am getting an nil back from the system.pathForFile call,

which results in a run-time error. :frowning:

LogCat:

I/Corona  ( 3766): Path:    nil
I/Corona  ( 3766): Runtime error
I/Corona  ( 3766): …uilds)/folder access(default)/folder access/main.lua:7: bad argument #1 to ‘chdir’ (string expected, got nil)
I/Corona  ( 3766): stack traceback:
I/Corona  ( 3766):     [C]: ?
I/Corona  ( 3766):     [C]: in function ‘chdir’
I/Corona  ( 3766):     …uilds)/folder access(default)/folder access/main.lua:7: in main chunk
D/dalvikvm( 3766): GC_CONCURRENT freed 243K, 4% free 7814K/8104K, paused 2ms+8ms, total 33ms
I/Corona  ( 3766): Runtime error
I/Corona  ( 3766):
I/Corona  ( 3766): stack traceback:
I/Corona  ( 3766):     [C]: ?
I/Corona  ( 3766):     [C]: in function ‘chdir’
I/Corona  ( 3766):     …uilds)/folder access(default)/folder access/main.lua:7: in main chunk

Hi,

you’re not conducting the test correctly. Yes, it’s possible to get the contents of the ResourceDirectory ROOT. However, as soon as you point your code to a FOLDER residing in ROOT you get trouble.

Here is a really small project that reproduces the bug as I reported it.

https://s3-eu-west-1.amazonaws.com/monkeybin/FileDemoBug.zip

The difference between your code and mine is that where you call:

  1. local path = system.pathForFile("", system.ResourceDirectory)

I do it like this:

  1. local path = system.pathForFile(“myFolder”, system.ResourceDirectory)

I have confirmed that listing the contents of the root like you do works fine in the Simulator. Getting the contents of a subfolder, like I need to do, is impossible and throws an exception:

ERROR: The resource file (myFolder) could not be found at case-sensitive path (/Users/haakon/Desktop/FileDemo/myFolder).

WARNING: Cannot create path for resource file ‘myFolder’. File does not exist.

Just a thought, did you try to put a forward slash infront of the myFolder path?

like this

local path = system.pathForFile("myFolder/", system.ResourceDirectory)

/Tommy

Yes. Makes no difference.

…oh man the forum just ate my original post :(  Let’s try again

The issue seems to be with what directory this brings back:

[lua]

local path = system.pathForFile("", system.ResourceDirectory)
print (“Path:”,path)  

[/lua]

On my Mac, I get: /Applications/CoronaSDK/Corona Simulator.app/Contents/Resources/CoronaResources.bundle/Contents/Resources    
 

I was expecting it to give me my project folder, or the sandbox, but it gave me this ‘virtual space’ inside the corona simulator instead.  There were widget files inside, along with the folder that I created with my earlier test, which explains why i was able to pull that directory listing without getting an error. 

I was able to get a folder listing by specifying the full folder path to the simulator’s sandbox on my mac:

[lua]

  new_folder_path = “/Users/raymonddelia/Library/Application Support/Corona Simulator/folder access-59F5788A4F8488A6F0E9C61EA944F2C7/myFolder”
[/lua]

I had to copy in the ‘myFolder’ contents to the sandbox folder , since the simulator did not do it automatically.

Since that worked, I tried pointing to my project file:

[lua]
   new_folder_path = “/Users/raymonddelia/Documents/Corona Projects/folder access/myFolder”

[/lua]

It’s pretty odd that  specifying the folder/file seems to actually point to the project’s structure, like in your example.

It looks like the big show stopper is android, where we don’t get a path for the directory at all.

 

Ha - good catch! Ok, so it’s just to sit back and wait for a Corona Bug Fix to happen…

Coronaah staff - when? :slight_smile:

There are a couple of important notes.

1.  system.pathForFile() name says what it does… a path for a file.  It expects to get a path to a file, not a directory.  Now the underlying OS may allow this to work, but by doing so, you are taking advantage of a case that is not designed or tested for.  This will probably fail on Windows because of NTFS.  Unix tends to treat directories as files, so it might work under the Linux based Android or the Unix based Mac OS-X and iOS, but Corona Labs expects the first parameter to be to a file.

  1. Under Android, there is no system.ResourcesDirectory that has your APK’s file content.  It’s a renamed zip file.  We extract some files that are needed for Corona SDK to work, but as LFS’s name states it’s Lua File System, its just a wrapper around the native OS’s file system.  An APK isn’t a directory and cannot be accessed with LFS.  Native developers on Android have to deal with this same issue.   On iOS, the app bundle is a folder in the file system, so LFS should be able to read it.

Are you seeing conditions outside of these two that LFS’s is not working?

Thanks,

Rob

Hi Rob,

thanks for that. We’ve been using the #1 case you outlined for the last year in both Android and iOS without any problems, but using the DocumentsDirectory. I’ll change our apps over to using the root path and instead run chdir to switch to the directory - just to be sure.

As for #2 - how do you suggest a game should pick up files from the bundle on Android? Is the only way to download files and place them in the documents directory? We have a huge folder with json files in a subfolder in the system.ResourcesFolder, and we need to be able to read those files from the game…

You pretty much have to read them and save them out to the documents directory.

Rob

Corona staff - what is your comment? 

Hi Haakon.  Are you seeing this in the simulator or on device?  If device are you building for Android or iOS?

Hey Haakon,

I have the following snippet working in the simulator, and on my iPhone 5:

[lua]

local lfs = require “lfs”

local path = system.pathForFile("", system.ResourceDirectory)
print (“Path:”,path)   --nil means path does not exist
local new_folder_path
local success = lfs.chdir( path ) – returns true on success  , if path is nil, you will get an error

if success then
   lfs.mkdir( “myFolder” ) --not ‘really’ used, I created the folder in my project, and put files in it…
   new_folder_path = lfs.currentdir() … “/myFolder”
end

local files = lfs.dir(new_folder_path) – This throws an exception.

local f

print(“Files in new folder”)
for myFile in  lfs.dir(new_folder_path) do
   --file is the current file or directory name
   print( "Found file: " … myFile )
end

print(“Files in root path”)
for myFile in  lfs.dir(path) do
   --file is the current file or directory name
   print( "Found file: " … myFile )
end

[/lua]

This lists the following in xcode:

Dec  3 16:48:14 iPhone SpringBoard[33] <Warning>: LICreateIconForImage passed NULL CGImageRef image
Dec  3 16:48:15 iPhone kernel[0] <Debug>: launchd[2247] Container: /private/var/mobile/Applications/89194237-9E68-4B71-AA5F-9E628F8D6D34 (sandbox)
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Path:    /var/mobile/Applications/89194237-9E68-4B71-AA5F-9E628F8D6D34/folderaccess.app
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Files in new folder
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: .
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: …
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxEmpty.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxExplosive.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxExplosiveAlt.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxExplosive_disabled.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItem.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItemAlt.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItemAlt_disabled.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxItem_disabled.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: boxWarning.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: brickWall.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: bridge.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: bridgeLogs.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: castle.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: castleCenter.png
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Files in root path
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: .
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: …
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: CoronaResources.bundle
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: CoronaSDK3rdPartyLicenses.txt
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: FacebookSDKResources.bundle
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: Info.plist
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: MainWindow-iPad.nib
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: MainWindow.nib
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: PkgInfo
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: ResourceRules.plist
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: _CodeSignature
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: embedded.mobileprovision
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: folderaccess
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: myFolder
Dec  3 16:48:15 iPhone folderaccess[2247] <Warning>: Found file: resource.car
 

Just tried the same code on my Nexus 7, and am getting an nil back from the system.pathForFile call,

which results in a run-time error. :frowning:

LogCat:

I/Corona  ( 3766): Path:    nil
I/Corona  ( 3766): Runtime error
I/Corona  ( 3766): …uilds)/folder access(default)/folder access/main.lua:7: bad argument #1 to ‘chdir’ (string expected, got nil)
I/Corona  ( 3766): stack traceback:
I/Corona  ( 3766):     [C]: ?
I/Corona  ( 3766):     [C]: in function ‘chdir’
I/Corona  ( 3766):     …uilds)/folder access(default)/folder access/main.lua:7: in main chunk
D/dalvikvm( 3766): GC_CONCURRENT freed 243K, 4% free 7814K/8104K, paused 2ms+8ms, total 33ms
I/Corona  ( 3766): Runtime error
I/Corona  ( 3766):
I/Corona  ( 3766): stack traceback:
I/Corona  ( 3766):     [C]: ?
I/Corona  ( 3766):     [C]: in function ‘chdir’
I/Corona  ( 3766):     …uilds)/folder access(default)/folder access/main.lua:7: in main chunk

Hi,

you’re not conducting the test correctly. Yes, it’s possible to get the contents of the ResourceDirectory ROOT. However, as soon as you point your code to a FOLDER residing in ROOT you get trouble.

Here is a really small project that reproduces the bug as I reported it.

https://s3-eu-west-1.amazonaws.com/monkeybin/FileDemoBug.zip

The difference between your code and mine is that where you call:

  1. local path = system.pathForFile("", system.ResourceDirectory)

I do it like this:

  1. local path = system.pathForFile(“myFolder”, system.ResourceDirectory)

I have confirmed that listing the contents of the root like you do works fine in the Simulator. Getting the contents of a subfolder, like I need to do, is impossible and throws an exception:

ERROR: The resource file (myFolder) could not be found at case-sensitive path (/Users/haakon/Desktop/FileDemo/myFolder).

WARNING: Cannot create path for resource file ‘myFolder’. File does not exist.

Just a thought, did you try to put a forward slash infront of the myFolder path?

like this

local path = system.pathForFile("myFolder/", system.ResourceDirectory)

/Tommy

Yes. Makes no difference.