26

I want to make sure that an ofstream has been written to the disk device. What's the portable way (portable on POSIX systems) of doing this?

Does that solve the problem if I open the file separately in read-only append mode to get a file descriptor and call fsync with it? Like this:

ofstream out(filename);
/* ...
   write content into out
   ...
*/
out.close();

int fd = open(filename, O_APPEND);
fsync(fd);
close(fd);
Vik
  • 495
  • 1
  • 5
  • 11

4 Answers4

12

If you're able to use Boost, try a file_descriptor_sink based stream, eg.:

#include <boost/filesystem.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <unistd.h>

boost::filesystem::path filePath("some-file");
boost::iostreams::stream<boost::iostreams::file_descriptor_sink> file(filePath);

//  Write some stuff to file.

//  Ensure the buffer's written to the OS ...
file.flush();

//  Tell the OS to sync it with the real device.
//
::fdatasync(file->handle());
parsley72
  • 8,449
  • 8
  • 65
  • 98
mrtimdog
  • 487
  • 4
  • 13
6

Unfortunately, looking through the standard there is nothing provided by basic_filebuf or any of the basic_[io]?fstream class templates to allow you to extract the underlying OS file descriptor (in the way that fileno() does for C stdio I/O).

Nor is there an open() method or constructor that takes such a file descriptor as a parameter (which would allow you to open the file using a different mechanism and record the filehandle).

There is basic_ostream::flush(), however I suspect that this does not in fact call fsync() -- I expect that, like fflush() in stdio, it only makes sure that the user-space runtime library buffers are flushed, meaning that the OS could still be buffering the data.

So in short there appears to be no way to do this portably. :(

What to do? My suggestion is to subclass basic_filebuf<C, T>:

template <typename charT, typename traits = std::char_traits<charT> >
class my_basic_filebuf : public basic_filebuf<charT, traits> {
    ....

public:
    int fileno() { ... }
    ....
};

typedef my_basic_filebuf<char> my_filebuf;

To use it, you can construct an ofstream using the default constructor, then assign the new buffer with rdbuf():

my_filebuf buf;
buf.open("somefile.txt");

ofstream ofs;
ofs.rdbuf(&buf);

ofs << "Writing to somefile.txt..." << endl;
int fd = static_cast<my_filebuf*>(ofs.rdbuf())->fileno();

Of course you could also derive a new class from basic_ostream to make the process of opening a file and retrieving its file descriptor more convenient.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • @BЈовић: (1) It does; I'm not sure what your problem is. (2) You commented on a post from 2009 1 hour ago and it already has 1 upvote: that doesn't look like a sockpuppet account at all. – j_random_hacker Mar 28 '18 at 08:25
  • I am not sure how my comment disapeared, but your answer is useless. In the most important parts, there are dots. My problem is that your "example" doesn't compile. So, what if it is from 2009? Google pointed me to this QA, and I expected to find it helpfull. But it is not. Also, he is not asking for a portable way to do, but using posix. – BЈовић Mar 28 '18 at 09:19
  • Your comment was probably deleted because it was rude. – j_random_hacker Mar 28 '18 at 09:55
4

std::filebuf probably has a file descriptor somewhere inside it, but getting at it requires horrible implementation-specific hacks.

Here is one such horrible hack for libstdc++.

#include <fstream>
#include <unistd.h>

int GetFileDescriptor(std::filebuf& filebuf)
{
  class my_filebuf : public std::filebuf
  {
  public:
    int handle() { return _M_file.fd(); }
  };

  return static_cast<my_filebuf&>(filebuf).handle();
}

int main()
{
  std::ofstream out("test");

  out << "Hello, world!";
  out.flush();

  fsync(GetFileDescriptor(*out.rdbuf()));
}
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
3

You cannot portably do fsync() on a file descriptor open for reading, at all. In Linux, fsync() is documented as generating EBADF if the descriptor is not in write mode.

unwind
  • 391,730
  • 64
  • 469
  • 606