C++ boost::filesystem::remove_all(path) 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21164993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:30:59  来源:igfitidea点击:

How does boost::filesystem::remove_all(path) work?

c++boost

提问by AlexandraC

I am trying to remove all directories, subdirectories and the contained files from a specific path using boost::filesystem::remove_all(path). I also want to display an error message in case a file is open in another program. Does boost::filesystem::remove_all(path) throw an exception in this case?

我正在尝试使用 boost::filesystem::remove_all(path) 从特定路径中删除所有目录、子目录和包含的文件。如果文件在另一个程序中打开,我还想显示错误消息。在这种情况下 boost::filesystem::remove_all(path) 会抛出异常吗?

Or is there another way I can achieve this?

或者有另一种方法可以实现这一目标吗?

回答by Remus Rusanu

this does not fit in a comment so I'm posting as an answer

这不适合评论所以我发布作为答案

Just look in the source: http://www.boost.org/doc/libs/1_55_0/libs/filesystem/src/operations.cpp

只需查看源代码:http: //www.boost.org/doc/libs/1_55_0/libs/filesystem/src/operations.cpp

  BOOST_FILESYSTEM_DECL
  boost::uintmax_t remove_all(const path& p, error_code* ec)
  {
    error_code tmp_ec;
    file_type type = query_file_type(p, &tmp_ec);
    if (error(type == status_error, tmp_ec, p, ec,
      "boost::filesystem::remove_all"))
      return 0;

    return (type != status_error && type != file_not_found) // exists
      ? remove_all_aux(p, type, ec)
      : 0;
  }

remove_all_auxis defined few lines above and so is remove_file_or_directory, remove_file, remove_directoryand so forth and so on. The primitive operations are:

remove_all_aux被定义几行以上,所以是remove_file_or_directoryremove_fileremove_directory等等等等。原始操作是:

# if defined(BOOST_POSIX_API)
... 
#   define BOOST_REMOVE_DIRECTORY(P)(::rmdir(P)== 0)
#   define BOOST_DELETE_FILE(P)(::unlink(P)== 0)
...
# else  // BOOST_WINDOWS_API
...
#   define BOOST_REMOVE_DIRECTORY(P)(::RemoveDirectoryW(P)!= 0)
#   define BOOST_DELETE_FILE(P)(::DeleteFileW(P)!= 0)
...
# endif

The behavior of removing a locked file will be whatever your platform will provide.

删除锁定文件的行为将取决于您的平台将提供什么。

回答by AlexandraC

I am posting some code examples to clarify this issue. There are 2 scenarios.

我发布了一些代码示例来澄清这个问题。有2种情况。

In the first scenario I am using the remove_all function to delete the whole directory from a certain path and then I create a new directory at the same path:

在第一个场景中,我使用 remove_all 函数从某个路径删除整个目录,然后在同一路径创建一个新目录:

try
{
if(exists(directory_path))
{
   remove_all(directory_path);
}
    create_directory(directory_path);   
}
catch(filesystem_error const & e)
{
    //display error message 
}

This works just as expected, but then I have a second scenario where I am trying to delete certain folders from a path and then create the new directory:

这正如预期的那样工作,但是我有第二种情况,我试图从路径中删除某些文件夹,然后创建新目录:

try
    {
        if(exists(directory_path))
        {
            for ( boost::filesystem::directory_iterator itr(directory_path); itr != end_itr; itr++)
            {
                std::string folder = itr->path().filename().string();
                if(folder == FOLDER1 || folder == FOLDER2 || folder == FOLDER3)     
                      remove_all(itr->path());
            } 
         }          
        create_directory(directory_path);   
    }
    catch(filesystem_error const & e)
    {    
                 //display error message
    }

In this case the exception is not thrown in case a file is open in another program. The files just get deleted. Why does this happen?

在这种情况下,如果文件在另一个程序中打开,则不会抛出异常。文件只是被删除。为什么会发生这种情况?

回答by James Kanze

It depends on which overload of remove_allyou call; this is clearly documented in the documentation for the function. (What isn't clear is, if you use the function which reports errors by means of an error code, does the function continue, or does it return after the first error?)

这取决于remove_all你调用的是哪个重载;这在函数的文档中清楚记录。(不清楚的是,如果您使用通过错误代码报告错误的函数,该函数是继续执行还是在第一个错误后返回?)