Module Virtfs.TreeSource

Describes a file tree containing files or directories (a directory being a file tree). Unlike Path, which can be used in an application to abstract paths, Tree is mainly used to mount virtual file systems, which are particularly useful for writing unit tests.

Representation

The tree module is quite rigid and imposes the File vs Directory structure, but makes no assumptions whatsoever about the contents of files and the metadata associated with files and directories.

Sourcetype ('a, 'metadata) t

Describes a tree (a directory or file list).

A tree is a list of items, where an item can be a file or a directory (which is a list of files).

Sourcemodule Item : sig ... end

Describes the contents of a file system, which may be files or directories.

Building Trees

Building a tree generally involves lifting a list of items.

Sourceval make : ?scope_metadata:(Path.t -> 'metadata option) -> scope:Path.t -> ('a, 'metadata) Item.t list -> ('a, 'metadata) t

make ?scope_metadata ?scope items builds a tree. The scope parameter allows you to define the tree in a given path. The scope_metadata function allows you to attach metadata to each intermediate directory in the scope.

Sourceval from_root : ('a, 'metadata) Item.t list -> ('a, 'metadata) t

from_root is like make but using Path.root as scope.

Sourceval from_cwd : ('a, 'metadata) Item.t list -> ('a, 'metadata) t

from_cwd is like make but using Path.cwd as scope.

Sourceval dir : ?metadata:'metadata -> name:string -> ('a, 'metadata) Item.t list -> ('a, 'metadata) Item.t
Sourceval file : ?metadata:'metadata -> name:string -> 'a -> ('a, 'metadata) Item.t

Operation on Trees

Sourceval fetch : path:Path.t -> ('a, 'metadata) t -> ('a, 'metadata) Item.t option

fetch ~path fs try to reach the item at the position path.

Sourceval prism : scope:Path.t -> ('a, 'metadata) t -> ('a, 'metadata) t

prism fs scope returns a sub-tree based on a path (scope).

Sourceval update : path:Path.t -> (previous:('a, 'metadata) Item.t option -> path:Path.t -> ('a, 'metadata) Item.t option) -> ('a, 'metadata) t -> ('a, 'metadata) t

update ~path callback fs generic function to modify the filetree, it is the callback function (returning an option) that describes whether the file should be created or deleted.

Sourceval touch : path:Path.t -> ?if_exists:(('a, 'metadata) Item.t -> ('a, 'metadata) Item.t) -> ?metadata:'metadata -> 'a -> ('a, 'metadata) t -> ('a, 'metadata) t

touch ~path ?metadata content fs returns a new filesystem where, if the target exists, if_exists is applied; otherwise, a file is created.

Sourceval rm : path:Path.t -> ('a, 'metadata) t -> ('a, 'metadata) t

rm ~path fs remove the item by a given path.

Sourceval rm_file : path:Path.t -> ('a, 'metadata) t -> ('a, 'metadata) t

rm_file ~path fs remove the file by a given path.

Sourceval rm_dir : path:Path.t -> ('a, 'metadata) t -> ('a, 'metadata) t

rm_dir ~path fs remove the directory by a given path.

Sourceval mv : target:Path.t -> source:Path.t -> ('a, 'metadata) t -> ('a, 'metadata) t

mv fs ~target ~source move source as target. If the target exists, or the given source does not exists, fs remains unchanged.

Misc

As the purpose of the virtual file system is primarily for testing, the API provides a collection of inspection tools.

Sourceval ls : ?scope:Path.t -> ('a, 'metadata) t -> string list

ls fs returns a flat list of strings, equivalent to applying the Unix ls command.

Sourceval tree : ('a, 'metadata) t -> string

tree fs returns a character string that prints the given tree fs in tree form, similar to the tree command (in Unix).

Sourceval cat : to_string:('a -> string) -> ('a, 'metadata) t -> Path.t -> string

cat ~to_string fs path Returns a string that resembles the output of the cat command in Unix (without concatenation).

A Dummy File System Implementation

The implementation is not abstract, which allows generic functions to be used on a Simple tree (mostly used for tests).

Sourcemodule Simple : sig ... end

A truly very naive implementation of a file system where the contents of files are strings and their metadata only associates modification dates.