is_regular_file后我有什么保证
What guarantee do I have after is_regular_file?
考虑一个boost::filesystem::path p
,是否可以同时boost::filesystem::is_regular_file(p) == true
和std::ifstream(p.c_str()).is_open() == false
?如果是,在哪种情况下?
上下文是为比较函数编写断言:
bool identical_files(const boost::filesystem::path& p1, const boost::filesystem::path& p2)
{
assert(boost::filesystem::is_regular_file(p1));
assert(boost::filesystem::is_regular_file(p2));
std::ifstream f1(p1.c_str());
assert(f1.is_open()); // IS THIS REDUNDANT ???
std::ifstream f2(p2.c_str());
assert(f2.is_open());
// ...
// ...
// ...
}
您唯一的保证是在调用时,路径是常规文件。由于文件系统隐式是一种争用条件,因此 boost::filesystem::is_regular_file(p1)
和 std::ifstream f1(p1.c_str())
之间的调用实际上可能引用两个不同的对象。
考虑以下场景:
- 进程 1 调用
boost::filesystem::is_regular_file(p1)
,成功并确定它是一个"正常"文件 - 进程 2 删除
p1
指向的路径 - 进程 1 调用
std::ifstream f1(p1.c_str())
,但无法打开文件
你能在这里看到竞争条件吗?
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 什么是移动语义上下文中的"Regular Type"?