path
lib/path.vklib/path.vk Pure-Veko lexical POSIX/cross-platform path manipulation and root confinement. Zero capabilities required.
is-abs? = fn [p str] bool
Returns true if p is an absolute POSIX (/a/b) or Windows drive (C:/a/b) path.
normalize = fn [p str] str
Lexically normalizes p, converting backslashes to /, collapsing duplicate slashes,
and resolving . and .. segments cleanly.
use [path]
if (path/normalize 'a/b/../c/./d.vk') != 'a/c/d.vk' [ err 'normalize' ] else [ .ok ]
join = fn [a str, b str] str
Joins two path segments a and b and normalizes the result.
use [path]
if (path/join 'src/lib' '../main.vk') != 'src/main.vk' [ err 'path/join' ] else [ .ok ]
join-all = fn [parts block[str]] str
Joins a block of path segments and normalizes the result.
split-parts = fn [p str] block[str]
Splits a normalized path into its non-empty path segments.
dir = fn [p str] str
Returns the parent directory portion of p.
use [path]
if (path/dir 'a/b/c.vk') != 'a/b' [ err 'path/dir' ] else [ .ok ]
base = fn [p str] str
Returns the final filename component of p.
use [path]
if (path/base 'a/b/c.vk') != 'c.vk' [ err 'path/base' ] else [ .ok ]
ext = fn [p str] str
Returns the file extension including the leading . (or '' if none).
use [path]
if (path/ext 'src/main.vk') != '.vk' or (path/stem 'src/main.vk') != 'main' [ err 'ext/stem' ] else [ .ok ]
with-ext = fn [p str, new_ext str] str
Replaces (or appends) the file extension of p with new_ext (with or without leading .).
confine = fn [root str, rel str] str
Safely resolves rel inside root, returning err if rel is absolute or escapes root via ...
use [path]
ok_p = path/confine '/srv/app' 'assets/logo.svg'
bad_p = path/confine/or '/srv/app' '../../etc/passwd' ['blocked']
if ok_p != '/srv/app/assets/logo.svg' or bad_p != 'blocked' [ err 'confine' ] else [ .ok ]