File.readLink
func File.readLink(path: string): Result[string, Error]File.readLink returns the target of the symbolic link at path, as it is stored.
Details
- The target comes back as text, exactly as File.symlink stored it: a relative target stays relative to the link's directory, and nothing is cleaned or resolved.
File.readLinkreads one link. When the target is itself a link, it is not followed; use File.realPath for the file at the end of the chain.- A dangling link still has a target, so
File.readLinkisOkfor it. File.readLinkisErrwhen nothing is atpath, or when what is there is not a symbolic link.
Basic examples
Read the target of a link:
try File.write("notes.txt", "buy milk")
try File.symlink("notes.txt", "current")
println(File.readLink("current"))Ok{ value: "notes.txt" }A relative target is stored relative to the link's directory, and comes back that way:
try File.mkdir("site")
try File.write("site/index.html", "<h1>Home</h1>")
try File.mkdir("public")
try File.symlink("../site/index.html", "public/home.html")
println(File.readLink("public/home.html"))
println(File.read("public/home.html"))Ok{ value: "../site/index.html" }
Ok{ value: "<h1>Home</h1>" }Scope
An absolute target comes back absolute:
try File.symlink("/etc/hosts", "hosts")
println(File.readLink("hosts"))Ok{ value: "/etc/hosts" }The text is not cleaned: a trailing slash, . and .. stay as they were written. Unicode names come back unchanged:
try File.symlink("./drafts//old/../new/", "latest")
try File.symlink("café ☕.txt", "menu")
println(File.readLink("latest"))
println(File.readLink("menu"))Ok{ value: "./drafts//old/../new/" }
Ok{ value: "café ☕.txt" }A link whose target does not exist still has a target:
try File.symlink("gone.txt", "broken")
println(File.readLink("broken"))
println(File.exists("broken"))Ok{ value: "gone.txt" }
falseA link to a link gives the next link, not the file at the end. File.realPath follows the whole chain:
try File.write("v2.txt", "second")
try File.symlink("v2.txt", "stable")
try File.symlink("stable", "default")
println(File.readLink("default"))
println(File.realPath("default").map({ Path.base(x) }))Ok{ value: "stable" }
Ok{ value: "v2.txt" }A regular file, a directory, and a missing path are each an Err:
try File.write("plain.txt", "text")
try File.mkdir("folder")
println(File.readLink("plain.txt"))
println(File.readLink("folder"))
println(File.readLink("nowhere"))Err{ error: Error{ message: "readlink plain.txt: invalid argument" } }
Err{ error: Error{ message: "readlink folder: invalid argument" } }
Err{ error: Error{ message: "readlink nowhere: no such file or directory" } }Applications
Report which release a live link points at, as deploy tools do:
try File.mkdir("releases")
for name in ["v1.4.0", "v1.5.0"] {
try File.mkdir("releases/${name}")
}
try File.symlink("releases/v1.5.0", "live")
let target = try File.readLink("live")
println("live release: ${Path.base(target)}")live release: v1.5.0Find the dangling links in a directory: File.readDir says which entries are links, and File.exists follows each one:
try File.mkdir("bin")
try File.write("bin/tool-1.0", "")
try File.symlink("tool-1.0", "bin/tool")
try File.symlink("helper-0.9", "bin/helper")
for entry in try File.readDir("bin") {
let path = Path.join(["bin", entry.name])
if !entry.isSymlink || File.exists(path) {
continue
}
let target = try File.readLink(path)
println("${path} -> ${target} (missing)")
}bin/helper -> helper-0.9 (missing)Turn a relative target into a path from the working directory by joining it to the link's directory:
func linkTarget(link: string): Result[string, Error] {
let target = try File.readLink(link)
Result.ok(if Path.isAbs(target) { target } else { Path.join([Path.dir(link), target]) })
}
try File.mkdir("docs")
try File.write("docs/guide.md", "# Guide")
try File.mkdir("docs/old")
try File.symlink("../guide.md", "docs/old/guide.md")
println(linkTarget("docs/old/guide.md"))Ok{ value: "docs/guide.md" }Say which link failed with Result.wrapErr:
let r = File.readLink("config/active") | Result.wrapErr("find the active config")
println(r)Err{ error: Error{ message: "find the active config: readlink config/active: no such file or directory" } }Properties and relations
File.readLink gives back the target File.symlink was given, byte for byte:
let pairs = [
["a.txt", "l1"],
["../up/b.txt", "l2"],
["/abs/c", "l3"],
["dir/", "l4"],
["./x//y", "l5"],
["ünï cødé", "l6"],
]
for [target, link] in pairs {
try File.symlink(target, link)
assert((try File.readLink(link)) == target)
}Possible issues
A relative target is relative to the link's directory, not to the working directory. Reading the target as a path from the working directory misses the file:
try File.mkdir("conf")
try File.write("conf/app.toml", "port = 8080")
try File.mkdir("conf/active")
try File.symlink("../app.toml", "conf/active/app.toml")
let target = try File.readLink("conf/active/app.toml")
println(File.read(target))
println(File.read("conf/active/app.toml"))Err{ error: Error{ message: "open ../app.toml: no such file or directory" } }
Ok{ value: "port = 8080" }To read what a link leads to, read the link itself: File.read follows it.
See also
File.symlink · File.realPath · File.stat · File.readDir · File.exists · File.remove · Path.join · Path.dir
