ghc::filesystem::remove_all fails if symlink target is pointing to a read only filesystem
CedricDC opened this issue · 4 comments
Hi!
The call to ghc::filesystem::remove_all(path)
(same with the version with the std::error_code
) appears to fail when "path" is a symlink on a read-write filesystem that points to a non-empty directory on a read-only filesystem. It works correctly when the symlink target is a file or if the target directory is empty. The call returns an error with the "Read-only file system" message.
Tested on Ubuntu 18.04 with gcc 9.2.0 with filesystem 1.0.8 as well as current master. Using std::filesystem
instead, the symlink is deleted as expected.
Test procedure:
- Create virtual filesystem as described here : https://www.thegeekdiary.com/how-to-create-virtual-block-device-loop-device-filesystem-in-linux/ (or use real one, whatever is available)
- Create directory test_dir with a file in it
- Remount filesystem as read-only.
- Create symlink (called sym_test_dir in the example below) to test_dir from a different, read-write filesystem and try to delete that symlink via
remove_all
Sample executable:
#include <iostream>
#include <string>
#include "ghc_filesystem.hpp"
namespace fs = ghc::filesystem;
void removeSomething(const std::string& name) {
std::error_code ec_remove;
fs::remove_all(name, ec_remove);
if (ec_remove.value() != 0) {
std::string msg = ec_remove.message();
std::cout << "Could not remove " << name << ": " << msg << std::endl;
} else {
std::cout << "Happily removed " << name << std::endl;
}
}
int main() {
std::string symlink_path = "sym_test_dir";
// try to remove non-empty dir --> fails with "Read-only file system"
removeSomething(symlink_dir);
return 0;
}
Thanks!
Thank you for the detailed report, I'll look into it.
@CedricDC
Please replace line
filesystem/include/ghc/filesystem.hpp
Line 4743 in 7bc5c17
to
auto fs = symlink_status(p, tec);
and run your test.