fstat / lstat

fstat(resource $stream): array|false — lstat(string $filename): array|false

Fetches the whole stat field set in one call (fstat from an open handle, stat/lstat from a path): the full size/mode/mtime/ino/nlink family; lstat does NOT follow a symlink (it reports the link itself: size = length of the stored target path) while stat follows — the closing entry for the earlier fileperms/filesize/filemtime single-field trio.

C C23

C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void) {
    char path[] = "/tmp/fst_c_XXXXXX";
    int fd = mkstemp(path);
    char payload[100];
    memset(payload, 'x', sizeof payload);
    write(fd, payload, sizeof payload); // 100 bytes
    chmod(path, 0644);

    struct stat st;
    fstat(fd, &st); // ONE call: the full stat field block from an open descriptor
    printf("%d\n", (int)st.st_size);             // 100
    printf("%o\n", (unsigned)st.st_mode & 0777); // 644
    struct stat st2;
    stat(path, &st2);
    printf("%d\n", st.st_ino == st2.st_ino); // 1: fstat/stat agree on the inode

    // lstat does NOT follow the final symlink; stat does
    char linkpath[64];
    snprintf(linkpath, sizeof linkpath, "%s_lnk", path);
    symlink(path, linkpath);
    stat(linkpath, &st);
    lstat(linkpath, &st2);
    printf("%d\n", (int)st.st_size);                     // 100: followed to the target
    printf("%d\n", (int)st2.st_size == (int)strlen(path)); // 1: size = stored path length
    printf("%d\n", S_ISLNK(st2.st_mode));                // 1

    close(fd);
    unlink(linkpath);
    unlink(path);
    return 0;
}
Output
100
644
1
100
1
1

C++ C++26

C++26
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <unistd.h>

int main() {
    char path[] = "/tmp/fst_cpp_XXXXXX";
    int fd = mkstemp(path);
    char payload[100];
    memset(payload, 'x', sizeof payload);
    write(fd, payload, sizeof payload); // 100 bytes
    chmod(path, 0644);

    struct stat st;
    fstat(fd, &st); // ONE call: the full stat field block from an open descriptor
    std::printf("%d\n", (int)st.st_size);             // 100
    std::printf("%o\n", static_cast<unsigned>(st.st_mode) & 0777); // 644
    struct stat st2;
    stat(path, &st2);
    std::printf("%d\n", st.st_ino == st2.st_ino); // 1: fstat/stat agree

    // lstat does NOT follow the final symlink; stat does
    char linkpath[64];
    snprintf(linkpath, sizeof linkpath, "%s_lnk", path);
    symlink(path, linkpath);
    stat(linkpath, &st);
    lstat(linkpath, &st2);
    std::printf("%d\n", (int)st.st_size);                     // 100: followed
    std::printf("%d\n", (int)st2.st_size == (int)strlen(path)); // 1: stored path length
    std::printf("%d\n", S_ISLNK(st2.st_mode));                // 1

    close(fd);
    unlink(linkpath);
    unlink(path);
    return 0;
}
Output
100
644
1
100
1
1

C# v11.0.100

v11.0.100
using System.IO;

var path = Path.GetTempFileName();
File.WriteAllText(path, new string('x', 100)); // 100 bytes
File.SetUnixFileMode(path, UnixFileMode.UserRead | UnixFileMode.UserWrite |
    UnixFileMode.GroupRead | UnixFileMode.OtherRead); // 0644

var fi = new FileInfo(path); // FileInfo = stat(2): Length/attributes in one record
Console.WriteLine(fi.Length); // 100
Console.WriteLine(Convert.ToString((int)File.GetUnixFileMode(path), 8)); // 644

// stat vs lstat: FileInfo resolves links; the ReparsePoint flag sees the link itself
var link = path + "_lnk";
File.CreateSymbolicLink(link, path);
// stat follows the link: opening it streams the TARGET's bytes
using (var s = new FileStream(link, FileMode.Open, FileAccess.Read))
{
    Console.WriteLine(s.Length); // 100: followed to the target
}
Console.WriteLine(File.GetAttributes(link).HasFlag(FileAttributes.ReparsePoint)); // True
Console.WriteLine(new FileInfo(link).LinkTarget == path); // the stored target

