开发者

Windows: File rename and directory iteration clash

开发者 https://www.devze.com 2023-01-09 23:48 出处:网络
I\'m using boost::filesystem to rename a file like this: boost::filesystem::rename(tmpFileName, targetFile);

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

精彩评论

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