ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r804438 - in /ant/sandbox/antlibs/compress/trunk/src: main/org/apache/ant/compress/ main/org/apache/ant/compress/taskdefs/ main/org/apache/ant/compress/util/ tests/antunit/
Date Sat, 15 Aug 2009 05:57:05 GMT
Author: bodewig
Date: Sat Aug 15 05:57:04 2009
New Revision: 804438

URL: http://svn.apache.org/viewvc?rev=804438&view=rev
Log:
rudimentary tar task that can only create new archives, doesn't create directory entries and
needs a whole lot more tests

Added:
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Tar.java
  (with props)
Modified:
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ArStreamFactory.java
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/CpioStreamFactory.java
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamFactory.java
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/TarStreamFactory.java
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ZipStreamFactory.java
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/untar-test.xml

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml Sat Aug
15 05:57:04 2009
@@ -32,6 +32,10 @@
     name="unzip"
     classname="org.apache.ant.compress.taskdefs.Unzip"
     />
+  <taskdef
+    name="tar"
+    classname="org.apache.ant.compress.taskdefs.Tar"
+    />
 
   <typedef
     name="tarentry"

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
Sat Aug 15 05:57:04 2009
@@ -18,8 +18,12 @@
 
 package org.apache.ant.compress.taskdefs;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.zip.ZipException;
 
@@ -33,32 +37,39 @@
 import org.apache.ant.compress.util.EntryHelper;
 import org.apache.ant.compress.util.StreamFactory;
 
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
 import org.apache.commons.compress.archivers.zip.ZipExtraField;
 import org.apache.commons.compress.archivers.zip.ZipShort;
 
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
 import org.apache.tools.ant.Task;
+import org.apache.tools.ant.taskdefs.Expand;
 import org.apache.tools.ant.types.ArchiveFileSet;
 import org.apache.tools.ant.types.EnumeratedAttribute;
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.resources.ArchiveResource;
 import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.util.FileUtils;
 
 /**
  * Base implementation of tasks creating archives.
  */
 public abstract class ArchiveBase extends Task {
     private final StreamFactory factory;
+    private final EntryBuilder builder;
 
     private Resource dest;
     private List/*<ResourceCollection>*/ sources = new ArrayList();
     private Mode mode = new Mode();
     private String encoding;
 
-    protected ArchiveBase(StreamFactory factory) {
+    protected ArchiveBase(StreamFactory factory, EntryBuilder builder) {
         this.factory = factory;
+        this.builder = builder;
     }
 
     /**
@@ -106,6 +117,21 @@
             // create mode
             mode = new Mode();
         }
+        ResourceWithFlags[] toAdd;
+        try {
+            toAdd = findSources();
+        } catch (IOException ioex) {
+            throw new BuildException("Failed to read sources", ioex);
+        }
+        if (toAdd.length == 0) {
+            log("No sources, nothing to do", Project.MSG_WARN);
+        } else {
+            try {
+                writeArchive(dest, toAdd);
+            } catch (IOException ioex) {
+                throw new BuildException("Failed to write archive", ioex);
+            }
+        }
     }
 
     /**
@@ -121,14 +147,77 @@
     }
 
     /**
+     * Find all the resources with their flags that should be added to
+     * the archive.
+     */
+    protected ResourceWithFlags[] findSources() throws IOException {
+        ArrayList l = new ArrayList();
+        for (Iterator rcs = sources.iterator(); rcs.hasNext(); ) {
+            ResourceCollection rc = (ResourceCollection) rcs.next();
+            ResourceCollectionFlags rcFlags = getFlags(rc);
+            for (Iterator rs = rc.iterator(); rs.hasNext(); ) {
+                Resource r = (Resource) rs.next();
+                l.add(new ResourceWithFlags(r, rcFlags, getFlags(r)));
+            }
+        }
+        return (ResourceWithFlags[]) l.toArray(new ResourceWithFlags[l.size()]);
+    }
+
+    protected void writeArchive(Resource dest, ResourceWithFlags[] src)
+        throws IOException {
+        FileUtils fu = FileUtils.getFileUtils();
+        ArchiveOutputStream out = null;
+        try {
+            out =
+                factory.getArchiveStream(new BufferedOutputStream(dest
+                                                                  .getOutputStream()),
+                                         Expand.NATIVE_ENCODING.equals(encoding)
+                                         ? null : encoding);
+            for (int i = 0; i < src.length; i++) {
+                String name = src[i].getResource().getName();
+                if (src[i].getCollectionFlags().hasFullpath()) {
+                    name = src[i].getCollectionFlags().getFullpath();
+                } else if (src[i].getCollectionFlags().hasPrefix()) {
+                    String prefix = src[i].getCollectionFlags().getPrefix();
+                    if (!prefix.endsWith("/")) {
+                        prefix = prefix + "/";
+                    }
+                    name = prefix + name;
+                }
+
+                ArchiveEntry ent = builder.buildEntry(name, src[i]);
+                out.putArchiveEntry(ent);
+                if (!src[i].getResource().isDirectory()) {
+                    InputStream in = null;
+                    try {
+                        in = src[i].getResource().getInputStream();
+
+                        byte[] buffer = new byte[8192];
+                        int count = 0;
+                        do {
+                            out.write(buffer, 0, count);
+                            count = in.read(buffer, 0, buffer.length);
+                        } while (count != -1);
+                    } finally {
+                        fu.close(in);
+                    }
+                }
+                out.closeArchiveEntry();
+
+            }
+        } finally {
+            fu.close(out);
+        }
+    }
+
+    /**
      * Extracts flags from a resource.
      *
-     * <p>All those exceptions are only here for the code that
-     * translates Ant's ZipExtraFields to CC ZipExtraFields and should
-     * never actually be thrown.</p>
+     * <p>ZipExceptions are only here for the code that translates
+     * Ant's ZipExtraFields to CC ZipExtraFields and should never
+     * actually be thrown.</p>
      */
-    protected ResourceFlags getFlags(Resource r)
-        throws ZipException, InstantiationException, IllegalAccessException {
+    protected ResourceFlags getFlags(Resource r) throws ZipException {
         if (r instanceof ArchiveResource) {
             if (r instanceof CommonsCompressArchiveResource) {
                 if (r instanceof TarResource) {
@@ -162,9 +251,16 @@
                     new ZipExtraField[extra == null ? 0 : extra.length];
                 if (extra != null && extra.length > 0) {
                     for (int i = 0; i < extra.length; i++) {
-                        ex[i] = ExtraFieldUtils
-                            .createExtraField(new ZipShort(extra[i].getHeaderId()
-                                                           .getValue()));
+                        try {
+                            ex[i] = ExtraFieldUtils
+                                .createExtraField(new ZipShort(extra[i]
+                                                               .getHeaderId()
+                                                               .getValue()));
+                        } catch (InstantiationException e) {
+                            throw new BuildException(e);
+                        } catch (IllegalAccessException e) {
+                            throw new BuildException(e);
+                        }
                         byte[] b = extra[i].getCentralDirectoryData();
                         ex[i].parseFromCentralDirectoryData(b, 0, b.length);
                         b = extra[i].getLocalFileDataData();
@@ -438,4 +534,8 @@
         public ResourceCollectionFlags getCollectionFlags() { return rcFlags; }
         public ResourceFlags getResourceFlags() { return rFlags; }
     }
+
+    public static interface EntryBuilder {
+        ArchiveEntry buildEntry(String name, ResourceWithFlags resource);
+    }
 }

Added: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Tar.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Tar.java?rev=804438&view=auto
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Tar.java
(added)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Tar.java
Sat Aug 15 05:57:04 2009
@@ -0,0 +1,82 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.ant.compress.taskdefs;
+
+import org.apache.ant.compress.util.TarStreamFactory;
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarConstants;
+
+/**
+ * Creates tar archives.
+ */
+public class Tar extends ArchiveBase {
+    public Tar() {
+        super(new TarStreamFactory(),
+              new ArchiveBase.EntryBuilder() {
+                public ArchiveEntry buildEntry(String name,
+                                               ArchiveBase.ResourceWithFlags r) {
+                    boolean isDir = r.getResource().isDirectory();
+                    TarArchiveEntry ent =
+                        new TarArchiveEntry(name,
+                                            isDir ? TarConstants.LF_DIR
+                                            : TarConstants.LF_NORMAL);
+                    ent.setModTime(r.getResource().getLastModified());
+                    ent.setSize(isDir ? 0 : r.getResource().getSize());
+
+                    if (r.getResourceFlags().hasModeBeenSet()) {
+                        ent.setMode(r.getResourceFlags().getMode());
+                    } else if (!isDir
+                               && r.getCollectionFlags().hasModeBeenSet()) {
+                        ent.setMode(r.getCollectionFlags().getMode());
+                    } else if (isDir
+                               && r.getCollectionFlags().hasDirModeBeenSet()) {
+                        ent.setMode(r.getCollectionFlags().getDirMode());
+                    }
+
+                    if (r.getResourceFlags().hasUserIdBeenSet()) {
+                        ent.setUserId(r.getResourceFlags().getUserId());
+                    } else if (r.getCollectionFlags().hasUserIdBeenSet()) {
+                        ent.setUserId(r.getCollectionFlags().getUserId());
+                    }
+
+                    if (r.getResourceFlags().hasGroupIdBeenSet()) {
+                        ent.setGroupId(r.getResourceFlags().getGroupId());
+                    } else if (r.getCollectionFlags().hasGroupIdBeenSet()) {
+                        ent.setGroupId(r.getCollectionFlags().getGroupId());
+                    }
+ 
+                    if (r.getResourceFlags().hasUserNameBeenSet()) {
+                        ent.setUserName(r.getResourceFlags().getUserName());
+                    } else if (r.getCollectionFlags().hasUserNameBeenSet()) {
+                        ent.setUserName(r.getCollectionFlags().getUserName());
+                    }
+
+                    if (r.getResourceFlags().hasGroupNameBeenSet()) {
+                        ent.setGroupName(r.getResourceFlags().getGroupName());
+                    } else if (r.getCollectionFlags().hasGroupNameBeenSet()) {
+                        ent.setGroupName(r.getCollectionFlags().getGroupName());
+                    }
+ 
+                    return ent;
+                }
+            });
+    }
+
+}
\ No newline at end of file

Propchange: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Tar.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ArStreamFactory.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ArStreamFactory.java?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ArStreamFactory.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ArStreamFactory.java
Sat Aug 15 05:57:04 2009
@@ -20,9 +20,12 @@
 
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
+import org.apache.commons.compress.archivers.ar.ArArchiveOutputStream;
 
 public class ArStreamFactory implements StreamFactory {
 
@@ -36,4 +39,14 @@
         return new ArArchiveInputStream(stream);
     }
 
+    /**
+     * @param stream the stream to write to, should be buffered
+     * @param encoding the encoding of the entry names, ignored
+     */
+    public ArchiveOutputStream getArchiveStream(OutputStream stream,
+                                                String encoding)
+        throws IOException {
+        return new ArArchiveOutputStream(stream);
+    }
+
 }
\ No newline at end of file

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/CpioStreamFactory.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/CpioStreamFactory.java?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/CpioStreamFactory.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/CpioStreamFactory.java
Sat Aug 15 05:57:04 2009
@@ -20,9 +20,12 @@
 
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
+import org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream;
 
 public class CpioStreamFactory implements StreamFactory {
 
@@ -36,4 +39,13 @@
         return new CpioArchiveInputStream(stream);
     }
 
+    /**
+     * @param stream the stream to write to, should be buffered
+     * @param encoding the encoding of the entry names, ignored
+     */
+    public ArchiveOutputStream getArchiveStream(OutputStream stream,
+                                                String encoding)
+        throws IOException {
+        return new CpioArchiveOutputStream(stream);
+    }
 }
\ No newline at end of file

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamFactory.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamFactory.java?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamFactory.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamFactory.java
Sat Aug 15 05:57:04 2009
@@ -20,11 +20,13 @@
 
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
 
 /**
- * Creates input streams for the supported archive formats.
+ * Creates streams for the supported archive formats.
  */
 public interface StreamFactory {
 
@@ -37,4 +39,14 @@
                                                String encoding)
         throws IOException;
 
+
+    /**
+     * @param stream the stream to write to, should be buffered
+     * @param encoding the encoding of the entry names, ignored by all
+     * formats except zip
+     */
+    public ArchiveOutputStream getArchiveStream(OutputStream stream,
+                                                String encoding)
+        throws IOException;
+
 }
\ No newline at end of file

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/TarStreamFactory.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/TarStreamFactory.java?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/TarStreamFactory.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/TarStreamFactory.java
Sat Aug 15 05:57:04 2009
@@ -20,9 +20,12 @@
 
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
 
 public class TarStreamFactory implements StreamFactory {
 
@@ -36,4 +39,13 @@
         return new TarArchiveInputStream(stream);
     }
 
+    /**
+     * @param stream the stream to write to, should be buffered
+     * @param encoding the encoding of the entry names, ignored
+     */
+    public ArchiveOutputStream getArchiveStream(OutputStream stream,
+                                                String encoding)
+        throws IOException {
+        return new TarArchiveOutputStream(stream);
+    }
 }
\ No newline at end of file

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ZipStreamFactory.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ZipStreamFactory.java?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ZipStreamFactory.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/ZipStreamFactory.java
Sat Aug 15 05:57:04 2009
@@ -20,9 +20,12 @@
 
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 
 public class ZipStreamFactory implements StreamFactory {
 
@@ -36,4 +39,15 @@
         return new ZipArchiveInputStream(stream, encoding, true);
     }
 
+    /**
+     * @param stream the stream to write to, should be buffered
+     * @param encoding the encoding of the entry names
+     */
+    public ArchiveOutputStream getArchiveStream(OutputStream stream,
+                                                String encoding)
+        throws IOException {
+        ZipArchiveOutputStream o = new ZipArchiveOutputStream(stream);
+        o.setEncoding(encoding);
+        return o;
+    }
 }
\ No newline at end of file

Modified: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/untar-test.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/untar-test.xml?rev=804438&r1=804437&r2=804438&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/tests/antunit/untar-test.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/untar-test.xml Sat Aug 15 05:57:04
2009
@@ -38,6 +38,18 @@
        />
   </target>
 
+  <target name="testAgainstAntlibTarTask" depends="setUp">
+    <cmp:tar destfile="${input}/test.tar">
+      <fileset dir="."/>
+    </cmp:tar>
+    <cmp:untar src="${input}/test.tar" dest="${output}"/>
+    <au:assertFileExists file="${output}/untar-test.xml"/>
+    <au:assertFilesMatch
+       actual="${output}/untar-test.xml"
+       expected="untar-test.xml"
+       />
+  </target>
+
   <target name="testAgainstNativeTar" depends="setUp">
     <cmp:untar src="../resources/asf-logo.gif.tar" dest="${output}" />
     <au:assertFileExists file="${output}/asf-logo.gif"/>



Mime
View raw message