在下面的代码片段中,有没有办法处理ENOSPC
?
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
// open input file stream of the bzip2 file
std::ifstream ifs("file.bz2");
// open output stream to the "full" device
// full device is a "utility-device" to check how applications handle ENOSPC
// more details in "man full"
std::ofstream ofs("/dev/full");
// Setup the iostreams filter
boost::iostreams::filtering_streambuf<boost::iostreams::output> filters;
filters.push(boost::iostreams::bzip2_decompressor());
filters.push(ofs);
// "run" the filter
boost::iostreams::copy(ifs, filters);
如果我strace
使用已编译的二进制文件,代码似乎会无限调用writev()
相同的数据并返回ENOSPC
错误。
writev(4, [{NULL, 0}, {"DATA DATA "..., 4096}], 2) = -1 ENOSPC (No space left on device)
如何处理此错误或将其作为错误从boost::iostreams::copy()
.
exceptions()
是否可以在对象上设置适当的ofstream
?我试过ofs.exceptions(std::ios::badbit | std::ios::failbit)
了,但没有任何区别。
上面的代码使用 GCC 编译并在 Linux 上运行。提升版本 1.55。