Skip to contentSkip to conversation

entry.c: use dir-iterator to avoid explicit dir traversal

PATCH In review [email protected] 4 participants 6 messages Dec 8, 2019 – Dec 16, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 entry.c: use dir-iterator to avoid explicit dir traversal

From: Plato <[email protected]>

Replace usage of opendir/readdir/closedir API to traverse directories recursively, at remove_subtree() function, by the dir-iterator API. This simplifies the code and avoids recursive calls to remove_subtree().

---
Hello,

This is my first patch.
I hope I cc'd the correct people and didn't mess up.

The changes pass the test suite t/ and Travis CI.
Please point out any mistakes.

Thanks for your time! :)

 entry.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
entry.c +15 -17
index 53380bb614..e7f4881d3b 100644
@@ -2,6 +2,8 @@
 #include "blob.h"
 #include "object-store.h"
 #include "dir.h"
+#include "iterator.h"
+#include "dir-iterator.h"
 #include "streaming.h"
 #include "submodule.h"
 #include "progress.h"
@@ -50,29 +52,25 @@ static void create_directories(const char *path, int path_len,
 
 static void remove_subtree(struct strbuf *path)
 {
-	DIR *dir = opendir(path->buf);
-	struct dirent *de;
-	int origlen = path->len;
+	int ok;
+	unsigned int flags = DIR_ITERATOR_PEDANTIC;
+	struct dir_iterator *iter = dir_iterator_begin(path->buf, flags);
 
-	if (!dir)
+	if (!iter)
 		die_errno("cannot opendir '%s'", path->buf);
-	while ((de = readdir(dir)) != NULL) {
-		struct stat st;
 
-		if (is_dot_or_dotdot(de->d_name))
+	while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
+		if (is_dot_or_dotdot(iter->path.buf))
 			continue;
 
-		strbuf_addch(path, '/');
-		strbuf_addstr(path, de->d_name);
-		if (lstat(path->buf, &st))
-			die_errno("cannot lstat '%s'", path->buf);
-		if (S_ISDIR(st.st_mode))
-			remove_subtree(path);
-		else if (unlink(path->buf))
-			die_errno("cannot unlink '%s'", path->buf);
-		strbuf_setlen(path, origlen);
+		if (unlink(iter->path.buf)) {
+			die_errno("cannot unlink '%s'", iter->path.buf);
+		}
 	}
-	closedir(dir);
+
+	if (ok != ITER_DONE)
+		die(_("failed to iterate over '%s'"), path->buf);
+
 	if (rmdir(path->buf))
 		die_errno("cannot rmdir '%s'", path->buf);
 }
Signature · 1 line
2.24.0
#1 lore raw
Quoted 6 lines · On 12/8/2019 1:04 PM, [email protected] wrote:

From: Plato <[email protected]>

Replace usage of opendir/readdir/closedir API to traverse directories recursively, at remove_subtree() function, by the dir-iterator API. This simplifies the code and avoids recursive calls to remove_subtree().

Quoted 6 lines

---
Hello,

This is my first patch.
I hope I cc'd the correct people and didn't mess up.

Welcome, Plato!

Quoted 32 lines

The changes pass the test suite t/ and Travis CI.
Please point out any mistakes.

Thanks for your time! :)

 entry.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
entry.c +5 -3
index 53380bb614..e7f4881d3b 100644
@@ -2,6 +2,8 @@
 #include "blob.h"
 #include "object-store.h"
 #include "dir.h"
+#include "iterator.h"
+#include "dir-iterator.h"
 #include "streaming.h"
 #include "submodule.h"
 #include "progress.h"
