From: Matheus Tavares Bernardino <matheus.bernardino@usp.br>
To: otalpster@gmail.com
Cc: git <git@vger.kernel.org>, Michael Haggerty <mhagger@alum.mit.edu>
Subject: Re: [PATCH] entry.c: use dir-iterator to avoid explicit dir traversal
Date: Mon, 9 Dec 2019 14:41:31 -0300 [thread overview]
Message-ID: <CAHd-oW4MzH9Wrb2zNPF0JBN8+FRB4NtDX0166Qt2Kbqs0mDgcw@mail.gmail.com> (raw)
In-Reply-To: <20191208180439.19018-1-otalpster@gmail.com>
On Sun, Dec 8, 2019 at 3:06 PM <otalpster@gmail.com> wrote:
>
> From: Plato <otalpster@gmail.com>
>
> 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().
>
> Signed-off-by: Plato <otalpster@gmail.com>
> ---
> Hello,
>
> This is my first patch.
Hello Plato, and welcome!
Thanks for working on this.
> 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(-)
>
> diff --git a/entry.c b/entry.c
> index 53380bb614..e7f4881d3b 100644
> --- a/entry.c
> +++ b/entry.c
> @@ -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);
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"?
> - 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.
> 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/1493226219-33423-1-git-send-email-bnmvco@gmail.com/
[2]: https://public-inbox.org/git/cover.1562801254.git.matheus.bernardino@usp.br/
[3]: https://public-inbox.org/git/1493226219-33423-3-git-send-email-bnmvco@gmail.com/
next prev parent reply other threads:[~2019-12-09 17:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-08 18:04 [PATCH] entry.c: use dir-iterator to avoid explicit dir traversal otalpster
2019-12-09 15:21 ` Derrick Stolee
2019-12-09 17:41 ` Matheus Tavares Bernardino [this message]
2019-12-09 21:18 ` Junio C Hamano
2019-12-10 4:38 ` Matheus Tavares Bernardino
2019-12-16 16:12 ` otalpster
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAHd-oW4MzH9Wrb2zNPF0JBN8+FRB4NtDX0166Qt2Kbqs0mDgcw@mail.gmail.com \
--to=matheus.bernardino@usp.br \
--cc=git@vger.kernel.org \
--cc=mhagger@alum.mit.edu \
--cc=otalpster@gmail.com \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).