gpt4 book ai didi

c++ - boost::iostreams::copy - sink - ENOSPC(设备上没有剩余空间)错误处理

转载 作者:太空狗 更新时间:2023-10-29 21:00:54 25 4
gpt4 key购买 nike

在下面的代码片段中,有没有办法处理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() 的错误抛出。

是否可以在 ofstream 对象上设置适当的 exceptions()?我尝试了 ofs.exceptions(std::ios::badbit | std::ios::failbit) 但它没有任何区别。

上面的代码是用 GCC 编译并在 Linux 上运行的。 Boost 版本 1.55。

最佳答案

它陷入了non_blocking_adaptor<Device>::write(...) 的无限循环中:

std::streamsize result = 0;
while (result < n) {
std::streamsize amt =
iostreams::write(device_, s + result, n - result);
result += amt;
}
return result;

iostream::write(device_, ...一直返回 0(所以 n 保持 n,amt 和结果保持 0)。


这似乎是 Boost IOstreams 中的错误。可能是在添加了对非阻塞(同步)IO 的初步支持时引入的。根据文档,这应该是一项正在进行的工作。

特别有启发性的是:http://www.boost.org/doc/libs/1_55_0/libs/iostreams/doc/guide/asynchronous.html

Filters

Filters are allowed to propagate temporary failure notifications: if a downstream Device consumes or produces fewer characters than requested by a Filter, and if as a result the Filter is not able to satisfy a read or write request, the Filter may return a value indicating that input or output is temporarily unavailable. It is hoped that this ability will suffice to allow the current Filter concepts to be used with both aynchronous and non-blocking i/o. However, in order to be useful with blocking i/o as well, a Filter must never return a temporary failure notification unless it has received a such notification from a downstream Device. This requirement is summarized by stating that Filters must be blocking-preserving. See Blocking.

(大胆的想法)似乎 IOStreams 通过将 E_NOSPC 条件转换为临时故障通知 违反了这一原则。


boost::iostreams::copy源和目标间接设备的特殊情况。在这种情况下,两者都是间接的。现在特殊情况将水槽包裹在 non_blocking_adaptor 中.我不知道为什么,这似乎与从同一文档页面获取的以下一般建议相矛盾:

Streams and Stream Buffers

Although the Boost.Iostreams Filter and Device concepts can accommodate non-blocking i/o, the C++ standard library stream and stream buffer interfaces cannot, since they lack a means to distinguish between temporary and permanent failures to satisfy a read or write request. As a result, non-blocking Devices do not work properly with the templates stream, stream_buffer, filtering_stream and filtering_streambuf.

我尝试用 file_sink 替换文件和 file_source相反,但没有任何变化。 :(

这是我简化的测试用例,它仍然重现了问题:

#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>

int main()
{
using namespace boost::iostreams;
file_source ifs("/dev/zero");
file_sink ofs("/dev/full");

filtering_streambuf<output> filters(ofs);
copy(ifs, filters);
}

也许您应该将此作为错误报告给开发人员/邮件列表。

关于c++ - boost::iostreams::copy - sink - ENOSPC(设备上没有剩余空间)错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20414577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com