Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
joeh76
Posts: 14
Registered: ‎11-20-2013
My Device: Blackberry Z10

Problem recursively creating directory paths outside sandbox on blackberry10 using cordova2.9 and webworks

[ Edited ]

Hi

 

Can somebody explain to me what's going on here? The code below works perfectly well on Android but does not work on blackberry 10.

 

For the first two directory structures in the array, I do not see parentDir.getDirectory() being called, however the last path in the array ("dir/3/dir6") is created successfully in the "parentFolder" folder in blackberry.io.home folder.

 

var dirList;
	
    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);
	
	

    // device APIs are available
    //
    function onDeviceReady() {
		alert("device ready");
		blackberry.io.sandbox = false;
		dirList = ["dir1/dir4/", "dir2/dir5/", "dir3/dir6/"];
		getFileSystem();
    }
	
	function getFileSystem(){
    	window.requestFileSystem(
    			LocalFileSystem.PERSISTENT, 0,
    			function onFileSystemSuccess(fileSystem)
    			{
    				console.log("Success getting filesystem !!!");
    				createDirectoryRecursive(fileSystem);
    			},
    			function(error){
    				console.log("Failed to get the filesystem !!!!!");
    			}
    		);
    			
    }
    		
    function createDirectoryRecursive(fs){
    	var i;
    	for(i = 0; i < dirList.length; i++){
    		createDirs(fs.root, dirList[i], -1);
    	}
    }
    			
    function createDirs(parentDir, filePath, index)
    {
    	console.log("createDirs params ===> parentDir=" + parentDir.toURL() + " filePath=" + filePath + " index=" + index); 
    	var arrDirs = filePath.split("/");
	console.log("number of levels in path = " + arrDirs.length);
    	if (index >= (arrDirs.length - 1))
    	{
    		console.log("Done with " + filePath);
    	}
    	else{
    		var dirName = "parentFolder";
    			
    		if (index >= 0)
    		{
    			dirName = arrDirs[index];
    			console.log("current dirName is " + dirName);
    		}
    					
    		//if device is Blackberry, build up a full directory path as we are trying to install outside of sandbox
		var path, dirToCreate = ""
    		if(device.platform == "blackberry10"){
    				
    			path = "parentFolder/";
    			console.log("Paths ======> arrDirs = " + arrDirs + " index = " +index);
    			for (i = 0; i <= index; i++){
    				path += arrDirs[i] + "/";
    				console.log("path = " + path + " i = " + i + " index = " +  index);
    			}
    					
    			dirToCreate = blackberry.io.home + "/" + path;
    			dirToCreate = dirToCreate.substring(0, dirToCreate.length - 1);
    			console.log("Paths Trying to create " + dirToCreate);
    			dirName = dirToCreate;
    		}
    					
    		parentDir.getDirectory(dirName, {create: true, exclusive: false},
    							   function (directoryEntry) {
    										console.log("getDirectory callback =======> created directory " + directoryEntry.fullPath);
    										console.log("getDirectory callback =======> Current arrdirs " + arrDirs);
    										createDirs(directoryEntry, filePath, index + 1);
    								   },
    								   function (error) {console.log("Failed to get directory " + dirName + " Error code : " + error.code);});
    									   
    	}
    }

 

The code actually does enter cordova's DirectoryEntry.getDirectory function for all the directories in the array, however no callbacks (success or failure) are called. Only the last directory path in the list is processed and created successfully on the device.

 

Thanks

Joe

Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C

Re: Problem recursively creating directory paths outside sandbox on blackberry10 using cordova2.9 and webworks

This is an issue with any language

 

You need to de-construct the path then go as far as you can without a fail and build on top

 

For example if you have /a/b/c and want /a/b/c/d/e/f you get as far as c then a fail so you have to construct d, e and f

 

This is not a fault with amy specific language - its the way the OS works (any OS)

 

Certain languages cater for being sloppy but - do you wanna be a sloopy coder?

 

 




Click the like button if you find my posts useful!
Please use plain text.