File.Info
type File.Info = { name: string, size: int, isDir: bool, isSymlink: bool, modified: Time.Instant, mode: int, uid: int | null, gid: int | null }File.Info describes the file at a path, as File.stat reports it. modified is the time of the last write. mode holds the permission bits and the setuid, setgid and sticky bits, at most 0o7777; on Windows a file is 0o444 when read-only and 0o666 when not, and a directory adds 0o111. isSymlink is true only when stat was told not to follow a link. name is the last element of the path, also when a link is followed. size is a regular file's length in bytes; for a directory, and for a link described with follow false, it is what the host reports, as is a link's own mode. uid and gid are the ids of the user and the group that own the file, or null on Windows and WebAssembly, which have none.
Details
modeis a plainintand prints in decimal:0o644prints as420. Print it withString.fromInt(mode, base: 8).modedoes not say what kind of file it is;isDirandisSymlinkdo.modifiedis rounded as the file system keeps times: to 2 seconds on FAT, to the second on HFS+, to the nanosecond on ext4 and APFS. The access time is not inFile.Info.uidandgidare the numeric ids of the owning user and group, asls -nshows them, andnullon Windows and WebAssembly, which have no such ids. File.chown changes them.- A
File.Infois a snapshot: it does not change when the file does. Call File.stat again for the current values. - To tell only directories, files and links apart in a listing, File.readDir gives a File.Entry per name, with
name,isDir, andisSymlink, and no call toFile.stat.
Basic examples
Get the File.Info of a file with File.stat and read its fields:
try File.write("notes.txt", "buy milk\n")
let info = try File.stat("notes.txt")
println(info.name)
println(info.size)
println(info.isDir)notes.txt
9
falseA File.Info prints as a record. File.touch fixes modified here; uid and gid are those of the user who runs it:
try File.write("notes.txt", "buy milk\n")
try File.touch("notes.txt", time: try Time.parseInstant("2026-03-01T09:30:00Z"))
try File.chmod("notes.txt", 0o644)
println(try File.stat("notes.txt"))For a user and a group whose ids are 1000, it prints:
File.Info{ name: "notes.txt", size: 9, isDir: false, isSymlink: false, modified: 2026-03-01T09:30:00Z, mode: 420, uid: 1000, gid: 1000 }Scope
A directory has isDir set:
try File.mkdir("photos")
let info = try File.stat("photos")
println("${info.name} ${info.isDir}")photos trueBy default File.stat follows a symbolic link and describes its target, so isSymlink is false. With follow: false it describes the link itself:
try File.write("v2.txt", "second")
try File.symlink("v2.txt", "latest")
let target = try File.stat("latest")
let link = try File.stat("latest", follow: false)
println("${target.name} ${target.isSymlink} ${target.size}")
println("${link.name} ${link.isSymlink}")latest false 6
latest truemode holds the setuid, setgid and sticky bits too, so it is a number in 0 ..= 0o7777:
try File.mkdir("shared")
try File.chmod("shared", 0o1777)
let info = try File.stat("shared")
println(String.fromInt(info.mode, base: 8))1777A file the program creates belongs to the user it runs as, so its uid is Os.uid; both are null on Windows and WebAssembly:
try File.write("mine.txt", "x")
let info = try File.stat("mine.txt")
println(info.uid == Os.uid())trueDestructure the fields you need:
try File.write("report.csv", "id,total\n1,40\n")
let { name, size } = try File.stat("report.csv")
println("${name}: ${size} bytes")report.csv: 14 bytesFile.Info is a record type, so a literal builds one, for example to test a function that takes a File.Info without touching the disk:
func describe(info: File.Info): string {
if info.isDir { "${info.name}/" } else { "${info.name} (${info.size})" }
}
let when = try Time.parseInstant("2026-01-01T00:00:00Z")
let file = File.Info{
name: "a.txt", size: 3, isDir: false, isSymlink: false, modified: when, mode: 0o644, uid: null, gid: null,
}
let dir = File.Info{
name: "src", size: 0, isDir: true, isSymlink: false, modified: when, mode: 0o755, uid: null, gid: null,
}
println(describe(file))
println(describe(dir))a.txt (3)
src/Applications
Add up the size of every file in a tree:
try File.mkdir("site/css")
try File.write("site/index.html", "<h1>Hi</h1>")
try File.write("site/css/main.css", "body{}")
var total = 0
for path in try File.walk("site") {
total += (try File.stat(Path.join(["site", path]))).size
}
println("${total} bytes")17 bytesAdd up only the files directly in a directory. File.list names subdirectories too, so isDir leaves them out; their size is what the host reports:
try File.mkdir("project")
try File.mkdir("project/src")
try File.write("project/src/main.go", "package main\n")
try File.write("project/README.md", "# Project\n")
try File.write("project/go.mod", "module project\n")
var total = 0
for name in try File.list("project") {
let info = try File.stat(Path.join(["project", name]))
if !info.isDir { total += info.size }
}
println("${total} bytes")25 bytesRebuild an output only when its source changed after it, as make does:
func stale(source: string, output: string): Result[bool, Error] {
if !File.exists(output) { return Result.ok(true) }
let src = try File.stat(source)
let out = try File.stat(output)
Result.ok(Time.compare(src.modified, out.modified) > 0)
}
try File.write("page.md", "# Title")
try File.write("page.html", "<h1>Title</h1>")
try File.touch("page.md", time: try Time.parseInstant("2026-05-02T10:00:00Z"))
try File.touch("page.html", time: try Time.parseInstant("2026-05-01T10:00:00Z"))
println(stale("page.md", "page.html"))
println(stale("page.md", "page.pdf"))Ok{ value: true }
Ok{ value: true }Find the most recently changed file in a directory:
try File.mkdir("backups")
let days = ["2026-04-01", "2026-04-03", "2026-04-02"]
for day in days {
let path = "backups/${day}.tar"
try File.write(path, "")
try File.touch(path, time: try Time.parseInstant("${day}T03:00:00Z"))
}
let infos: Array[File.Info] = []
for name in try File.list("backups") {
infos.push(try File.stat("backups/${name}"))
}
let newest = infos.maxBy({ x.modified.epochNanos() })
println(newest?.name)2026-04-03.tarShow a mode as ls -l does, one letter per permission bit:
func permissions(info: File.Info): string {
var out = if info.isDir { "d" } else { "-" }
for shift in [6, 3, 0] {
let rwx = Bits.and(Bits.shiftRight(info.mode, shift), 0o7)
out += if Bits.and(rwx, 4) != 0 { "r" } else { "-" }
out += if Bits.and(rwx, 2) != 0 { "w" } else { "-" }
out += if Bits.and(rwx, 1) != 0 { "x" } else { "-" }
}
out
}
try File.write("deploy.sh", "make\n")
try File.chmod("deploy.sh", 0o750)
try File.mkdir("public")
try File.chmod("public", 0o755)
println(permissions(try File.stat("deploy.sh")))
println(permissions(try File.stat("public")))-rwxr-x---
drwxr-xr-xProperties and relations
size is the number of bytes File.readBytes reads:
try File.mkdir("data")
for [path, text] in [["data/a.txt", ""], ["data/café ☕.txt", "café ☕"], ["data/b.txt", "x\ny\n"]] {
try File.write(path, text)
let info = try File.stat(path)
assert(info.size == (try File.readBytes(path)).length())
}modified reads back the time File.touch set, and mode the mode File.chmod set:
let when = try Time.parseInstant("2026-07-04T12:00:00Z")
try File.touch("stamp", time: when)
try File.chmod("stamp", 0o640)
let info = try File.stat("stamp")
assert(info.modified == when)
assert(info.mode == 0o640)With the default follow, isSymlink is false, even for a link:
try File.write("target.txt", "")
try File.symlink("target.txt", "alias")
for path in ["target.txt", "alias"] {
assert(!(try File.stat(path)).isSymlink)
}
assert((try File.stat("alias", follow: false)).isSymlink)Possible issues
mode prints in decimal. Format it in base 8 to see the familiar digits:
try File.write("notes.txt", "")
try File.chmod("notes.txt", 0o644)
let info = try File.stat("notes.txt")
println(info.mode)
println(String.fromInt(info.mode, base: 8))420
644Checking isSymlink after a plain File.stat never finds a link, since File.stat followed it. Pass follow: false:
try File.write("real.txt", "")
try File.symlink("real.txt", "shortcut")
println((try File.stat("shortcut")).isSymlink)
println((try File.stat("shortcut", follow: false)).isSymlink)false
trueWith follow: false, a link to a directory describes the link, so isDir is false:
try File.mkdir("photos")
try File.symlink("photos", "pics")
let target = try File.stat("pics")
let link = try File.stat("pics", follow: false)
println("${target.isDir} ${target.isSymlink}")
println("${link.isDir} ${link.isSymlink}")true false
false trueA link whose target is missing is Err from a plain File.stat, and follow: false still describes it:
try File.symlink("missing.txt", "broken")
println(File.stat("broken") is Err)
println((try File.stat("broken", follow: false)).isSymlink)true
trueA File.Info does not follow later changes to the file:
try File.write("log.txt", "one\n")
let before = try File.stat("log.txt")
try File.write("log.txt", "one\ntwo\n")
println(before.size)
println((try File.stat("log.txt")).size)4
8See also
File.stat · File.Entry · File.readDir · File.chmod · File.touch · File.list · File.walk · File.exists · Time.Instant · String.fromInt
