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

Re: [PATCH] entry.c: use dir-iterator to avoid explicit dir traversal

From
Derrick Stolee <stolee@gmail.com>
Date
Dec 9, 2019, 15:21 UTC
Message-ID
<06024905-b00e-0fd6-f985-64a360caac2a@gmail.com>
In-Reply-To
<20191208180439.19018-1-otalpster@gmail.com>
On 12/8/2019 1:04 PM, otalpster@gmail.com wrote:
Show 12 quoted lines
> 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.
> I hope I cc'd the correct people and didn't mess up.
Welcome, Plato!
Show 42 quoted 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(-)
> 
> 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);
> -	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.

Show 6 quoted lines
> -		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!

Show 6 quoted lines
> -		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.
Show 9 quoted lines
>  	}
> -	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

Previous: otalpster@gmail.comNext: Matheus Tavares Bernardino
Message 2 of 6 in “entry.c: use dir-iterator to avoid explicit dir traversal”
  1. entry.c: use dir-iterator to avoid explicit dir traversalotalpster@gmail.com, Dec 8, 2019
  2. Derrick StoleeDec 9, 2019
  3. Matheus Tavares BernardinoDec 9, 2019
  4. Junio C HamanoDec 9, 2019
  5. Matheus Tavares BernardinoDec 10, 2019
  6. otalpsterDec 16, 2019

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.