git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Török Edwin" <edwintorok@gmail.com>
To: git@vger.kernel.org
Cc: "Török Edwin" <edwintorok@gmail.com>
Subject: [PATCH] Fix 'git clean' failure on NFS.
Date: Fri, 18 Jun 2010 14:07:15 +0300	[thread overview]
Message-ID: <1276859235-13534-1-git-send-email-edwintorok@gmail.com> (raw)
In-Reply-To: <loom.20100618T122039-876@post.gmane.org>

readdir() used to do a single pass, which is not always enough.
If you unlink some files/rmdir some subdirs then there might be some
files you haven't seen yet *before* the readdir cursor (files get rearranged
in the directory, etc.).

The fix is to do an additional readdir() pass if we unlinked/rmdired something.
This is easily accomplished by using rewinddir.

Signed-off-by: Török Edwin <edwintorok@gmail.com>
---
 dir.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/dir.c b/dir.c
index 5615f33..7260907 100644
--- a/dir.c
+++ b/dir.c
@@ -999,6 +999,7 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 	struct dirent *e;
 	int ret = 0, original_len = path->len, len;
 	int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
+	int did_rm;
 	unsigned char submodule_head[20];
 
 	if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
@@ -1013,6 +1014,8 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 		strbuf_addch(path, '/');
 
 	len = path->len;
+	do {
+	did_rm = 0;
 	while ((e = readdir(dir)) != NULL) {
 		struct stat st;
 		if (is_dot_or_dotdot(e->d_name))
@@ -1023,15 +1026,24 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 		if (lstat(path->buf, &st))
 			; /* fall thru */
 		else if (S_ISDIR(st.st_mode)) {
-			if (!remove_dir_recursively(path, only_empty))
+			if (!remove_dir_recursively(path, only_empty)) {
+				did_rm = 1;
 				continue; /* happy */
-		} else if (!only_empty && !unlink(path->buf))
+			}
+		} else if (!only_empty && !unlink(path->buf)) {
+			did_rm = 1;
 			continue; /* happy, too */
+		}
 
 		/* path too long, stat fails, or non-directory still exists */
 		ret = -1;
 		break;
 	}
+	/* if we unlinked/rmdir-ed anything there might be files we haven't seen
+	 * yet before the readdir() cursor, rewind and walk directory again
+	 * until we can't unlink any more. */
+	rewinddir(dir);
+	} while (did_rm && !ret);
 	closedir(dir);
 
 	strbuf_setlen(path, original_len);
-- 
1.7.0

  reply	other threads:[~2010-06-18 11:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-17 14:26 [BUG] git clean not working on FreeBSD/NFS Török Edwin
2010-06-17 15:18 ` Henrik Grubbström
2010-06-18 11:03   ` Török Edwin
2010-06-18 11:07     ` Török Edwin [this message]
2010-06-18 12:17       ` [PATCH] Fix 'git clean' failure on NFS Johannes Sixt
2010-06-18 13:09         ` Török Edwin
2010-06-18 12:29       ` Erik Faye-Lund
2010-06-18 13:01         ` Török Edwin
2010-06-18 13:26           ` Erik Faye-Lund
2010-06-18 13:50             ` Török Edwin

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=1276859235-13534-1-git-send-email-edwintorok@gmail.com \
    --to=edwintorok@gmail.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).