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

Re: [PATCH v7 5/5] remove_subtree(): reimplement using iterators

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Apr 3, 2017, 03:42 UTC
Message-ID
<92bd74cc-1ad9-608b-f431-017d35ce72bc@alum.mit.edu>
In-Reply-To
<1491163388-41255-6-git-send-email-bnmvco@gmail.com>
On 04/02/2017 10:03 PM, Daniel Ferreira wrote:
Show 10 quoted lines
> From: Daniel Ferreira <daniel.calibeta@gmail.com>
> 
> 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>

This patch has a different author email than the others. If possible, please choose one email address that you will use for your commits, and try to be consistent.

Show 46 quoted lines
> ---
>  entry.c | 38 ++++++++++++--------------------------
>  1 file changed, 12 insertions(+), 26 deletions(-)
> 
> diff --git a/entry.c b/entry.c
> index d2b512d..bd06f41 100644
> --- a/entry.c
> +++ b/entry.c
> @@ -3,6 +3,8 @@
>  #include "dir.h"
>  #include "streaming.h"
>  #include "submodule.h"
> +#include "iterator.h"
> +#include "dir-iterator.h"
>  
>  static void create_directories(const char *path, int path_len,
>  			       const struct checkout *state)
> @@ -45,33 +47,17 @@ static void create_directories(const char *path, int path_len,
>  	free(buf);
>  }
>  
> -static void remove_subtree(struct strbuf *path)
> +static void remove_subtree(const char *path)
>  {
> -	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))
> -			die_errno("cannot unlink '%s'", path->buf);
> -		strbuf_setlen(path, origlen);
> +	struct dir_iterator *diter = dir_iterator_begin(path, DIR_ITERATOR_POST_ORDER_TRAVERSAL | DIR_ITERATOR_LIST_ROOT_DIR);

The line above is way too long. Try to limit lines to 80 characters max. (This is documented in `Documentation/CodingGuidelines`.)

Show 23 quoted lines
> +	while (dir_iterator_advance(diter) == ITER_OK) {
> +		if (S_ISDIR(diter->st.st_mode)) {
> +			if (rmdir(diter->path.buf))
> +				die_errno("cannot rmdir '%s'", diter->path.buf);
> +		} else if (unlink(diter->path.buf))
> +			die_errno("cannot unlink '%s'", diter->path.buf);
>  	}
> -	closedir(dir);
> -	if (rmdir(path->buf))
> -		die_errno("cannot rmdir '%s'", path->buf);
>  }
>  
>  static int create_file(const char *path, unsigned int mode)
> @@ -312,7 +298,7 @@ int checkout_entry(struct cache_entry *ce,
>  				return 0;
>  			if (!state->force)
>  				return error("%s is a directory", path.buf);
> -			remove_subtree(&path);
> +			remove_subtree(path.buf);
>  		} else if (unlink(path.buf))
>  			return error_errno("unable to unlink old '%s'", path.buf);
>  	} else if (state->not_new)
> 
That's a gratifying decrease in code size :-)
Michael
Previous: Daniel Ferreira
Message 13 of 13 in “[GSoC] remove_subtree(): reimplement using iterators”
  1. 0/5 [GSoC] remove_subtree(): reimplement using iteratorsDaniel Ferreira, Apr 2, 2017
  2. 1/5 dir_iterator: add tests for dir_iterator APIDaniel Ferreira, Apr 2, 2017
  3. Stefan BellerApr 3, 2017
  4. 2/5 remove_subtree(): test removing nested directoriesDaniel Ferreira, Apr 2, 2017
  5. Stefan BellerApr 3, 2017
  6. 3/5 dir_iterator: add helpers to dir_iterator_advanceDaniel Ferreira, Apr 2, 2017
  7. Stefan BellerApr 3, 2017
  8. 4/5 dir_iterator: refactor state machine modelDaniel Ferreira, Apr 2, 2017
  9. Michael HaggertyApr 3, 2017
  10. Daniel Ferreira (theiostream)Apr 3, 2017
  11. Michael HaggertyApr 4, 2017
  12. 5/5 remove_subtree(): reimplement using iteratorsDaniel Ferreira, Apr 2, 2017
  13. Michael HaggertyApr 3, 2017

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.