Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file2: platform-independent walk #2541

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions io/js/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ private[fs2] trait FilesCompanionPlatform {
override def isWritable(path: Path): F[Boolean] =
access(path, fsMod.constants.W_OK)

override def isSameFile(path1: Path, path2: Path): F[Boolean] =
F.pure(path1.absolute == path2.absolute)

override def list(path: Path): Stream[F, Path] =
Stream
.bracket(F.fromPromise(F.delay(fsPromisesMod.opendir(path.toString))))(dir =>
Expand Down
3 changes: 3 additions & 0 deletions io/jvm/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ private[file] trait FilesCompanionPlatform {
def isWritable(path: Path): F[Boolean] =
Sync[F].delay(JFiles.isWritable(path.toNioPath))

def isSameFile(path1: Path, path2: Path): F[Boolean] =
Sync[F].blocking(JFiles.isSameFile(path1.toNioPath, path2.toNioPath))

def list(path: Path): Stream[F, Path] =
_runJavaCollectionResource[JStream[JPath]](
Sync[F].blocking(JFiles.list(path.toNioPath)),
Expand Down
38 changes: 25 additions & 13 deletions io/shared/src/main/scala/fs2/io/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import cats.effect.std.Hotswap
import cats.syntax.all._

import scala.concurrent.duration._
import cats.Traverse

/** Provides operations related to working with files in the effect `F`.
*
Expand Down Expand Up @@ -136,6 +137,8 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
def isSymbolicLink(path: Path): F[Boolean]
def isWritable(path: Path): F[Boolean]

def isSameFile(path1: Path, path2: Path): F[Boolean]

/** Gets the contents of the specified directory.
*/
def list(path: Path): Stream[F, Path]
Expand Down Expand Up @@ -299,7 +302,7 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
}

object Files extends FilesCompanionPlatform {
private[file] abstract class UnsealedFiles[F[_]: Async] extends Files[F] {
private[file] abstract class UnsealedFiles[F[_]](implicit F: Async[F]) extends Files[F] {

def readAll(path: Path, chunkSize: Int, flags: Flags): Stream[F, Byte] =
Stream.resource(readCursor(path, flags)).flatMap { cursor =>
Expand Down Expand Up @@ -345,31 +348,40 @@ object Files extends FilesCompanionPlatform {

def walk(start: Path, maxDepth: Int, followLinks: Boolean): Stream[F, Path] = {

def go(start: Path, maxDepth: Int, ancestry: List[Option[FileKey]]): Stream[F, Path] =
def go(start: Path, maxDepth: Int, ancestry: List[Either[Path, FileKey]]): Stream[F, Path] =
if (maxDepth == 0)
Stream.eval(exists(start, followLinks)).as(start)
else
Stream.eval(getBasicFileAttributes(start, followLinks = false)).flatMap { attr =>
(if (attr.isDirectory)
list(start)
.flatMap { path =>
go(path, maxDepth - 1, attr.fileKey :: ancestry)
go(path, maxDepth - 1, attr.fileKey.toRight(start) :: ancestry)
}
.recoverWith { case _ =>
Stream.empty
}
else if (attr.isSymbolicLink && followLinks)
Stream.eval(getBasicFileAttributes(start, followLinks = true)).flatMap { attr =>
if (!ancestry.contains(attr.fileKey))
list(start)
.flatMap { path =>
go(path, maxDepth - 1, attr.fileKey :: ancestry)
}
.recoverWith { case _ =>
Stream.empty
}
else
Stream.raiseError(new FileSystemLoopException(start.toString))
val fileKey = attr.fileKey
val isCycle = Traverse[List].existsM(ancestry) {
case Right(ancestorKey) => F.pure(fileKey.contains(ancestorKey))
case Left(ancestorPath) => isSameFile(start, ancestorPath)
}

Stream.eval(isCycle).flatMap { isCycle =>
if (!isCycle)
list(start)
.flatMap { path =>
go(path, maxDepth - 1, attr.fileKey.toRight(start) :: ancestry)
}
.recoverWith { case _ =>
Stream.empty
}
else
Stream.raiseError(new FileSystemLoopException(start.toString))
}

}
else
Stream.empty) ++ Stream.emit(start)
Expand Down