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

[PATCH] Try to remove the given path even if it can't be opened

From
Alex Riesen <raa.lkml@gmail.com>
Date
Apr 1, 2011, 08:29 UTC
Message-ID
<AANLkTikfmXiZQquWi4STTCUy0qoY9J_waJ44nrPAvB1d@mail.gmail.com>

Consider unreadable empty directories. rmdir(2) will remove them just fine, assuming the parent directory is modifiable.

Noticed by Linus.
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
On Fri, Apr 1, 2011 at 00:01, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
> Which is kind of understandable, but at the same time, if it's empty,
> a "rmdir()" will just work. So git gave up a bit too soon.
...
Show 5 quoted lines
> Now, I realize that if the directory isn't empty, and is unreadable,
> we really should give up (although a better error message about _why_
> we failed may be in order) rather than try to chmod it or anything
> like that. But the simple "try to rmdir it" might be a good addition
> for the trivial case.

It is not tested, but looks trivial. The system I made it on is a Cygwin machine, and a test from last master pull is still running (since two days). And sorry, it is not based on master. Should apply without problems, though.

---
 dir.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
diff --git a/dir.c b/dir.c
index 325fb56..7251426 100644
--- a/dir.c
+++ b/dir.c
@@ -1191,8 +1191,11 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 		return 0;

 	dir = opendir(path->buf);
-	if (!dir)
+	if (!dir) {
+		if (rmdir(path->buf) == 0)
+			return 0;
 		return -1;
+	}
 	if (path->buf[original_len - 1] != '/')
 		strbuf_addch(path, '/');
-- 
1.7.2.2.240.g7d094


From 861871ebfe72b6839526eaa4fe8e5c4b6eec924e Mon Sep 17 00:00:00 2001
From: Alex Riesen <raa.lkml@gmail.com>
Date: Fri, 1 Apr 2011 09:37:07 +0200
Subject: [PATCH] Try to remove the given path even if it can't be opened

Consider unreadable empty directories. rmdir(2) will remove
them just fine, assuming the parent directory is modifiable.

Noticed by Linus.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 dir.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/dir.c b/dir.c
index 325fb56..7251426 100644
--- a/dir.c
+++ b/dir.c
@@ -1191,8 +1191,11 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 		return 0;
 
 	dir = opendir(path->buf);
-	if (!dir)
+	if (!dir) {
+		if (rmdir(path->buf) == 0)
+			return 0;
 		return -1;
+	}
 	if (path->buf[original_len - 1] != '/')
 		strbuf_addch(path, '/');
 
-- 
1.7.2.2.240.g7d094
Next: Michael J Gruber
Message 1 of 6 in “Try to remove the given path even if it can't be opened”
  1. Try to remove the given path even if it can't be openedAlex Riesen, Apr 1, 2011
  2. Michael J GruberApr 1, 2011
  3. Alex RiesenApr 1, 2011
  4. Junio C HamanoApr 1, 2011
  5. Try to remove the given path even if it can't be openedAlex Riesen, Apr 2, 2011
  6. Junio C HamanoApr 2, 2011

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.