Class/Object

sclib.io.fs

FSDir

Related Docs: object FSDir | package fs

Permalink

case class FSDir(path: Path) extends FSEntry[FSDir] with Product with Serializable

Represents a 'Directory'

to iterate over a directory tree, there are the following functions available:

the method signature from foreach, map and flatMap shown here are simplified

this functions doesn't work recursive by default. for a recursive behaviour, use their counterpart functions with the 'R' suffix: lsR, foreachR, mapR or collectR.

to control the recursive level, you can give the lsR, foreachR, mapR or collectR function a 'depth' argument.

iterate over a directory tree

you can give foreach, map and flatMap a traverse function which receives a FSEntryImpl or a Try[FSEntryImpl].

assume the following directory tree:

root@main:/tmp/sclib-example # ls -lR /tmp/sclib-example/
total 1
drwx------  2 j  wheel  5 May  4 10:51 pub
d---------  2 j  wheel  5 May  4 10:51 sec

/tmp/sclib-example/pub:
total 2
-rw-r--r--  1 j  wheel  0 May  4 10:51 a
-rw-r--r--  1 j  wheel  0 May  4 10:51 b
-rw-r--r--  1 j  wheel  0 May  4 10:51 c

/tmp/sclib-example/sec:
total 1
----------  1 j  wheel  0 May  4 10:51 a

fail on first error: if you get it a function which receives a FSEntryImpl and an exception occurs, the function execution stops and a Failure are returned.

scala> import sclib.io.fs._
scala> dir("/tmp/sclib-example").get.foreachR(println(_: FSEntryImpl))
FSDir(/tmp/sclib-example/sec)
res0: scala.util.Try[Unit] = Failure(java.nio.file.AccessDeniedException: /tmp/sclib-example/sec)

exceptions intercepted: if you get it a function which receives a Try[FSEntryImpl] and an exception occurs, the function execution continues.

scala> import sclib.io.fs._
scala> import scala.util.Try
scala> dir("/tmp/sclib-example").get.foreachR(println(_: Try[FSEntryImpl]))
Success(FSDir(/tmp/sclib-example/sec))
Failure(java.nio.file.AccessDeniedException: /tmp/sclib-example/sec)
Success(FSDir(/tmp/sclib-example/pub))
Success(FSFile(/tmp/sclib-example/pub/c))
Success(FSFile(/tmp/sclib-example/pub/b))
Success(FSFile(/tmp/sclib-example/pub/a))

check the member documentation for examples

