@@ -385,14 +385,22 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
385
385
* For example, to eagerly walk a directory while following symbolic links, emitting all
386
386
* paths as a single chunk, use `walk(start, WalkOptions.Eager.withFollowLinks(true))`.
387
387
*/
388
- def walk (start : Path , options : WalkOptions ): Stream [F , Path ]
388
+ def walk (start : Path , options : WalkOptions ): Stream [F , Path ] =
389
+ walkWithAttributes(start, options).map(_.path)
389
390
390
391
/** Creates a stream of paths contained in a given file tree down to a given depth.
391
392
*/
392
393
@ deprecated(" Use walk(start, WalkOptions.Default.withMaxDepth(..).withFollowLinks(..))" , " 3.10" )
393
394
def walk (start : Path , maxDepth : Int , followLinks : Boolean ): Stream [F , Path ] =
394
395
walk(start, WalkOptions .Default )
395
396
397
+ /** Like `walk` but returns a `PathInfo`, which provides both the `Path` and `BasicFileAttributes`. */
398
+ def walkWithAttributes (start : Path ): Stream [F , PathInfo ] =
399
+ walkWithAttributes(start, WalkOptions .Default )
400
+
401
+ /** Like `walk` but returns a `PathInfo`, which provides both the `Path` and `BasicFileAttributes`. */
402
+ def walkWithAttributes (start : Path , options : WalkOptions ): Stream [F , PathInfo ]
403
+
396
404
/** Writes all data to the file at the specified path.
397
405
*
398
406
* The file is created if it does not exist and is truncated.
@@ -517,44 +525,46 @@ object Files extends FilesCompanionPlatform with FilesLowPriority {
517
525
case _ : NoSuchFileException => ()
518
526
})
519
527
520
- def walk (start : Path , options : WalkOptions ): Stream [F , Path ] = {
528
+ def walkWithAttributes (start : Path , options : WalkOptions ): Stream [F , PathInfo ] = {
521
529
522
- def go (start : Path , maxDepth : Int , ancestry : List [Either [Path , FileKey ]]): Stream [F , Path ] =
523
- Stream .emit(start) ++ {
524
- if (maxDepth == 0 ) Stream .empty
525
- else
526
- Stream .eval(getBasicFileAttributes(start, followLinks = false )).mask.flatMap { attr =>
527
- if (attr.isDirectory)
528
- list(start).mask.flatMap { path =>
529
- go(path, maxDepth - 1 , attr.fileKey.toRight(start) :: ancestry)
530
+ def go (
531
+ start : Path ,
532
+ maxDepth : Int ,
533
+ ancestry : List [Either [Path , FileKey ]]
534
+ ): Stream [F , PathInfo ] =
535
+ Stream .eval(getBasicFileAttributes(start, followLinks = false )).mask.flatMap { attr =>
536
+ Stream .emit(PathInfo (start, attr)) ++ {
537
+ if (maxDepth == 0 ) Stream .empty
538
+ else if (attr.isDirectory)
539
+ list(start).mask.flatMap { path =>
540
+ go(path, maxDepth - 1 , attr.fileKey.toRight(start) :: ancestry)
541
+ }
542
+ else if (attr.isSymbolicLink && options.followLinks)
543
+ Stream .eval(getBasicFileAttributes(start, followLinks = true )).mask.flatMap { attr =>
544
+ val fileKey = attr.fileKey
545
+ val isCycle = Traverse [List ].existsM(ancestry) {
546
+ case Right (ancestorKey) => F .pure(fileKey.contains(ancestorKey))
547
+ case Left (ancestorPath) => isSameFile(start, ancestorPath)
530
548
}
531
- else if (attr.isSymbolicLink && options.followLinks)
532
- Stream .eval(getBasicFileAttributes(start, followLinks = true )).mask.flatMap {
533
- attr =>
534
- val fileKey = attr.fileKey
535
- val isCycle = Traverse [List ].existsM(ancestry) {
536
- case Right (ancestorKey) => F .pure(fileKey.contains(ancestorKey))
537
- case Left (ancestorPath) => isSameFile(start, ancestorPath)
538
- }
539
549
540
- Stream .eval(isCycle).flatMap { isCycle =>
541
- if (! isCycle)
542
- list(start).mask.flatMap { path =>
543
- go(path, maxDepth - 1 , attr.fileKey.toRight(start) :: ancestry)
544
- }
545
- else if (options.allowCycles)
546
- Stream .empty
547
- else
548
- Stream .raiseError(new FileSystemLoopException (start.toString))
550
+ Stream .eval(isCycle).flatMap { isCycle =>
551
+ if (! isCycle)
552
+ list(start).mask.flatMap { path =>
553
+ go(path, maxDepth - 1 , attr.fileKey.toRight(start) :: ancestry)
549
554
}
550
-
555
+ else if (options.allowCycles)
556
+ Stream .empty
557
+ else
558
+ Stream .raiseError(new FileSystemLoopException (start.toString))
551
559
}
552
- else
553
- Stream .empty
554
- }
560
+
561
+ }
562
+ else
563
+ Stream .empty
564
+ }
555
565
}
556
566
557
- Stream .eval(getBasicFileAttributes(start, options.followLinks)) >> go(
567
+ go(
558
568
start,
559
569
options.maxDepth,
560
570
Nil
0 commit comments