[英]std::filesystem::remove_all fails on read-only file
I am trying to remove a complete git repository clone from my disk using std::filesystem::remove_all()我正在尝试使用 std::filesystem::remove_all() 从我的磁盘中删除完整的 git 存储库克隆
std::filesystem::remove_all( myRepoName );
This throws an exception这会引发异常
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]
This file is read-only.该文件是只读的。 It can be deleted with no problem from windows explorer.
可以从 windows 资源管理器中毫无问题地删除它。
This in windows 10 and mingw g++ v11.2.这在 windows 10 和 mingw g++ v11.2 中。 So this seems to be a bug in the implementation of std::filesystem.
所以这似乎是 std::filesystem 实现中的一个错误。
Any workaround available?有什么可用的解决方法吗? Maybe
std::filesystem::permissions
https://en.cppreference.com/w/cpp/filesystem/permissions ?也许
std::filesystem::permissions
https://en.cppreference.com/w/cpp/filesystem/permissions ?
Tried adding尝试添加
std::filesystem::permissions(
"windex/.git/objects/pack/pack-3b1477e94a042244b9aed7021724d8a020be62c9.idx",
std::filesystem::perms::others_all );
but still get same error and the file remains readonly.但仍然得到相同的错误,并且文件仍然是只读的。
After experimenting, found that owner_write
does the job经过实验,发现
owner_write
做的工作
This code solves the problem by changing the readonly permission of any file that fails to delete.此代码通过更改任何无法删除的文件的只读权限来解决问题。
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.