diff --git a/bots/pr/src/main/java/org/openjdk/skara/bots/pr/PullRequestCheckIssueVisitor.java b/bots/pr/src/main/java/org/openjdk/skara/bots/pr/PullRequestCheckIssueVisitor.java index d7d935a10..76106047b 100644 --- a/bots/pr/src/main/java/org/openjdk/skara/bots/pr/PullRequestCheckIssueVisitor.java +++ b/bots/pr/src/main/java/org/openjdk/skara/bots/pr/PullRequestCheckIssueVisitor.java @@ -206,6 +206,13 @@ public void visit(ExecutableIssue issue) { readyForReview = false; } + @Override + public void visit(SymlinkIssue issue) { + messages.add(String.format("Symbolic links are not allowed (file: %s)", issue.path())); + failedChecks.add(issue.check().getClass()); + readyForReview = false; + } + @Override public void visit(BlacklistIssue issue) { log.fine("ignored: blacklisted commit"); diff --git a/cli/src/main/java/org/openjdk/skara/cli/JCheckCLIVisitor.java b/cli/src/main/java/org/openjdk/skara/cli/JCheckCLIVisitor.java index 56c024ffe..7f2c37849 100644 --- a/cli/src/main/java/org/openjdk/skara/cli/JCheckCLIVisitor.java +++ b/cli/src/main/java/org/openjdk/skara/cli/JCheckCLIVisitor.java @@ -234,6 +234,13 @@ public void visit(ExecutableIssue i) { } } + public void visit(SymlinkIssue i) { + if (!ignore.contains(i.check().name())) { + println(i, "file " + i.path() + " is symbolic link"); + hasDisplayedErrors = true; + } + } + public void visit(AuthorNameIssue i) { if (!ignore.contains(i.check().name())) { println(i, "missing author name"); diff --git a/jcheck/src/main/java/org/openjdk/skara/jcheck/IssueVisitor.java b/jcheck/src/main/java/org/openjdk/skara/jcheck/IssueVisitor.java index 5aac81a11..4571b1939 100644 --- a/jcheck/src/main/java/org/openjdk/skara/jcheck/IssueVisitor.java +++ b/jcheck/src/main/java/org/openjdk/skara/jcheck/IssueVisitor.java @@ -42,4 +42,5 @@ public interface IssueVisitor { void visit(ExecutableIssue issue); void visit(BlacklistIssue issue); void visit(BinaryIssue issue); + void visit(SymlinkIssue issue); } diff --git a/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheck.java b/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheck.java index 3f5194d62..269a6cb93 100644 --- a/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheck.java +++ b/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheck.java @@ -77,6 +77,7 @@ public class JCheck { new MessageCheck(utils), new IssuesCheck(utils), new ExecutableCheck(), + new SymlinkCheck(), new BlacklistCheck(blacklist) ); repositoryChecks = List.of( diff --git a/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheckConfiguration.java b/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheckConfiguration.java index f728d2c89..81f85163e 100644 --- a/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheckConfiguration.java +++ b/jcheck/src/main/java/org/openjdk/skara/jcheck/JCheckConfiguration.java @@ -73,7 +73,7 @@ private static INI convert(INI old) { config.add("jbs=JDK"); config.add("[checks]"); - var error = "error=blacklist,author,committer,reviewers,merge,hg-tag,message,issues,executable"; + var error = "error=blacklist,author,committer,reviewers,merge,hg-tag,message,issues,executable,symlink"; var shouldCheckWhitespace = false; var checkWhitespace = old.get("whitespace"); if (checkWhitespace == null || !checkWhitespace.asString().equals("lax")) { diff --git a/jcheck/src/main/java/org/openjdk/skara/jcheck/SymlinkCheck.java b/jcheck/src/main/java/org/openjdk/skara/jcheck/SymlinkCheck.java new file mode 100644 index 000000000..5dcfcfd4d --- /dev/null +++ b/jcheck/src/main/java/org/openjdk/skara/jcheck/SymlinkCheck.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.skara.jcheck; + +import org.openjdk.skara.vcs.Commit; +import org.openjdk.skara.vcs.openjdk.CommitMessage; + +import java.util.Iterator; +import java.util.ArrayList; +import java.util.logging.Logger; + +public class SymlinkCheck extends CommitCheck { + private final Logger log = Logger.getLogger("org.openjdk.skara.jcheck.symlink"); + + @Override + Iterator check(Commit commit, CommitMessage message, JCheckConfiguration conf) { + var metadata = CommitIssue.metadata(commit, message, conf, this); + + var issues = new ArrayList(); + for (var diff : commit.parentDiffs()) { + for (var patch : diff.patches()) { + if (patch.target().type().isPresent()) { + var type = patch.target().type().get(); + if (type.isSymbolicLink()) { + var path = patch.target().path().get(); + log.finer("issue: " + path + " is symbolic link"); + issues.add(new SymlinkIssue(path, metadata)); + } + } + } + } + + return issues.iterator(); + } + + @Override + public String name() { + return "symlink"; + } + + @Override + public String description() { + return "Files should not be symbolic links"; + } +} + diff --git a/jcheck/src/main/java/org/openjdk/skara/jcheck/SymlinkIssue.java b/jcheck/src/main/java/org/openjdk/skara/jcheck/SymlinkIssue.java new file mode 100644 index 000000000..408654b10 --- /dev/null +++ b/jcheck/src/main/java/org/openjdk/skara/jcheck/SymlinkIssue.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.skara.jcheck; + +import java.nio.file.Path; + +public class SymlinkIssue extends CommitIssue { + private final Path path; + + SymlinkIssue(Path path, CommitIssue.Metadata metadata) { + super(metadata); + this.path = path; + } + + public Path path() { + return path; + } + + @Override + public void accept(IssueVisitor v) { + v.visit(this); + } +} + diff --git a/jcheck/src/test/java/org/openjdk/skara/jcheck/JCheckTests.java b/jcheck/src/test/java/org/openjdk/skara/jcheck/JCheckTests.java index c207f86a3..69654c06c 100644 --- a/jcheck/src/test/java/org/openjdk/skara/jcheck/JCheckTests.java +++ b/jcheck/src/test/java/org/openjdk/skara/jcheck/JCheckTests.java @@ -197,6 +197,11 @@ public void visit(ExecutableIssue e) { issues.add(e); } + @Override + public void visit(SymlinkIssue e) { + issues.add(e); + } + @Override public void visit(AuthorNameIssue e) { issues.add(e); diff --git a/jcheck/src/test/java/org/openjdk/skara/jcheck/SymlinkCheckTests.java b/jcheck/src/test/java/org/openjdk/skara/jcheck/SymlinkCheckTests.java new file mode 100644 index 000000000..4254116b2 --- /dev/null +++ b/jcheck/src/test/java/org/openjdk/skara/jcheck/SymlinkCheckTests.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.skara.jcheck; + +import org.openjdk.skara.vcs.*; +import org.openjdk.skara.vcs.openjdk.*; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Iterator; +import java.util.List; +import java.util.ArrayList; +import java.time.ZonedDateTime; +import java.io.IOException; +import java.nio.file.Path; + +class SymlinkCheckTests { + private final Utilities utils = new Utilities(); + + private static final List CONFIGURATION = List.of( + "[general]", + "project = test", + "[checks]", + "error = symlink" + ); + + private static JCheckConfiguration conf() { + return JCheckConfiguration.parse(CONFIGURATION); + } + + private static List symlinkDiff(String filename) { + var patch = new TextualPatch(null, null, Hash.zero(), + Path.of(filename), FileType.fromOctal("120000"), Hash.zero(), + Status.from('A'), List.of()); + var diff = new Diff(Hash.zero(), Hash.zero(), List.of(patch)); + return List.of(diff); + } + + private static List diff(String filename, String line) { + var hunk = new Hunk(new Range(1, 0), List.of(), + new Range(1, 1), List.of(line)); + var patch = new TextualPatch(Path.of(filename), FileType.fromOctal("100644"), Hash.zero(), + Path.of(filename), FileType.fromOctal("100644"), Hash.zero(), + Status.from('M'), List.of(hunk)); + var diff = new Diff(Hash.zero(), Hash.zero(), List.of(patch)); + return List.of(diff); + } + + private static Commit commit(List diffs) { + var author = new Author("foo", "foo@localhost"); + var hash = new Hash("0123456789012345678901234567890123456789"); + var parents = List.of(hash); + var date = ZonedDateTime.now(); + var metadata = new CommitMetadata(hash, parents, author, author, date, List.of("Added symlink")); + return new Commit(metadata, diffs); + } + + private static Commit commitWithSymlink(String filename) { + return commit(symlinkDiff(filename)); + } + + private static Commit commitWithRegularFile(String filename, String line) { + return commit(diff(filename, line)); + } + + private static CommitMessage message(Commit c) { + return CommitMessageParsers.v1.parse(c); + } + + private List toList(Iterator i) { + var list = new ArrayList(); + while (i.hasNext()) { + list.add(i.next()); + } + return list; + } + + @Test + void commitWithSymlinkShouldFail() { + var commit = commitWithSymlink("symlink"); + var message = message(commit); + var check = new SymlinkCheck(); + var issues = toList(check.check(commit, message, conf())); + + assertEquals(1, issues.size()); + assertTrue(issues.get(0) instanceof SymlinkIssue); + var issue = (SymlinkIssue) issues.get(0); + assertEquals("symlink", issue.path().toString()); + } + + @Test + void commitWithoutSymlinkShouldPass() { + var commit = commitWithRegularFile("README.txt", "Hello, world"); + var message = message(commit); + var check = new SymlinkCheck(); + var issues = toList(check.check(commit, message, conf())); + assertEquals(List.of(), issues); + } +}