Skip to content

ghc::filesystem::remove_all fails if symlink target is pointing to a read only filesystem #132

Closed
@CedricDC

Description

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:

  1. 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)
  2. Create directory test_dir with a file in it
  3. Remount filesystem as read-only.
  4. 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!

Metadata

Assignees

Labels

POSIXPOSIX type backend is affectedavailable on masterFix is done on master branch, issue closed on next releasebugSomething isn't working

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions