use "time"
use @pony_os_realpath[Pointer[U8] iso^](path: Pointer[U8] tag)
use @pony_os_cwd[Pointer[U8]]()
primitive _PathSep
primitive _PathDot
primitive _PathDot2
primitive _PathOther
type _PathState is (_PathSep | _PathDot | _PathDot2 | _PathOther)
primitive Path
"""
Operations on paths that do not require a capability. The operations can be
used to manipulate path names, but give no access to the resulting paths.
All paths use `/` as the separator on every platform, including Windows.
Paths obtained through `Path.cwd()` and `Path.canonical()` are already
`/`-separated. `clean()` normalizes any remaining `\` on Windows.
`is_sep()` still accepts `\` on Windows for parsing external input.
Use `to_os()` when a backslash path is needed for Windows shell commands.
"""
fun is_sep(c: U8): Bool =>
"""
Determine if a byte is a path separator.
"""
ifdef windows then
(c == '/') or (c == '\\')
else
c == '/'
end
fun tag sep(): String =>
"""
Return the path separator as a string.
"""
"/"
fun is_abs(path: String): Bool =>
"""
Return true if the path is an absolute path.
"""
try
ifdef windows then
is_sep(path(0)?) or _drive_letter(path)
else
is_sep(path(0)?)
end
else
false
end
fun join(path: String, next_path: String): String =>
"""
Join two paths together. If the next_path is absolute, simply return it.
The returned path will be cleaned.
"""
if path.size() == 0 then
clean(next_path)
elseif next_path.size() == 0 then
clean(path)
elseif is_abs(next_path) then
clean(next_path)
else
try
if is_sep(path(path.size() - 1)?) then
if is_sep(next_path(0)?) then
return clean(path + next_path.trim(1))
else
return clean(path + next_path)
end
end
end
clean(path + sep() + next_path)
end
fun clean(path: String): String =>
"""
Replace multiple separators with a single `/`.
On Windows, normalize `\` to `/`.
Remove instances of `.` from the path.
Remove instances of `..` and the preceding path element from the path.
A leading `//` (exactly two separators) is preserved as a network path
prefix per POSIX; three or more leading separators collapse to one.
The result will have no trailing slash unless it is a root directory.
If the result would be empty, `"."` will be returned instead.
"""
let s = recover String(path.size()) end
let vol = volume(path)
ifdef windows then
for c in vol.values() do
if c == '\\' then s.push('/') else s.push(c) end
end
else
s.append(vol)
end
var state: _PathState = _PathOther
var i = vol.size()
var backtrack = ISize(-1)
let n = path.size()
try
var c = path(i)?
if is_sep(c) then
s.append(sep())
i = i + 1
state = _PathSep
elseif c == '.' then
i = i + 1
state = _PathDot
else
backtrack = s.size().isize()
end
while i < n do
c = path(i)?
if is_sep(c) then
match state
| _PathDot2 =>
if backtrack == -1 then
s.append("..")
s.append(sep())
else
s.delete(backtrack, -1)
try
backtrack = s.rfind(sep(), backtrack - 2)? + 1
else
backtrack = vol.size().isize()
end
if
(s.size() == 0) or
(s.compare_sub("../", 3, backtrack) is Equal)
then
backtrack = -1
end
end
| _PathOther =>
s.append(sep())
end
state = _PathSep
elseif c == '.' then
match \exhaustive\ state
| _PathSep =>
state = _PathDot
| _PathDot =>
state = _PathDot2
| _PathDot2 =>
backtrack = s.size().isize()
s.append("...")
state = _PathOther
| _PathOther =>
s.append(".")
end
else
match state
| _PathSep =>
backtrack = s.size().isize()
| _PathDot =>
backtrack = s.size().isize()
s.append(".")
| _PathDot2 =>
backtrack = s.size().isize()
s.append("..")
end
s.push(c)
state = _PathOther
end
i = i + 1
end
end
match state
| _PathDot2 =>
if backtrack == -1 then
s.append("..")
else
s.delete(backtrack, -1)
end
end
try
if is_sep(s(s.size() - 1)?) and (s.size() > 1) then
s.delete(-1, sep().size())
end
end
if s.size() > 0 then
s
else
"."
end
fun normcase(path: String): String =>
"""
Lowercases the path on case-insensitive platforms (Windows, macOS).
Returns the path unchanged on case-sensitive platforms.
"""
if Platform.windows() or Platform.osx() then
path.lower()
else
path
end
fun cwd(): String =>
"""
Returns the program's working directory. Setting the working directory is
not supported, as it is not concurrency-safe.
"""
recover String.from_cstring(@pony_os_cwd()) end
fun abs(path: String): String =>
"""
Returns a cleaned, absolute path.
"""
if is_abs(path) then
clean(path)
else
join(cwd(), path)
end
fun rel(to: String, target: String): String ? =>
"""
Returns a path such that Path.join(to, Path.rel(to, target)) == target.
Raises an error if this isn't possible.
"""
var to_clean = clean(to)
var target_clean = clean(target)
if to_clean == target_clean then
return "."
end
var to_i: ISize = 0
ifdef windows then
to_clean = abs(to_clean)
target_clean = abs(target_clean)
let to_vol = volume(to_clean)
let target_vol = volume(target_clean)
if to_vol != target_vol then
error
end
to_i = to_vol.size().isize()
end
var to_0 = to_i
var target_i = to_i
var target_0 = target_i
while true do
to_i =
try
to_clean.find(sep(), to_i)?
else
to_clean.size().isize()
end
target_i =
try
target_clean.find(sep(), target_i)?
else
target_clean.size().isize()
end
if
(to_i != target_i) or
(to_clean.compare_sub(target_clean, target_i.usize()) isnt Equal)
then
break
end
if to_i < to_clean.size().isize() then
to_i = to_i + 1
end
if target_i < target_clean.size().isize() then
target_i = target_i + 1
end
to_0 = to_i
target_0 = target_i
end
if
((to_i - to_0) == 2) and
(to_clean.compare_sub("..", 2, to_0) is Equal)
then
error
end
if to_0.usize() != to_clean.size() then
let result = recover String end
try
while true do
to_i = to_clean.find(sep(), to_i)? + 1
result.append("..")
result.append(sep())
end
end
result.append("..")
result.append(sep())
result.append(target_clean.trim(target_0.usize()))
result
else
target_clean.trim(target_0.usize())
end
fun split(path: String, separator: String = Path.sep()): (String, String) =>
"""
Splits the path into a pair, (head, tail) where tail is the last pathname
component and head is everything leading up to that. The tail part will
never contain a slash; if path ends in a slash, tail will be empty. If
there is no slash in path, head will be empty. If path is empty, both head
and tail are empty. The path in head will be cleaned before it is returned.
In all cases, join(head, tail) returns a path to the same location as path
(but the strings may differ). Also see the functions dir() and base().
"""
try
let i = _rfind_sep(path)?.usize()
(clean(path.trim(0, i)), path.trim(i + 1))
else
("", path)
end
fun base(path: String, with_ext: Bool = true): String =>
"""
Return the path after the last separator, or the whole path if there is no
separator.
If `with_ext` is `false`, the extension as defined by the `ext()` method
will be omitted from the result.
"""
let b =
try
path.trim(_rfind_sep(path)?.usize() + 1)
else
path
end
if with_ext then
b
else
let e_size = ext(b).size()
if e_size > 0 then
b.trim(0, b.size() - e_size - 1)
else
b
end
end
fun dir(path: String): String =>
"""
Return a cleaned path before the last separator, or the whole path if there
is no separator.
"""
try
clean(path.trim(0, _rfind_sep(path)?.usize()))
else
path
end
fun ext(path: String): String =>
"""
Return the file extension, i.e. the part after the last dot as long as that
dot is after all separators. Return an empty string for no extension.
"""
try
let i = path.rfind(".")?
let j =
try
_rfind_sep(path)?
else
i
end
if i >= j then
return path.trim(i.usize() + 1)
end
end
""
fun volume(path: String): String =>
"""
Return the volume at the beginning of the path, if there is one.
On Windows, this includes drive letters (`C:`), extended-length prefixes
(`\\?\`), and UNC bases (`\\server\share`).
On all platforms, a path beginning with exactly two separators followed by
a non-separator is treated as a network path (`//host/share`), matching the
POSIX rule that a pathname beginning with exactly two slashes is
implementation-defined. Three or more leading slashes are not a network
path and collapse to one in `clean()`.
"""
ifdef windows then
if path.compare_sub("""\\?\""", 4) is Equal then
if path.compare_sub("""UNC\""", 4, 4) is Equal then
return _network_share(path, 8)
end
if _drive_letter(path, 4) then
return path.trim(0, 6)
end
return ""
end
if _drive_letter(path) then
return path.trim(0, 2)
end
end
try
if
is_sep(path.at_offset(0)?) and
is_sep(path.at_offset(1)?) and
not is_sep(path.at_offset(2)?)
then
return _network_share(path, 2)
end
end
""
fun _drive_letter(path: String, offset: ISize = 0): Bool =>
"""
Look for a drive letter followed by a ':', returning true if we find it.
"""
try
let c = path.at_offset(offset)?
(((c >= 'A') and (c <= 'Z')) or ((c >= 'a') and (c <= 'z'))) and
(path.at_offset(offset + 1)? == ':')
else
false
end
fun _network_share(path: String, offset: ISize = 0): String =>
"""
Look for a host, a separator, and a resource. Return the path up to that
point if we found one, otherwise an empty String. Accepts both `/` and `\`
as separators since input may arrive un-normalized from external sources.
"""
try
let next = _find_sep(path, offset)? + 1
try
path.trim(0, _find_sep(path, next)?.usize())
else
path
end
else
""
end
fun _find_sep(path: String, offset: ISize = 0): ISize ? =>
"""
Find the first path separator at or after `offset`. On Windows, accepts
both `/` and `\`; on other platforms, only `/`.
"""
ifdef windows then
let fwd = try path.find("/", offset)? else ISize.max_value() end
let back = try path.find("\\", offset)? else ISize.max_value() end
if (fwd == ISize.max_value()) and (back == ISize.max_value()) then
error
end
if fwd < back then fwd else back end
else
path.find("/", offset)?
end
fun _rfind_sep(path: String, offset: ISize = -1): ISize ? =>
"""
Find the last path separator at or before `offset`. On Windows, accepts
both `/` and `\`; on other platforms, only `/`.
"""
ifdef windows then
let fwd = try path.rfind("/", offset)? else ISize.min_value() end
let back = try path.rfind("\\", offset)? else ISize.min_value() end
if (fwd == ISize.min_value()) and (back == ISize.min_value()) then
error
end
if fwd > back then fwd else back end
else
path.rfind("/", offset)?
end
fun to_os(path: String): String =>
"""
Convert forward slashes to the OS native separator. On Windows this
replaces `/` with `\`; on all other platforms it returns `path` unchanged.
Use when passing a path to `cmd.exe` or another Windows tool that
interprets `/` as an option flag.
"""
ifdef windows then
let s = path.clone()
let len = s.size()
var i = USize(0)
try
while i < len do
if s(i)? == '/' then
s(i)? = '\\'
end
i = i + 1
end
end
s
else
path
end
fun canonical(path: String): String ? =>
"""
Return the equivalent canonical absolute path. Raise an error if there
isn't one.
"""
let cstring = @pony_os_realpath(path.cstring())
if cstring.is_null() then
error
else
recover String.from_cstring(consume cstring) end
end
fun is_list_sep(c: U8): Bool =>
"""
Determine if a byte is a path list separator.
"""
ifdef windows then c == ';' else c == ':' end
fun list_sep(): String =>
"""
Return the path list separator as a string.
"""
ifdef windows then ";" else ":" end
fun split_list(path: String): Array[String] iso^ =>
"""
Separate a list of paths into an array of cleaned paths.
"""
let array = recover Array[String] end
var offset: ISize = 0
try
while true do
let next = path.find(list_sep(), offset)?
array.push(clean(path.trim(offset.usize(), next.usize())))
offset = next + 1
end
else
array.push(clean(path.trim(offset.usize())))
end
array
fun random(len: USize = 6): String =>
"""
Returns a pseudo-random base, suitable as a temporary file name or
directory name, but not guaranteed to not already exist.
"""
let letters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let s = recover String(len) end
var n = USize(0)
var r = Time.nanos().usize()
try
while n < len do
let c = letters(r % letters.size())?
r = r / letters.size()
s.push(c)
n = n + 1
end
end
s