[ease] [general] Add recursive_directory_after



commit c50af57f43826d209c4fbd3625db8fc485bcaf70
Author: Nate Stedman <natesm gmail com>
Date:   Fri Jul 23 20:07:24 2010 -0400

    [general] Add recursive_directory_after
    
    - Moved recursive_delete to general utilities
    - Added recursive_directory_after, which runs the
      directory callback after recursion
    - recursive_delete now uses recursive_delete_after.

 src/ease-temp.vala      |   36 +---------------------
 src/ease-utilities.vala |   76 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 73 insertions(+), 39 deletions(-)
---
diff --git a/src/ease-temp.vala b/src/ease-temp.vala
index ae5ea8b..8a9b104 100644
--- a/src/ease-temp.vala
+++ b/src/ease-temp.vala
@@ -248,7 +248,7 @@ public static class Ease.Temp : Object
 		while ((dir = dirs.poll_head()) != null)
 		{
 			try { recursive_delete(dir); }
-			catch (FileError e)
+			catch (GLib.Error e)
 			{
 				debug(e.message);
 			}
@@ -285,38 +285,4 @@ public static class Ease.Temp : Object
 		
 		return file.query_exists(null);
 	}
-	
-	/**
-	 * Recursively removes a directory.
-	 *
-	 * Ported from Will Thompson's code located [[http://git.collabora.co.uk/?p=telepathy-haze.git;a=blob;f=src/util.c;h=5cbb4fb30b181a6c0f32c08bdadffae43b6e6ec3;hb=HEAD|here]].
-	 *
-	 * @param path The directory to be recursively deleted.
-	 */
-	public static void recursive_delete(string path) throws FileError
-	{
-		string child_path;
-		var dir = GLib.Dir.open(path, 0);
-		
-		if (dir == null)
-		{
-			throw new FileError.NOENT(
-				_("Directory to remove doesn't exist: %s"), path);
-		}
-		
-		while ((child_path = dir.read_name()) != null)
-		{
-			var child_full_path = Path.build_filename(path, child_path);
-			if (FileUtils.test(child_full_path, FileTest.IS_DIR))
-			{
-				recursive_delete(child_full_path);
-			}
-			else // the path is a file
-			{
-				FileUtils.unlink(child_full_path);
-			}
-		}
-		
-		DirUtils.remove(path);
-	}
 }
diff --git a/src/ease-utilities.vala b/src/ease-utilities.vala
index ba9bb8d..a7954db 100644
--- a/src/ease-utilities.vala
+++ b/src/ease-utilities.vala
@@ -94,6 +94,7 @@ namespace Ease
 	 * and another for directories. These callbacks can both be null
 	 * (although if they both were, the call would do nothing). The directory
 	 * callback is executed before the recursion continues.
+	 * recursive_directory_after does the opposite.
 	 *
 	 * The directory callback is not performed on the toplevel directory.
 	 *
@@ -107,7 +108,39 @@ namespace Ease
 	                                RecursiveDirAction? file_action)
 	                                throws Error
 	{
-		do_recursive_directory(directory, directory_action, file_action, "");
+		do_recursive_directory(directory,
+		                       directory_action,
+		                       file_action,
+		                       "",
+		                       true);
+	}
+	
+	/**
+	 * Performs a recursive iteration on a directory, with callbacks.
+	 *
+	 * The caller can provide two { link RecursiveDirAction}s: one for files,
+	 * and another for directories. These callbacks can both be null
+	 * (although if they both were, the call would do nothing). The directory
+	 * callback is executed after the recursion continues. recursive_directory
+	 * does the opposite.
+	 *
+	 * The directory callback is not performed on the toplevel directory.
+	 *
+	 * @param directory The directory to iterate.
+	 * @param directory_action A { link RecursiveDirAction} to perform on all
+	 * directories.
+	 * @param file_action A { link RecursiveDirAction} to perform on all files.
+	 */
+	public void recursive_directory_after(string directory,
+	                                      RecursiveDirAction? directory_action,
+	                                      RecursiveDirAction? file_action)
+	                                      throws Error
+	{
+		do_recursive_directory(directory,
+		                       directory_action,
+		                       file_action,
+		                       "",
+		                       false);
 	}
 	
 	/**
@@ -117,7 +150,8 @@ namespace Ease
 	private void do_recursive_directory(string directory,
 	                                    RecursiveDirAction? directory_action,
 	                                    RecursiveDirAction? file_action,
-	                                    string rel_path)
+	                                    string rel_path,
+	                                    bool dir_first)
 	                                    throws Error
 	{
 		var dir = GLib.Dir.open(directory, 0);
@@ -129,13 +163,21 @@ namespace Ease
 			var child_rel_path = Path.build_filename(rel_path, child_path);
 			if (FileUtils.test(child_full_path, FileTest.IS_DIR))
 			{
-				if (directory_action != null)
+				if (directory_action != null && dir_first)
 				{
 					directory_action(child_rel_path, child_full_path);
 				}
+				
+				// recurse
 				do_recursive_directory(child_full_path,
 				                       directory_action, file_action,
-				                       child_rel_path);
+				                       child_rel_path,
+				                       dir_first);
+				
+				if (directory_action != null && !dir_first)
+				{
+					directory_action(child_rel_path, child_full_path);
+				}
 			}
 			else // the path is a file
 			{
@@ -148,6 +190,32 @@ namespace Ease
 	}
 	
 	public delegate void RecursiveDirAction(string path, string full_path);
+	
+	/**
+	 * Recursively removes a directory.
+	 *
+	 * @param path The directory to be recursively deleted.
+	 */
+	public static void recursive_delete(string path) throws GLib.Error
+	{
+		var dir = GLib.Dir.open(path, 0);
+		
+		if (dir == null)
+		{
+			throw new FileError.NOENT(
+				_("Directory to remove doesn't exist: %s"), path);
+		}
+		
+		recursive_directory_after(path,
+			(p, full_path) => {
+				DirUtils.remove(full_path);
+			},
+			(p, full_path) => {
+				FileUtils.unlink(full_path);
+			});
+		
+		DirUtils.remove(path);	
+	}
 
 	public double dmax(double a, double b)
 	{



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]