Skip to content

Commit

Permalink
[FIXED JENKINS-16846] fix logic and add test
Browse files Browse the repository at this point in the history
FilePath#moveAllChildrenTo faild,
when same name directory exist.

change Void to void, recursive_invoke is internal helper function.

thanks review by jglick
  • Loading branch information
kkkon authored and kkkon committed Feb 21, 2013
1 parent 263b4e4 commit 8a9e1ab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
31 changes: 28 additions & 3 deletions core/src/main/java/hudson/FilePath.java
Expand Up @@ -1657,15 +1657,40 @@ public void moveAllChildrenTo(final FilePath target) throws IOException, Interru
}
act(new FileCallable<Void>() {
private static final long serialVersionUID = 1L;
public Void invoke(File f, VirtualChannel channel) throws IOException {
File t = new File(target.getRemote());

private void recursive_invoke(final File t, final File f ) throws IOException {
for(File child : f.listFiles()) {
File target = new File(t, child.getName());
LOGGER.log(Level.FINE,"child=" + child.getAbsolutePath() + "\ntarget=" + target.getAbsolutePath() );
if ( child.isDirectory() && target.isDirectory() )
{
final String childName = child.getName();
final String targetName = target.getName();
if ( null != childName && null != targetName )
{
if ( 0 == childName.compareTo( targetName ) )
{
if ( Util.isSymlink( child ) ) {
if(!child.renameTo(target)) {
throw new IOException("Failed to rename "+child+" to "+target);
}
} else {
// avoid fail to exec child.renameTo(taget)
recursive_invoke( target, child );
}
continue;
}
}
}
if(!child.renameTo(target))
throw new IOException("Failed to rename "+child+" to "+target);
}
f.delete();
}
public Void invoke(File f, VirtualChannel channel) throws IOException {
File t = new File(target.getRemote());

recursive_invoke( t, f );

return null;
}
});
Expand Down
29 changes: 29 additions & 0 deletions core/src/test/java/hudson/FilePathTest.java
Expand Up @@ -577,4 +577,33 @@ private InputStream someZippedContent() throws IOException {

return new ByteArrayInputStream(buf.toByteArray());
}

@Bug(16846)
public void testMoveAllChildrenTo() throws IOException, InterruptedException {
final File tmp = Util.createTempDir();
try
{
final String dirname = "sub";
final File top = new File(tmp, "test");
final File sub = new File(top, dirname);
final File subsub = new File(sub, dirname);

subsub.mkdirs();

final File subFile1 = new File( sub.getAbsolutePath() + "/file1.txt" );
subFile1.createNewFile();
final File subFile2 = new File( subsub.getAbsolutePath() + "/file2.txt" );
subFile2.createNewFile();

final FilePath src = new FilePath(sub);
final FilePath dst = new FilePath(top);

// test conflict subdir
src.moveAllChildrenTo(dst);
}
finally
{
Util.deleteRecursive(tmp);
}
}
}

0 comments on commit 8a9e1ab

Please sign in to comment.