Windows:文件重命名和目录迭代冲突
我正在使用 boost::filesystem 重命名文件,如下所示:
boost::filesystem::rename(tmpFileName, targetFile);
tmpFileName / targetFile 的类型为 boost::filsystem::path。
执行此操作时,我在另一个线程中使用此代码迭代目录:
directory_iterator end_itr;
for (directory_iterator itr(dirInfoPath); itr != end_itr; ++itr)
{
path currentPath = itr->path();
if (is_directory(itr->status()))
{
// skip directories
}
else
{
std::string file_name = currentPath.leaf();
if (!boost::algorithm::starts_with(file_name, "new")
&& !boost::algorithm::starts_with(file_name, "finished")
&& boost::algorithm::ends_with(file_name, ".info"))
{
// save found filename in some variable
return true;
}
}
}
执行此代码时,重命名时出现异常:
boost::filesystem::rename: The process cannot access the file because it is being used by another process
迭代和重命名操作是否可能发生冲突,因为它们都访问目录 inode,或者我还有其他问题吗?
I'm using boost::filesystem to rename a file like this:
boost::filesystem::rename(tmpFileName, targetFile);
tmpFileName / targetFile are of type boost::filsystem::path.
While doing this, I iterate over the directory using this code in another thread:
directory_iterator end_itr;
for (directory_iterator itr(dirInfoPath); itr != end_itr; ++itr)
{
path currentPath = itr->path();
if (is_directory(itr->status()))
{
// skip directories
}
else
{
std::string file_name = currentPath.leaf();
if (!boost::algorithm::starts_with(file_name, "new")
&& !boost::algorithm::starts_with(file_name, "finished")
&& boost::algorithm::ends_with(file_name, ".info"))
{
// save found filename in some variable
return true;
}
}
}
When this code is executed, I get an exception while renaming:
boost::filesystem::rename: The process cannot access the file because it is being used by another process
Is it possible that the iteration and the rename operation clash, because they both access the directory inode, or do I have some other problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的代码不包含任何文件打开操作,因此无法
锁定
文件。您迭代目录
并重命名文件
,对吧?因此该文件可能确实被另一个应用程序(例如文件查看器或其他应用程序)使用,这是非常典型的错误。或者您已在其他地方的应用程序中打开它code you provided doesn't contain any file open operations, so it cannot
lock
the file. you iterate overdirectory
and renamingfile
, right? so it's possible this file is really used by another application like file viewer or something else, it's quite typical error. or you have it opened in your app somewhere else