Skip to content

File Functions

File builtins let a HypnoScript program read, write, and inspect the filesystem — anchoring trance state to disk.

Overview

FunctionSignatureBrief Description
ReadFile(path: string) -> stringRead entire file as string
WriteFile(path: string, content: string) -> voidWrite string to file (creates parents)
AppendFile(path: string, content: string) -> voidAppend string to file
DeleteFile(path: string) -> voidDelete a file
CreateDirectory(path: string) -> voidCreate a directory (recursively)
FileExists(path: string) -> booleanCheck whether a path exists
IsFile(path: string) -> booleanCheck whether a path is a file
IsDirectory(path: string) -> booleanCheck whether a path is a directory
ListDirectory(path: string) -> string[]List directory entries
GetFileSize(path: string) -> numberFile size in bytes
CopyFile(from: string, to: string) -> numberCopy a file, returns bytes copied
RenameFile(from: string, to: string) -> voidRename / move a file
GetFileExtension(path: string) -> stringExtract file extension (pure)
GetFileName(path: string) -> stringExtract file name (pure)
GetParentDirectory(path: string) -> stringExtract parent directory (pure)

Example

hyp
Focus {
    entrance {
        WriteFile("notes.txt", "You are getting sleepy...");
        AppendFile("notes.txt", "\nDeeper and deeper.");

        if (FileExists("notes.txt")) {
            induce content: string = ReadFile("notes.txt");
            observe content;
            observe "Size: " + GetFileSize("notes.txt") + " bytes";
        }

        DeleteFile("notes.txt");
    }
} Relax

Filesystem Sandbox

All file builtins can be confined to a single directory — a protective circle around the session. Activate the sandbox with the HYPNO_SANDBOX environment variable or the --sandbox <dir> flag on exec:

bash
hypnoscript exec script.hyp --sandbox ./workspace
# or
HYPNO_SANDBOX=./workspace hypnoscript exec script.hyp

With a sandbox root configured:

  • Relative paths ("notes.txt", "data/log.txt") resolve against the sandbox root — not the process working directory.
  • Escape attempts are rejected with a PermissionDenied error: .. traversal, absolute paths outside the root, and symlinks that point outside the root.
  • Pure path-string helpers (GetFileExtension, GetFileName, GetParentDirectory) never touch the filesystem and are unaffected.

Without a sandbox root, behavior is unchanged — file builtins access the filesystem freely.

hyp
Focus {
    entrance {
        // With --sandbox ./workspace:
        WriteFile("safe.txt", "ok");          // -> ./workspace/safe.txt
        ReadFile("../outside.txt");           // -> PermissionDenied error
        ReadFile("/etc/passwd");              // -> PermissionDenied error
    }
} Relax

See also

Released under the MIT License.