Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #595 from onemanbucket/jenkins-15418
[FIXED JENKINS-15418] Allow long paths on windows.
  • Loading branch information
jglick committed Oct 25, 2012
2 parents edbe2ae + f66e19c commit d7ef52e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
17 changes: 16 additions & 1 deletion core/src/main/java/hudson/util/jna/Kernel32Utils.java
Expand Up @@ -58,7 +58,22 @@ public static int waitForExitProcess(Pointer hProcess) throws InterruptedExcepti
}

public static int getWin32FileAttributes(File file) throws IOException {
return Kernel32.INSTANCE.GetFileAttributesW(new WString(file.getCanonicalPath()));
// allow lookup of paths longer than MAX_PATH
// http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx
String canonicalPath = file.getCanonicalPath();
String path = null;
if(canonicalPath.length() < 260) {
// path is short, use as-is
path = canonicalPath;
} else if(canonicalPath.startsWith("\\\\")) {
// network share
// \\server\share --> \\?\UNC\server\share
path = "\\\\?\\UNC\\" + canonicalPath.substring(2);
} else {
// prefix, canonical path should be normalized and absolute so this should work.
path = "\\\\?\\" + canonicalPath;
}
return Kernel32.INSTANCE.GetFileAttributesW(new WString(path));
}

public static boolean isJunctionOrSymlink(File file) throws IOException {
Expand Down
29 changes: 28 additions & 1 deletion core/src/test/java/hudson/FilePathTest.java
Expand Up @@ -474,5 +474,32 @@ public void testValidateAntFileMaskBounded() throws Exception {
Util.deleteRecursive(tmp);
}
}


@Bug(15418)
public void testDeleteLongPathOnWindows() throws Exception {
File tmp = Util.createTempDir();
try {
FilePath d = new FilePath(french, tmp.getPath());

// construct a very long path
StringBuilder sb = new StringBuilder();
while(sb.length() + tmp.getPath().length() < 260 - "very/".length()) {
sb.append("very/");
}
sb.append("pivot/very/very/long/path");

FilePath longPath = d.child(sb.toString());
longPath.mkdirs();
FilePath childInLongPath = longPath.child("file.txt");
childInLongPath.touch(0);

File firstDirectory = new File(tmp.getAbsolutePath() + "/very");
Util.deleteRecursive(firstDirectory);

assertFalse("Could not delete directory!", firstDirectory.exists());

} finally {
Util.deleteRecursive(tmp);
}
}
}

0 comments on commit d7ef52e

Please sign in to comment.