Skip to content

<filesystem>: remove does not delete read-only files #1511

Open
@SibiSiddharthan

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfilesystemC++17 filesystem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions