开发者

Windows: File rename and directory iteration clash

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 thr开发者_JAVA百科ead:

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?


code you provided doesn't contain any file open operations, so it cannot lock the file. you iterate over directory and renaming file, 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