<filesystem>: remove does not delete read-only files
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 11.2k
- Forks
- 1.7k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 22
Description
Descibe the bug
std::filesystem::remove() does not remove read-only files
The issue with this is that STL sets the FILE_ATTRIBUTE_READONLY bit when we remove the write permissions for a file.
remove should remove such files also.
Command-line test case
C:\Temp>type repro.cpp
#include <filesystem>
#include <iostream>
#include <fstream>
using namespace std;
namespace fs = std::filesystem;
int main()
{
fs::path testfile("testfile");
// Create the file
fstream file;
file.open(testfile, ios::out);
file.close();
// Remove write permissions
fs::permissions(testfile,
fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write,
fs::perm_options::remove);
error_code E;
//POSIX remove should remove this as well.
fs::remove(testfile, E);
//Should print '5 Access is denied.'
cout << E.value() << " " << E.message() << endl;
}
C:\Temp>cl /EHsc /std:c++17 /W4 /WX .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.23.28019.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
repro.cpp
Microsoft (R) Incremental Linker Version 14.23.28019.1
Copyright (C) Microsoft Corporation. All rights reserved.
/out:repro.exe
repro.obj
C:\Temp>.\repro.exe
5 Access is denied.
Expected behavior
std::filesystem::remove() should remove 'testfile'
STL version
Versions that support std::filesystem
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Windows command-line reproduction in repro.cpp and confirm that std::filesystem::remove returns error 5 after write permissions are removed. Trace the std::filesystem::remove implementation and its permission handling, then verify that the read-only testfile is deleted and the error_code remains clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100