changeset 751:558740169005

improve file delete()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 15 Jul 2016 15:56:19 -0600
parents f172f1dff4b6
children ea5f3a254b76
files blog/src/private/tools/backup.luan core/src/luan/modules/IoLuan.java
diffstat 2 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/blog/src/private/tools/backup.luan	Fri Jul 15 01:25:08 2016 -0600
+++ b/blog/src/private/tools/backup.luan	Fri Jul 15 15:56:19 2016 -0600
@@ -6,7 +6,7 @@
 return function()
 	local backup = Io.uri "site:/private/local/backup.zip"
 	backup.delete()
-	Db.backup(backup.to_string())
+	Db.backup(backup)
 
 	Io.stdout = Http.response.text_writer()
 	%>
--- a/core/src/luan/modules/IoLuan.java	Fri Jul 15 01:25:08 2016 -0600
+++ b/core/src/luan/modules/IoLuan.java	Fri Jul 15 15:56:19 2016 -0600
@@ -548,6 +548,22 @@
 			return new LuanFile(luan,tmp).table();
 		}
 
+		public void delete() throws LuanException {
+			if( file.exists() )
+				delete(file);
+		}
+
+		private static void delete(File file) throws LuanException {
+			File[] children = file.listFiles();
+			if( children != null ) {
+				for( File child : children ) {
+					delete(child);
+				}
+			}
+			if( !file.delete() )
+				throw new LuanException("couldn't delete file "+file);
+		}
+
 		@Override public LuanTable table() {
 			LuanTable tbl = super.table();
 			try {
@@ -561,7 +577,7 @@
 					File.class.getMethod( "isFile" ), file
 				) );
 				tbl.rawPut( "delete", new LuanJavaFunction(
-					File.class.getMethod( "delete" ), file
+					LuanFile.class.getMethod( "delete" ), this
 				) );
 				tbl.rawPut( "mkdir", new LuanJavaFunction(
 					File.class.getMethod( "mkdir" ), file