This is how you can do what you want:

#include <fcntl.h>
int fd = open('/path/to/dir', O_RDONLY);
fsync(fd);

Don't forget to close the fd file descriptor when no longer needed of course.

Contrary to some misconceptions, the atomicity of rename() does not guarantee the file will be persisted to disk. The atomicity guarantee only ensures that the metadata in the file system buffers is in a consistent state but not that it has been persisted to disk.

Answer from hetman on Stack Overflow
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009695399 › functions › fsync.html
fsync
All I/O operations shall be completed as defined for synchronized I/O file integrity completion. Upon successful completion, fsync() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.
Top answer
1 of 6
119

fflush() works on FILE*, it just flushes the internal buffers in the FILE* of your application out to the OS.

fsync works on a lower level, it tells the OS to flush its buffers to the physical media.

OSs heavily cache data you write to a file. If the OS enforced every write to hit the drive, things would be very slow. fsync (among other things) allows you to control when the data should hit the drive.

Furthermore, fsync/commit works on a file descriptor. It has no knowledge of a FILE* and can't flush its buffers. FILE* lives in your application, file descriptors live in the OS kernel, typically.

2 of 6
13

The standard C function fflush() and the POSIX system call fsync() are conceptually somewhat similar. fflush() operates on C file streams (FILE objects), and is therefore portable. fsync() operate on POSIX file descriptors. Both cause buffered data to be sent to a destination.

On a POSIX system, each C file stream has an associated file descriptor, and all the operations on a C file stream will be implemented by delegating, when necessary, to POSIX system calls that operate on the file descriptor.

One might think that a call to fflush on a POSIX system would cause a write of any data in the buffer of the file stream, followed by a call of fsync() for the file descriptor of that file stream. So on a POSIX system there would be no need to follow a call to fflush with a call to fsync(fileno(fp)). But is that the case: is there a call to fsync from fflush?

No, calling fflush on a POSIX system does not imply that fsync will be called.

The C standard for fflush says (emphasis added) it

causes any unwritten data for [the] stream to be delivered to the host environment to be written to the file

Saying that the data is to be written, rather than that is is written implies that further buffering by the host environment is permitted. That buffering by the "host environment" could include, for a POSIX environment, the internal buffering that fsync flushes. So a close reading of the C standard suggests that the standard does not require the POSIX implementation to call fsync.

The POSIX standard description of fflush does not declare, as an extension of the C semantics, that fsync is called.

🌐
Linux Hint
linuxhint.com › use-fsync-system-call-c
Fsync System Call in C – Linux Hint
Also, the fsync function stops the process of flush until all the updates taking place by several processes on a single file got done, and then the flush would happen using the fsync() function call. This article will demonstrate the working of the fsync() function in the C language.
🌐
Linux Man Pages
man7.org › linux › man-pages › man2 › fsync.2.html
fsync(2) - Linux manual page
fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) so that all changed information can be retrieved even if the system crashes or is rebooted.
🌐
GitHub
github.com › torvalds › linux › blob › master › fs › ext4 › fsync.c
linux/fs/ext4/fsync.c at master · torvalds/linux
* ext4fs fsync primitive · * * Big-endian to little-endian byte-swapping/bitmaps by · * David S. Miller (davem@caip.rutgers.edu), 1995 · * * Removed unnecessary code duplication for little endian machines · * and excessive __inline__s. * Andi Kleen, 1997 ·
Author   torvalds
🌐
Linux Man Pages
linux.die.net › man › 2 › fsync
fsync(2) - Linux man page
fsync(): _BSD_SOURCE || _XOPEN_SOURCE ... || _XOPEN_SOURCE >= 500 · fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) so that ...
🌐
GNU
gnu.org › software › libc › manual › html_node › Synchronizing-I_002fO.html
Synchronizing I/O (The GNU C Library)
Programs more often want to ensure ... | AC-Safe | See POSIX Safety Concepts. The fsync function can be used to make sure all data associated with the open file fildes is written to the device associated with the descriptor....
Find elsewhere
🌐
Qnx
qnx.com › developers › docs › 8.0 › com.qnx.doc.neutrino.lib_ref › topic › f › fsync.html
fsync()
Use the -l c option to qcc to link against this library. This library is usually included automatically. The fsync() function forces all queued I/O operations for the file specified by the filedes file descriptor to finish, synchronizing the file's state.
🌐
GitHub
gist.github.com › antirez › 387838
fsync-in-thread.c · GitHub
Save antirez/387838 to your computer and use it in GitHub Desktop. Download ZIP · Raw · fsync-in-thread.c · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
PJA University
users.pja.edu.pl › ~jms › qnx › help › watcom › clibref › qnx › fsync.html
fsync
The fsync() function writes to disk all the currently queued data for the open file specified by fd. All necessary file system information required to retrieve the data is also written to disk.
🌐
TutorialsPoint
tutorialspoint.com › unix_system_calls › fsync.htm
fsync() System Call in Linux
Python TechnologiesDatabasesComputer ProgrammingWeb DevelopmentJava TechnologiesComputer ScienceMobile DevelopmentBig Data & AnalyticsMicrosoft TechnologiesDevOpsLatest TechnologiesMachine LearningDigital MarketingSoftware QualityManagement Tutorials View All Categories ... fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) where that file resides.
🌐
LWN.net
lwn.net › Articles › 457667
Ensuring data reaches disk [LWN.net]
Remember that fwrite()s to the file pointer are buffered by the C library. It is not until an fflush() call is issued that the data is known to be written to disk. In essence, associating a file stream with a synchronous file descriptor means that an fsync() call is not needed on the file descriptor after the fflush().
🌐
Stack Overflow
stackoverflow.com › questions › 32575244 › using-fsync-for-saving-binary-files-in-c-or-c
windows - Using fsync() for saving binary files in C or C++ - Stack Overflow
I investigated this, found that this is because data written using fwrite is not guaranteed to be written to disk immediately. I was told that in order to save my data without the risk of losing it in case of another abrupt power outage, I should make use of fsync function.
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › fflush-and-fsync-378849
fflush and fsync?
Hello.... I want to ask a question on C programming on Linux. There are 2 functions: fflush and fsync, both said that they are putting the data to
🌐
Cplusplus
cplusplus.com › forum › general › 7343
How do I emulate fsync with ofstream? - C++ Forum
But that's not an indication that the data has physically been written to disk yet. I'm looking for a way to tell the OS to force the associated buffers from the file system disk cache out to the physical media. That's what fsync() does, which I've proven by closing a file with and without calling fsync.