boost :: filesystem :: verver()无法访问文件,因为另一个过程正在使用该文件

boost::filesystem::relative() cannot access the file because it is being used by another process

本文关键字:文件 过程 另一个 因为 verver boost 访问 filesystem      更新时间:2023-10-16

访问某些网络驱动器时,函数relative(path, base_path)canonical(path, base_path)引发了异常。消息始终是:

该过程无法访问该文件,因为它是由另一个进程

使用的

我仅在我们的IT部门运行并包含符号链接的某些共享网络驱动器上观察到了这种行为。我无法在本地驱动器或相邻计算机共享驱动器上引起同样的问题。我们的怀疑是,在网络驱动器上使用的存档/备份解决方案也是这里的驱动程序。到目前为止,已知的因素是:

  • 驱动器必须是网络共享(驱动器等(
  • 路径需要包含符号链接组件
  • 驱动器在备份/存档解决方案下运行

我的问题是

  • 这是boost::filesystem中的潜在错误?
  • 我错过了可以解决这个问题的潜在boost::filesystem技巧吗?

一种可能的解决方法是重新实现relative()功能以仅使用路径操作而无法访问文件系统。但是我想避免重新实现。

如果测试路径有问题,则可能显示出问题的小样本程序:

#include <vector>
#include <string>
#include <tuple>
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>
using namespace std;
using namespace boost::filesystem;
using boost::system::error_code;

int main()
{
    vector<string> testpaths = {
        "< path to a directory which is to test >",
    };
    for(auto & line : testpaths)
    {
        if(line.empty()) continue;   // skip empty lines
        cout << " path: " << line << "   ";
        path testpath(line.c_str());
        // simplified testing, use parent of parent
        path basepath = testpath.parent_path().parent_path();
        boost::system::error_code ec;
        path relpath = relative(testpath, basepath, ec);
        if(ec)  cout << "  ---> error: " << ec.message();
        else    cout << " ok, relative: " << relpath.string();
        cout << endl;
    }
}

我遇到了相同的问题,即路径仅使用boost 1.65.1:

包含目录
unexpected exception: boost::filesystem::weakly_canonical: The process cannot access the file because it is being used by another process; 

这也仅在路径包含符号链接时才发生在网络驱动器上。

看来这是一个同步问题。显然,使用Boost:文件系统无法并行访问相同的符号链接。我定义了一个自定义功能,该功能封装并同步了felly_caronical的访问:

static boost::recursive_mutex   sgCanonicalMutex;
boost::filesystem::path CanonicalPath(const boost::filesystem::path inPath)
{        
        boost::recursive_mutex::scoped_lock lk(sgCanonicalMutex);
        return boost::filesystem::weakly_canonical(inPath);
}

之后,更改问题不再发生。在BOOST :: FILESYSYSTEM ::状态的文档中,还有关于基础系统错误代码error_sharing_violation的注释。请参阅https://www.boost.org/doc/libs/1_70_0/libs/filesystem/doc/reference.html

我认为根本原因是在提升资源中: boost libs filesystem src operations.cpp

功能read_symlink包含

handle_wrapper h(
      create_file_handle(p.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING,
        FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, 0));

第三个参数(值0(是传递给CreateFilew的DWShareMode(请参阅https://learn.microsoft.com/en-us/windows/windows/desktop/api/fileapi/fileapi/nf-fileapi-nf-fileap--fileap-createatefilew(。此参数可能应为file_share_read。在最新的Boost 1.70中,这仍然保持不变。

我在多进程的情况下遇到了这个问题,因此@red Soft Adair的锁解决方案无济于事。在发布Boost Fix之前,我实现了以下内容:

auto full_path = boost::filesystem::path(path_str).lexically_normal();
MY_ASSERT(boost::filesystem::exists(full_path), "Path normalization of '%s' resulted in non-existing path '%s'", 
            path_str.c_str(), full_path.string().c_str());

通过我创建的测试来暴露此问题,这很好。lexically_normal在路径字符串上运行,因此它不在乎路径的存在。但是,当路径内涉及链接时,这很容易误解。

截至8月/Sept。2020年RSTDIO 1.2.5033和1.3.1073也出现了此问题 - 与Gihtub上的Rstudio Dev创建了一个问题。他们希望下一个提升更新将解决此错误。
尽管这是一个rstudio主题,但在这里将其放在这里 - 以防万一有人从rstudio方面遇到。