Package

sclib.io

fs

Permalink

package fs

working with files and directories

get a file / directory handle

to get a file handle, use any of the following functions:

for a directory handle:

example:
scala> import sclib.io.fs._
scala> file("/path/file-name")
res0: scala.util.Try[FSFile] = Success(FSFile(/path/file-name))
scala> dir("/path/dir-name")
res1: scala.util.Try[FSDir] = Success(FSDir(/path/dir-name))

this functions returns a Failure if the requested entry exists, but has a wrong type.

scala> import sclib.io.fs._
scala> file("a-file").flatMap(_.create)
res0: scala.util.Try[FSFile] = Success(FSFile(a-file))
scala> dir("a-file")
res1: scala.util.Try[FSDir] = Failure(java.lang.Exception: 'a-file' is a file)
scala> file("a-file").flatMap(_.delete)
res2: scala.util.Try[Unit] = Success(())
work with a handle

all functions which can throw a exception are wrapped in a Try, so it's easy to compose.

scala> import sclib.io.fs._
scala> for {
     |   fh <- file("file-name")
     |   _ <- fh.write("file content")
     |   c <- fh.slurp
     |   _ <- fh.delete
     | } yield c
res0: scala.util.Try[String] = Success(file content)
See also

sclib.io.fs.FSDir

sclib.io.fs.FSFile

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. fs
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

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

    Permalink

    Represents a 'Directory'

    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

  2. trait FSEntry[Self <: FSEntry[Self]] extends AnyRef

    Permalink

    File System Entry

  3. type FSEntryImpl = FSEntry[A] forSome {type A <: FSEntry[A]}

    Permalink

    Any FSEntry Implementation

  4. case class FSFile(path: Path) extends FSEntry[FSFile] with Product with Serializable

    Permalink

    Represents a 'File'

    Represents a 'File'

    the functions write, writeLines, append and appendLines expects a type-class instance (from Writable) for their to be written payload in scope. instances for string, char, short, int, long, float, double and some collection types are already defined (see Writable$).

    the write and append functions don't add a newline at the end / between the sequences. for functions which add newlines use writeLines and appendLines.

    Example:
    1. scala> import sclib.io._
      scala> for {
           |   fh <- file("/tmp/example")
           |   _ <- fh.writeLines("1. apple")                        // string
           |   _ <- fh.appendLines(List("2. banana", "3. cherry"))   // list of string
           |   _ <- fh.append(4)                                     // int
           |   _ <- fh.append('.')                                   // char
           |   _ <- fh.append(Vector(' ', 'd', 'o', 'g'))            // vector of char
           |   content <- fh.slurp
           |   _ <- fh.delete
           |
      res0: scala.util.Try[String] =
      Success(1. apple
      2. banana
      3. cherry
      4. dog)

      check the member documentation for examples

  5. case class FSIterator(start: FSDir, depth: Int = Integer.MAX_VALUE, includeStartDir: Boolean = false) extends Iterator[Try[FSEntryImpl]] with Product with Serializable

    Permalink

    a save file-system iterator.

    a save file-system iterator.

    it wraps every file operation in a Try.

    by default, the iterator start in the given directory, and walk recursively (deep first) over all entries. you can control the recursive level with the 'depth' argument, where 1 means only the content from the given directory. to start with the given directory, use 'includeStartDir = true'.

    assume the following directory tree:

    /tmp/sclib-example/a1/b1/a1b1file
    /tmp/sclib-example/a1/b1/c1/a1b1c1file
    /tmp/sclib-example/a2/b2/a2b2file
    

    default behaviour

    scala> FSIterator(dir("/tmp/sclib-example").get).foreach(println)
    Success(FSDir(/tmp/sclib-example/a1))
    Success(FSDir(/tmp/sclib-example/a1/b1))
    Success(FSFile(/tmp/sclib-example/a1/b1/a1b1file))
    Success(FSDir(/tmp/sclib-example/a1/b1/c1))
    Success(FSFile(/tmp/sclib-example/a1/b1/c1/a1b1c1file))
    Success(FSDir(/tmp/sclib-example/a2))
    Success(FSDir(/tmp/sclib-example/a2/b2))
    Success(FSFile(/tmp/sclib-example/a2/b2/a2b2file))
    

    with max-depth: 2 and the start directory included

    scala> FSIterator(dir("/tmp/sclib-example").get, depth = 2, includeStartDir = true).foreach(println)
    Success(FSDir(/tmp/sclib-example))
    Success(FSDir(/tmp/sclib-example/a1))
    Success(FSDir(/tmp/sclib-example/a1/b1))
    Success(FSDir(/tmp/sclib-example/a2))
    Success(FSDir(/tmp/sclib-example/a2/b2))
    

    start

    directory to start

    depth

    the maximum number of directory levels to visit

  6. trait Writable[A] extends AnyRef

    Permalink

    type-class for the FSFile.write / FSFile.append functions

Value Members

  1. object FSDir extends Serializable

    Permalink
  2. object FSPerm

    Permalink

    utility to calculate PosixFilePermissions from unix-style notation:

    utility to calculate PosixFilePermissions from unix-style notation:

      - FSPerm.calc(644)      -> Seq(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ)
      - FSPerm.mod("a=r,u+w") -> Seq(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ)
    

  3. object Writable

    Permalink

    Instances for the Writable type-class

  4. def dir(parent: FSDir, name: String): Try[FSDir]

    Permalink

    initialize a directory-handle from a given path

    initialize a directory-handle from a given path

    if the given path exists, and is a file, a failure are returned.

    this doesn't create the directory - use FSEntry.create(n:* / FSDir.createTemp to create it

  5. def dir(name: String): Try[FSDir]

    Permalink

    initialize a directory-handle from a given path

    initialize a directory-handle from a given path

    if the given path exists, and is a file, a failure are returned.

    this doesn't create the directory - use FSEntry.create(n:* / FSDir.createTemp to create it

  6. def dir(path: Path): Try[FSDir]

    Permalink

    initialize a directory-handle from a given path

    initialize a directory-handle from a given path

    if the given path exists, and is a file, a failure are returned.

    this doesn't create the directory - use FSEntry.create(n:* / FSDir.createTemp to create it

  7. def file(parent: FSDir, name: String): Try[FSFile]

    Permalink

    initialize a file-handle from a given path

    initialize a file-handle from a given path

    if the given path exists, and is a directory, a failure are returned.

    this doesn't create the file - use FSEntry.create(n:* / FSFile.createTemp / any write method to create it

  8. def file(name: String): Try[FSFile]

    Permalink

    initialize a file-handle from a given path

    initialize a file-handle from a given path

    if the given path exists, and is a directory, a failure are returned.

    this doesn't create the file - use FSEntry.create(n:* / FSFile.createTemp / any write method to create it

  9. def file(path: Path): Try[FSFile]

    Permalink

    initialize a file-handle from a given path

    initialize a file-handle from a given path

    if the given path exists, and is a directory, a failure are returned.

    this doesn't create the file - use FSEntry.create(n:* / FSFile.createTemp / any write method to create it

Inherited from AnyRef

Inherited from Any

Ungrouped