Skip to content

Commit

Permalink
Catch visitFileFailed during project copy process (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Feb 22, 2024
1 parent 30712bb commit 2d893db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/io/shiftleft/js2cpg/core/Js2Cpg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import better.files.File.LinkOptions
import io.shiftleft.js2cpg.passes._
import io.shiftleft.js2cpg.io.FileDefaults._
import io.shiftleft.js2cpg.io.FileUtils
import io.shiftleft.js2cpg.parser.{FreshJsonParser, PackageJsonParser}
import io.shiftleft.js2cpg.parser.PackageJsonParser
import io.shiftleft.js2cpg.preprocessing.NuxtTranspiler
import io.shiftleft.js2cpg.preprocessing.TranspilationRunner
import io.shiftleft.js2cpg.utils.MemoryMetrics
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/io/shiftleft/js2cpg/io/FileCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class FileCollector private (pathFilter: PathFilter) extends SimpleFileVisitor[P

override def visitFileFailed(file: Path, exc: IOException): FileVisitResult = {
exc match {
case loop: FileSystemLoopException =>
logger.debug(s"Cyclic symbolic link detected for file '$file'", loop)
case _: FileSystemLoopException =>
logger.warn(s"Cyclic symbolic link detected for file '$file' - ignoring")
case _ =>
logger.debug(s"Unable to visit file '$file'", exc)
logger.warn(s"Unable to visit file '$file'", exc)
}
FileVisitResult.CONTINUE
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/scala/io/shiftleft/js2cpg/io/FileUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import better.files.File

import java.nio.file.{Files, FileVisitResult, Path, SimpleFileVisitor}
import io.shiftleft.js2cpg.core.Config
import io.shiftleft.js2cpg.io.FileDefaults._
import io.shiftleft.js2cpg.io.FileDefaults.*
import io.shiftleft.utils.IOUtils
import org.slf4j.LoggerFactory

import java.io.IOException
import java.nio.charset.CharsetDecoder
import java.nio.charset.CodingErrorAction
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.FileSystemLoopException
import java.nio.file.FileVisitOption
import scala.collection.concurrent.TrieMap
import scala.collection.{mutable, SortedMap}
import scala.io.Codec
Expand Down Expand Up @@ -63,7 +66,7 @@ object FileUtils {
filterIgnoredFiles: Boolean = true
): List[Path] = {
val fileCollector = FileCollector(PathFilter(rootPath, config, filterIgnoredFiles, extensions))
Files.walkFileTree(rootPath, fileCollector)
Files.walkFileTree(rootPath, java.util.Set.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, fileCollector)
excludedPaths.addAll(fileCollector.excludedPaths)
fileCollector.files
}
Expand All @@ -83,6 +86,8 @@ object FileUtils {
if (from.isDirectory) {
Files.walkFileTree(
from.path,
java.util.Set.of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE,
new SimpleFileVisitor[Path] {
private def newPath(subPath: Path): Path =
destination.path.resolve(from.path.relativize(subPath))
Expand All @@ -102,6 +107,16 @@ object FileUtils {
}
result
}

override def visitFileFailed(file: Path, exc: IOException): FileVisitResult = {
exc match {
case _: FileSystemLoopException =>
logger.warn(s"Cyclic symbolic link detected for file '$file' - ignoring")
case _ =>
logger.warn(s"Unable to visit file '$file'", exc)
}
FileVisitResult.CONTINUE
}
}
)
} else {
Expand Down

0 comments on commit 2d893db

Please sign in to comment.