简体   繁体   English

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

[英]How does boost::filesystem::remove_all(path) work?

I am trying to remove all directories, subdirectories and the contained files from a specific path using boost::filesystem::remove_all(path). 我试图使用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)会抛出异常吗?

Or is there another way I can achieve this? 或者还有另一种方法可以达到这个目的吗?

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_aux is defined few lines above and so is remove_file_or_directory , remove_file , remove_directory and so forth and so on. remove_all_aux定义在上面几行,因此是remove_file_or_directoryremove_fileremove_directory等等。 The primitive operations are: 原始操作是:

# 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. 删除锁定文件的行为将是您的平台将提供的任何内容。

I am posting some code examples to clarify this issue. 我发布了一些代码示例来澄清这个问题。 There are 2 scenarios. 有两种情况。

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? 为什么会这样?

It depends on which overload of remove_all you call; 这取决于你调用remove_all重载; 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?) (不清楚的是,如果你使用通过错误代码报告错误的函数,函数是继续,还是在第一次错误后返回?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM