Return-Path: Delivered-To: apmail-ant-notifications-archive@minotaur.apache.org Received: (qmail 49501 invoked from network); 15 Aug 2009 05:57:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Aug 2009 05:57:21 -0000 Received: (qmail 33629 invoked by uid 500); 15 Aug 2009 05:57:28 -0000 Delivered-To: apmail-ant-notifications-archive@ant.apache.org Received: (qmail 33553 invoked by uid 500); 15 Aug 2009 05:57:28 -0000 Mailing-List: contact notifications-help@ant.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ant.apache.org Delivered-To: mailing list notifications@ant.apache.org Received: (qmail 33544 invoked by uid 99); 15 Aug 2009 05:57:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 15 Aug 2009 05:57:28 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 15 Aug 2009 05:57:25 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 71732238893B; Sat, 15 Aug 2009 05:57:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: notifications@ant.apache.org From: bodewig@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090815055705.71732238893B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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" /> + */ 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. * - *

All those exceptions are only here for the code that - * translates Ant's ZipExtraFields to CC ZipExtraFields and should - * never actually be thrown.

+ *

ZipExceptions are only here for the code that translates + * Ant's ZipExtraFields to CC ZipExtraFields and should never + * actually be thrown.

*/ - 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 @@ /> + + + + + + + + +