fsync / fdatasync

fsync(resource $stream): bool — fdatasync(resource $stream): bool

PHP 8.1 forced durability: pushes buffered writes to the storage medium - fsync covers data AND metadata, fdatasync data only (POSIX-only; on Windows fdatasync is aliased to fsync); true means the data hit the medium.

C C23

C23
/* fsync(2): POSIX flush of data(+metadata) to the medium; fdatasync(2)
   is the data-only variant (Linux; macOS has fcntl F_FULLFSYNC instead,
   Windows maps to _commit). Zero-init not needed here (no raw buffers). */
#include <stdio.h>
#include <unistd.h>

int main(void) {
    FILE *fp = fopen("codeabc_fsync_probe.tmp", "w");
    if (!fp) return 1;
    fputs("persist me\n", fp);
    printf("%d\n", fsync(fileno(fp)) == 0); /* 0 = flushed to medium */
    fclose(fp);
    printf("%d\n", access("codeabc_fsync_probe.tmp", F_OK) == 0);
    unlink("codeabc_fsync_probe.tmp");
    return 0;
}
Output
1
1

C++ C++26

C++26
// fsync(2) via the C stdio layer - the POSIX original; fdatasync(2) is
// the data-only sibling on Linux (macOS: fcntl F_FULLFSYNC; Windows: _commit).
#include <cstdio>
#include <unistd.h>

int main() {
    std::FILE *fp = std::fopen("codeabc_fsync_probe.tmp", "w");
    if (!fp) return 1;
    std::fputs("persist me\n", fp);
    std::printf("%d\n", ::fsync(fileno(fp)) == 0);
    std::fclose(fp);
    std::printf("%d\n", ::access("codeabc_fsync_probe.tmp", F_OK) == 0);
    std::remove("codeabc_fsync_probe.tmp");
}
Output
1
1

C# v11.0.100

v11.0.100
// FileStream.Flush(true) is the fsync twin: write-through to the storage
// medium (Flush(false) only pushes through OS buffers).
var fs = new FileStream("codeabc_fsync_probe.tmp", FileMode.Create, FileAccess.Write);
var bytes = System.Text.Encoding.UTF8.GetBytes("persist me\n");
fs.Write(bytes);
fs.Flush(true); // data (+metadata) to the medium
Console.WriteLine(fs.Length == bytes.Length);
fs.Close();
Console.WriteLine(File.Exists("codeabc_fsync_probe.tmp"));
File.Delete("codeabc_fsync_probe.tmp");
Output
True
True

Go v1.27.4

v1.27.4
// (*os.File).Sync is the fsync twin; Go has no separate fdatasync -
// Sync covers data (metadata handling is OS-specific underneath).
package main

import (
	"fmt"
	"os"
)

func main() {
	f, _ := os.Create("codeabc_fsync_probe.tmp")
	f.WriteString("persist me\n")
	fmt.Println(f.Sync() == nil) // flushed to the storage medium
	info, _ := f.Stat()
	fmt.Println(info.Size() == 11)
	f.Close()
	os.Remove("codeabc_fsync_probe.tmp")
}
Output
true
true

Java v27.0.1

v27.0.1
// FileChannel.force(true) is the fsync twin (data + metadata);
// force(false) is the fdatasync-style data-only hint.
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws Exception {
        try (RandomAccessFile raf = new RandomAccessFile("codeabc_fsync_probe.tmp", "rw");
             FileChannel ch = raf.getChannel()) {
            ch.write(ByteBuffer.wrap("persist me\n".getBytes(StandardCharsets.UTF_8)));
            ch.force(true); // data + metadata to the medium
            System.out.println(ch.size() == 11);
        }
        System.out.println(Files.exists(Path.of("codeabc_fsync_probe.tmp")));
        Files.deleteIfExists(Path.of("codeabc_fsync_probe.tmp"));
    }
}
Output
true
true

JavaScript v26.13.1

v26.13.1
// Node: fs.fsyncSync pushes the fd's buffered writes to the medium
// (data + metadata); there is no separate fdatasync in the API.
const fs = require("node:fs");
const fd = fs.openSync("codeabc_fsync_probe.tmp", "w");
fs.writeSync(fd, "persist me\n");
fs.fsyncSync(fd); // flushed to the storage medium
fs.closeSync(fd);
console.log(fs.statSync("codeabc_fsync_probe.tmp").size === 11);
fs.unlinkSync("codeabc_fsync_probe.tmp");
Output
true

