Removing readonly files on windows with std.file

WebFreak001 d.forum at webfreak.org
Sun Mar 8 16:38:37 UTC 2020


On Saturday, 7 March 2020 at 20:01:07 UTC, Jonathan Marler wrote:
> 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?

I made a package for this solving this exact issue I had too with 
the .git folder!

https://code.dlang.org/packages/rm-rf
https://github.com/WebFreak001/rm-rf/blob/master/source/rm/rf.d

It's public domain so you can just look over the few lines it has 
and decide if you want to copy paste the file into your project 
or depend on it on dub :p


More information about the Digitalmars-d mailing list