Standard library · System and tools

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
On this page
  1. Types
  2. Functions

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
The two parts of a path.
NameTypeDescription
dirstr = ""The directory part.
basestr = ""The file name.
fs-path-Join.salam
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("")
end
Output/home/sara/docs/cv.pdf /home/sara/docs cv.pdf /home/sara/docs /home/sara/docs cv.pdf cv .pdf cv /a/./b/../c//d c true false true docs/cv.pdf true false true false

Functions

Join

func Join(a: str, b: str): str
Joins two parts of a path with the right separator.

The example for this item is shown with the ones above: fs-path-Join.salam

Split

func Split(p: str): PathParts
Splits a path into its directory and its file name.

The example for this item is shown with the ones above: fs-path-Join.salam

Normalize

func Normalize(p: str): str
Turns Windows \ 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
Simplifies a path: it resolves . 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): bool
True when the path is absolute.

The example for this item is shown with the ones above: fs-path-Join.salam

Abs

func Abs(p: str): str
Makes the path absolute against the current directory.

The example for this item is shown with the ones above: fs-path-Join.salam

Rel

func Rel(base: str, target: str): str
The path of target relative to base.

The example for this item is shown with the ones above: fs-path-Join.salam

Parent

func Parent(p: str): str
The parent directory.

The example for this item is shown with the ones above: fs-path-Join.salam

Dirname

func Dirname(p: str): str
The directory part of a path.

The example for this item is shown with the ones above: fs-path-Join.salam

Filename

func Filename(p: str): str
The file name with its extension.

The example for this item is shown with the ones above: fs-path-Join.salam

Basename

func Basename(p: str): str
The file name without the directory; the same as the file name.

The example for this item is shown with the ones above: fs-path-Join.salam

Ext

func Ext(p: str): str
The extension, with its dot.

The example for this item is shown with the ones above: fs-path-Join.salam

Stem

func Stem(p: str): str
The file name without the extension.

The example for this item is shown with the ones above: fs-path-Join.salam

IsSafe

func IsSafe(base: str, p: str): bool
True when the given path, once simplified, still lies inside the base directory.

The example for this item is shown with the ones above: fs-path-Join.salam

Validate

func Validate(p: str): bool
True when the path is not empty; it checks nothing else.

The example for this item is shown with the ones above: fs-path-Join.salam