This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Forcing fsync of ofstream with libstdc++-3?
- From: "Kenneth C. Schalk" <ken at xorian dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 25 Feb 2004 00:08:48 -0500 (EST)
- Subject: Forcing fsync of ofstream with libstdc++-3?
I'm trying to get some older C++ code to work correctly on Fedora Core
1, which uses gcc 3.3.2. The application is a server designed to be
fault-tolerant. It uses fstreams for writing files. Under certain
circumstances, it needs to guarantee that some information has been
committed to disk.
With older C++ run-times, it would do something like:
fsync(ofs.rdbuf()->fd())
But since the fd method was removed from the filebuf, that's no longer
an option. It's no longer possible to construct an fstream from a
file descriptor or to attach an fstream to a file descriptor, so it's
not possible to get a fstream and a corresponding file descriptor that
way. I've run a simple test program under strace, and I'm sure that
flushing an ofstream does *not* call fsync.
I'm aware of the __gnu_cxx::stdio_filebuf class, but I'd rather not
use that as I'd like to maintain portability to different compilers
and C++ run-times.
Essentially it looks to me as though my choices are:
- Write my own fstream-like class that allows me to force an fsync.
- Re-write significant pieces of code to use C stdio instead of C++
streams.
- Use temporary ostringstream objects to build up the bytes to be
written and then write them to the file using C stdio.
None of these particularly appeals to me, so I'm hoping someone can
suggest another reasonably portable option.
--Ken Schalk Vesta: Build Smarter
www.vestasys.org