使用std :: filesystem的linux错误或文件系统树无法访问?

问题描述 投票:0回答:1

我正在尝试打印系统目录树,但是在使用根目录或其附近时,程序将引发异常,如下所示

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: status: Too many levels of symbolic links [/home/asmmo/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0]

并且使用根本身,程序将引发

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: status: Too many levels of symbolic links [/sys/kernel/software_nodes/node2/dw-apb-uart.2/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1/firmware_node/physical_node1]

我使用的代码是:

#include<iostream>
#include<filesystem>
#include<fstream>


void processPth(const std::filesystem::path&, std::ostream& = std::cout, const size_t& =0);

int main() {

    std::filesystem::path p{LR"(/home/asmmo)"};//or p{LR"(/)"} for the root
    std::ofstream myFile{"tree.txt"};
    processPth(p, myFile);

}




void processPth(const std::filesystem::path & p, std::ostream& ostream , const size_t& level ){
    if(!std::filesystem::exists(p)) return;//base case

    if(std::filesystem::is_regular_file(p))
        ostream<<std::string(2*level, ' ')<<"File: "<< p.filename()<<"\tSize: "<<std::filesystem::directory_entry(p).file_size()<<"\n";
    else if(std::filesystem::is_directory(p))
    {
        ostream<<std::string(2*level, ' ')<<"Directory: "<< p.filename()<<"\n";
        for(const auto& it : std::filesystem::directory_iterator(p))
            processPth( it, ostream, level+1);
    }


}

我试图使用终端到达那些v0,结果如下

asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0/v0$ cd v0
asmmo@asmmo:~/.local/share/webkitgtk/databases/indexeddb/v0/v0/v0/v0/v0/v0/v0/v0

[当我尝试为附近的目录打印树时(如以下代码所示,它可以正常工作。

int main() {

    std::filesystem::path p {std::filesystem::current_path()};
    std::ofstream myFile{"tree.txt"};
    processPth(p, myFile);

}
c++ linux ubuntu filesystems c++17
1个回答
1
投票

如果文件系统的符号链接链接回到其父目录之一,则以递归方式搜索该目录将以无限递归结束,因为该符号链接将使您不断回到父目录。

系统仅允许这么多次递归,如果递归太深,则会返回错误。

这样递归遍历目录时,您可能应该忽略符号链接。您可以使用std::filesystem::is_symlink测试路径是否为符号链接。

© www.soinside.com 2019 - 2024. All rights reserved.