From f23b93a6d5e9e0435532bd06a1061d640fdbcdf0 Mon Sep 17 00:00:00 2001 From: mawinter69 Date: Sat, 22 Jun 2019 22:45:03 +0200 Subject: [PATCH] [JENKINS-57855] read-only flag of folders on windows (#4059) * [JENKINS-57855] read-only flag of folders on windows On windows the current way of removing the read only flag is not working for folders. Using the Files api this can be solved. * code formatting --- .../java/jenkins/util/io/PathRemover.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/jenkins/util/io/PathRemover.java b/core/src/main/java/jenkins/util/io/PathRemover.java index ece2e04bbf..8fd8688b16 100644 --- a/core/src/main/java/jenkins/util/io/PathRemover.java +++ b/core/src/main/java/jenkins/util/io/PathRemover.java @@ -24,18 +24,13 @@ package jenkins.util.io; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.Functions; -import hudson.Util; -import org.kohsuke.accmod.Restricted; -import org.kohsuke.accmod.restrictions.NoExternalUse; - -import javax.annotation.Nonnull; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.NoSuchFileException; import java.nio.file.Path; +import java.nio.file.attribute.DosFileAttributeView; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.util.ArrayList; @@ -45,6 +40,15 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.annotation.Nonnull; + +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.Functions; +import hudson.Util; + @Restricted(NoExternalUse.class) public class PathRemover { @@ -285,6 +289,14 @@ public class PathRemover { } catch (UnsupportedOperationException ignored) { // PosixFileAttributes not supported, fall back to old IO. } + } else { + /* + * If on Windows a folder has a read only attribute set, the file.setWritable(true) doesn't work (JENKINS-57855) + */ + DosFileAttributeView dos = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); + if (dos != null) { + dos.setReadOnly(false); + } } /* -- GitLab