Browse Source

Add nested filesets to tar through which file permissions can be set.

This is done through an octal mode attribute. The groupname and username
may also be set. See build.xml for an example usage.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268581 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 24 years ago
parent
commit
3c6126f29b
4 changed files with 148 additions and 25 deletions
  1. +26
    -8
      build.xml
  2. +104
    -16
      src/main/org/apache/tools/ant/taskdefs/Tar.java
  3. +9
    -0
      src/main/org/apache/tools/ant/types/FileSet.java
  4. +9
    -1
      src/main/org/apache/tools/tar/TarEntry.java

+ 26
- 8
build.xml View File

@@ -68,10 +68,11 @@
-->
<property name="dist.name" value="jakarta-${name}-${version}"/>
<property name="dist.base" value="distribution"/>
<property name="defaultdist.dir" value="dist"/>

<target name="setup-distproperties">

<property name="dist.dir" value="dist"/>
<property name="dist.dir" value="${defaultdist.dir}"/>
<property name="dist.bin" value="${dist.dir}/bin"/>
<property name="dist.lib" value="${dist.dir}/lib"/>
<property name="dist.docs" value="${dist.dir}/docs"/>
@@ -397,10 +398,18 @@
includes="${dist.name}/**"
excludes="${dist.name}/lib/optional.jar"/>
<tar longfile="gnu"
tarfile="${dist.base}/${dist.name}-bin.tar"
basedir="${dist.name}/.."
includes="${dist.name}/**"
excludes="${dist.name}/lib/optional.jar"/>
tarfile="${dist.base}/${dist.name}-bin.tar">
<fileset dir="${dist.name}/.." mode="755" username="ant" group="ant">
<include name="${dist.name}/bin/ant"/>
<include name="${dist.name}/bin/antRun"/>
</fileset>
<fileset dir="${dist.name}/.." username="ant" group="ant">
<include name="${dist.name}/**"/>
<exclude name="${dist.name}/bin/ant"/>
<exclude name="${dist.name}/bin/antRun"/>
<exclude name="${dist.name}/lib/optional.jar"/>
</fileset>
</tar>
<gzip zipfile="${dist.base}/${dist.name}-bin.tar.gz"
src="${dist.base}/${dist.name}-bin.tar"/>
<delete file="${dist.base}/${dist.name}-bin.tar"/>
@@ -415,9 +424,17 @@
basedir="${dist.name}/.."
includes="${dist.name}/**"/>
<tar longfile="gnu"
tarfile="${dist.base}/${dist.name}-src.tar"
basedir="${dist.name}/.."
includes="${dist.name}/**"/>
tarfile="${dist.base}/${dist.name}-src.tar" >
<fileset dir="${dist.name}/.." mode="755" username="ant" group="ant">
<include name="${dist.name}/bootstrap.sh"/>
<include name="${dist.name}/build.sh"/>
</fileset>
<fileset dir="${dist.name}/.." username="ant" group="ant">
<include name="${dist.name}/**"/>
<exclude name="${dist.name}/bootstrap.sh"/>
<exclude name="${dist.name}/build.sh"/>
</fileset>
</tar>
<gzip zipfile="${dist.base}/${dist.name}-src.tar.gz"
src="${dist.base}/${dist.name}-src.tar"/>
<delete file="${dist.base}/${dist.name}-src.tar"/>
@@ -432,6 +449,7 @@
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${dist.base}" />
<delete dir="${defaultdist.dir}" />
<delete>
<fileset dir="." includes="**/*~" defaultexcludes="no"/>
</delete>


+ 104
- 16
src/main/org/apache/tools/ant/taskdefs/Tar.java View File

@@ -55,9 +55,11 @@
package org.apache.tools.ant.taskdefs;

import java.io.*;
import java.util.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.util.*;
import org.apache.tools.tar.*;
import org.apache.tools.ant.types.*;

/**
* Creates a TAR archive.
@@ -76,6 +78,16 @@ public class Tar extends MatchingTask {
String longFileMode = null;
Vector filesets = new Vector();
Vector fileSetFiles = new Vector();

public TarFileSet createFileSet() {
TarFileSet fileset = new TarFileSet();
filesets.addElement(fileset);
return fileset;
}
/**
* This is the name/location of where to create the tar file.
*/
@@ -107,19 +119,32 @@ public class Tar extends MatchingTask {
location);
}