PHP v8.6.0

v8.6.0
// fsync / fdatasync (PHP 8.1): push buffered writes to the storage medium.
// fsync flushes data AND metadata; fdatasync skips metadata (POSIX only -
// on Windows fdatasync is aliased to fsync).
$file = "codeabc_fsync_probe.tmp";
$fp = fopen($file, "w");
fwrite($fp, "persist me\n");
var_dump(fsync($fp)); // true: data now on the storage medium
var_dump(fdatasync($fp)); // true: data-only sync
fclose($fp);
clearstatcache(true, $file);
echo filesize($file), "\n"; // 11: content durable
unlink($file);
Output
bool(true)
bool(true)
11

Python v3.15.2

v3.15.2
# os.fsync is the direct twin (data + metadata); os.fdatasync is the
# data-only variant - Linux only, so feature-check before calling.
import os
fd = os.open("codeabc_fsync_probe.tmp", os.O_WRONLY | os.O_CREAT)
os.write(fd, b"persist me\n")
os.fsync(fd) # data + metadata
if hasattr(os, "fdatasync"): # data-only variant (platform-dependent)
    os.fdatasync(fd)
os.close(fd)
print(os.path.getsize("codeabc_fsync_probe.tmp") == 11)
os.unlink("codeabc_fsync_probe.tmp")
Output
True

Rust v1.99.1

v1.99.1
// std::fs::File::sync_all is fsync (data + metadata); sync_data is the
// fdatasync-style data-only variant. Both guarantee medium durability.
use std::fs::File;
use std::io::Write;

fn main() {
    let mut f = File::create("codeabc_fsync_probe.tmp").unwrap();
    f.write_all(b"persist me\n").unwrap();
    f.sync_all().unwrap(); // fsync semantics
    f.sync_data().unwrap(); // fdatasync-style
    println!("{}", f.metadata().unwrap().len() == 11);
    drop(f);
    println!("{}", std::path::Path::new("codeabc_fsync_probe.tmp").exists());
    std::fs::remove_file("codeabc_fsync_probe.tmp").unwrap();
}
Output
true
true

TypeScript v26.13.1

v26.13.1
// Same as Node: fs.fsyncSync flushes the fd to the storage medium.
import * as fs from "node:fs";
const fd: number = fs.openSync("codeabc_fsync_probe.tmp", "w");
fs.writeSync(fd, "persist me\n");
fs.fsyncSync(fd);
fs.closeSync(fd);
console.log(fs.statSync("codeabc_fsync_probe.tmp").size === 11);
fs.unlinkSync("codeabc_fsync_probe.tmp");
Output
true

Language differences

PHP: fsync($stream)/fdatasync($stream) since 8.1; semantics align with the like-named POSIX syscalls - fsync flushes data plus metadata (mtime/size), fdatasync data only (saving one metadata journal write - a POSIX-only optimization; on Windows fdatasync is simply aliased to fsync); fwrite only reaches user space or the OS page cache - lost on power failure, so the correct closing sequence for durable paths (databases, logs, queues) is fwrite + fdatasync + fclose; the argument must be an open stream resource; returns bool (true = flushed). C: fsync(2) is the POSIX original (unistd.h; Windows _commit(fileno(fp))); macOS has no fdatasync(2) - stronger guarantees go through fcntl(F_FULLFSYNC) (this cross-platform entry uses only fsync(2), present on both ends). C++: same as C. C#: FileStream.Flush(true) - the bool overload writes through to the medium (Flush(false) only pushes past OS buffers). Go: (*os.File).Sync() (no separate fdatasync). Java: FileChannel.force(true) - the boolean parameter = flush metadata, verbatim alignment with the fsync/fdatasync split. JavaScript/TypeScript: Node fs.fsyncSync(fd) (no separate fdatasync). Python: os.fsync(fd) is the direct twin; os.fdatasync exists on Linux only (absent from the macOS os module) - feature-check before calling (exactly this entry’s approach). Rust: File::sync_all() (=fsync) and sync_data() (=fdatasync) both live in std - the only language of the ten with both in the standard API. Teaching anchor: two durability tiers (data vs data+metadata) crossed with an availability matrix (POSIX has both, Windows unifies them, macOS fsync only, Rust std has both); tests assert fsync returning true plus file size/existence - never assert "the medium actually received it" (that is a hardware-level contract).