@@ -50,29 +52,25 @@ static void create_directories(const char *path, int path_len,
 
 static void remove_subtree(struct strbuf *path)
 {
-	DIR *dir = opendir(path->buf);
-	struct dirent *de;
-	int origlen = path->len;
+	int ok;
+	unsigned int flags = DIR_ITERATOR_PEDANTIC;
+	struct dir_iterator *iter = dir_iterator_begin(path->buf, flags);
 

entry.c static void create_directories(const char *path, int path_len, · line 64

Quoted 10 lines
entry.c +3 -4
-	if (!dir)
+	if (!iter)
 		die_errno("cannot opendir '%s'", path->buf);
-	while ((de = readdir(dir)) != NULL) {
-		struct stat st;
 
-		if (is_dot_or_dotdot(de->d_name))
+	while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
+		if (is_dot_or_dotdot(iter->path.buf))
 			continue;

The documentation in dir-iterator.h seems to skip . and .. for you. This 'if' shouldn't be needed.

entry.c static void create_directories(const char *path, int path_len, · line 70

Quoted 6 lines
entry.c +0 -6
-		strbuf_addch(path, '/');
-		strbuf_addstr(path, de->d_name);
-		if (lstat(path->buf, &st))
-			die_errno("cannot lstat '%s'", path->buf);
-		if (S_ISDIR(st.st_mode))
-			remove_subtree(path);

It took me reading dir-iterator.h to realize that the iterator is recursive, because I'm new to this API. This is a nice cleanup!

entry.c static void create_directories(const char *path, int path_len, · line 68

Quoted 6 lines
entry.c +3 -3
-		else if (unlink(path->buf))
-			die_errno("cannot unlink '%s'", path->buf);
-		strbuf_setlen(path, origlen);
+		if (unlink(iter->path.buf)) {
+			die_errno("cannot unlink '%s'", iter->path.buf);
+		}

nit: don't use braces for a single-line block.

entry.c static void create_directories(const char *path, int path_len, · line 76

Quoted 9 lines
entry.c +4 -1
 	}
-	closedir(dir);
+
+	if (ok != ITER_DONE)
+		die(_("failed to iterate over '%s'"), path->buf);
+
 	if (rmdir(path->buf))
 		die_errno("cannot rmdir '%s'", path->buf);
 }

Thanks,
-Stolee

#2 lore raw
In reply to [email protected]
Quoted 7 lines · On Sun, Dec 8, 2019 at 3:06 PM <[email protected]> wrote:

From: Plato <[email protected]>

Replace usage of opendir/readdir/closedir API to traverse directories recursively, at remove_subtree() function, by the dir-iterator API. This simplifies the code and avoids recursive calls to remove_subtree().

Quoted 5 lines

---
Hello,

This is my first patch.

Hello Plato, and welcome!
Thanks for working on this.

Quoted 27 lines

I hope I cc'd the correct people and didn't mess up.

The changes pass the test suite t/ and Travis CI.
Please point out any mistakes.

Thanks for your time! :)

 entry.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
entry.c +2 -0
index 53380bb614..e7f4881d3b 100644
@@ -2,6 +2,8 @@
 #include "blob.h"
 #include "object-store.h"
 #include "dir.h"
+#include "iterator.h"
+#include "dir-iterator.h"
 #include "streaming.h"
 #include "submodule.h"
 #include "progress.h"
@@ -50,29 +52,25 @@ static void create_directories(const char *path, int path_len,
 
 static void remove_subtree(struct strbuf *path)
 {

entry.c static void create_directories(const char *path, int path_len, · line 60

Quoted 10 lines
entry.c +4 -4
-       DIR *dir = opendir(path->buf);
-       struct dirent *de;
-       int origlen = path->len;
+       int ok;
+       unsigned int flags = DIR_ITERATOR_PEDANTIC;
+       struct dir_iterator *iter = dir_iterator_begin(path->buf, flags);
 
-       if (!dir)
+       if (!iter)
                die_errno("cannot opendir '%s'", path->buf);

Nitpick: since dir_iterator_begin() might fail for reasons other than an opendir() error, I think the error message here could be more generic. Maybe "failed to start iterator over %s"?

entry.c static void create_directories(const char *path, int path_len, · line 63

Quoted 6 lines
entry.c +2 -3
-       while ((de = readdir(dir)) != NULL) {
-               struct stat st;
 
-               if (is_dot_or_dotdot(de->d_name))
+       while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
+               if (is_dot_or_dotdot(iter->path.buf))

This check is already done by dir-iterator internally, so you may remove it here.

entry.c static void create_directories(const char *path, int path_len, · line 66

Quoted 12 lines
entry.c +1 -9
                        continue;
 
-               strbuf_addch(path, '/');
-               strbuf_addstr(path, de->d_name);
-               if (lstat(path->buf, &st))
-                       die_errno("cannot lstat '%s'", path->buf);
-               if (S_ISDIR(st.st_mode))
-                       remove_subtree(path);
-               else if (unlink(path->buf))
-                       die_errno("cannot unlink '%s'", path->buf);
-               strbuf_setlen(path, origlen);
+               if (unlink(iter->path.buf)) {

unlink()-ing a directory in Linux will return a EISDIR error. So I think you still need to use S_ISDIR() to check if iter->path.buf is a directory and call rmdir(), in this case.

However, note that the dir-iterator API gives entries in pre-order. I.e. a directory appears before its subentries. In the use case of remove_subtree(), though, we need to traverse in post-order, since we have to remove the subentries before removing the directory where they reside. My suggestion is that you add a preliminary patch, implementing a new DIR_ITERATOR_POST_ORDER flag to dir-iterator.h, and then use it in this patch.

You may also want to check this[1] series, which worked towards the same goal of converting remove_subtree(). It ended up not getting merged, back them, but some of the patches were re-used in this[2] series which got merged. I think you could also re-use some of the code from [1] that implements the post-order traversing and a test[3] for remove_subtree().

Thanks,
Matheus

[1]: https://public-inbox.org/git/[email protected]/
[2]: https://public-inbox.org/git/[email protected]/
[3]: https://public-inbox.org/git/[email protected]/

#3 lore raw
Quoted 18 lines · Matheus Tavares Bernardino

entry.c static void create_directories(const char *path, int path_len, · line 66

Quoted 13 lines

The changes pass the test suite t/ and Travis CI.
Please point out any mistakes.
...

entry.c +1 -9
-               strbuf_addch(path, '/');
-               strbuf_addstr(path, de->d_name);
-               if (lstat(path->buf, &st))
-                       die_errno("cannot lstat '%s'", path->buf);
-               if (S_ISDIR(st.st_mode))
-                       remove_subtree(path);
-               else if (unlink(path->buf))
-                       die_errno("cannot unlink '%s'", path->buf);
-               strbuf_setlen(path, origlen);
+               if (unlink(iter->path.buf)) {

unlink()-ing a directory in Linux will return a EISDIR error. So I think you still need to use S_ISDIR() to check if iter->path.buf is a directory and call rmdir(), in this case.

Quoted 7 lines

However, note that the dir-iterator API gives entries in pre-order. I.e. a directory appears before its subentries. In the use case of remove_subtree(), though, we need to traverse in post-order, since we have to remove the subentries before removing the directory where they reside. My suggestion is that you add a preliminary patch, implementing a new DIR_ITERATOR_POST_ORDER flag to dir-iterator.h, and then use it in this patch.

Thanks for a review and a few hints to nudge a new contributor in the right direction. Very much appreciated.

I wonder why the bugs in this patch weren't caught by self test we already have, by the way. We need a bit better test coverage, perhaps?

#4 lore raw
Quoted 29 lines · Junio C Hamano
Quoted 18 lines · Matheus Tavares Bernardino

entry.c static void create_directories(const char *path, int path_len, · line 66

Quoted 13 lines

The changes pass the test suite t/ and Travis CI.
Please point out any mistakes.
...

entry.c +1 -9
-               strbuf_addch(path, '/');
-               strbuf_addstr(path, de->d_name);
-               if (lstat(path->buf, &st))
-                       die_errno("cannot lstat '%s'", path->buf);
-               if (S_ISDIR(st.st_mode))
-                       remove_subtree(path);
-               else if (unlink(path->buf))
-                       die_errno("cannot unlink '%s'", path->buf);
-               strbuf_setlen(path, origlen);
+               if (unlink(iter->path.buf)) {

unlink()-ing a directory in Linux will return a EISDIR error. So I think you still need to use S_ISDIR() to check if iter->path.buf is a directory and call rmdir(), in this case.

Quoted 7 lines

However, note that the dir-iterator API gives entries in pre-order. I.e. a directory appears before its subentries. In the use case of remove_subtree(), though, we need to traverse in post-order, since we have to remove the subentries before removing the directory where they reside. My suggestion is that you add a preliminary patch, implementing a new DIR_ITERATOR_POST_ORDER flag to dir-iterator.h, and then use it in this patch.

Quoted 6 lines

Thanks for a review and a few hints to nudge a new contributor in the right direction. Very much appreciated.

I wonder why the bugs in this patch weren't caught by self test we already have, by the way. We need a bit better test coverage, perhaps?

I think there is no current test that covers remove_subtree() being called with nested directories. But we could use the test proposed by Daniel[1], which does fail when this current patch is applied. So, maybe, Plato could also include this test in v2, before performing the dir-iterator convertion.

[1]: https://public-inbox.org/git/[email protected]/

author
#5 lore raw
Quoted 38 lines · Matheus Tavares Bernardino
Quoted 29 lines · Junio C Hamano
Quoted 18 lines · Matheus Tavares Bernardino

entry.c static void create_directories(const char *path, int path_len, · line 66

Quoted 13 lines

The changes pass the test suite t/ and Travis CI.
Please point out any mistakes.
...

entry.c +1 -9
-               strbuf_addch(path, '/');
-               strbuf_addstr(path, de->d_name);
-               if (lstat(path->buf, &st))
-                       die_errno("cannot lstat '%s'", path->buf);
-               if (S_ISDIR(st.st_mode))
-                       remove_subtree(path);
-               else if (unlink(path->buf))
-                       die_errno("cannot unlink '%s'", path->buf);
-               strbuf_setlen(path, origlen);
+               if (unlink(iter->path.buf)) {

unlink()-ing a directory in Linux will return a EISDIR error. So I think you still need to use S_ISDIR() to check if iter->path.buf is a directory and call rmdir(), in this case.

Quoted 7 lines

However, note that the dir-iterator API gives entries in pre-order. I.e. a directory appears before its subentries. In the use case of remove_subtree(), though, we need to traverse in post-order, since we have to remove the subentries before removing the directory where they reside. My suggestion is that you add a preliminary patch, implementing a new DIR_ITERATOR_POST_ORDER flag to dir-iterator.h, and then use it in this patch.

Quoted 6 lines

Thanks for a review and a few hints to nudge a new contributor in the right direction. Very much appreciated.

I wonder why the bugs in this patch weren't caught by self test we already have, by the way. We need a bit better test coverage, perhaps?

Quoted 7 lines

I think there is no current test that covers remove_subtree() being called with nested directories. But we could use the test proposed by Daniel[1], which does fail when this current patch is applied. So, maybe, Plato could also include this test in v2, before performing the dir-iterator convertion.

[1]: https://public-inbox.org/git/[email protected]/

Hello!
Thanks for the warm welcome and sorry for the late reply. Your review and hints you provided were very helpful and appreciated.

Yes, I can work and include the test in v2, as well as performing the dir-iterator conversion. The required process and the necessary changes I need to make were very clear.

This week I'm very busy. I'll work on this in great detail from next week.

Thanks,
Plato