Question

I'm trying to read a content of a directory at startup. The folder config is at the same level as the manifest.json. I've tried the following code:

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem; 
window.directoryEntry = window.directoryEntry || window.webkitDirectoryEntry;
function onInitFs(fs) {
    fs.root.getDirectory('config', {}, function(dirEntry){
          var dirReader = dirEntry.createReader();
          dirReader.readEntries(function(entries) {
            for(var i = 0; i < entries.length; i++) {
              var entry = entries[i];
              if (entry.isDirectory){
                console.log('Directory: ' + entry.fullPath);
              }
              else if (entry.isFile){
                console.log('File: ' + entry.fullPath);
              }
            }

          }, errorHandler);
        }, errorHandler);
    };

    window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);

but the callback for getDirectory is never called. My manifest.json has {"fileSystem": ["write", "directory"]} has permissions.

Was it helpful?

Solution

First off, your manifest.json is probably irrelevant here. The fileSystem permission is for the chrome.fileSystem API, not the HTML File API, which you are using. I don't think you need any special permissions in your manifest to use these functions.

Second, if the success callback for getDirectory is never being called, then your error handler should be. Is it being called? If so, with what arguments?

I've tried this code in Chrome Canary (Chrome 35), and it works fine, with one change: I have to create the directory first. Without a call like

fs.root.getDirectory("config", {create: true}, function(newDirEntry) {
    console.log(newDirEntry);
}, makeErrorHandler("createDir"));

first, I get a NotFoundError error from your getDirectory call.

It's possible that this is happening because you are attempting to use the TEMPORARY filesystem rather than the PERSISTENT filesystem. If you want a directory to be present on startup, then you should request that it be persistent.

Alternately, add the key create: true to your getDirectory options, and handle the case where the directory didn't exist, but was created.

OTHER TIPS

@supercalifragilistichespirali - the package directory is read only for security reasons, you cannot make any changes there. If you want to store data you should use sandbox file system, which Ian described in this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top