std::filesystem::remove_all中的潜在错误,带有clang++



请勿在家尝试此操作

我对std::filesystem::remove_all有一个奇怪的问题。我已经编写了一个程序,将N文件写入单个目录中的磁盘,然后删除所有文件(这是有充分理由的(。然而,当我使用std::filesystem::remove_all时,我会收到这样的错误:

filesystem error: cannot remove all: Structure needs cleaning [./tmp_storage] [./tmp_storage/2197772]

并且文件夹没有被删除(显然调用失败(,并且在显示文件系统"是"之后调用CCD_;"损坏":

$ ls tmp_storage/
ls: cannot access 'tmp_storage/2197772': Structure needs cleaning
ls: cannot access 'tmp_storage/5493417': Structure needs cleaning
...

我必须修复文件系统。完整的程序如下所示:

#include <fmt/core.h>
#include <CLI/CLI.hpp>
#include <filesystem>
#include <fstream>
#include <string>
#include <exception>
int main(int argc, char** argv)
{
size_t num_files{64000000};
CLI::App app("Writes N number of files to dir in file system to check the maximum number of files in a directory");
app.add_option("-c,--count", num_files, fmt::format("How many files generate [Default: {}]", num_files));
CLI11_PARSE(app, argc, argv);
std::string base_path = "./tmp_storage";
if (!std::filesystem::exists(base_path))
{
std::filesystem::create_directory(base_path); 
}
size_t i;
for (i = 1; i <= num_files; ++i)
{
std::string file_path = fmt::format("{}/{}", base_path, std::to_string(i));
std::ofstream out(file_path, std::ios::binary);
if (out.fail())
{
break; 
}
try
{
out << std::to_string(i); 
}
catch(const std::exception& e)
{
fmt::print("{}n", e.what());
}
}
fmt::print("Wrote {} out of {} filesn", i, num_files);
try
{
std::filesystem::remove_all(base_path);
}
catch(const std::exception& e)
{
fmt::print("{}n", e.what());
}

fmt::print("Donen");

return 0; 
}

使用以下Makefile编译:

CC = clang++
CXX_FLAGS = -std=c++17
LINK_FLAGS = -lfmt
all:
$(CC) $(CXX_FLAGS) main.cpp -o main $(LINK_FLAGS)

我已经能够在Fedora服务器33/34和Ubuntu上复制使用XFS的Fedora和使用EXT4和XFS的Ubuntu的行为。这是std::filesystem::remov_all中的错误还是我做错了什么?

对于Fedora,内核版本为:Linux 5.12.12-300.fc34.x86_64 x86_64,带有clang版本

clang version 12.0.0 (Fedora 12.0.0-2.fc34)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

我试图使用这个修改后的程序(删除fmt和cli11依赖项(在Fedora 34上重现这一点:

#include <filesystem>
#include <fstream>
#include <string>
#include <exception>
int main(int argc, char** argv)
{
size_t num_files{64000000};
if (argc > 1)
num_files = std::stol(argv[1]);
std::string base_path = "./tmp_storage";
try
{
if (!std::filesystem::exists(base_path))
{
std::filesystem::create_directory(base_path); 
}
size_t i;
for (i = 1; i <= num_files; ++i)
{
auto si = std::to_string(i);
std::string file_path = base_path + '/' + si;
std::ofstream out(file_path, std::ios::binary);
if (out.fail())
throw std::system_error(errno, std::generic_category(), "ofstream failed: " + file_path);
try
{
out << si;
}
catch(const std::exception& e)
{
std::puts(e.what());
}
}
std::printf("Wrote %zu out of %zu filesn", i - 1, num_files);
std::filesystem::remove_all(base_path);
}
catch(const std::exception& e)
{
std::puts(e.what());
}

std::puts("Done");

return 0; 
}

使用ext4或xfs或默认安装选项btrfs,我无法重现F34中的错误。我也无法在另一台使用xfs的服务器上复制它,使用clang 13.0.0、libstdc++-11.2.1和内核5.14.0。这意味着我无法调试std::filesystem实现破坏文件系统的地方,也无法将其报告给内核团队。

我不确定代码是否遇到内核错误,或者硬件是否有故障。您检查过系统日志在文件系统损坏时所说的内容吗?内核哪里有错误?

编辑:另外,您的磁盘是否使用LVM?我想我所有的测试都没有LVM。

注意:这不是底层和操作系统问题的解决方案,而是在C++中避免问题的一种方法。

我们需要对原始代码进行的更改是";最小";。对尝试块进行所有更改

try
{
std::filesystem::remove_all(base_path);
}
catch(const std::exception& e)
{
fmt::print("{}n", e.what());
}

并用顺序删除替换:CCD_ 8。

for (auto& path : std::filesystem::directory_iterator(base_path))
{
std::filesystem::remove(path);
}

将原始代码更改为

#include <fmt/core.h>
#include <CLI/CLI.hpp>
#include <filesystem>
#include <fstream>
#include <string>
#include <exception>
int main(int argc, char** argv)
{
size_t num_files{64000000};

CLI::App app("Writes N number of files to dir in file system to check the maximum number of files in a directory");
app.add_option("-c,--count", num_files, fmt::format("How many files generate [Default: {}]", num_files));
CLI11_PARSE(app, argc, argv);
std::string base_path = "./tmp_storage";
if (!std::filesystem::exists(base_path))
{
std::filesystem::create_directory(base_path); 
}
size_t i;
for (i = 1; i <= num_files; ++i)
{
std::string file_path = fmt::format("{}/{}", base_path, std::to_string(i));
std::ofstream out(file_path, std::ios::binary);
if (out.fail())
{
break; 
}
try
{
out << std::to_string(i); 
}
catch(const std::exception& e)
{
fmt::print("{}n", e.what());
}
}
fmt::print("Wrote {} out of {} filesn", i, num_files);
try
{
for (auto& path : std::filesystem::directory_iterator(base_path))
{
std::filesystem::remove(path); 
}
}
catch(const std::exception& e)
{
fmt::print("{}n", e.what());
}

fmt::print("Donen");

return 0; 
}

最新更新