All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Haggerty <mhagger@alum.mit.edu>
To: Daniel Ferreira <bnmvco@gmail.com>, git@vger.kernel.org
Cc: gitster@pobox.com, sbeller@google.com, pclouds@gmail.com
Subject: Re: [PATCH v4 3/5] remove_subtree(): reimplement using iterators
Date: Wed, 29 Mar 2017 12:01:12 +0200	[thread overview]
Message-ID: <679957e0-bb06-2dd9-bfc5-024da5ab49ce@alum.mit.edu> (raw)
In-Reply-To: <1490747533-89143-4-git-send-email-bnmvco@gmail.com>

On 03/29/2017 02:32 AM, Daniel Ferreira wrote:
> Use dir_iterator to traverse through remove_subtree()'s directory tree,
> avoiding the need for recursive calls to readdir(). Simplify
> remove_subtree()'s code.
> 
> A conversion similar in purpose was previously done at 46d092a
> ("for_each_reflog(): reimplement using iterators", 2016-05-21).
> 
> Signed-off-by: Daniel Ferreira <bnmvco@gmail.com>
> ---
>  entry.c | 31 ++++++++++---------------------
>  1 file changed, 10 insertions(+), 21 deletions(-)
> 
> diff --git a/entry.c b/entry.c
> index c6eea24..bbebd16 100644
> --- a/entry.c
> +++ b/entry.c
> @@ -2,6 +2,8 @@
>  #include "blob.h"
>  #include "dir.h"
>  #include "streaming.h"
> +#include "iterator.h"
> +#include "dir-iterator.h"
> 
>  static void create_directories(const char *path, int path_len,
>  			       const struct checkout *state)
> @@ -46,29 +48,16 @@ static void create_directories(const char *path, int path_len,
> 
>  static void remove_subtree(struct strbuf *path)

The reason that this function took a `strbuf` as argument was that it
used to modify the strbuf while it was working. Now that `dir_iterator`
does all of that work, you could simplify it to take a `const char *`
argument, and change the caller to pass `path.buf`.

>  {
> -	DIR *dir = opendir(path->buf);
> -	struct dirent *de;
> -	int origlen = path->len;
> -
> -	if (!dir)
> -		die_errno("cannot opendir '%s'", path->buf);
> -	while ((de = readdir(dir)) != NULL) {
> -		struct stat st;
> -
> -		if (is_dot_or_dotdot(de->d_name))
> -			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))
> +	struct dir_iterator *diter = dir_iterator_begin(path->buf, DIR_ITERATOR_DEPTH_FIRST);
> +
> +	while (dir_iterator_advance(diter) == ITER_OK) {
> +		if (S_ISDIR(diter->st.st_mode)) {
> +			if (rmdir(diter->path.buf))
> +				die_errno("cannot rmdir '%s'", path->buf);
> +		} else if (unlink(diter->path.buf))
>  			die_errno("cannot unlink '%s'", path->buf);

The two `die_errno()` calls must be changed to use `diter->path.buf`
rather than `path->buf`.

> -		strbuf_setlen(path, origlen);
>  	}
> -	closedir(dir);
> +
>  	if (rmdir(path->buf))
>  		die_errno("cannot rmdir '%s'", path->buf);
>  }

Michael


  reply	other threads:[~2017-03-29 10:01 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-29  0:32 [PATCH v4 0/5] [GSoC] remove_subtree(): reimplement using iterators Daniel Ferreira
2017-03-29  0:32 ` [PATCH v4 1/5] dir_iterator: add helpers to dir_iterator_advance Daniel Ferreira
2017-03-29  4:32   ` Michael Haggerty
2017-03-29  0:32 ` [PATCH v4 2/5] dir_iterator: iterate over dir after its contents Daniel Ferreira
2017-03-29  9:56   ` Michael Haggerty
2017-03-29 10:44     ` Michael Haggerty
2017-03-29 16:46     ` Junio C Hamano
2017-03-30  4:59       ` Michael Haggerty
2017-03-30  6:08         ` Junio C Hamano
2017-03-30  6:39           ` Michael Haggerty
2017-03-30 11:08             ` Duy Nguyen
2017-04-02  4:25               ` Daniel Ferreira (theiostream)
2017-04-05  9:21                 ` Duy Nguyen
2017-03-30 17:26             ` Junio C Hamano
2017-03-29  0:32 ` [PATCH v4 3/5] remove_subtree(): reimplement using iterators Daniel Ferreira
2017-03-29 10:01   ` Michael Haggerty [this message]
2017-03-29  0:32 ` [PATCH v4 4/5] remove_subtree(): test removing nested directories Daniel Ferreira
2017-03-29  0:32 ` [PATCH v4 5/5] files_reflog_iterator: amend use of dir_iterator Daniel Ferreira
2017-03-29 10:45   ` Michael Haggerty

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=679957e0-bb06-2dd9-bfc5-024da5ab49ce@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=bnmvco@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=sbeller@google.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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.