You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2013/12/13 17:18:46 UTC

[04/19] git commit: CB-5407: Move remove methods into FS modules

CB-5407: Move remove methods into FS modules


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/919f99f8
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/919f99f8
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/919f99f8

Branch: refs/heads/dev
Commit: 919f99f818895800f28589d8f2c2f8b023ec74cb
Parents: 0d0af8f
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Nov 27 13:35:26 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500

----------------------------------------------------------------------
 src/android/ContentFilesystem.java | 10 ++++++
 src/android/FileUtils.java         | 59 ++++++++++++++++++++++-----------
 src/android/Filesystem.java        |  4 +++
 src/android/LocalFilesystem.java   | 33 ++++++++++++++++++
 4 files changed, 86 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 6b34641..7325ac6 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -54,5 +54,15 @@ public class ContentFilesystem implements Filesystem {
 			String fileName, JSONObject options, boolean directory) throws IOException {
 		throw new IOException("Cannot create content url");
 	}
+	@Override
+	public boolean removeFileAtLocalURL(LocalFilesystemURL inputURL)
+			throws NoModificationAllowedException {
+		throw new NoModificationAllowedException("Cannot remove content url");
+	}
+	@Override
+	public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL)
+			throws NoModificationAllowedException {
+		throw new NoModificationAllowedException("Cannot remove content url");
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 32e2a4a..a373d50 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -288,7 +288,7 @@ public class FileUtils extends CordovaPlugin {
         else if (action.equals("remove")) {
             final String fname=args.getString(0);
             threadhelper( new FileOp( ){
-                public void run() throws NoModificationAllowedException, InvalidModificationException {
+                public void run() throws NoModificationAllowedException, InvalidModificationException, MalformedURLException {
                     boolean success= remove(fname);
                     if (success) {
                         notifyDelete(fname);
@@ -302,7 +302,7 @@ public class FileUtils extends CordovaPlugin {
         else if (action.equals("removeRecursively")) {
             final String fname=args.getString(0);
             threadhelper( new FileOp( ){
-                public void run() throws FileExistsException {
+                public void run() throws FileExistsException, MalformedURLException, NoModificationAllowedException {
                     boolean success = removeRecursively(fname);
                     if (success) {
                         callbackContext.success();
@@ -743,16 +743,26 @@ public class FileUtils extends CordovaPlugin {
      * @param filePath the directory to be removed
      * @return a boolean representing success of failure
      * @throws FileExistsException
+     * @throws NoModificationAllowedException 
+     * @throws MalformedURLException 
      */
-    private boolean removeRecursively(String filePath) throws FileExistsException {
-        File fp = createFileObject(filePath);
+    private boolean removeRecursively(String baseURLstr) throws FileExistsException, NoModificationAllowedException, MalformedURLException {
+        try {
+        	LocalFilesystemURL inputURL = new LocalFilesystemURL(baseURLstr);
+        	// You can't delete the root directory.
+        	if ("".equals(inputURL.fullPath) || "/".equals(inputURL.fullPath)) {
+        		throw new NoModificationAllowedException("You can't delete the root directory");
+        	}
 
-        // You can't delete the root directory.
-        if (atRootDirectory(filePath)) {
-            return false;
+        	Filesystem fs = this.filesystemForURL(inputURL);
+        	if (fs == null) {
+        		throw new MalformedURLException("No installed handlers for this URL");
+        	}
+        	return fs.recursiveRemoveFileAtLocalURL(inputURL);
+        
+        } catch (IllegalArgumentException e) {
+        	throw new MalformedURLException("Unrecognized filesystem URL");
         }
-
-        return removeDirRecursively(fp);
     }
 
     /**
@@ -762,7 +772,9 @@ public class FileUtils extends CordovaPlugin {
      * @return a boolean representing success of failure
      * @throws FileExistsException
      */
+    /* TODO: Remove when no longer needed */
     private boolean removeDirRecursively(File directory) throws FileExistsException {
+    	
         if (directory.isDirectory()) {
             for (File file : directory.listFiles()) {
                 removeDirRecursively(file);
@@ -784,21 +796,26 @@ public class FileUtils extends CordovaPlugin {
      * @return a boolean representing success of failure
      * @throws NoModificationAllowedException
      * @throws InvalidModificationException
+     * @throws MalformedURLException 
      */
-    private boolean remove(String filePath) throws NoModificationAllowedException, InvalidModificationException {
-        File fp = createFileObject(filePath);
+    private boolean remove(String baseURLstr) throws NoModificationAllowedException, InvalidModificationException, MalformedURLException {
+        try {
+        	LocalFilesystemURL inputURL = new LocalFilesystemURL(baseURLstr);
+        	// You can't delete the root directory.
+        	if ("".equals(inputURL.fullPath) || "/".equals(inputURL.fullPath)) {
 
-        // You can't delete the root directory.
-        if (atRootDirectory(filePath)) {
-            throw new NoModificationAllowedException("You can't delete the root directory");
-        }
+        		throw new NoModificationAllowedException("You can't delete the root directory");
+        	}
 
-        // You can't delete a directory that is not empty
-        if (fp.isDirectory() && fp.list().length > 0) {
-            throw new InvalidModificationException("You can't delete a directory that is not empty.");
+        	Filesystem fs = this.filesystemForURL(inputURL);
+        	if (fs == null) {
+        		throw new MalformedURLException("No installed handlers for this URL");
+        	}
+        	return fs.removeFileAtLocalURL(inputURL);
+        
+        } catch (IllegalArgumentException e) {
+        	throw new MalformedURLException("Unrecognized filesystem URL");
         }
-
-        return fp.delete();
     }
 
     /**
@@ -854,6 +871,7 @@ public class FileUtils extends CordovaPlugin {
      * @param filePath to directory
      * @return true if we are at the root, false otherwise.
      */
+    /* TODO: Remove when no longer needed */
     private boolean atRootDirectory(String filePath) {
         filePath = FileHelper.getRealPath(filePath, cordova);
 
@@ -871,6 +889,7 @@ public class FileUtils extends CordovaPlugin {
      * @param filePath
      * @return
      */
+    /* TODO: Remove when no longer needed */
     private File createFileObject(String filePath) {
         filePath = FileHelper.getRealPath(filePath, cordova);
 

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 19692ad..a4730ba 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -12,4 +12,8 @@ public interface Filesystem {
 	JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, String fileName,
 			JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException;
 
+	boolean removeFileAtLocalURL(LocalFilesystemURL inputURL) throws InvalidModificationException, NoModificationAllowedException;
+
+	boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL) throws FileExistsException, NoModificationAllowedException;
+
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 563f8ed..aca9d15 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -148,4 +148,37 @@ public class LocalFilesystem implements Filesystem {
         return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemType, directory);
 	}
 
+	@Override
+	public boolean removeFileAtLocalURL(LocalFilesystemURL inputURL) throws InvalidModificationException {
+
+        File fp = new File(filesystemPathForURL(inputURL));
+
+        // You can't delete a directory that is not empty
+        if (fp.isDirectory() && fp.list().length > 0) {
+            throw new InvalidModificationException("You can't delete a directory that is not empty.");
+        }
+
+        return fp.delete();
+	}
+
+	@Override
+	public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL) throws FileExistsException {
+        File directory = new File(filesystemPathForURL(inputURL));
+    	return removeDirRecursively(directory);
+	}
+	
+	protected boolean removeDirRecursively(File directory) throws FileExistsException {
+        if (directory.isDirectory()) {
+            for (File file : directory.listFiles()) {
+                removeDirRecursively(file);
+            }
+        }
+
+        if (!directory.delete()) {
+            throw new FileExistsException("could not delete: " + directory.getName());
+        } else {
+            return true;
+        }
+	}
+
 }