Self Type
FSDir
Linear Supertypes
Serializable, Serializable, Product, Equals, FSEntry[FSDir], AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. FSDir
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. FSEntry
  7. AnyRef
  8. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new FSDir(path: Path)

    Permalink
    Attributes
    protected[sclib.io.fs]

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. lazy val absNormalizedPath: Path

    Permalink

    absolute, normalized path

    absolute, normalized path

    Definition Classes
    FSEntry
  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def chmod(mode: String): Try[FSDir]

    Permalink

    set entry permission mode

    set entry permission mode

    mode

    : list of unix like symbolic permissions notation

    Definition Classes
    FSEntry
    Example:
    1. chmod("a=r,u+w") -> chmod(Seq(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ))
      chmod("a+x")     -> current permissions + Seq(OWNER_EXECUTE, GROUP_EXECUTE, OTHERS_EXECUTE)
  7. def chmod(n: Int): Try[FSDir]

    Permalink

    set entry permission mode

    set entry permission mode

    n

    : unix like file permission notation

    Definition Classes
    FSEntry
    Example:
    1. chmod(700) -> chmod(Seq(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE))
      chmod(644) -> chmod(Seq(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ))
  8. def chmod(perms: PosixFilePermission*): Try[FSDir]

    Permalink

    set entry permission mode

    set entry permission mode

    perms

    : sequence of http://http://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFilePermission.html

    Definition Classes
    FSEntry
    Example:
    1. scala> import java.nio.file.attribute.PosixFilePermission._
      scala> for {
           |   wd <- dir("sclib-example")
           |   wd <- wd.createTemp
           |   fh <- file(wd, "a-file").flatMap(_.create)
           |   _ <- fh.chmod(OWNER_READ)
           |   mod <- fh.lsmod
           |   _ <- wd.deleteR()
           | } yield mod
      res0: scala.util.Try[Seq[java.nio.file.attribute.PosixFilePermission]] = Success(List(OWNER_READ))
  9. def chmodR(mode: String): Try[FSDir]

    Permalink

    set entry permission mode recursive

    set entry permission mode recursive

    fail on any error

    Definition Classes
    FSEntry
    See also

    FSEntry.chmod(mode:String)*

  10. def chmodR(n: Int): Try[FSDir]

    Permalink

    set entry permission mode recursive

    set entry permission mode recursive

    fail on any error

    Definition Classes
    FSEntry
    See also

    FSEntry.chmod(n:Int)*

  11. def chmodR(perms: PosixFilePermission*): Try[FSDir]

    Permalink

    set entry permission mode recursive

    set entry permission mode recursive

    fail on any error

    Definition Classes
    FSEntry
    See also

    FSEntry.chmod(perms:java\.nio\.file\.attribute\.PosixFilePermission*)*

  12. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  13. def collect[A](pf: PartialFunction[Try[FSEntryImpl], A]): Iterator[A]

    Permalink
  14. def collectR[A](pf: PartialFunction[Try[FSEntryImpl], A], depth: Int = Integer.MAX_VALUE): Iterator[A]

    Permalink

    Example:
    1. scala>  import sclib.io.fs._
      scala> import scala.util.Success
      scala> import scala.collection.SortedSet
      scala> dir("wd").flatMap(_.createTemp).map{ wd =>
           |   //
           |   // create some files - some with a 'f' prefix, others with a 'h' prefix
           |   List("f1", "h2", "f3", "h4", "f5").map(file(wd, _).flatMap(_.create))
           |   //
           |   // create some directories
           |   List("fd1", "hd2", "fd3", "hd4", "fd5").map(dir(wd, _).flatMap(_.create))
           |   //
           |   // collect only files, which doesn't have a 'h' prefix
           |   val fileNames = wd.collectR{
           |     case Success(f: FSFile) if ! f.name.startsWith("h") => f.name
           |   }.toList
           |   //
           |   // cleanup
           |   wd.deleteR()
           |   //
           |   fileNames.to[SortedSet]
           | }
      res0: scala.util.Try[SortedSet[String]] = Success(TreeSet(f1, f3, f5))
  15. def copy(target: FSDir, options: CopyOption*): Try[FSDir]

    Permalink

    copy the entry (file -> file, dir -> dir)

    copy the entry (file -> file, dir -> dir)

    options

    http://docs.oracle.com/javase/8/docs/api/java/nio/file/StandardCopyOption.html

    Definition Classes
    FSEntry
    Example:
    1. scala> for {
           |   wd <- dir("sclib-example")
           |   wd <- wd.createTemp
           |   src <- file(wd, "a-file")
           |   _ <- src.write("content")
           |   dst <- file(wd, "b-file")
           |   _ <- src.copy(dst)
           |   c <- dst.slurp
           |   _ <- wd.deleteR
           | } yield c
      res0: scala.util.Try[String] = Success(content)
  16. def copyTo(target: FSDir, options: CopyOption*): Try[FSDir]

    Permalink

    copy the entry to the given dir

    copy the entry to the given dir

    if the target dir doesn't exists, it's being created

    use FSEntry.copyToR(target* for a recursive copy operation

    Definition Classes
    FSEntry
  17. def copyToR(target: FSDir, options: CopyOption*): Try[FSDir]

    Permalink

    recursively copy the entry to the given dir

    recursively copy the entry to the given dir

    if the target dir doesn't exists, it's being created

    Definition Classes
    FSEntry
  18. def create(mode: String): Try[FSDir]

    Permalink

    create the filesystem entry with the given permissions

    create the filesystem entry with the given permissions

    the path hierarchy must exist - use FSEntry.mkDirs(attrs: FileAttribute*) to create the hierarchy at first.

    mode

    unix like symbolic mode

    Definition Classes
    FSEntry
    Example:
    1. create("a=r,u+w") -> create the entry with the PosixFilePermissions: OWNER_READ, OWNER_WRITE, GROUP_READ and OTHERS_READ
      scala> import scala.collection.SortedSet
      scala> for {
           |   wd <- dir("/tmp/sclib")
           |   wd <- wd.createTemp
           |   fh <- file(wd, "example")
           |   _ <- fh.create("a=r,u+wx")
           |   perm <- fh.lsmod
           |   _ <- wd.deleteR
           | } yield perm.to[SortedSet]
      res0: scala.util.Try[scala.collection.SortedSet[java.nio.file.attribute.PosixFilePermission]] = Success(TreeSet(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, OTHERS_READ))
  19. def create(n: Int): Try[FSDir]

    Permalink

    create the filesystem entry with the given permissions

    create the filesystem entry with the given permissions

    the path hierarchy must exist - use FSEntry.mkDirs(attrs: FileAttribute*) to create the hierarchy at first.

    n

    unix like numeric mode

    Definition Classes
    FSEntry
    Example:
    1. scala> import scala.collection.SortedSet
      scala> for {
           |   wd <- dir("/tmp/sclib")
           |   wd <- wd.createTemp
           |   fh <- file(wd, "example")
           |   _ <- fh.create(740)
           |   perm <- fh.lsmod
           |   _ <- wd.deleteR
           | } yield perm.to[SortedSet]
      res0: scala.util.Try[SortedSet[java.nio.file.attribute.PosixFilePermission]] = Success(TreeSet(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ))
  20. def create(perms: PosixFilePermission*): Try[FSDir]

    Permalink

    create the filesystem entry with the given permissions

    create the filesystem entry with the given permissions

    the path hierarchy must exist - use FSEntry.mkDirs(attrs:java\.nio\.file\.attribute\.FileAttribute[_]*)* to create the hierarchy at first.

    Definition Classes
    FSEntry
  21. def create: Try[FSDir]

    Permalink

    create the filesystem entry with default permissions

    create the filesystem entry with default permissions

    the path hierarchy must exist - use FSEntry.mkDirs(attrs:java\.nio\.file\.attribute\.FileAttribute[_]*)* to create the hierarchy at first.

    Definition Classes
    FSEntry
  22. def createTemp: Try[FSDir]

    Permalink

    create a new temporary filesystem entry.

    create a new temporary filesystem entry. the actual name is used for the prefix.

    if no absolute path is given, the entry is created in the temp directory (determined by: System.getProperty("java.io.tmpdir")).

    Definition Classes
    FSEntry
    Example:
    1. scala> for {
           |   fh <- dir("a-temp-dir")
           |   // override the original fh
           |   fh <- fh.createTemp
           |   _ <- fh.delete
           |   // fh.name is something like 'a-temp-dir7526811586167481701'
           | } yield fh.name.matches("a-temp-dir\\d+")
      res0: scala.util.Try[Boolean] = Success(true)
      scala> for {
           |   fh <- file("a-temp-file")
           |   // override the original fh
           |   fh <- fh.createTemp
           |   _ <- fh.delete
           |   // fh.name is something like 'a-temp-file9014711075515420555'
           | } yield fh.name.matches("a-temp-file\\d+")
      res0: scala.util.Try[Boolean] = Success(true)
      scala> for {
           |   fh <- file("a-temp-file.txt")
           |   // override the original fh
           |   fh <- fh.createTemp
           |   _ <- fh.delete
           |   // fh.name is something like 'a-temp-file2068553337840465580.txt'
           | } yield fh.name.matches("a-temp-file\\d+\\.txt")
      res1: scala.util.Try[Boolean] = Success(true)
  23. def delete(): Try[Unit]

    Permalink

    delete the file

    delete the file

    returns a Failure if the file doesn't exist.

    Definition Classes
    FSEntry
    See also

    FSEntry.deleteIfExists

  24. def deleteIfExists(): Try[Boolean]

    Permalink

    delete the file if it exists

    delete the file if it exists

    returns

    true if the file was deleted, false if it doesn't exist.

    Definition Classes
    FSEntry
  25. def deleteR(): Try[Unit]

    Permalink

    delete the directory recursive

    delete the directory recursive

    this function is NOT atomic - when a error occurs, it returns a Failure but the successful deleted files are lost.

  26. def depth(other: FSEntryImpl): Int

    Permalink

    relative depth to a given FSEntry

    relative depth to a given FSEntry

    Definition Classes
    FSEntry
    Example:
    1. scala> for {
           |  a <- dir("sclib-example")
           |  b <- dir(a, "x/y/z/..")
           | } yield a.depth(b)
      res0: scala.util.Try[Int] = Success(2)
  27. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  28. def exists: Boolean

    Permalink

    check if the file exists

    check if the file exists

    Definition Classes
    FSEntry
  29. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  30. def flatMap[A](tf: TraverseFunction[A]): Result

    Permalink
  31. def flatMapR[A](tf: TraverseFunction[A], depth: Int = Integer.MAX_VALUE): Result

    Permalink

    apply the given function recursive to every FSEntry start from this directory

    apply the given function recursive to every FSEntry start from this directory

    returns

    a Iterator where for each entry the given function was applied

  32. def foreach[A](tf: TraverseFunction[A]): Result

    Permalink
  33. def foreachR[A](tf: TraverseFunction[A], depth: Int = Integer.MAX_VALUE): Result

    Permalink

    apply the given function recursive to every FSEntry start from this directory

  34. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  35. def isDirectory: Boolean

    Permalink

    check if the file is a directory

    check if the file is a directory

    Definition Classes
    FSEntry
  36. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  37. def isRegularFile: Boolean

    Permalink

    check if the file is a regular file

    check if the file is a regular file

    Definition Classes
    FSEntry
  38. def ls: Iterator[Try[FSEntryImpl]]

    Permalink
  39. def lsR: Iterator[Try[FSEntryImpl]]

    Permalink

    recursive directory content, start from this directory.

    recursive directory content, start from this directory.

    shortcut to use without parenthesize

    to control the depth, use FSDir.lsR(depth:Int)*

  40. def lsR(depth: Int = Integer.MAX_VALUE): Iterator[Try[FSEntryImpl]]

    Permalink

    recursive directory content, start from this directory.

    recursive directory content, start from this directory.

    depth

    maximum depth level

    returns

    a Iterator with the directory entries

  41. def lsmod: Try[Seq[PosixFilePermission]]

    Permalink

    get entry permission mode

    get entry permission mode

    Definition Classes
    FSEntry
  42. def map[A](tf: TraverseFunction[A]): Result

    Permalink
  43. def mapR[A](tf: TraverseFunction[A], depth: Int = Integer.MAX_VALUE): Result

    Permalink

    apply the given function recursive to every FSEntry start from this directory

    apply the given function recursive to every FSEntry start from this directory

    returns

    a Iterator where for each entry the given function was applied

  44. def mkDirs: Try[FSDir]

    Permalink

  45. def mkDirs(attrs: FileAttribute[_]*): Try[FSDir]

    Permalink

    create the directories

    create the directories

    - when called on a FSDir, the directroy's path (inclusive parents) are created - when called on a FSFile the parent hierarchy are created

    attrs

    http://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFileAttributes.html

    Definition Classes
    FSEntry
  46. def move(target: FSDir, options: CopyOption*): Try[FSDir]

    Permalink

    move the entry

  47. def moveTo(dir: FSDir, options: CopyOption*): Try[FSDir]

    Permalink

    move the entry to the given directory, and keep the name

    move the entry to the given directory, and keep the name

    options

    http://docs.oracle.com/javase/8/docs/api/java/nio/file/StandardCopyOption.html

    Definition Classes
    FSEntry
  48. def mtime(millis: Long): Try[Unit]

    Permalink

    set the modification time

    set the modification time

    millis

    file modification time in milliseconds, since the epoch (1970-01-01T00:00:00Z)

    Definition Classes
    FSEntry
  49. def mtime: Try[Long]

    Permalink

    file modification time in milliseconds, since the epoch (1970-01-01T00:00:00Z)

    file modification time in milliseconds, since the epoch (1970-01-01T00:00:00Z)

    Definition Classes
    FSEntry
  50. lazy val name: String

    Permalink

    file / directory name

    file / directory name

    Definition Classes
    FSEntry
  51. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  52. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  53. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  54. val path: Path

    Permalink

    underlying path

    underlying path

    Definition Classes
    FSDirFSEntry
  55. def renameTo(newName: String, options: CopyOption*): Try[FSDir]

    Permalink

    rename the entry

    rename the entry

    options

    http://docs.oracle.com/javase/8/docs/api/java/nio/file/StandardCopyOption.html

    Definition Classes
    FSEntry
    Example:
    1. scala> for {
           |   wd <- dir("sclib-example")
           |   wd <- wd.createTemp
           |   src <- file(wd, "a-file")
           |   _ <- src.create
           |   dst <- src.renameTo("b-file")
           |   res = (src.exists, dst.exists)
           |   _ <- wd.deleteR
           | } yield res
      res0: scala.util.Try[(Boolean, Boolean)] = Success((false,true))
  56. def size: Try[Long]

    Permalink

    file size in bytes

    file size in bytes

    Definition Classes
    FSEntry
  57. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  58. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  59. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  60. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  61. def withPath(p: Path): FSDir

    Permalink
    Attributes
    protected[sclib.io.fs]
    Definition Classes
    FSDirFSEntry

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from FSEntry[FSDir]

Inherited from AnyRef

Inherited from Any

Ungrouped