提交 301ba7af 编写于 作者: M Matt Sicker

[JENKINS-55448] Fix Util.delete regression

This adds path normalization behavior back to the delete methods to match the behavior of the previous version of these methods.
Signed-off-by: NMatt Sicker <boards@gmail.com>
上级 5dfef2bd
......@@ -188,7 +188,7 @@ public class PathRemover {
private static Optional<IOException> tryRemoveFile(@Nonnull Path path) {
try {
removeOrMakeRemovableThenRemove(path);
removeOrMakeRemovableThenRemove(path.normalize());
return Optional.empty();
} catch (IOException e) {
return Optional.of(e);
......@@ -196,16 +196,18 @@ public class PathRemover {
}
private static List<IOException> tryRemoveRecursive(@Nonnull Path path) {
List<IOException> accumulatedErrors = Util.isSymlink(path) ? new ArrayList<>() :
tryRemoveDirectoryContents(path);
tryRemoveFile(path).ifPresent(accumulatedErrors::add);
Path normalized = path.normalize();
List<IOException> accumulatedErrors = Util.isSymlink(normalized) ? new ArrayList<>() :
tryRemoveDirectoryContents(normalized);
tryRemoveFile(normalized).ifPresent(accumulatedErrors::add);
return accumulatedErrors;
}
private static List<IOException> tryRemoveDirectoryContents(@Nonnull Path path) {
Path normalized = path.normalize();
List<IOException> accumulatedErrors = new ArrayList<>();
if (!Files.isDirectory(path)) return accumulatedErrors;
try (DirectoryStream<Path> children = Files.newDirectoryStream(path)) {
if (!Files.isDirectory(normalized)) return accumulatedErrors;
try (DirectoryStream<Path> children = Files.newDirectoryStream(normalized)) {
for (Path child : children) {
accumulatedErrors.addAll(tryRemoveRecursive(child));
}
......@@ -254,7 +256,7 @@ public class PathRemover {
$ rm x
rm: x not removed: Permission denied
*/
Path parent = path.getParent();
Path parent = path.getParent().normalize();
if (parent != null && !Files.isWritable(parent)) {
makeWritable(parent);
}
......
......@@ -30,6 +30,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.Timeout;
import org.jvnet.hudson.test.Issue;
import java.io.File;
import java.io.FileWriter;
......@@ -39,6 +40,7 @@ import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
......@@ -346,6 +348,23 @@ public class PathRemoverTest {
}
}
@Test
@Issue("JENKINS-55448")
public void testForceRemoveRecursive_ContainsDotPath() throws IOException {
File folder = tmp.newFolder();
File d1 = new File(folder, "d1");
File d1f1 = new File(d1, "d1f1");
File f2 = new File(folder, "f2");
mkdirs(d1);
touchWithFileName(d1f1, f2);
Path path = Paths.get(d1.getPath(), "..", "d1");
PathRemover remover = PathRemover.newSimpleRemover();
remover.forceRemoveRecursive(path);
assertTrue("Unable to delete directory: " + folder, Files.notExists(path));
}
private static void mkdirs(File... dirs) {
for (File dir : dirs) {
assertTrue("Could not mkdir " + dir, dir.mkdir());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册