if (baseDir == null) {
throw new BuildException("basedir attribute must be set!",
location);
}
if (!baseDir.exists()) {
throw new BuildException("basedir does not exist!", location);
if (baseDir != null) {
if (!baseDir.exists()) {
throw new BuildException("basedir does not exist!", location);
}
// add the main fileset to the list of filesets to process.
TarFileSet mainFileSet = new TarFileSet(fileset);
mainFileSet.setDir(baseDir);
mainFileSet.setDefaultexcludes(useDefaultExcludes);
filesets.addElement(mainFileSet);
}

DirectoryScanner ds = super.getDirectoryScanner(baseDir);

String[] files = ds.getIncludedFiles();
// check if tr is out of date with respect to each
// fileset
boolean upToDate = true;
for (Enumeration e = filesets.elements(); e.hasMoreElements();) {
TarFileSet fs = (TarFileSet)e.nextElement();
String[] files = fs.getFiles(project);
if (!archiveIsUpToDate(files)) {
upToDate = false;
break;
}
}

if (archiveIsUpToDate(files)) {
if (upToDate) {
log("Nothing to do: "+tarFile.getAbsolutePath()+" is up to date.",
Project.MSG_INFO);
return;
@@ -140,11 +165,15 @@ public class Tar extends MatchingTask {
else if (longFileMode.equalsIgnoreCase(GNU)) {
tOut.setLongFileMode(TarOutputStream.LONGFILE_GNU);
}

for (int i = 0; i < files.length; i++) {
File f = new File(baseDir,files[i]);
String name = files[i].replace(File.separatorChar,'/');
tarFile(f, tOut, name);
for (Enumeration e = filesets.elements(); e.hasMoreElements();) {
TarFileSet fs = (TarFileSet)e.nextElement();
String[] files = fs.getFiles(project);
for (int i = 0; i < files.length; i++) {
File f = new File(baseDir,files[i]);
String name = files[i].replace(File.separatorChar,'/');
tarFile(f, tOut, name, fs);
}
}
} catch (IOException ioe) {
String msg = "Problem creating TAR: " + ioe.getMessage();
@@ -160,7 +189,8 @@ public class Tar extends MatchingTask {
}
}

protected void tarFile(File file, TarOutputStream tOut, String vPath)
protected void tarFile(File file, TarOutputStream tOut, String vPath,
TarFileSet tarFileSet)
throws IOException
{
FileInputStream fIn = new FileInputStream(file);
@@ -169,6 +199,12 @@ public class Tar extends MatchingTask {
TarEntry te = new TarEntry(vPath);
te.setSize(file.length());
te.setModTime(file.lastModified());
if (!file.isDirectory()) {
te.setMode(tarFileSet.getMode());
}
te.setUserName(tarFileSet.getUserName());
te.setGroupName(tarFileSet.getGroup());
tOut.putNextEntry(te);
byte[] buffer = new byte[8 * 1024];
@@ -190,4 +226,56 @@ public class Tar extends MatchingTask {
mm.setTo(tarFile.getAbsolutePath());
return sfs.restrict(files, baseDir, null, mm).length == 0;
}

static public class TarFileSet extends FileSet {
private String[] files = null;
private int mode = 0100644;
private String userName = "";
private String groupName = "";
public TarFileSet(FileSet fileset) {
super(fileset);
}
public TarFileSet() {
super();
}
public String[] getFiles(Project p) {
if (files == null) {
DirectoryScanner ds = getDirectoryScanner(p);
files = ds.getIncludedFiles();
}
return files;
}
public void setMode(String octalString) {
this.mode = 0100000 | Integer.parseInt(octalString, 8);
}
public int getMode() {
return mode;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setGroup(String groupName) {
this.groupName = groupName;
}
public String getGroup() {
return groupName;
}
}
}

+ 9
- 0
src/main/org/apache/tools/ant/types/FileSet.java View File

@@ -85,6 +85,15 @@ public class FileSet extends DataType {
super();
}

protected FileSet(FileSet fileset) {
this.dir = fileset.dir;
this.defaultPatterns = fileset.defaultPatterns;
this.additionalPatterns = fileset.additionalPatterns;
this.useDefaultExcludes = fileset.useDefaultExcludes;
}

/**
* Makes this instance in effect a reference to another PatternSet
* instance.


+ 9
- 1
src/main/org/apache/tools/tar/TarEntry.java View File

@@ -305,6 +305,14 @@ public class TarEntry implements TarConstants {
public void setName(String name) {
this.name = new StringBuffer(name);
}

/**
* Set the mode for this entry
*/
public void setMode(int mode) {
this.mode = mode;
}
/**
* Get this entry's user id.
@@ -580,5 +588,5 @@ public class TarEntry implements TarConstants {
this.devMajor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
offset += DEVLEN;
this.devMinor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
}
}
}

Loading…
Cancel
Save