How to do fsync on an ofstream?

萝らか妹 提交于 2019-12-03 06:55:45

问题


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);

回答1:


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

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());



回答2:


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.




回答3:


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()));
}



回答4:


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.



来源:https://stackoverflow.com/questions/676787/how-to-do-fsync-on-an-ofstream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!