File.Delete(link);
File.Delete(path);
Output
100
644
100
True
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	path := fmt.Sprintf("%s/fst_go_%d", os.TempDir(), os.Getpid())
	os.WriteFile(path, make([]byte, 100), 0644) // 100 bytes
	os.Chmod(path, 0644)

	f, _ := os.Open(path)
	fi, _ := f.Stat() // fstat: the full field block from an open descriptor
	fmt.Println(fi.Size())               // 100
	fmt.Printf("%o\n", fi.Mode().Perm()) // 644
	fi2, _ := os.Stat(path)
	fmt.Println(os.SameFile(fi, fi2)) // true: fstat/stat agree on the inode
	f.Close()

	// os.Lstat does NOT follow the final symlink; os.Stat does
	link := path + "_lnk"
	os.Symlink(path, link)
	s, _ := os.Stat(link)
	l, _ := os.Lstat(link)
	fmt.Println(s.Size())                     // 100: followed to the target
	fmt.Println(l.Size() == int64(len(path))) // true: size = stored path length
	fmt.Println(l.Mode()&os.ModeSymlink != 0) // true

	os.Remove(link)
	os.Remove(path)
}
Output
100
644
true
100
true
true

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;

public class Main {
    public static void main(String[] args) throws Exception {
        Path path = Files.createTempFile("fst", ".dat");
        Files.write(path, new byte[100]); // 100 bytes
        Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rw-r--r--"));

        // readAttributes = stat(2): the full field block in ONE call
        PosixFileAttributes st = Files.readAttributes(path, PosixFileAttributes.class);
        System.out.println(st.size());                          // 100
        System.out.println(PosixFilePermissions.toString(st.permissions())); // rw-r--r--

        // NOFOLLOW_LINKS = lstat: does NOT follow the final symlink
        Path link = path.resolveSibling(path.getFileName() + "_lnk");
        Files.createSymbolicLink(link, path);
        PosixFileAttributes followed =
                Files.readAttributes(link, PosixFileAttributes.class);
        PosixFileAttributes notFollowed =
                Files.readAttributes(link, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        System.out.println(followed.size()); // 100: followed to the target
        System.out.println(notFollowed.size() == path.toString().length()); // stored path length
        System.out.println(notFollowed.isSymbolicLink()); // true

        Files.delete(link);
        Files.delete(path);
    }
}
Output
100
rw-r--r--
100
true
true

JavaScript v26.13.1

v26.13.1
const fs = require("fs");
const os = require("os");
const path = require("path");
const dir = fs.mkdtempSync(os.tmpdir() + "/fst_");
const file = path.join(dir, "data.bin");
fs.writeFileSync(file, Buffer.alloc(100)); // 100 bytes
fs.chmodSync(file, 0o644);

const fd = fs.openSync(file, "r");
const st = fs.fstatSync(fd); // fstat: the full field block from an open descriptor
console.log(st.size);                       // 100
console.log((st.mode & 0o777).toString(8)); // 644
console.log(st.ino === fs.statSync(file).ino); // true: fstat/stat agree on inode
fs.closeSync(fd);

// lstatSync does NOT follow the final symlink; statSync does
const link = path.join(dir, "data.lnk");
fs.symlinkSync(file, link, "file");
console.log(fs.statSync(link).size); // 100: followed to the target
console.log(fs.lstatSync(link).size === Buffer.byteLength(file)); // true
console.log(fs.lstatSync(link).isSymbolicLink()); // true

fs.unlinkSync(link);
fs.unlinkSync(file);
fs.rmSync(dir, { recursive: true, force: true });
Output
100
644
true
100
true
true

PHP v8.6.0

v8.6.0
$path = tempnam(sys_get_temp_dir(), 'fst_');
file_put_contents($path, str_repeat('x', 100)); // 100 bytes
chmod($path, 0644);

$f = fopen($path, 'r');
$st = fstat($f); // ONE call: the full stat field array from an open handle
echo $st['size'], "\n";                       // 100
echo sprintf('%o', $st['mode'] & 0777), "\n"; // 644
var_dump($st['ino'] === stat($path)['ino']);  // fstat/stat agree on the inode
fclose($f);

// lstat does NOT follow the final symlink; stat does
$link = $path . '_lnk';
symlink($path, $link);
echo stat($link)['size'], "\n"; // 100 — followed to the target
// lstat reports the LINK ITSELF: size = stored target path length
var_dump(lstat($link)['size'] === strlen($path));
var_dump(is_link($link));

unlink($link);
unlink($path);
Output
100
644
bool(true)
100
bool(true)
bool(true)

Python v3.15.2

v3.15.2
import os
import stat as statmod
import tempfile

fd, path = tempfile.mkstemp(prefix="fst_")
os.write(fd, b"x" * 100)  # 100 bytes
os.close(fd)
os.chmod(path, 0o644)

st = os.stat(path)  # stat(2): the full field block in ONE call
print(st.st_size)                  # 100
print(oct(st.st_mode & 0o777)[2:])  # 644

# os.lstat does NOT follow the final symlink; os.stat does
link = path + "_lnk"
os.symlink(path, link)
print(os.stat(link).st_size)                 # 100: followed to the target
print(os.lstat(link).st_size == len(path))   # True: size = stored path length
print(statmod.S_ISLNK(os.lstat(link).st_mode))  # True

os.unlink(link)
os.unlink(path)
Output
100
644
100
True
True

Rust v1.99.1

v1.99.1
use std::fs;
use std::os::unix::fs::PermissionsExt;

fn main() {
    let path = std::env::temp_dir().join(format!("fst_rs_{}", std::process::id()));
    fs::write(&path, vec![b'x'; 100]).unwrap(); // 100 bytes
    fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();

    let st = fs::metadata(&path).unwrap(); // stat(2): the full field block
    println!("{}", st.len());                      // 100
    println!("{:o}", st.permissions().mode() & 0o777); // 644

    // symlink_metadata = lstat: does NOT follow the final symlink
    let link = path.with_extension("lnk");
    std::os::unix::fs::symlink(&path, &link).unwrap();
    println!("{}", fs::metadata(&link).unwrap().len()); // 100: followed
    let lst = fs::symlink_metadata(&link).unwrap();
    println!("{}", lst.len() == path.to_string_lossy().len() as u64); // stored path length
    println!("{}", lst.file_type().is_symlink()); // true

    fs::remove_file(&link).unwrap();
    fs::remove_file(&path).unwrap();
}
Output
100
644
100
true
true

TypeScript v26.13.1

v26.13.1
import * as fs from "fs";
import * as os from "os";
import * as path from "path";

const dir = fs.mkdtempSync(`${os.tmpdir()}/fst_`);
const file = path.join(dir, "data.bin");
fs.writeFileSync(file, Buffer.alloc(100)); // 100 bytes
fs.chmodSync(file, 0o644);

const fd = fs.openSync(file, "r");
const st = fs.fstatSync(fd); // fstat: the full field block from an open descriptor
console.log(st.size);                       // 100
console.log((st.mode & 0o777).toString(8)); // 644
console.log(st.ino === fs.statSync(file).ino); // true: fstat/stat agree on inode
fs.closeSync(fd);

// lstatSync does NOT follow the final symlink; statSync does
const link = path.join(dir, "data.lnk");
fs.symlinkSync(file, link, "file");
console.log(fs.statSync(link).size); // 100: followed to the target
console.log(fs.lstatSync(link).size === Buffer.byteLength(file)); // true
console.log(fs.lstatSync(link).isSymbolicLink()); // true

fs.unlinkSync(link);
fs.unlinkSync(file);
fs.rmSync(dir, { recursive: true, force: true });
Output
100
644
true
100
true
true

Language differences

PHP: fstat($stream) returns the full field array in one call (size/mode/mtime/ino/nlink/...) — the closing entry for the earlier fileperms/filesize/filemtime single-field trio; lstat does NOT follow a symlink (it reports the link itself: size = length of the stored target path, mode carries the link-type bits) while stat follows; file_exists/is_file/filesize all follow links (dangling -> false).

C/C++: fstat(2)/stat(2)/lstat(2) fill the same struct stat; S_ISLNK tests the link-type bit.

C#: a FileInfo record carries Length/attributes; measured caveat: FileInfo.Length on a symlink reports the link itself (lstat semantics) — following semantics require opening a FileStream (shown); LinkTarget reads the stored target.

Go: File.Stat()/os.Stat/os.Lstat; os.SameFile compares inodes without platform fuss.

Java: Files.readAttributes (add LinkOption.NOFOLLOW_LINKS for lstat); PosixFileAttributes carries permissions/owner/timestamps/size in one shot.

JavaScript: fs.fstatSync/statSync/lstatSync; lstatSync().isSymbolicLink().

Python: os.stat/os.lstat + stat.S_ISLNK/S_IMODE.

Rust: fs::metadata (follows) / fs::symlink_metadata (does not follow — different name, same semantics as lstat); std::os::unix::fs::MetadataExt exposes raw ino/nlink/mode fields.

TypeScript: same as JS.

Closing perspective: one call for all fields vs three single-field entries — when you need several fields, a single stat/fstat beats repeated single-field calls (and sidesteps the stat cache). mtime/ctime/atime are environment values, so the example only asserts the deterministic trio size/mode/ino.