Skip to contentSkip to conversation

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

PATCH In review Alex Riesen 3 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---

Quoted 2 lines · Linus Torvalds

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.

...

Quoted 5 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(-)
dir.c +4 -1
index 325fb56..7251426 100644
@@ -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, '/');
 
Signature · 1 line
1.7.2.2.240.g7d094

From 861871ebfe72b6839526eaa4fe8e5c4b6eec924e Mon Sep 17 00:00:00 2001
From: Alex Riesen <[email protected]>
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.

---
 dir.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
dir.c +4 -1
index 325fb56..7251426 100644
@@ -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, '/');
 
Signature · 1 line
1.7.2.2.240.g7d094
#1 lore raw

Alex Riesen venit, vidit, dixit 01.04.2011 10:29:

Quoted 11 lines

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

Noticed by Linus.

---

Quoted 2 lines · Linus Torvalds

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.

Quoted 8 lines

...

Quoted 5 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

Famous last words...

Quoted 14 lines

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(-)
dir.c +0 -0
index 325fb56..7251426 100644
@@ -1191,8 +1191,11 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 		return 0;
 

dir.c int remove_dir_recursively(struct strbuf *path, int flag) · line 1201

Quoted 10 lines
dir.c +4 -1
 	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, '/');
 

How about simply

if (!dir)

	return rmdir(path->buf);

like we do later on in that function?

Michael

#2 lore raw
Quoted 7 lines · Michael J Gruber

How about simply

if (!dir)
       return rmdir(path->buf);

like we do later on in that function?

I'm used to try to keep the returned value of a function I modify, and I'm also used to not trust the return values of the functions I don't control. That's to my defense.

But you're unquestionably right.

#3 lore raw
In reply to Alex Riesen
Quoted 10 lines · Alex Riesen

--0016e6d9a16eca69d0049fd73526
Content-Type: text/plain; charset=UTF-8

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

Noticed by Linus.

---

Please don't do an attachment that has an inline patch and then attach the patch itself again in base64. It is extremely annoying.

#4 lore raw

PATCH 1/1 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.
Fix suggested by Michael Gruber and Linus.

---
Junio C Hamano, Fri, Apr 01, 2011 20:08:36 +0200:

Quoted 3 lines

Please don't do an attachment that has an inline patch and then attach the patch itself again in base64. It is extremely annoying.

Sorry. Hard to notice on GMail.

The extended error information is a little bit tricky: there is at least four error cases (opendir, stat, unlink and rmdir) and there is a closedir, which resets errno to output the error in the caller of remove_dir_recursively.

 dir.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
dir.c +1 -1
index 325fb56..532bcb6 100644
@@ -1192,7 +1192,7 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 
 	dir = opendir(path->buf);
 	if (!dir)
-		return -1;
+		return rmdir(path->buf);
 	if (path->buf[original_len - 1] != '/')
 		strbuf_addch(path, '/');
 
Signature · 1 line
1.7.4
#5 lore raw
Quoted 4 lines · Alex Riesen
Quoted 2 lines

Please don't do an attachment that has an inline patch and then attach the patch itself again in base64. It is extremely annoying.

Sorry. Hard to notice on GMail.

Thanks. I've already queued 0235017 (clean: unreadable directory may still be rmdir-able if it is empty, 2011-04-01) with a trivial test.