代码之家  ›  专栏  ›  技术社区  ›  ravenspoint

使用std::filesystem::remove_all删除git-reo

  •  0
  • ravenspoint  · 技术社区  · 4 年前

    我正试图使用std::filesystem::remove_all()从磁盘中删除一个完整的git存储库克隆

    std::filesystem::remove_all( myRepoName );
    

    这引发了一个异常

    terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
      what():  filesystem error: cannot remove all: Input/output error [windex] 
    [windex\.git\objects\pack\pack-6592b2cba8201deb33301b149bcb61af6b4be49b.idx]
    

    此文件是只读的。它可以从windows资源管理器中毫无问题地删除。

    这在windows 10和mingw g++v11.2中。因此,这似乎是std::filesystem实现中的一个错误。

    有可用的变通方法吗?大概 std::filesystem::permissions https://en.cppreference.com/w/cpp/filesystem/permissions ?

    尝试添加

        std::filesystem::permissions(
            "windex/.git/objects/pack/pack-3b1477e94a042244b9aed7021724d8a020be62c9.idx",
            std::filesystem::perms::others_all );
    

    但仍然会出现相同的错误,并且该文件仍然是只读的。

    经过实验,发现 owner_write 做这项工作

    0 回复  |  直到 4 年前
        1
  •  0
  •   ravenspoint    4 年前

    此代码通过更改任何无法删除的文件的只读权限来解决问题。

    int count = 0;
    while (count < 5)   //give up after 5, there should be just 2 in a git repo
    {       
        try
        {
            std::filesystem::remove_all(myRepoName);
            break;     // all done
        }
        catch (std::filesystem::filesystem_error &e)
        {
            std::this_thread::sleep_for (std::chrono::milliseconds(250));
            std::string f ( e.what() );
            int p = f.find("[");
            p = f.find("[",p+1);
            f = f.substr(p+1);
            f = f.substr(0,f.length()-1);
            std::filesystem::permissions(
                f, std::filesystem::perms::owner_write );
            count++;
        }
    }
    
    推荐文章