Skip to content

Commit

Permalink
[JENKINS-13444] Do not follow symlinks with ant pattern
Browse files Browse the repository at this point in the history
Do not follow symlinks when ant patterns are used. To make it the same
behaviour as without ant patterns.
  • Loading branch information
ThomasHerrlin authored and vjuranek committed Jun 22, 2014
1 parent 6ecea96 commit bc6d13e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/hudson/plugins/ws_cleanup/Cleanup.java
Expand Up @@ -33,7 +33,7 @@ public Cleanup(List<Pattern> patterns, boolean deleteDirs, EnvironmentVariablesN
if (environment != null) { // allow slave environment to overwrite delete cmd
this.deleteCommand = environment.getEnvVars().expand(command);
}

if (patterns == null) { // if pattern is not set up, delete everything
patterns = new ArrayList<Pattern>();
patterns.add(new Pattern("**/*", PatternType.INCLUDE));
Expand All @@ -51,6 +51,7 @@ public Object invoke(File f, VirtualChannel channel) throws IOException, Interru
doDelete(cmdList);
} else {
DirectoryScanner ds = new DirectoryScanner();
ds.setFollowSymlinks(false);
ds.setBasedir(f);
ArrayList<String> includes = new ArrayList<String>();
ArrayList<String> excludes = new ArrayList<String>();
Expand All @@ -76,13 +77,16 @@ public Object invoke(File f, VirtualChannel channel) throws IOException, Interru
if (deleteDirs) {
length += ds.getIncludedDirsCount();
}
final String[] nonFollowedSymlinks = ds.getNotFollowedSymlinks();
length += nonFollowedSymlinks.length;
String[] toDelete = new String[length];
System.arraycopy(ds.getIncludedFiles(), 0, toDelete, 0, ds.getIncludedFilesCount());
if (deleteDirs) {
System.arraycopy(ds.getIncludedDirectories(), 0, toDelete, ds.getIncludedFilesCount(),
ds.getIncludedDirsCount());
}

System.arraycopy(nonFollowedSymlinks, 0, toDelete, ds.getIncludedFilesCount() + ds.getIncludedDirsCount(),
nonFollowedSymlinks.length);
for (String path : toDelete) {
if (deleteCommand != null) {
List<String> cmdList = fixQuotesAndExpand((new File(f, path)).getPath());
Expand All @@ -105,7 +109,7 @@ public Object invoke(File f, VirtualChannel channel) throws IOException, Interru
*/
private List<String> fixQuotesAndExpand(String fullPath) {
String tempCommand = null;
if(deleteCommand.contains("%s")) {
if (deleteCommand.contains("%s")) {
tempCommand = deleteCommand.replaceAll("%s", "\"" + StringEscapeUtils.escapeJava(fullPath) + "\"");
} else {
tempCommand = deleteCommand + " " + fullPath;
Expand Down

0 comments on commit bc6d13e

Please sign in to comment.