Removing readonly files on windows with std.file

Jonathan Marler johnnymarler at gmail.com
Sat Mar 7 20:01:07 UTC 2020


After dealing with annoying failures to remove directories on 
windows with std.file.rmdirRecurse for too long, I finally 
decided to look into it.

It looks like rmdirRecurse fails because I'm trying to remove a 
directory that contains a git repository in it.  It turns out 
that git marks some files as READONLY to indicate that they 
shouldn't be modified.  However, this will cause the DeleteFile 
function (which is what rmdirRecurse will eventually call) to 
fail.  The docs say that you must remove the READONLY attribute 
before the file can be deleted.

Note that deleting a directory/file from Windows Explorer or 
calling `del` from the command-line will remove READONLY files no 
problem.  I could create a pull request to modify std.file to 
handle this, however, I'm not sure what the right solution is.  
We could modify std.file.remove to be able to remove readonly 
files, but maybe there's use cases where people want the removal 
to fail if it is marked as readonly?  Although, the 
counter-argument to this would be that the standard mechanisms to 
delete files ignore the READONLY attribute and delete them 
anyway, so maybe D's default behavior should match?

We could also enhance the API to allow the user to specify the 
behavior.  By either defining new functions like 
"removeForce/rmdirForceRecurse", or adding an options argument 
remove(file, options), rmdirRecurse(dir, options).

What do people think is the right solution here?



More information about the Digitalmars-d mailing list