boost::filesystem 递归获取每个文件的大小

boost::filesystem recursively getting size of each file

当参数是目录时,为什么这段代码会抛出错误?

使用 boost::recursive_directory_iterator 和使用 std::cout 语句,我可以看到它从不打印目录;只有文件。但是,当我尝试调用 boost::filesystem::file_size() 时,会抛出一个错误,基本上是说我正在尝试获取目录的文件大小。

错误(参数为 "/home"):

terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
  what():  boost::filesystem::file_size: Operation not permitted: "/home/lost+found"
Aborted
#include <iostream>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

int main(int argc, char* argv[])
{
    if (argc != 2) return -1;

    const fs::path file{argv[1]};

    if (!fs::exists(file)) return -1;

    if (fs::is_regular_file(file))
        std::cout << file << "   [ " << fs::file_size(file) << " ]\n";

    else if (fs::is_directory(file))
        for (const fs::directory_entry& f : fs::recursive_directory_iterator{file})
            std::cout << f.path().filename() << "   [ " << fs::file_size(f.path()) << " ]\n";
}

编译:g++ -Wall -Wextra -pedantic-errors -std=c++14 -lboost_system -lboost_filesystem -O2 -Os -s test3.cpp -o test3

尝试实际递归地获取大小:

size_t du(fs::path p) {
    return fs::is_regular_file(p)
       ? file_size(p)
       : boost::accumulate(fs::directory_iterator{p}, 0ull, [](auto a, auto p){return a+du(p);});
}

这将通过对所有基础目录中的文件求和(accumulate)来对目录起作用。

Live On Coliru

#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/range/numeric.hpp>

namespace fs = boost::filesystem;
size_t du(fs::path p) {
    std::cout << __FUNCTION__ << "(" << p << ")\n";
    return fs::is_regular_file(p)
       ? file_size(p)
       : boost::accumulate(fs::directory_iterator{p}, 0ull, [](auto a, auto p){return a+du(p);});
}

int main(int argc, char* argv[])
{
    if (argc != 2) return -1;

    std::cout << "Size is " << du(argv[1]) << "\n";
}

启用调试 std::cout

Size is du(".")
du("./main.cpp")
du("./a.out")
22435

你得到的错误:

terminate called after throwing an instance of 'boost::filesystem::filesystem_error' what(): boost::filesystem::file_size: Operation not permitted: "/home/lost+found" Aborted

表示获取不到/home/lost+found的大小。通常,lost+found 是一个文件夹,file_size only get the size of regular files.

我了解到循环不显示此文件夹的名称。这可能是因为编译器正在评估 fs::file_size(f.path()) 并在调用 operator<< 文件名之前抛出异常,因此它不会被打印出来。

我认为应该修改循环以在询问大小之前检查常规文件:

for (const fs::directory_entry& f : fs::recursive_directory_iterator(folder)) {
  if (fs::is_regular_file(f.path())) {
    std::cout << f.path().filename() << "   [ " << fs::file_size(f.path()) << " ]\n";
  }
}