Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-4816

Blackberry File plug-in issues creating files outside sandbox

    XMLWordPrintableJSON

Details

    Description

      I've updated to the latest file plug-in with the automatic un-sandbox changes when requesting a path outside the sandboxed area (these are in the dev branch today). However there are still some issues with this. I'm using blackberry.io to get the shared path where I know I should be able to write to.

      I've broken down the process of writing to a file in 5 steps at the bottom of this issue. First we get the file system, once we have that we get our directory - note we do this because we want to create a sub-folder if it's not already there, getFile does not permit this from what we can tell. When we get the directory, everything looks ok - we can see the sandbox flag is turned off. However, in step 3 when we go to get the file we simply pass in the name of the file as we are calling from the directory. The issue here is when we call getFile with the current logic it thinks it is sandboxed still.

      If you take a look at the DirectoryEntry.js file for DirectoryEntry.prototype.getFile you will notice it's checking the path parameter alone to determine whether it is sandboxed. It's not factoring in the currentPath into this equation. As a result, it goes into the else for sandbox disabled, sets the sandbox state to true, then tries to get the file using currentPath (which is outside the sandbox) as well as the path (file name passed in) and fails. It shouldn't be going into the else... really the fileUtils.isOutsideSandbox should be checking currentPath + "/" + path in this case.

      I think we need to ensure we are following the API documentation carefully, we need support both relative and absolute paths. Below are some snippets from the docs:

      getDirectory
      path: The path to the directory to be looked up or created. Either an absolute path, or a relative path from this DirectoryEntry. (DOMString)

      getFile
      path: The path to the file to be looked up or created. Either an absolute path, or a relative path from this DirectoryEntry. (DOMString)

      In my opinion this means the logic in DirectoryEntry.js for getDirectory and getFile should be updated. The first thing we should do is check to see if the path is absolute, i.e. the first char is "/" - if it is, we shouldn't be using the currentPath here at all I would think? I made some minor updates to this logic locally and it is working now - at least for how we use it! I will be making a pull request to GitHub shortly for this.

      Below is a snippet of how we were trying to write to a file:

      //Assuming you have blackberry.io plug-in and cordova file plug-in installed...
      function FileManager() {}

      FileManager.fileDir = blackberry.io.sharedFolder + "/documents/test/"; FileManager.fileName = "test.txt";

      // Step 1 - Get file system
      window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, FileManager.gotWriteFS, null);

      //Step 2 - Get the directory
      FileManager.gotWriteFS = function (fileSystem) {
      //Get/Create the directory we want to use
      fileSystem.root.getDirectory(FileManager.fileDir,

      { create: true, exclusive: false }

      , FileManager.gotWriteDirEntry, null); };

      //Step 3 - Get the file
      FileManager.gotWriteDirEntry = function (dirEntry) {
      //Get the file from the current directory
      dirEntry.getFile(FileManager.fileName,

      { create: true, exclusive: false }

      , FileManager.gotWriteFileEntry, null); };

      //Step 4 - Create a writer
      FileManager.gotWriteFileEntry = function (fileEntry)

      { fileEntry.createWriter(FileManager.gotFileWriter, null); }

      ;

      //Step 5 - Write the data
      FileManager.gotFileWriter = function (writer) {
      writer.write("something to write");
      };

      Attachments

        Activity

          People

            bryanhiggins Bryan Higgins
            dpeacock David Peacock
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: