Scryer Prolog

Module files

:- use_module(library(files)).

Predicates for reasoning about files and directories.

In this library, directories and files are represented as lists of characters. This is an ideal representation:

  • Lists of characters can be conveniently reasoned about with DCGs and built-in Prolog predicates from library(lists). This alone is already a very compelling argument to use them.
  • Other Scryer libraries such as library(http/http_open) also already use lists of characters to represent paths.
  • File names are mostly ephemeral, so it is good for efficiency that they can quickly allocated transiently on the heap, leaving the atom table mostly unaffected. Indexing is almost never needed for file names. If needed, it should be added to the engine.
  • The previous point is also good for security, since the system leaves little trace of which files were even accessed.
  • Scryer Prolog represents lists of characters extremely compactly.

directory_files(+Directory, -Files).

Returns the list of files and directories available at a specific directory in the current system.

file_size(+File, -Size).

Returns the size (in bytes) of a file. The file must exist.

file_exists(+File).

Succeeds if File is a file that exists in the current system.

directory_exists(+Directory).

Succeeds if Directory is a directory that exists in the current system.

make_directory(+Directory).

Succeeds if it creates a new directory named Directory in the current system. If you want to create a nested directory, use make_directory_path/1.

make_directory_path(+Directory).

Similar to make_directory/1 but recursively creates directories if they're missing. Equivalent to mkdir -p in Unix.

delete_file(+File).

Succeeds if deletes File from the current system.

rename_file(+File, +Renamed).

Succeeds if File is renamed to Renamed

file_copy(+File, +Copied).

Succeeds if File is copied to Copied

delete_directory(+Directory).

Succeeds if Directory is deleted from the current system. Directory must be empty.

working_directory(Dir0, Dir).

Dir0 is the current working directory, and the working directory is changed to Dir.

Use working_directory/2 to determine the current working directory, and leave it as is.

path_canonical(Ps, Cs).

True iff Cs is the canonical, absolute path of Ps.

All intermediate components are normalized, and all symbolic links are resolved.

The predicate fails in the following situations, though not necessarily only in these cases:

  1. Ps is a path that does not exist.
  2. A non-final component in Ps is not a directory.

file_modification_time(+File, -T).

For a file File that must exist, it returns a time stamp T with the modification time

T is a time stamp compatible with library(time).

file_access_time(+File, -T).

For a file File that must exist, it returns a time stamp T with the access time

T is a time stamp compatible with library(time).

file_creation_time(+File, -T).

For a file File that must exist, it returns a time stamp T with the creation time

T is a time stamp compatible with library(time).

path_segments(Ps, Segments).

True iff Segments are the segments of Ps.

Segments is the list of components of the path Ps that are separated by the platform-specific directory separator. Each segment is a list of characters.

At least one of the arguments must be instantiated.

Examples:


?- path_segments("/hello/there", Segments).
   Segments = [[],"hello","there"].
?- path_segments(Path, ["hello","there"]).
   Path = "hello/there".

To obtain the platform-specific directory separator, you can use:


?- path_segments(Separator, ["",""]).
   Separator = "/".