![](/img/trans.png)
[英]Potential bug in std::filesystem::remove_all with clang++
[英]std::filesystem::remove_all fails on read-only file
我正在尝试使用 std::filesystem::remove_all() 从我的磁盘中删除完整的 git 存储库克隆
std::filesystem::remove_all( myRepoName );
这会引发异常
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot remove all: Input/output error [windex]
[windex\.git\objects\pack\pack-6592b2cba8201deb33301b149bcb61af6b4be49b.idx]
该文件是只读的。 可以从 windows 资源管理器中毫无问题地删除它。
这在 windows 10 和 mingw g++ v11.2 中。 所以这似乎是 std::filesystem 实现中的一个错误。
有什么可用的解决方法吗? 也许std::filesystem::permissions
https://en.cppreference.com/w/cpp/filesystem/permissions ?
尝试添加
std::filesystem::permissions(
"windex/.git/objects/pack/pack-3b1477e94a042244b9aed7021724d8a020be62c9.idx",
std::filesystem::perms::others_all );
但仍然得到相同的错误,并且文件仍然是只读的。
经过实验,发现owner_write
做的工作
此代码通过更改任何无法删除的文件的只读权限来解决问题。
int count = 0;
while (count < 5)
{
try
{
std::filesystem::remove_all(myRepoName);
break;
}
catch (std::filesystem::__cxx11::filesystem_error &e)
{
std::this_thread::sleep_for (std::chrono::milliseconds(250));
std::string f ( e.what() );
int p = f.find("[");
p = f.find("[",p+1);
f = f.substr(p+1);
f = f.substr(0,f.length()-1);
std::filesystem::permissions(
f, std::filesystem::perms::owner_write );
count++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.