git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Adam Milazzo <Adam.Milazzo@microsoft.com>
Cc: "git\@vger.kernel.org" <git@vger.kernel.org>
Subject: Re: BUG: git clean -d cannot remove files from read-only directories
Date: Thu, 20 Feb 2020 10:47:57 -0800	[thread overview]
Message-ID: <xmqqk14hayea.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <xmqqo8ttaz3p.fsf@gitster-ct.c.googlers.com> (Junio C. Hamano's message of "Thu, 20 Feb 2020 10:32:42 -0800")

Junio C Hamano <gitster@pobox.com> writes:

> Adam Milazzo <Adam.Milazzo@microsoft.com> writes:
>
>> 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.
>
> It is how UNIX-like filesystem works, isn't it?
>
> As long as a directory D is writable/executable, any regular file
> inside it can be removed regardless of the perm-bits of the file.
>
> 	mkdir d
> 	touch d/a
> 	chmod -w d
> 	rm d/a
>
> would not let you remove the file d/a from d/, exactly because you
> cannot modify d/ (it is not writable).

I realize that the above is probably a bit too terse for those who
are not familiar with the UNIX/POSIX-like filesystems.

A directory is a record of what files (and subdirectories) are in
it.  If you add a file to or remove a file from a directory, you
are updating that record---so you need to be able to write to the
directory.  You do not have to be able to write (or even to read for
that matter) to the file you are adding or removing to the directory,
because adding or removing a file from a directory involves only
updating the record kept in the directory about what files and
subdirectories are in that directory (hence you need to be able to
write to the directory---but perm bits on the files you are adding
or removing do not play a role in this operation).

A collorary is that modifying an existing file in a directory does
not have to change what is recorded in the directory---what files
are in the directory does not change.  You only need to be able to
write the file, so you can edit a file in a read-only directory.  

Note that some editors, however, implement an "edit" as "rename the
original file as a backup, create a new copy of a file, and edit
that new copy".  With such an editor, you cannot "edit" a writable
file in a read-only directory and the reason would by now be obvious
to you once you understand the explanation above.  Both renaming the
original and creating the new copy would be updating what files are
in the directory, requiring you to be able to write to the directory.

Hope this helps.


  reply	other threads:[~2020-02-20 18:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-20 18:27 BUG: git clean -d cannot remove files from read-only directories Adam Milazzo
2020-02-20 18:32 ` Junio C Hamano
2020-02-20 18:47   ` Junio C Hamano [this message]
2020-02-20 18:46 ` Daniel Knittl-Frank
2020-02-21  1:45 ` brian m. carlson
  -- strict thread matches above, loose matches on Subject: below --
2020-02-20 18:55 Adam Milazzo
2020-02-20 19:45 ` Elijah Newren
2020-02-20 19:29 Adam Milazzo
2020-02-21  0:52 Adam Milazzo
2020-02-21  1:08 ` Elijah Newren

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqk14hayea.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=Adam.Milazzo@microsoft.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).