Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove external dependency on ant-filesystem-tasks #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
18 changes: 0 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,13 @@
<artifactId>matrix-project</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.atlassian</groupId>
<artifactId>ant-filesystem-tasks</artifactId>
<version>0.0.2</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>atlassian-public</id>
<url>https://maven.atlassian.com/repository/public</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package org.jenkinsci.plugins.artifactdeployer.service;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FilterSet;
import org.apache.tools.ant.types.FilterSetCollection;

public class CopyWithPerms extends Copy
{

static final String LINE_SEPARATOR = System.getProperty("line.separator");

private boolean preservePermissions = true;

public boolean isPreservePermissions()
{
return preservePermissions;
}

public void setPreservePermissions(boolean preservePermissions)
{
this.preservePermissions = preservePermissions;
}

/**
* Actually does the file (and possibly empty directory) copies.
* This is a good method for subclasses to override.
*/
@Override
protected void doFileOperations() {
if (fileCopyMap.size() > 0) {
log("Copying " + fileCopyMap.size()
+ " file" + (fileCopyMap.size() == 1 ? "" : "s")
+ " to " + destDir.getAbsolutePath());

Enumeration e = fileCopyMap.keys();
while (e.hasMoreElements()) {
String fromFile = (String) e.nextElement();
String[] toFiles = (String[]) fileCopyMap.get(fromFile);

for (int i = 0; i < toFiles.length; i++) {
String toFile = toFiles[i];

if (fromFile.equals(toFile)) {
log("Skipping self-copy of " + fromFile, verbosity);
continue;
}
try {
log("Copying " + fromFile + " to " + toFile, verbosity);

FilterSetCollection executionFilters =
new FilterSetCollection();
if (filtering) {
executionFilters
.addFilterSet(getProject().getGlobalFilterSet());
}
for (Enumeration filterEnum = getFilterSets().elements();
filterEnum.hasMoreElements();) {
executionFilters
.addFilterSet((FilterSet) filterEnum.nextElement());
}
fileUtils.copyFile(new File(fromFile), new File(toFile),
executionFilters,
getFilterChains(), forceOverwrite,
preserveLastModified,
/* append: */ false, getEncoding(),
getOutputEncoding(), getProject()
/*, getForce()*/); //ant 1.8.2

if (preservePermissions) {
int perms = PermissionsUtils.getPermissions(new File(fromFile));
PermissionsUtils.setPermissions(new File(toFile), perms);
}

} catch (IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile
+ " due to " + getDueTo(ioe);
File targetFile = new File(toFile);
if (targetFile.exists() && !targetFile.delete()) {
msg += " and I couldn't delete the corrupt " + toFile;
}
if (failonerror) {
throw new BuildException(msg, ioe, getLocation());
}
log(msg, Project.MSG_ERR);
}
}
}
}
if (includeEmpty) {
Enumeration e = dirCopyMap.elements();
int createCount = 0;
while (e.hasMoreElements()) {
String[] dirs = (String[]) e.nextElement();
for (int i = 0; i < dirs.length; i++) {
File d = new File(dirs[i]);
if (!d.exists()) {
if (!d.mkdirs()) {
log("Unable to create directory "
+ d.getAbsolutePath(), Project.MSG_ERR);
} else {
createCount++;
}
}
}
}
if (createCount > 0) {
log("Copied " + dirCopyMap.size()
+ " empty director"
+ (dirCopyMap.size() == 1 ? "y" : "ies")
+ " to " + createCount
+ " empty director"
+ (createCount == 1 ? "y" : "ies") + " under "
+ destDir.getAbsolutePath());
}
}
}


/**
* Returns a reason for failure based on
* the exception thrown.
* If the exception is not IOException output the class name,
* output the message
* if the exception is MalformedInput add a little note.
*/
private String getDueTo(Exception ex) {
boolean baseIOException = ex.getClass() == IOException.class;
StringBuffer message = new StringBuffer();
if (!baseIOException || ex.getMessage() == null) {
message.append(ex.getClass().getName());
}
if (ex.getMessage() != null) {
if (!baseIOException) {
message.append(" ");
}
message.append(ex.getMessage());
}
if (ex.getClass().getName().indexOf("MalformedInput") != -1) {
message.append(LINE_SEPARATOR);
message.append(
"This is normally due to the input file containing invalid");
message.append(LINE_SEPARATOR);
message.append("bytes for the character encoding used : ");
message.append(
(getEncoding() == null
? fileUtils.getDefaultEncoding() : getEncoding()));
message.append(LINE_SEPARATOR);
}
return message.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
package org.jenkinsci.plugins.artifactdeployer.service;

import com.atlassian.ant.tasks.CopyWithPerms;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.FileSet;
import org.jenkinsci.plugins.artifactdeployer.ArtifactDeployerException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jenkinsci.plugins.artifactdeployer.service;

import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;

import org.jruby.ext.posix.POSIX;
import org.jruby.ext.posix.POSIX.ERRORS;
import org.jruby.ext.posix.POSIXFactory;
import org.jruby.ext.posix.POSIXHandler;
astoltz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Provides convenience methods for manipulating file permissions, as best as is achievable with the Java 6 API.
*/
public class PermissionsUtils
{
private static POSIX posix = POSIXFactory.getPOSIX(new PermissionsUtils.AntPOSIXHandler(), true);

public static int getPermissions(File file)
{
return posix.stat(file.getAbsolutePath()).mode() & 0777;
}

public static void setPermissions(File file, int perms)
{
posix.chmod(file.getAbsolutePath(), perms);
}

/**
* Minimal POSIX handler for Ant tasks. There's scope for improvement here, like redirecting warnings through the Ant
* log, failing on errors, etc...
*/
public static class AntPOSIXHandler implements POSIXHandler
{

public File getCurrentWorkingDirectory()
{
return new File(".");
}

public String[] getEnv()
{
return new String[]{};
}

public PrintStream getErrorStream()
{
return System.err;
}

public InputStream getInputStream()
{
return System.in;
}

public PrintStream getOutputStream()
{
return System.out;
}

public int getPID()
{
return 0;
}

public boolean isVerbose()
{
return false;
}

public void unimplementedError(String message)
{
throw new RuntimeException(message);
}

public void warn(WARNING_ID arg0, String arg1, Object... arg2)
{
System.err.println(arg0 + ": " + String.format(arg1, arg2));
}


public void error(ERRORS error, String extraData)
{
System.err.println(error + ": " + extraData);
}

}

}