Skip to content

Commit

Permalink
[JENKINS-11073] don't fail copyToWithPermissions if setting last-modi…
Browse files Browse the repository at this point in the history
…fied fails on Windows (more reliable detection of Windows platforms than before)
  • Loading branch information
kutzi committed Dec 30, 2011
1 parent 922b2c7 commit b4fe626
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
34 changes: 23 additions & 11 deletions core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,28 @@ public Void invoke(File f, VirtualChannel channel) throws IOException {
}
});
}

private void setLastModifiedIfPossible(final long timestamp) throws IOException, InterruptedException {
String message = act(new FileCallable<String>() {
private static final long serialVersionUID = -828220335793641630L;
public String invoke(File f, VirtualChannel channel) throws IOException {
if(!f.setLastModified(timestamp)) {
if (Functions.isWindows()) {
// On Windows this seems to fail often. See JENKINS-11073
// Therefore don't fail, but just log a warning
return "Failed to set the timestamp of "+f+" to "+timestamp;
} else {
throw new IOException("Failed to set the timestamp of "+f+" to "+timestamp);
}
}
return null;
}
});

if (message!=null) {
LOGGER.warning(message);
}
}

/**
* Checks if the file is a directory.
Expand Down Expand Up @@ -1424,17 +1446,7 @@ public void copyToWithPermission(FilePath target) throws IOException, Interrupte
copyTo(target);
// copy file permission
target.chmod(mode());

try {
target.touch(lastModified());
} catch (IOException e) {
// On Windows this seems to fail often. See JENKINS-11073
if (!target.isUnix()) {
LOGGER.warning("Failed to set timestamp on " + target.getRemote());
} else { // rethrow
throw new IOException2(e);
}
}
target.setLastModifiedIfPossible(lastModified());
}

/**
Expand Down
40 changes: 40 additions & 0 deletions core/src/test/java/hudson/FilePathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Chmod;
import org.jvnet.hudson.test.Bug;

/**
Expand Down Expand Up @@ -315,5 +317,43 @@ public void testIsUnix() {
"/home/test");
assertTrue(unixPath.isUnix());
}

/**
* Tests that permissions are kept when using {@link FilePath#copyToWithPermission(FilePath)}.
* Also tries to check that a problem with setting the last-modified date on Windows doesn't fail the whole copy
* - well at least when running this test on a Windows OS. See JENKINS-11073
*/
public void testCopyToWithPermission() throws IOException, InterruptedException {
File tmp = Util.createTempDir();
try {
File child = new File(tmp,"child");
FilePath childP = new FilePath(child);
childP.touch(4711);

Chmod chmodTask = new Chmod();
chmodTask.setProject(new Project());
chmodTask.setFile(child);
chmodTask.setPerm("0400");
chmodTask.execute();

FilePath copy = new FilePath(british,tmp.getPath()).child("copy");
childP.copyToWithPermission(copy);

assertEquals(childP.mode(),copy.mode());
if (!Functions.isWindows()) {
assertEquals(childP.lastModified(),copy.lastModified());
}

// JENKINS-11073:
// Windows seems to have random failures when setting the timestamp on newly generated
// files. So test that:
for (int i=0; i<100; i++) {
copy = new FilePath(british,tmp.getPath()).child("copy"+i);
childP.copyToWithPermission(copy);
}
} finally {
Util.deleteRecursive(tmp);
}
}

}

0 comments on commit b4fe626

Please sign in to comment.