git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: BUG: git clean -d cannot remove files from read-only directories

From
Daniel Knittl-Frank <knittl89@googlemail.com>
Date
Feb 20, 2020, 18:46 UTC
Message-ID
<CACx-yZ0e-rAsJbeKr5WO32kG-HChTva4nhpfZ98B7C+YiJ4KXw@mail.gmail.com>
In-Reply-To
<BL0PR2101MB10898F2F84B06B1A6B23228380130@BL0PR2101MB1089.namprd21.prod.outlook.com>
Hi Adam,
On Thu, Feb 20, 2020 at 7:27 PM Adam Milazzo <Adam.Milazzo@microsoft.com> wrote:
Show 14 quoted lines
>
> Repro steps:
> 1. git init
> 2. mkdir d
> 3. touch d/a
> 4. chmod -w d
> 5. git clean -fd
>
> Actual result:
> Git doesn't remove anything, saying "warning: failed to remove d/a".
>
>
> Expected result:
> Git should remove the subdirectory 'd' along with its contents. Note that git can remove a read-only file (touch b; chmod -w b; git clean -f) with no problem.

this is not a limitation of Git, but of how Linux Filesystems work. When you delete a file, you do not modify the file, but you change the directory which contains the file. In other words, if you have a directory which is write protected (you do not have the write permission), you cannot add, remove, or rename files. You may however still modify the content existing files.

Your repro will yield the same result when executed without Git, i.e. plain shell commands:

$ mkdir d ; touch d/a ; chmod a-w d $ rm -rf d rm: cannot remove 'd/a': Permission denied

If the write permission were removed before touching the file, this would already fail:

$ mkdir d ; chmod a-w d $ touch d/a touch: cannot touch 'd/a': Permission denied

In summary: this is expected and I doubt Git can do much in such a case. After all, the directory is marked as read-only, so why should Git be able to write it? :)

> […]
>
> * It seems inconsistent for "git clean" to be able to remove read-only files but not files from read-only directories.

Again, deleting a file will modify its containing directory, not the file itself. Making a file read-only does not protect it from being renamed or deleted, that's what directory permissions are for.

- Daniel
-- 
typed with http://neo-layout.org
Previous: Junio C HamanoNext: brian m. carlson
Message 4 of 5 in “BUG: git clean -d cannot remove files from read-only directories”
  1. Adam MilazzoFeb 20, 2020
  2. Junio C HamanoFeb 20, 2020
  3. Junio C HamanoFeb 20, 2020
  4. Daniel Knittl-FrankFeb 20, 2020
  5. brian m. carlsonFeb 21, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.