changeset 761:99356cfde2f0

remove horrible java zip
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 Jul 2016 01:52:20 -0600
parents 2a91bde4e1e1
children 3f461f85243d
files core/src/luan/modules/IoLuan.java lucene/src/luan/modules/lucene/Lucene.luan
diffstat 2 files changed, 30 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
diff -r 2a91bde4e1e1 -r 99356cfde2f0 core/src/luan/modules/IoLuan.java
--- a/core/src/luan/modules/IoLuan.java	Tue Jul 19 19:15:02 2016 -0600
+++ b/core/src/luan/modules/IoLuan.java	Wed Jul 20 01:52:20 2016 -0600
@@ -32,9 +32,6 @@
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-import java.util.zip.ZipOutputStream;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -228,25 +225,6 @@
 			}
 		}
 
-		public void unzip(Object path) throws IOException, LuanException {
-			File pathFile = objToFile(path);
-			if( pathFile==null )
-				throw new LuanException( "bad argument #1 to 'unzip' (string or file table expected)" );
-			ZipInputStream in = new ZipInputStream(new BufferedInputStream(inputStream()));
-			ZipEntry entry;
-			while( (entry = in.getNextEntry()) != null ) {
-				if( entry.isDirectory() )
-					continue;
-				File file = new File(pathFile,entry.getName());
-				file.getParentFile().mkdirs();
-				OutputStream out = new FileOutputStream(file);
-				Utils.copyAll(in,out);
-				out.close();
-				file.setLastModified(entry.getTime());
-			}
-			in.close();
-		}
-
 		public LuanTable table() {
 			LuanTable tbl = new LuanTable();
 			try {
@@ -272,9 +250,6 @@
 				tbl.rawPut( "exists", new LuanJavaFunction(
 					LuanIn.class.getMethod( "exists" ), this
 				) );
-				tbl.rawPut( "unzip", new LuanJavaFunction(
-					LuanIn.class.getMethod( "unzip", Object.class ), this
-				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
@@ -351,55 +326,6 @@
 			return binaryWriter(new BufferedOutputStream(outputStream()));
 		}
 
-		public void zip(LuanState luan,Object basePathObj,LuanTable filePathList) throws LuanException, IOException {
-			File basePathFile = objToFile(basePathObj);
-			if( basePathFile==null )
-				throw new LuanException( "bad argument #1 to 'zip' (string or file table expected)" );
-			String[] filePaths;
-			if( filePathList==null ) {
-				File file = basePathFile.getCanonicalFile();
-				filePaths = new String[]{file.toString()};
-				basePathFile = file.getParentFile();
-			} else {
-				List list = filePathList.asList();
-				filePaths = new String[list.size()];
-				for( int i=0; i<filePaths.length; i++ ) {
-					Object obj = list.get(i);
-					if( !(obj instanceof String) )
-						throw new LuanException("file paths must be strings");
-					filePaths[i] = (String)obj;
-				}
-			}
-			String basePath = basePathFile.toString() + '/';
-			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream()));
-			zip(out,basePath,filePaths);
-			out.close();
-		}
-
-		private static void zip(ZipOutputStream out,String basePath,String[] filePaths) throws LuanException, IOException {
-			for( String filePath : filePaths ) {
-				File file = new File(filePath);
-				if( file.isDirectory() ) {
-					String[] children = file.list();
-					for( int i=0; i<children.length; i++ ) {
-						children[i] = filePath + "/" + children[i];
-					}
-					zip(out,basePath,children);
-				} else {
-					if( !filePath.startsWith(basePath) )
-						throw new LuanException(filePath+" not in "+basePath);
-					String relPath = filePath.substring(basePath.length());
-					ZipEntry entry = new ZipEntry(relPath);
-					entry.setTime(file.lastModified());
-					out.putNextEntry(entry);
-					InputStream in = new FileInputStream(file);
-					Utils.copyAll(in,out);
-					in.close();
-					out.closeEntry();
-				}
-			}
-		}
-
 		@Override public LuanTable table() {
 			LuanTable tbl = super.table();
 			try {
@@ -412,9 +338,6 @@
 				tbl.rawPut( "binary_writer", new LuanJavaFunction(
 					LuanIO.class.getMethod( "binary_writer" ), this
 				) );
-				tbl.rawPut( "zip", new LuanJavaFunction(
-					LuanIO.class.getMethod( "zip", LuanState.class, Object.class, LuanTable.class ), this
-				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
@@ -725,7 +648,7 @@
 			add( schemes, "https", LuanState.class, String.class, LuanTable.class );
 			add( schemes, "luan", LuanState.class, String.class );
 			add( schemes, "stdin", LuanState.class );
-			add( schemes, "os", String.class );
+			add( schemes, "os", LuanState.class, String.class, LuanTable.class );
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
 		}
@@ -818,10 +741,22 @@
 
 
 	public static final class LuanOs extends LuanIO {
+		private final String cmd;
 		private final Process proc;
 
-		private LuanOs(String cmd) throws IOException {
-			this.proc = Runtime.getRuntime().exec(cmd);
+		private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+			this.cmd = cmd;
+			File dir = null;
+			if( options != null ) {
+				Map map = options.asMap(luan);
+				Object obj = map.remove("dir");
+				dir = objToFile(obj);
+				if( dir==null )
+					throw new LuanException( "bad option 'dir' (string or file table expected)" );
+				if( !map.isEmpty() )
+					throw new LuanException( "unrecognized options: "+map );
+			}
+			this.proc = Runtime.getRuntime().exec(cmd,null,dir);
 		}
 
 		@Override public InputStream inputStream() throws IOException {
@@ -855,7 +790,7 @@
 			int exitVal = proc.exitValue();
 			if( exitVal != 0 ) {
 				Reader err = new InputStreamReader(proc.getErrorStream());
-				String error = Utils.readAll(err);
+				String error = "error in: "+cmd+"\n"+Utils.readAll(err);
 				err.close();
 				throw new LuanException(error);
 			}
@@ -880,8 +815,8 @@
 		}
 	}
 
-	public static LuanTable os(String cmd) throws IOException {
-		return new LuanOs(cmd).table();
+	public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+		return new LuanOs(luan,cmd,options).table();
 	}
 
 
diff -r 2a91bde4e1e1 -r 99356cfde2f0 lucene/src/luan/modules/lucene/Lucene.luan
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Tue Jul 19 19:15:02 2016 -0600
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Wed Jul 20 01:52:20 2016 -0600
@@ -103,13 +103,16 @@
 	end
 
 	function index.zip(zip_file)
-		index.snapshot( function(dir,file_names)
-			local t = {}
+		index.snapshot( function(dir_path,file_names)
+			zip_file.delete()
+			local zip_path = zip_file.canonical().to_string()
+			local dir = uri("file:"..dir_path)
+			local dir_name = dir.name()
+			local options = {dir=dir.parent()}
 			for _, file_name in ipairs(file_names) do
-				t[#t+1] = dir.."/"..file_name
+				local cmd = "zip "..zip_path.." "..dir_name.."/"..file_name
+				Io.uri("os:"..cmd,options).read_text()
 			end
-			local base = uri("file:"..dir).parent().to_string()
-			zip_file.zip(base,t)
 		end )
 	end
 
@@ -120,7 +123,7 @@
 			index.zip(before_restore)
 			java_index.close()
 			lucene_dir.delete()
-			zip_file.unzip(lucene_dir.parent().to_string())
+			Io.uri("os:unzip "..zip_file.canonical().to_string(),{dir=lucene_dir.parent()}).read_text()
 			java_index.reopen()
 		end )
 	end
@@ -140,7 +143,9 @@
 
 		function Rpc.functions.lucene_restore(password,zip_file)
 			Io.password == password or error "wrong password"
-			index.restore(zip_file)
+			local backup_zip = uri("file:"..index.dir).parent().child("backup.zip")
+			backup_zip.write(zip_file)
+			index.restore(backup_zip)
 		end
 
 	else