|
From: <chr...@us...> - 2009-03-31 06:28:11
|
Revision: 5195
http://jnode.svn.sourceforge.net/jnode/?rev=5195&view=rev
Author: chrisboertien
Date: 2009-03-31 06:28:08 +0000 (Tue, 31 Mar 2009)
Log Message:
-----------
refactored archive commands to fs
created new plugins org.jnode.fs.command.archive for new commands
and org.apache.tools.archive for access to tar/bzip/zip ant classes
Modified Paths:
--------------
trunk/shell/descriptors/org.jnode.shell.command.xml
Added Paths:
-----------
trunk/fs/descriptors/org.apache.tools.archive.xml
trunk/fs/descriptors/org.jnode.fs.command.archive.xml
trunk/fs/src/fs/org/jnode/fs/command/archive/
trunk/fs/src/fs/org/jnode/fs/command/archive/ArchiveCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/BUnzipCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/BZip.java
trunk/fs/src/fs/org/jnode/fs/command/archive/BZipCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/GUnzipCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/GZip.java
trunk/fs/src/fs/org/jnode/fs/command/archive/GZipCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/TarCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/UnzipCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/ZCatCommand.java
trunk/fs/src/fs/org/jnode/fs/command/archive/Zip.java
trunk/fs/src/fs/org/jnode/fs/command/archive/ZipCommand.java
Removed Paths:
-------------
trunk/shell/src/shell/org/jnode/shell/command/GUNZIPCommand.java
trunk/shell/src/shell/org/jnode/shell/command/GZIP.java
trunk/shell/src/shell/org/jnode/shell/command/GZIPCommand.java
trunk/shell/src/shell/org/jnode/shell/command/ZCATCommand.java
Added: trunk/fs/descriptors/org.apache.tools.archive.xml
===================================================================
--- trunk/fs/descriptors/org.apache.tools.archive.xml (rev 0)
+++ trunk/fs/descriptors/org.apache.tools.archive.xml 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plugin SYSTEM "jnode.dtd">
+
+<plugin id="org.apache.tools.archive"
+ name="Ant archive tools"
+ version="1.7.1"
+ provider-name="Apache Ant"
+ provider-url="http://ant.apache.org"
+ license-name="apache2.0">
+
+ <runtime>
+ <library name="ant.jar">
+ <export name="org.apache.tools.tar.*" />
+ <export name="org.apache.tools.zip.*" />
+ <export name="org.apache.tools.bzip.*" />
+ </library>
+ </runtime>
+
+ <extension point="org.jnode.security.permissions">
+ <permission class="java.io.FilePermission" name="<<ALL FILES>>" actions="read,write,delete"/>
+ <permission class="java.lang.RuntimePermission" name="setSecurityManager" actions="*" />
+ <permission class="java.lang.RuntimePermission" name="setIO" actions="*" />
+ <permission class="java.lang.RuntimePermission" name="exitVM" actions="*" />
+ <permission class="java.lang.RuntimePermission" name="createClassLoader" actions="*" />
+ <permission class="org.jnode.security.JNodePermission" name="getVmClass" actions="*" />
+ <permission class="java.util.PropertyPermission" name="*" actions="read,write"/>
+ </extension>
+</plugin>
Added: trunk/fs/descriptors/org.jnode.fs.command.archive.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.archive.xml (rev 0)
+++ trunk/fs/descriptors/org.jnode.fs.command.archive.xml 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,157 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plugin SYSTEM "jnode.dtd">
+
+<plugin id="org.jnode.fs.command.archive"
+ name="JNode archive commands"
+ version="@VERSION@"
+ license-name="lgpl"
+ provider-name="JNode.org">
+
+ <requires>
+ <import plugin="org.apache.tools.archive" />
+ <import plugin="org.jnode.shell" />
+ </requires>
+
+ <runtime>
+ <library name="jnode-fs.jar">
+ <export name="org.jnode.fs.command.archive.*" />
+ </library>
+ </runtime>
+
+ <extension point="org.jnode.shell.aliases">
+ <alias name="bzip2" class="org.jnode.fs.command.archive.BZipCommand" />
+ <alias name="bunzip2" class="org.jnode.fs.command.archive.BUnzipCommand" />
+ <alias name="bzcat" class="org.jnode.fs.command.archive.BZCatCommand" />
+ <alias name="gzip" class="org.jnode.fs.command.archive.GZipCommand" />
+ <alias name="gunzip" class="org.jnode.fs.command.archive.GUnzipCommand" />
+ <alias name="tar" class="org.jnode.fs.command.archive.TarCommand" />
+ <alias name="unzip" class="org.jnode.fs.command.archive.UnzipCommand" />
+ <alias name="zcat" class="org.jnode.fs.command.archive.ZCatCommand" />
+ <alias name="zip" class="org.jnode.fs.command.archive.ZipCommand" />
+ </extension>
+
+ <extension point="org.jnode.shell.syntaxes">
+
+ <syntax alias="tar">
+ <sequence>
+ <alternatives description="tar operations">
+ <option argLabel="doAppend" shortName="r" longName="append" />
+ <option argLabel="doCreate" shortName="c" longName="create" />
+ <option argLabel="doConcat" shortName="A" longName="concatenate" />
+ <option argLabel="doDelete" longName="delete" />
+ <option argLabel="doDiff" shortName="d" longName="diff" />
+ <option argLabel="doExtract" shortName="x" longName="extract" />
+ <option argLabel="doList" shortName="t" longName="list" />
+ <option argLabel="doUpdate" shortName="u" longName="update" />
+ </alternatives>
+ <optionSet description="tar options">
+ <option argLabel="backup" longName="backup" />
+ <option argLabel="bzip" shortName="j" longName="bzip2" />
+ <option argLabel="debug" longName="debug" />
+ <option argLabel="dir" shortName="C" longName="directory" />
+ <option argLabel="exclude" longName="exclude" label="pattern" />
+ <option argLabel="file" shortName="f" longName="file" />
+ <option argLabel="fileList" shortName="T" longName="files-from"/>
+ <option argLabel="gzip" shortName="z" longName="gzip" />
+ <option argLabel="interact" shortName="w" longName="interactive" />
+ <option argLabel="keepFiles" shortName="k" longName="keep-old-files" />
+ <option argLabel="noRecurse" longName="no-recursion" />
+ <option argLabel="recurse" longName="recursion" />
+ <option argLabel="removeFiles" longName="remove-files" />
+ <option argLabel="showTotals" longName="totals" />
+ <option argLabel="suffix" longName="suffix" />
+ <option argLabel="useStdout" shortName="O" longName="to-stdout" />
+ <option argLabel="verbose" shortName="v" longName="verbose" />
+ <option argLabel="verify" shortName="W" longName="verify" />
+ <option argLabel="xfile" shortName="X" longName="exclude-from"/>
+ <!--
+ <option argLabel="minDate" shortName="N" longName="newer" />
+ <option argLabel="minMTime" longName="newer-mtime" />
+ -->
+ </optionSet>
+ <repeat minCount="0">
+ <argument argLabel="paths" label="file/dir"/>
+ </repeat>
+ </sequence>
+ </syntax>
+ <syntax alias="gzip">
+ <empty description="compress standard input to standard output" />
+ <sequence description="compress files">
+ <optionSet>
+ <option argLabel="decompress" shortName="d" longName="decompress" />
+ <option argLabel="noname" shortName="n" longName="no-name" />
+ <option argLabel="name" shortName="N" longName="name" />
+ <option argLabel="stdout" shortName="c" longName="stdout" />
+ <option argLabel="force" shortName="f" longName="force" />
+ <option argLabel="quiet" shortName="q" longName="quiet" />
+ <option argLabel="verbose" shortName="v" longName="verbose" />
+ <option argLabel="lvl1" shortName="1" longName="fast" />
+ <option argLabel="debug" longName="debug" />
+ <option argLabel="recursive" shortName="r" longName="recursive" />
+ <option argLabel="test" shortName="t" longName="test" />
+ <option argLabel="list" shortName="l" longName="list" />
+ <option argLabel="lvl9" shortName="9" longName="best" />
+ </optionSet>
+ <optional eager="true"><option argLabel="suffix" shortName="S" longName="suffix" /></optional>
+ <repeat minCount="1">
+ <argument argLabel="file" />
+ </repeat>
+ </sequence>
+ </syntax>
+ <syntax alias="gunzip">
+ <empty description="decompress standard input to standard output" />
+ <sequence description="decompress files">
+ <optionSet>
+ <option argLabel="debug" longName="debug" />
+ <option argLabel="recursive" shortName="r" longName="recursive" />
+ <option argLabel="test" shortName="t" longName="test" />
+ <option argLabel="list" shortName="l" longName="list" />
+ <option argLabel="noname" shortName="n" longName="no-name" />
+ <option argLabel="name" shortName="N" longName="name" />
+ <option argLabel="stdout" shortName="c" longName="stdout" />
+ <option argLabel="force" shortName="f" longName="force" />
+ <option argLabel="quiet" shortName="q" longName="quiet" />
+ <option argLabel="verbose" shortName="v" longName="verbose" />
+ </optionSet>
+ <optional eager="true"><option argLabel="suffix" shortName="S" longName="suffix" /></optional>
+ <repeat minCount="1">
+ <argument argLabel="file" />
+ </repeat>
+ </sequence>
+ </syntax>
+ <syntax alias="zcat">
+ <empty description="decompress standard input to standard output" />
+ <sequence description="decompress files to standard output">
+ <optionSet>
+ <option argLabel="quiet" shortName="q" longName="quiet" />
+ <option argLabel="verbose" shortName="v" longName="verbose" />
+ <option argLabel="debug" longName="debug" />
+ </optionSet>
+ <repeat minCount="1">
+ <argument argLabel="file" />
+ </repeat>
+ </sequence>
+ </syntax>
+ <syntax alias="zip">
+ </syntax>
+ <syntax alias="unzip">
+ </syntax>
+ <syntax alias="bzip2">
+ </syntax>
+ <syntax alias="bunzip2">
+ </syntax>
+ <syntax alias="bzcat">
+ </syntax>
+ </extension>
+
+ <extension point="org.jnode.security.permissions">
+ <permission class="java.io.FilePermission" name="<<ALL FILES>>" actions="read,write,delete"/>
+ <permission class="java.net.SocketPermission" name="*:0-" actions="connect,resolve"/>
+ <permission class="java.util.PropertyPermission" name="user.dir" actions="read,write"/>
+ <permission class="java.util.PropertyPermission" name="*" actions="read,write"/>
+ <permission class="java.net.NetPermission" name="specifyStreamHandler"/>
+ <permission class="java.lang.RuntimePermission" name="modifyThreadGroup"/>
+ <permission class="java.lang.RuntimePermission" name="exitVM"/>
+ <permission class="org.jnode.security.JNodePermission" name="getVmClass"/>
+ </extension>
+</plugin>
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/ArchiveCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/ArchiveCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/ArchiveCommand.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,185 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+import org.jnode.shell.AbstractCommand;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.IOException;
+
+public class ArchiveCommand extends AbstractCommand {
+
+ protected static final int OUT_ERROR = 0x01;
+ protected static final int OUT_WARN = 0x02;
+ protected static final int OUT_NOTICE = 0x04;
+ protected static final int OUT_DEBUG = 0x80;
+
+ protected int outMode = OUT_ERROR | OUT_WARN;
+
+ protected PrintWriter stdoutWriter;
+ protected PrintWriter stderrWriter;
+ protected Reader stdinReader;
+
+ private byte[] buffer;
+
+ protected ArchiveCommand(String s) {
+ super(s);
+ }
+
+ protected void setup() {
+ stdoutWriter = getOutput().getPrintWriter();
+ stderrWriter = getError().getPrintWriter();
+ stdinReader = getInput().getReader();
+ }
+
+ protected void createStreamBuffer(int size) {
+ buffer = new byte[size];
+ }
+
+ /**
+ * Pipes the contents of the InputStream into the OutputStream.
+ *
+ * This is most usefull for applying a stream filter that reads data from a source
+ * and pipes the contents to an output stream.
+ *
+ * @param InputStream stream to read from
+ * @param OutputStream stream to write to
+ * @param int size of buffer to use.
+ */
+ protected void processStream(InputStream in , OutputStream out) throws IOException {
+ int len;
+ if (buffer == null) buffer = new byte[4096];
+ while ((len = in.read(buffer)) > 0) {
+ out.write(buffer, 0, len);
+ }
+ }
+
+ /**
+ * Opens a FileOutputStream for a file.
+ *
+ * If there is a problem opening the stream, the exception is caught and an error message
+ * is displayed.
+ *
+ * @param File the file to open the stream on
+ * @param boolean if the file exists, delete it first
+ * @param boolean if delete is true, this forces the deletion without prompting the user
+ * @return an OutputStream on the file, or null if there was a problem. null could also be
+ * returned if the delete option was chosen and the user said no to overwriting.
+ */
+ protected OutputStream openFileWrite(File file , boolean delete , boolean forced) {
+ try {
+ boolean createNew = true;
+ if (file.exists()) {
+ if (delete) {
+ if (forced) {
+ file.delete();
+ } else {
+ if (prompt_yn(file + "exists. Overwrite? ")) {
+ file.delete();
+ } else {
+ notice("Skipping " + file);
+ return null;
+ }
+ }
+ } else {
+ return new FileOutputStream(file);
+ }
+ }
+ if (createNew && !file.createNewFile()) {
+ error("Could not create file: " + file);
+ return null;
+ }
+ return new FileOutputStream(file);
+ } catch (IOException ioe) {
+ error("Could not open stream: " + file + " : " + ioe.getLocalizedMessage());
+ return null;
+ }
+ }
+
+ /**
+ * Opens a FileInputStream on a file.
+ *
+ * If there is a problem opening the stream, the IOException is caught, and an
+ * error message displayed to the console.
+ *
+ * @param the file to open the stream on
+ * @return the InputStream or null if there was a problem.
+ */
+ protected InputStream openFileRead(File file) {
+ try {
+ return new FileInputStream(file);
+ } catch (IOException ioe) {
+ error("Cannot open stream: " + file + " : " + ioe.getLocalizedMessage());
+ return null;
+ }
+ }
+
+ /**
+ * Prompt the user with a question asking for a yes or no answer.
+ *
+ * @param String the question to ask the user
+ * @return true if the user said yes, false if the user said no
+ */
+ protected boolean prompt_yn(String s) {
+ int choice;
+ for (;;) {
+ stdoutWriter.print(s + " [y/n]");
+ try {
+ choice = stdinReader.read();
+ } catch (IOException _) {
+ choice = 0;
+ }
+ stdoutWriter.println();
+ if (choice == 'y' || choice == 'n') break;
+ }
+
+ return choice == 'y';
+ }
+
+ protected void out(String s) {
+ stdoutWriter.println(s);
+ }
+
+ protected void debug(String s) {
+ if ((outMode & OUT_DEBUG) == OUT_DEBUG) {
+ stderrWriter.print("debug: ");
+ stderrWriter.println(s);
+ }
+ }
+
+ protected void notice(String s) {
+ if ((outMode & OUT_NOTICE) == OUT_NOTICE) stdoutWriter.println(s);
+ }
+
+ protected void warn(String s) {
+ if ((outMode & OUT_WARN) == OUT_WARN) stdoutWriter.println(s);
+ }
+
+ protected void error(String s) {
+ if ((outMode & OUT_ERROR) == OUT_ERROR) stderrWriter.println(s);
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/BUnzipCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/BUnzipCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/BUnzipCommand.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,33 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+public class BUnzipCommand extends BZip {
+
+ public BUnzipCommand() {
+ super("decompresses data in bzip2 format");
+ registerArguments();
+ }
+
+ public void execute() {
+ setup();
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/BZip.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/BZip.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/BZip.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,28 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+public class BZip extends ArchiveCommand {
+
+ public BZip(String s) {
+ super(s);
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/BZipCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/BZipCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/BZipCommand.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,32 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+public class BZipCommand extends BZip {
+
+ public BZipCommand() {
+ super("compresses data with bzip2");
+ }
+
+ public void execute() {
+ setup();
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/GUnzipCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/GUnzipCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/GUnzipCommand.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,97 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.StringArgument;
+
+/**
+ * Decompresses data in the gzip format.
+ *
+ * If a list of files is given, the files will be decompressed. The original files will be deleted
+ * and replaced with decompressed versions unless told to write to standard output, then the original
+ * files are left as-is.
+ *
+ * If no files are given, then standard input is decompressed to standard output.
+ *
+ * @author Chris Boertien
+ */
+
+public class GUnzipCommand extends GZip {
+
+ private static final String msg_stdout = "Write output on standard output, keep original files";
+ private static final String msg_force = "force overwrite of output files and compress links";
+ private static final String msg_list = "list compressed file contents";
+ private static final String msg_noname = "do not save or restore the original name and time stamp";
+ private static final String msg_name = "save or restore the original name and time stamp";
+ private static final String msg_quiet = "suppress all warning";
+ private static final String msg_recurse = "operate recursively on directories";
+ private static final String msg_suffix = "use suffix SUF on compressed files";
+ private static final String msg_test = "test compressed file integrity";
+ private static final String msg_verbose = "verbose mode";
+ private static final String msg_file = "the files to compress, use stdin if FILE is '-' or no files are listed";
+
+ private final FileArgument ArgFile = new FileArgument("file", Argument.OPTIONAL | Argument.MULTIPLE, msg_file);
+ private final StringArgument ArgSuffix = new StringArgument("suffix", Argument.OPTIONAL, msg_suffix);
+ private final FlagArgument ArgStdout = new FlagArgument("stdout", Argument.OPTIONAL, msg_stdout);
+ private final FlagArgument ArgForce = new FlagArgument("force", Argument.OPTIONAL, msg_force);
+ private final FlagArgument ArgList = new FlagArgument("list", Argument.OPTIONAL, msg_list);
+ private final FlagArgument ArgNoname = new FlagArgument("noname", Argument.OPTIONAL, msg_noname);
+ private final FlagArgument ArgName = new FlagArgument("name", Argument.OPTIONAL, msg_name);
+ private final FlagArgument ArgQuiet = new FlagArgument("quiet", Argument.OPTIONAL, msg_quiet);
+ private final FlagArgument ArgRecurse = new FlagArgument("recursive", Argument.OPTIONAL, msg_recurse);
+ private final FlagArgument ArgTest = new FlagArgument("test", Argument.OPTIONAL, msg_test);
+ private final FlagArgument ArgVerbose = new FlagArgument("verbose", Argument.OPTIONAL, msg_verbose);
+ private final FlagArgument ArgDebug = new FlagArgument("debug", Argument.OPTIONAL, " ");
+
+ public GUnzipCommand() {
+ super("decompresses files/data");
+ registerArguments(ArgFile, ArgSuffix, ArgStdout, ArgForce, ArgList, ArgNoname, ArgName, ArgQuiet, ArgRecurse,
+ ArgTest, ArgVerbose, ArgDebug);
+ }
+
+ public void execute() {
+ if (ArgQuiet.isSet()) {
+ outMode = 0;
+ } else {
+ if (ArgDebug.isSet()) {
+ outMode |= OUT_DEBUG;
+ }
+ if (ArgVerbose.isSet()) {
+ outMode |= OUT_NOTICE;
+ }
+ }
+
+ if (ArgSuffix.isSet()) suffix = ArgSuffix.getValue();
+
+ if (ArgList.isSet()) mode = GZIP_LIST;
+ else if (ArgTest.isSet()) mode = GZIP_TEST;
+ else mode = GZIP_DECOMPRESS;
+
+ try {
+ execute(ArgFile.getValues(), ArgForce.isSet(), ArgStdout.isSet(), ArgRecurse.isSet());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/GZip.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/GZip.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/GZip.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,220 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+/**
+ *
+ * TODO implement test/list
+ * TODO use GZIP env variable
+ * TODO implement name/noname flag usage
+ * @author chris boertien
+ */
+public class GZip extends ArchiveCommand {
+
+ private static final String msg_exists_prompt = " already exists; do you wish to overwrite (y or n)? ";
+ private static final String msg_exists_skip = "gzip: skipping file: ";
+ private static final String msg_err_open = "gzip: Cannot open file: ";
+ private static final String msg_err_create = "gzip: Cannot create file: ";
+ private static final String msg_err_bad_suffix = "gzip: Invalid suffix, expecting ";
+
+ private static final int BUFFER_SIZE = 4096;
+
+ private File[] files;
+ private InputStream stdinStream;
+ private OutputStream stdoutStream;
+
+ protected static final int GZIP_LIST = 1;
+ protected static final int GZIP_TEST = 2;
+ protected static final int GZIP_DECOMPRESS = 3;
+ protected static final int GZIP_COMPRESS = 4;
+
+ protected String suffix = ".gz";
+ protected int mode;
+
+ protected GZip(String s) {
+ super(s);
+ createStreamBuffer(BUFFER_SIZE);
+ }
+
+ public void execute(File[] files , boolean forced , boolean use_stdout , boolean recurse) throws IOException {
+ setup();
+ stdinStream = getInput().getInputStream();
+ stdoutStream = getOutput().getOutputStream();
+
+ switch(mode) {
+ case GZIP_LIST :
+ list(processFiles(files, recurse)); return;
+ case GZIP_TEST :
+ test(processFiles(files, recurse)); return;
+ case GZIP_COMPRESS :
+ compress(processFiles(files, recurse), forced, use_stdout); return;
+ case GZIP_DECOMPRESS :
+ decompress(processFiles(files, recurse), forced, use_stdout); return;
+ }
+ }
+
+ protected void compress(File[] files , boolean forced , boolean use_stdout) throws IOException {
+ InputStream in;
+ OutputStream out = null;
+ GZIPOutputStream gzout = null;
+ File gzFile = null;
+ float sizeDiff;
+
+ debug("Compress");
+ debug("forced=" + forced);
+ debug("use_stdout=" + use_stdout);
+
+ if (files == null) {
+ debug("stdin > stdout");
+ processStream(stdinStream, new GZIPOutputStream(stdoutStream, BUFFER_SIZE));
+ } else {
+ if (use_stdout) gzout = new GZIPOutputStream(stdoutStream, BUFFER_SIZE);
+ if (use_stdout) debug("files > stdout");
+ else debug("files > files");
+
+ for (File file : files) {
+ debug(file.getName());
+ if (!file.exists()) {
+ error("File does not exist: " + file.getName());
+ continue;
+ }
+ if (!use_stdout) {
+ gzFile = new File(file.getAbsolutePath() + suffix);
+ if ((out = openFileWrite(gzFile, true, forced)) == null) continue;
+ gzout = new GZIPOutputStream(out, BUFFER_SIZE);
+ }
+ if ((in = openFileRead(file)) == null) continue;
+ processStream(in, gzout);
+ if (!use_stdout) {
+ gzout.finish();
+ gzout.close();
+ sizeDiff = ((float) gzFile.length() / (float) file.length()) * 100;
+ notice(file + ":\t" + sizeDiff + "% -- replaced with " + gzFile);
+ file.delete();
+ }
+ in.close();
+ }
+ if (use_stdout) gzout.finish();
+ }
+ }
+
+ protected void decompress(File[] files , boolean forced , boolean use_stdout) throws IOException {
+ InputStream in;
+ OutputStream out = null;
+ String name;
+ File file = null;
+ float sizeDiff;
+
+ debug("Decompress");
+ debug("forced=" + forced);
+ debug("use_stdout=" + use_stdout);
+
+ if (use_stdout) out = stdoutStream;
+
+ if (files == null) {
+ debug("stdin > stdout");
+ processStream(new GZIPInputStream(stdinStream, BUFFER_SIZE), stdoutStream);
+ } else {
+ if (use_stdout) debug("files > stdout");
+ else debug("files > files");
+
+ for (File gzFile : files) {
+ debug(gzFile.getName());
+ if (!gzFile.exists()) {
+ error("File not found: " + gzFile);
+ continue;
+ }
+ if (!gzFile.getName().endsWith(suffix)) {
+ notice("gzip: " + file + ": unknown suffix -- ignored");
+ continue;
+ }
+ if (!use_stdout) {
+ name = gzFile.getAbsolutePath();
+ name = name.substring(0, name.length() - suffix.length());
+ file = new File(name);
+ if ((out = openFileWrite(file, true, forced)) == null) continue;
+ }
+
+ if ((in = new GZIPInputStream(openFileRead(gzFile), BUFFER_SIZE)) == null) continue;
+ processStream(in, out);
+ in.close();
+ if (!use_stdout) {
+ sizeDiff = ((float) gzFile.length() / (float) file.length()) * 100;
+ notice(gzFile + ":\t" + sizeDiff + "% -- replaced with " + file);
+ gzFile.delete();
+ out.close();
+ }
+ }
+ }
+ }
+
+ protected void test(File[] files) {}
+
+ protected void list(File[] files) {}
+
+ private File[] processFiles(File[] files , boolean recurse) {
+ if (files == null || files.length == 0) return null;
+
+ debug("processFiles(files(" + files.length + ")," + recurse + ")");
+
+ ArrayList<File> _files = new ArrayList<File>();
+
+ for (File file : files) {
+ debug(file.getName());
+ if (file.getName().equals("-")) {
+ debug("found stdin");
+ return null;
+ }
+ if (!file.exists()) {
+ error("Cannot find file: " + file);
+ continue;
+ }
+ if (file.isDirectory() && recurse) {
+ debug("searching directory: " + file);
+ File[] dirList = file.listFiles();
+ for (File subFile : dirList) {
+ debug(subFile.getName());
+ if (subFile.isFile()) {
+ debug(subFile + " added");
+ _files.add(subFile);
+ }
+ }
+ } else {
+ if (file.isFile()) {
+ debug(file + " added");
+ _files.add(file);
+ }
+ }
+ }
+
+ if (_files.size() == 0) return null;
+ debug("Found " + _files.size() + " files");
+ return _files.toArray(files);
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/GZipCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/GZipCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/GZipCommand.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,106 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.StringArgument;
+
+/**
+ * Compresses or decompresses data in the gzip format.
+ *
+ * If a list of files is given, the files will be (de)compressed. The original files will be deleted
+ * and replaced with (de)compressed versions unless told to write to standard output, then the original
+ * files are left as-is.
+ *
+ * If no files are given, then standard input is (de)compressed to standard output.
+ *
+ * @author Chris Boertien
+ */
+public class GZipCommand extends GZip {
+
+ private static final String msg_stdout = "Write output on standard output, keep original files";
+ private static final String msg_decomp = "decompress";
+ private static final String msg_force = "force overwrite of output files and compress links";
+ private static final String msg_list = "list compressed file contents";
+ private static final String msg_noname = "do not save or restore the original name and time stamp";
+ private static final String msg_name = "save or restore the original name and time stamp";
+ private static final String msg_quiet = "suppress all warning";
+ private static final String msg_recurse = "operate recursively on directories";
+ private static final String msg_suffix = "use suffix SUF on compressed files";
+ private static final String msg_test = "test compressed file integrity";
+ private static final String msg_verbose = "verbose mode";
+ private static final String msg_fast = "compress faster";
+ private static final String msg_best = "compress better";
+ private static final String msg_file = "the files to compress, use stdin if FILE is '-' or no files are listed";
+
+ private final FileArgument ArgFile = new FileArgument("file", Argument.OPTIONAL | Argument.MULTIPLE, msg_file);
+ private final StringArgument ArgSuffix = new StringArgument("suffix", Argument.OPTIONAL, msg_suffix);
+ private final FlagArgument ArgStdout = new FlagArgument("stdout", Argument.OPTIONAL, msg_stdout);
+ private final FlagArgument ArgDecomp = new FlagArgument("decompress", Argument.OPTIONAL, msg_decomp);
+ private final FlagArgument ArgForce = new FlagArgument("force", Argument.OPTIONAL, msg_force);
+ private final FlagArgument ArgList = new FlagArgument("list", Argument.OPTIONAL, msg_list);
+ private final FlagArgument ArgNoname = new FlagArgument("noname", Argument.OPTIONAL, msg_noname);
+ private final FlagArgument ArgName = new FlagArgument("name", Argument.OPTIONAL, msg_name);
+ private final FlagArgument ArgQuiet = new FlagArgument("quiet", Argument.OPTIONAL, msg_quiet);
+ private final FlagArgument ArgRecurse = new FlagArgument("recursive", Argument.OPTIONAL, msg_recurse);
+ private final FlagArgument ArgTest = new FlagArgument("test", Argument.OPTIONAL, msg_test);
+ private final FlagArgument ArgVerbose = new FlagArgument("verbose", Argument.OPTIONAL, msg_verbose);
+ private final FlagArgument ArgLvl1 = new FlagArgument("lvl1", Argument.OPTIONAL, msg_fast);
+ private final FlagArgument ArgLvl9 = new FlagArgument("lvl9", Argument.OPTIONAL, msg_best);
+ private final FlagArgument ArgDebug = new FlagArgument("debug", Argument.OPTIONAL, " ");
+
+ public GZipCommand() {
+ super("compresses and decompresses files/data");
+ registerArguments(ArgFile, ArgSuffix, ArgDecomp, ArgNoname, ArgName, ArgStdout, ArgForce,
+ ArgQuiet, ArgVerbose, ArgLvl1, ArgLvl9, ArgRecurse, ArgTest, ArgList, ArgDebug);
+ }
+
+ public void execute() {
+ if (ArgQuiet.isSet()) {
+ outMode = 0;
+ } else {
+ if (ArgDebug.isSet()) {
+ outMode |= OUT_DEBUG;
+ }
+ if (ArgVerbose.isSet()) {
+ outMode |= OUT_NOTICE;
+ }
+ }
+
+ if (ArgSuffix.isSet()) suffix = ArgSuffix.getValue();
+
+ if (ArgList.isSet()) mode = GZIP_LIST;
+ else if (ArgTest.isSet()) mode = GZIP_TEST;
+ else if (ArgDecomp.isSet()) mode = GZIP_DECOMPRESS;
+ else mode = GZIP_COMPRESS;
+
+ try {
+ execute(ArgFile.getValues(), ArgForce.isSet(), ArgStdout.isSet(), ArgRecurse.isSet());
+ } catch (Exception e) {
+ e.printStackTrace();
+ exit(1);
+ }
+ }
+
+
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/TarCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/TarCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/TarCommand.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,396 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+import org.apache.tools.tar.TarEntry;
+import org.apache.tools.tar.TarInputStream;
+import org.apache.tools.tar.TarOutputStream;
+
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.StringArgument;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+public class TarCommand extends ArchiveCommand {
+
+ private static final String help_append = "append entries to an archive";
+ private static final String help_concat = "concatenate multiple archives";
+ private static final String help_create = "create a new tar archive";
+ private static final String help_delete = "delete entries from an archive";
+ private static final String help_diff = "find differences between the archive and file system";
+ private static final String help_extract = "extract the entries from an archive";
+ private static final String help_list = "list the contents of an archive";
+ private static final String help_update = "only append files that are newer than the copy in the archive";
+
+ private static final String help_backup = "backup files instead of overwriting";
+ private static final String help_bzip = "compress the archive with bzip2";
+ private static final String help_dir = "change to directory";
+ private static final String help_exclude = "exclude files matching <pattern>";
+ private static final String help_file = "use the given archive";
+ private static final String help_file_list = "get names to extract or archive from <file>";
+ private static final String help_gzip = "compress the archive with gzip";
+ private static final String help_interact = "ask for confirmation for every action";
+ private static final String help_keep_old = "keep existing files; don't overwrite from archive";
+ private static final String help_norecurse = "do not recurse into directories";
+ private static final String help_paths = "files and directories to include in archive";
+ private static final String help_recurse = "recurse into directories";
+ private static final String help_remove = "remove files after adding them to the archive";
+ private static final String help_stdout = "extract files to stdout";
+ private static final String help_suffix = "append <suffix> to backup files (default ~)";
+ private static final String help_totals = "display total bytes written after creating the archive";
+ private static final String help_verbose = "list files processed";
+ private static final String help_verify = "verify the archive after writing it";
+ private static final String help_xfile = "exclude files matching patterns in <file>";
+
+ private static final int TAR_APPEND = 0x01;
+ private static final int TAR_CREATE = 0x02;
+ private static final int TAR_CONCAT = 0x04;
+ private static final int TAR_DELETE = 0x08;
+ private static final int TAR_UPDATE = 0x10;
+ private static final int TAR_LIST = 0x20;
+ private static final int TAR_DIFF = 0x40;
+ private static final int TAR_EXTRACT = 0x80;
+ private static final int TAR_INSERT = TAR_APPEND | TAR_CREATE;
+
+ private final FlagArgument DoAppend = new FlagArgument("doAppend", Argument.OPTIONAL, help_append);
+ private final FlagArgument DoConcat = new FlagArgument("doConcat", Argument.OPTIONAL, help_concat);
+ private final FlagArgument DoCreate = new FlagArgument("doCreate", Argument.OPTIONAL, help_create);
+ private final FlagArgument DoDelete = new FlagArgument("doDelete", Argument.OPTIONAL, help_delete);
+ private final FlagArgument DoDiff = new FlagArgument("doDiff", Argument.OPTIONAL, help_diff);
+ private final FlagArgument DoExtract = new FlagArgument("doExtract", Argument.OPTIONAL, help_extract);
+ private final FlagArgument DoList = new FlagArgument("doList", Argument.OPTIONAL, help_list);
+ private final FlagArgument DoUpdate = new FlagArgument("doUpdate", Argument.OPTIONAL, help_update);
+
+ private final FlagArgument Backup = new FlagArgument("backup", Argument.OPTIONAL, help_backup);
+ private final FlagArgument UseBzip = new FlagArgument("bzip", Argument.OPTIONAL, help_bzip);
+ private final FlagArgument Debug = new FlagArgument("debug", Argument.OPTIONAL, " ");
+ private final FileArgument ChangeDir = new FileArgument("dir", Argument.OPTIONAL, help_dir);
+ private final StringArgument Exclude = new StringArgument("exclude", Argument.OPTIONAL, help_exclude);
+ private final FileArgument Archive = new FileArgument("file", Argument.OPTIONAL, help_file);
+ private final FileArgument FileList = new FileArgument("fileList", Argument.OPTIONAL, help_file_list);
+ private final FlagArgument UseGzip = new FlagArgument("gzip", Argument.OPTIONAL, help_gzip);
+ private final FlagArgument Interact = new FlagArgument("interact", Argument.OPTIONAL, help_interact);
+ private final FlagArgument KeepFiles = new FlagArgument("keepFiles", Argument.OPTIONAL, help_keep_old);
+ private final FlagArgument NoRecurse = new FlagArgument("noRecurse", Argument.OPTIONAL, help_norecurse);
+ private final FlagArgument Recurse = new FlagArgument("recurse", Argument.OPTIONAL, help_recurse);
+ private final FlagArgument RemoveFiles = new FlagArgument("removeFiles", Argument.OPTIONAL, help_remove);
+ private final FlagArgument ShowTotals = new FlagArgument("showTotals", Argument.OPTIONAL, help_totals);
+ private final StringArgument Suffix = new StringArgument("suffix", Argument.OPTIONAL, help_suffix);
+ private final FlagArgument UseStdout = new FlagArgument("useStdout", Argument.OPTIONAL, help_stdout);
+ private final FlagArgument Verbose = new FlagArgument("verbose", Argument.OPTIONAL, help_verbose);
+ private final FlagArgument Verify = new FlagArgument("verify", Argument.OPTIONAL, help_verify);
+ private final FileArgument ExcludeFile = new FileArgument("xfile", Argument.OPTIONAL, help_xfile);
+
+ private final FileArgument Paths = new FileArgument("paths", Argument.OPTIONAL | Argument.MULTIPLE, help_paths);
+
+ private File archive;
+ private File excludeFile;
+ private File fileList;
+ private String suffix = "~";
+ private String exclude = "";
+ private int mode;
+ private boolean recurse;
+ private boolean pipeInOut;
+ private boolean backup;
+ private boolean bzip;
+ private boolean gzip;
+ private boolean interact;
+ private boolean verify;
+ private boolean use_stdout;
+ private boolean showTotals;
+ private boolean keepOldFiles;
+
+ public TarCommand() {
+ super("Create/Modify/Extract tape archives");
+ registerArguments(DoAppend, DoConcat, DoCreate, DoDelete, DoDiff, DoExtract, DoList, DoUpdate,
+ Backup, UseBzip, Debug, ChangeDir, Exclude, Archive, FileList, UseGzip, Interact,
+ KeepFiles, NoRecurse, Recurse, RemoveFiles, ShowTotals, Suffix, UseStdout, Verbose,
+ Verify, Paths, ExcludeFile);
+ }
+
+ public void execute() {
+ setup();
+ if (!checkMode()) {
+ error("required options -Acdtrux not found, or multiple options set");
+ exit(1);
+ }
+ if (DoAppend.isSet()) mode = TAR_APPEND;
+ else if (DoConcat.isSet()) mode = TAR_CONCAT;
+ else if (DoCreate.isSet()) mode = TAR_CREATE;
+ else if (DoDelete.isSet()) mode = TAR_DELETE;
+ else if (DoDiff.isSet()) mode = TAR_DIFF;
+ else if (DoExtract.isSet()) mode = TAR_EXTRACT;
+ else if (DoList.isSet()) mode = TAR_LIST;
+ else if (DoUpdate.isSet()) mode = TAR_UPDATE;
+
+ if (Debug.isSet()) outMode |= OUT_DEBUG;
+ if (Verbose.isSet()) outMode |= OUT_NOTICE;
+
+ if (Suffix.isSet()) suffix = Suffix.getValue();
+ if (Exclude.isSet()) exclude = Exclude.getValue();
+ if (ExcludeFile.isSet()) excludeFile = ExcludeFile.getValue();
+ if (FileList.isSet()) fileList = FileList.getValue();
+
+ backup = Backup.isSet();
+ bzip = UseBzip.isSet();
+ gzip = UseGzip.isSet();
+ interact = Interact.isSet();
+ verify = Verify.isSet();
+ use_stdout = UseStdout.isSet();
+ showTotals = ShowTotals.isSet();
+ keepOldFiles = KeepFiles.isSet();
+ recurse = !NoRecurse.isSet();
+ if (Archive.isSet()) archive = Archive.getValue();
+ else error("No archive given");
+ //if (!(pipeInOut = !Archive.isSet())) archive = Archive.getValue();
+
+ debug("Mode: " + mode);
+ debug("Archive: " + archive);
+ debug("Suffix: " + suffix);
+ debug("Exclude: " + exclude);
+ debug("Exclude File: " + excludeFile);
+ debug("File List: " + fileList);
+ debug("Backup: " + backup);
+ debug("BZip: " + bzip);
+ debug("GZip: " + gzip);
+ debug("Interactive: " + interact);
+ debug("Recurse: " + recurse);
+ debug("Verify: " + verify);
+ debug("Use StdOut: " + use_stdout);
+ debug("Keep Old Files: " + keepOldFiles);
+ debug("Show Totals: " + showTotals);
+ debug("pipeInOut: " + pipeInOut);
+
+ try {
+ if ((mode & TAR_INSERT) != 0) {
+ File[] files;
+ files = processFiles(Paths.getValues(), recurse);
+ insert(files);
+ return;
+ }
+ if ((mode & TAR_EXTRACT) != 0) {
+ extract();
+ }
+ if ((mode & TAR_LIST) != 0) {
+ list();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ exit(2);
+ }
+ }
+
+ private void insert(File[] files) throws IOException {
+ debug("insert");
+ InputStream in;
+ OutputStream out;
+ TarOutputStream tout;
+ TarEntry entry;
+
+ if (!use_stdout) {
+ if (mode == TAR_CREATE || (mode == TAR_APPEND && !archive.exists())) createArchive();
+
+ if ((out = openFileWrite(archive, false, false)) == null) {
+ error("Could not open stream: " + archive);
+ exit(1);
+ }
+ } else {
+ debug("out=stdout");
+ out = getOutput().getOutputStream();
+ }
+
+ tout = new TarOutputStream(out);
+ debug("begin");
+ for (File file : files) {
+ notice(file.getName());
+ entry = new TarEntry(file);
+ tout.putNextEntry(entry);
+
+ if (!file.isDirectory()) {
+ if ((in = openFileRead(file)) == null) continue;
+ processStream(in, tout);
+ in.close();
+ }
+ tout.closeEntry();
+ }
+ tout.close();
+ debug("end");
+ }
+
+ private void extract() throws IOException {
+ TarEntry entry;
+ InputStream in = null;
+ OutputStream out;
+ TarInputStream tin;
+ File file;
+
+ if (archive != null) {
+ if (archive.exists()) {
+ if ((in = openFileRead(archive)) == null) {
+ exit(1);
+ }
+ } else {
+ error("File does not exist: " + archive);
+ exit(1);
+ }
+ } else {
+ in = getInput().getInputStream();
+ }
+
+ tin = new TarInputStream(in);
+
+ if (use_stdout) {
+ debug("out=stdout");
+ out = getOutput().getOutputStream();
+ }
+
+ debug("begin");
+ while ((entry = tin.getNextEntry()) != null) {
+ notice(entry.getName());
+ file = new File(entry.getName());
+ if (entry.isDirectory()) {
+ if (!file.exists()) {
+ file.mkdirs();
+ }
+ continue;
+ }
+ if ((out = openFileWrite(file, true, false)) == null) {
+ continue;
+ }
+ tin.copyEntryContents(out);
+ out.close();
+ }
+ tin.close();
+ debug("end");
+ }
+
+ private void list() throws IOException {
+ TarEntry entry;
+ InputStream in = null;
+ TarInputStream tin;
+
+ if (archive == null || !archive.exists()) {
+ error("Cannot find file: " + archive);
+ exit(1);
+ }
+ if ((in = openFileRead(archive)) == null) {
+ exit(1);
+ }
+
+ tin = new TarInputStream(in);
+
+ while ((entry = tin.getNextEntry()) != null) {
+ out(entry.getName());
+ }
+ }
+
+ private void diff() throws IOException {
+ TarEntry entry;
+ InputStream in = null;
+ TarInputStream tin;
+ File file;
+
+ if (archive == null || !archive.exists()) {
+ error("Cannot find file: " + archive);
+ exit(1);
+ }
+ if ((in = openFileRead(archive)) == null) {
+ exit(1);
+ }
+
+ tin = new TarInputStream(in);
+
+ while ((entry = tin.getNextEntry()) != null) {
+ file = new File(entry.getName());
+
+ if (!file.exists()) {
+ out(file + ": Warning: No such file or directory");
+ }
+
+ if (file.lastModified() != entry.getModTime().getTime()) {
+ out(file + ": Mod time differs");
+ }
+
+ if (file.length() != entry.getSize()) {
+ out(file + ": Size differs");
+ }
+ }
+ }
+
+ private void createArchive() {
+ try {
+ if (archive.exists()) {
+ archive.delete();
+ }
+ debug("creating archive: " + archive);
+ archive.createNewFile();
+ } catch (IOException e) {
+ error("Could not create file: " + archive);
+ exit(1);
+ }
+ }
+
+ private File[] processFiles(File[] files , boolean recurse) {
+ // FIXME object pollution
+ ArrayList<File> _files = new ArrayList<File>();
+
+ for (File file : files) {
+ if (!file.exists()) {
+ continue;
+ }
+ if (file.getName().equals(".") || file.getName().equals("..")) {
+ continue;
+ }
+
+ if (file.isDirectory()) {
+ if (recurse) {
+ _files.add(file);
+ Collections.addAll(_files, processFiles(file.listFiles(), recurse));
+ }
+ continue;
+ }
+ _files.add(file);
+ }
+
+ return _files.toArray(files);
+ }
+
+ private boolean checkMode() {
+ int check = 0;
+ if (DoAppend.isSet()) check++;
+ if (DoCreate.isSet()) check++;
+ if (DoConcat.isSet()) check++;
+ if (DoDelete.isSet()) check++;
+ if (DoDiff.isSet()) check++;
+ if (DoExtract.isSet()) check++;
+ if (DoList.isSet()) check++;
+ if (DoUpdate.isSet()) check++;
+ return check == 1;
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/UnzipCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/UnzipCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/UnzipCommand.java 2009-03-31 06:28:08 UTC (rev 5195)
@@ -0,0 +1,42 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.fs.command.archive;
+
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.StringArgument;
+
+/**
+ * @author chris boertien
+ */
+public class UnzipCommand extends Zip {
+
+ public UnzipCommand() {
+ super("extracts entries from zip archives");
+ //registerArguments(ZipfileArg,PatternsArg);
+ }
+
+ public void execute() {
+ setup();
+ }
+}
Added: trunk/fs/src/fs/org/jnode/fs/command/archive/ZCatCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/archive/ZCatCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/command/archive/ZCatCommand.j...
[truncated message content] |