Reference · reference
fs module
use fs gives a script file input and output. It replaces the standalone fs module of wscript-std, which config-weave does not register. Every function that can fail returns Result[…, string], where the error string names the path and the operating system error. The predicates exists, lexists, is_file and is_dir return a plain bool. Path strings are passed through to the operating system unchanged; use the path module to build them.
read and read_bytes
read
read_bytes
read returns the file as text. read_bytes returns the raw bytes as a list of integers. Both follow a final symlink.
read_with and read_bytes_with
read_with
read_bytes_with
The same as read and read_bytes, with the options write_with takes: Value::Null for the defaults, or {follow_symlinks: bool}. An unknown key returns Err. With follow_symlinks set to false, a path whose final component is a symlink returns Err("cannot read <path>: it is a symlink and follow_symlinks is false"). Use it when a root-run script reads a file in a user-owned directory, where a planted symlink could point the read at a file such as /etc/shadow. The Unix and Windows guarantees are the same as for write_with below.
write, write_bytes and append
write
write_bytes
append
write and write_bytes replace the file's contents. append adds text to the end of the file and creates the file when it is absent.
write, write_bytes and append follow a final symlink: they change the file the link points to.
write_with and write_bytes_with
write_with
write_bytes_with
The same as write and write_bytes, with options. opts is Value::Null for the defaults or a map with the key below. An unknown key returns Err. With Value::Null each call behaves exactly like write or write_bytes.
| Key | Type | Default | Meaning |
|---|---|---|---|
| follow_symlinks | bool | true | false refuses a final symlink with Err instead of writing through it |
With follow_symlinks set to false, a path whose final component is a symlink returns Err("cannot write <path>: it is a symlink and follow_symlinks is false") and the link's target is not touched. Use it when a root-run script writes into a user-owned directory, where a planted symlink would otherwise redirect the write onto another file. Symlinks earlier in the path are followed. On Unix the refusal is part of the open (O_NOFOLLOW), so it has no race. On Windows the path is checked for a symlink or other reparse point first and then opened, so a link swapped in between the two steps is followed.
let no_follow = Map
write_with?
copy and move
copy
move
copy copies one file. move renames a file or a directory.
delete and delete_dir
delete
delete_dir
delete removes a file or a symlink. delete_dir removes a directory and everything in it.
mkdir
mkdir
Creates a directory, including any missing parents.
exists, lexists, is_file and is_dir
exists lexists is_file is_dir Each predicate answers one question about a path. A path that cannot be read answers false.
exists, is_file and is_dir follow symlinks: they describe the link's target, so a dangling symlink answers false from all three. lexists does not follow a final symlink: it answers true for any symlink, dangling or not. Use lexists to ask whether something occupies a path before creating a link there.
list_dir
list_dir
Returns the names of the entries in a directory, sorted.
metadata
metadata
Returns a map describing the path. Read it with the accessors of the Value type. metadata does not follow a final symlink: for a link it describes the link itself, with is_symlink set to true, and it succeeds for a dangling link.
| Key | Type | Meaning |
|---|---|---|
| size | int | size in bytes |
| modified | int | modification time, seconds since the Unix epoch |
| readonly | bool | the read-only attribute |
| is_file | bool | the path is a regular file |
| is_dir | bool | the path is a directory |
| is_symlink | bool | the path is a symlink |
| mode | int | Unix permission bits (st_mode & 0o7777, so 0o644 reads as 420), 0 on other platforms |
| uid | int | Unix owner user id, Value::Null on other platforms |
| gid | int | Unix owner group id, Value::Null on other platforms |
glob
glob
Expands a glob pattern to the matching paths, sorted.
temp_file and temp_dir
temp_file
temp_dir
Each creates a fresh temporary file or directory and returns its path.
symlink and read_link
symlink
read_link
symlink creates a link at link that points to target. read_link returns the target a symlink points to.
chown
chown
Changes the owner and the group of a path. Unix only: on Windows every call returns Err. owner and group each take one of three values.
| Value | Meaning |
|---|---|
| Value::String(name) | a user or group name; an all-digit string that names no account is used as a numeric id |
| Value::Int(id) | a numeric uid or gid |
| Value::Null | leave that part unchanged |
An unknown name returns Err("unknown owner '<name>'") or Err("unknown group '<name>'"). chown does not follow a final symlink: on a link it changes the link itself, never the file the link points to. This stops a symlink planted in a user-owned directory from redirecting a root-run chown onto another file.
write?
chown?
chmod
chmod
Sets the permission bits of a path. Unix only: on Windows every call returns Err. mode is Value::Int(n), where 448 is 0o700, or Value::String of octal digits, as chmod(1) reads them: "700", "0700" and "0440" all work. Any other value, a string with a non-octal digit, or a mode above 0o7777 returns Err. Read the result back from the mode key of metadata.
chmod refuses a final symlink instead of following it, and returns Err("cannot chmod <path>: it is a symlink, which chmod does not follow"). Linux cannot change the mode of a link itself, and following it would let a symlink planted in a user-owned directory redirect a root-run chmod onto another file, as with chown. Symlinks earlier in the path are followed.
chmod?
use fs
use hash
let path = "/etc/app/app.conf"
if !is_file
let current = sha256_file?
let wanted = sha256
if current == wanted else
}