MyNixOS website logo
Description

A simple directory-like tree datatype, with useful IO functions.

A simple directory-like tree datatype, with useful IO functions and Foldable and Traversable instance

Provides a simple data structure mirroring a directory tree on the filesystem, as well as useful functions for reading and writing file and directory structures in the IO monad.

Importing the library and optional (useful) Foldable and Traverable libraries:

import System.Directory.Tree
import qualified Data.Foldable as F
import qualified Data.Traversable as T

Write a hand-made directory tree of textfiles (strings) to the disk. Simulates creating a new user Tux's home directory on a unix machine:

writeDirectory$ "/home" :/ Dir "Tux" [File "README" "Welcome!"]

"read" a directory by opening all the files at a filepath with readFile, returning an 'AnchoredDirTree String' (d2). Then check for any IO failures:

do (base :/ d2) <- readDirectory "../parent_dir/dir2/"
   let failed = anyFailed d2
   if failed then ...

Use Foldable instance function to concat a directory dir of text files into a single file under the same directory:

do (b :/ dt) <- readDirectory dir
   let f = F.concat dt
   return$ b :/ File "ALL_TEXT" f

Open all the files in the current directory as lazy bytestrings, ignoring the base path in Anchored wrapper:

import qualified Data.ByteString.Lazy as B
do (_ :/ dTree) <- readDirectoryWith B.readFile "./"

This version also offers an experimental function readDirectoryWithL that does lazy directory IO, allowing you to treat the returned DirTree as if it were a normal lazily-generated data structure.

For example, the following does only the amount of IO necessary to list the file names of the children of the root directory, similar to "ls /":

do d <- readDirectoryWithL readFile "/"
   mapM_ (putStrLn . name) $ contents $ free d

Any ideas or suggestions for improvements are most welcome :-)

CHANGES: from 0.11

  • export System.Directory.Tree.transformDir as requested

  • add test suite to cabal file

  • remove redundant removeNonexistent (thanks to dmwit for patch)

Metadata

Version

0.12.1

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows