Skip to content

Commit 5ac6cbc

Browse files
author
Johannes Coetzee
committed
fix path issues
1 parent 03dcd95 commit 5ac6cbc

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

joern-cli/frontends/php2cpg/src/main/scala/io/joern/php2cpg/utils/PathFilter.scala

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.joern.php2cpg.utils
22

3-
import io.joern.php2cpg.utils.PathFilter.correctPath
3+
import io.joern.php2cpg.utils.PathFilter.standardisePath
44
import org.slf4j.LoggerFactory
55

66
import java.io.File
@@ -15,7 +15,15 @@ class PathFilter(excludeOverrides: Option[List[String]]) {
1515
private val anyDirSuffix = s"$sep.*"
1616

1717
def dirExclude(dirName: String): Regex = {
18-
s"${startOrSep}${correctPath(dirName)}${anyDirSuffix}".r
18+
val path = escapeSeparators(standardisePath(dirName))
19+
s"${startOrSep}$path${anyDirSuffix}".r
20+
}
21+
22+
private def escapeSeparators(filename: String): String = {
23+
if (File.separator == "/")
24+
filename
25+
else
26+
filename.replaceAll(raw"\\", raw"\\\\")
1927
}
2028

2129
private val excludeRegexes = {
@@ -35,12 +43,15 @@ object PathFilter {
3543
".git"
3644
)
3745

38-
def correctPath(filename: String): String = {
39-
val nameParts = filename.split("/")
40-
Paths.get(nameParts.head, nameParts.tail: _*).toString
46+
def standardisePath(filename: String): String = {
47+
// Synthetic filename, so don't correct
48+
if (filename.contains("<"))
49+
filename
50+
else
51+
Paths.get(filename).toString
4152
}
4253

43-
def apply(excludes: Option[List[String]]) = {
54+
def apply(excludes: Option[List[String]]): PathFilter = {
4455
new PathFilter(excludes)
4556
}
4657
}

joern-cli/frontends/php2cpg/src/test/scala/io/joern/php2cpg/querying/FileTests.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class FileTests extends PhpCode2CpgFixture {
2323

2424
private def getCpg(): TestCpg = {
2525
val cpg = code("", fileName = testFiles.head)
26-
testFiles.tail.foreach(path => cpg.moreCode("", fileName = PathFilter.correctPath(path)))
26+
testFiles.tail.foreach(path => cpg.moreCode("", fileName = PathFilter.standardisePath(path)))
2727
cpg
2828
}
2929

3030
"test, vendor, and .git directories should be filtered out by default" in {
3131
val cpg = getCpg()
3232

3333
cpg.file.name.toSet shouldBe Set("a.php", "src/b.php", "src/sub/c.php", "misc/g.php", "nottests/j.php", "<unknown>")
34-
.map(PathFilter.correctPath)
34+
.map(PathFilter.standardisePath)
3535
}
3636

3737
"custom excludes should exclude the correct directories" in {
@@ -49,6 +49,6 @@ class FileTests extends PhpCode2CpgFixture {
4949
"sub/tests/i.php",
5050
"nottests/j.php",
5151
"<unknown>"
52-
)
52+
).map(PathFilter.standardisePath)
5353
}
5454
}

0 commit comments

Comments
 (0)