FSYNC(2)
πΎ FSYNC(2) β the system calls manual, bestie edition β¨
okay so picture this: you're in section 2 of the manual, the System Calls Manual zone, and we're talking about two little functions that are lowkey the reason your files don't just evaporate. they're fdatasync and fsync. iconic behavior. let's get into it π«Ά
πΈ NAME πΈ
fdatasync, fsync β synchronise changes to a file π
π LIBRARY π
Standard C Library (libc, -lc). yeah bestie, you know the one.
β¨ SYNOPSIS β¨
#include <unistd.h>
int
fdatasync(int fd);
int
fsync(int fd);
π¦ DESCRIPTION π¦
so. the fsync() system call is the main character here. what it does is it takes all modified data and attributes of the file that your file descriptor fd points at, and it moves them to a permanent storage device. no cap. normally that means every single in-core modified copy of buffers for that file gets written out to a disk. all of them. periodt. π₯
then we have fdatasync(), which is like fsync()'s chiller cousin. it causes all modified data of fd to be moved to a permanent storage device too β but here's the tea β unlike fsync(), it does not guarantee that the file attributes or metadata you need to access the file are actually committed to that permanent storage. so it's data only, no metadata promises. π
now, when should you reach for which one?
- use
fsync()if your program needs a file to be in a known state β for example, when you're building a simple transaction facility. that's the vibe. - if the file metadata has already been committed, then using
fdatasync()can be more efficient thanfsync(). efficiency queen π
one more thing, and this matters: both fdatasync() and fsync() calls are cancellation points. iykyk.
π RETURN VALUES π
the fsync() function returns the value 0 if it's successful π
otherwise it returns the value -1, and the global variable errno gets set to indicate the error. sad girl hours π
π ERRORS π
the fsync() and fdatasync() calls fail if:
[EBADF]β thefdargument is not a valid descriptor. bestie that's not a real fd π[EINVAL]β thefdargument refers to a socket, not to a file. wrong vibe entirely.[EIO]β an I/O error occurred while reading from or writing to the file system. oof.[EINTEGRITY]β corrupted data was detected while reading from the file system. genuinely tragic π
π SEE ALSO π
fsync(1), sync(2), syncer(4), sync(8)
go read those too, they're all part of the same friend group π«Ά
π HISTORY π
- the
fsync()system call appeared in 4.2BSD. a legend from day one β - the
fdatasync()system call appeared in FreeBSD 11.1. the newer sibling, still slaying.
π BUGS π
β οΈ okay so real talk: thefdatasync()system call currently does not guarantee that enqueuedaio(4)requests for the file referenced byfdare completed before the syscall returns. so like... don't assume. noted. π
and that's the whole page, bestie π this was the Debian build, dated March 30, 2020, and it lives under the title FSYNC(2) in the System Calls Manual. now go flush your buffers. literally. π§β¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved
- Copyright (c) 2016 The FreeBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms