fs/path
Working with file paths: joining them, separating the name and the extension, absolute and relative paths, and a safety check.
import fs.path
Do not build paths by concatenating strings; Join puts the right separator in for you. When a path comes from a user, check it with IsSafe before opening it, so that nobody climbs out of the permitted directory with ../...
Types
PathParts
struct PathParts| Name | Type | Description |
|---|---|---|
dir | str = "" | The directory part. |
base | str = "" | The file name. |
import fs.path
func main:
p := path.Join("/home/sara", "docs/cv.pdf")
println p
parts := path.Split(p)
println parts.dir, parts.base
println path.Dirname(p), path.Parent(p), path.Filename(p), path.Basename(p)
println path.Ext(p), path.Stem(p)
println path.Normalize("/a/./b/../c//d"), path.Clean("a/b/../../c")
println path.IsAbs(p), path.IsAbs("docs"), path.IsAbs(path.Abs("docs"))
println path.Rel("/home/sara", "/home/sara/docs/cv.pdf")
println path.IsSafe("/srv/files", "a/b.txt"), path.IsSafe("/srv/files", "../../etc/passwd")
println path.Validate("docs/cv.pdf"), path.Validate("")
endFunctions
Join
func Join(a: str, b: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
Split
func Split(p: str): PathPartsThe example for this item is shown with the ones above: fs-path-Join.salam
Normalize
func Normalize(p: str): str\ separators into / and does nothing else. To resolve .. and ., see Clean.The example for this item is shown with the ones above: fs-path-Join.salam
Clean
func Clean(p: str): str. and .. and removes repeated separators.The example for this item is shown with the ones above: fs-path-Join.salam
IsAbs
func IsAbs(p: str): boolThe example for this item is shown with the ones above: fs-path-Join.salam
Abs
func Abs(p: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
Rel
func Rel(base: str, target: str): strtarget relative to base.The example for this item is shown with the ones above: fs-path-Join.salam
Parent
func Parent(p: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
Dirname
func Dirname(p: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
Filename
func Filename(p: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
Basename
func Basename(p: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
Ext
func Ext(p: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
Stem
func Stem(p: str): strThe example for this item is shown with the ones above: fs-path-Join.salam
IsSafe
func IsSafe(base: str, p: str): boolbase directory.The example for this item is shown with the ones above: fs-path-Join.salam
Validate
func Validate(p: str): boolThe example for this item is shown with the ones above: fs-path-Join.salam