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

Re: [PATCH v3 1/2] [GSoC] dir_iterator: iterate over dir after its contents

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Mar 26, 2017, 22:48 UTC
Message-ID
<f8beff4b-8e50-99ac-715d-549ae40bba0c@alum.mit.edu>
In-Reply-To
<1490465551-71056-2-git-send-email-bnmvco@gmail.com>
On 03/25/2017 07:12 PM, Daniel Ferreira wrote:
Show 45 quoted lines
> Create an option for the dir_iterator API to iterate over a directory
> path only after having iterated through its contents. This feature was
> predicted, although not implemented by 0fe5043 ("dir_iterator: new API
> for iterating over a directory tree", 2016-06-18).
> 
> This is useful for recursively removing a directory and calling rmdir()
> on a directory only after all of its contents have been wiped.
> 
> An "options" member has been added to the dir_iterator struct. It
> contains the "iterate_dirs_after_files" flag, that enables the feature
> when set to 1. Default behavior continues to be iterating over directory
> paths before its contents.
> 
> Two inline functions have been added to dir_iterator's code to avoid
> code repetition inside dir_iterator_advance() and make code more clear.
> 
> No particular functions or wrappers for setting the options struct's
> fields have been added to avoid either breaking the current dir_iterator
> API or over-engineering an extremely simple option architecture.
> 
> Signed-off-by: Daniel Ferreira <bnmvco@gmail.com>
> ---
>  dir-iterator.c | 100 ++++++++++++++++++++++++++++++++++++++++++++-------------
>  dir-iterator.h |   7 ++++
>  2 files changed, 84 insertions(+), 23 deletions(-)
> 
> [...]
> diff --git a/dir-iterator.h b/dir-iterator.h
> index 27739e6..4304913 100644
> --- a/dir-iterator.h
> +++ b/dir-iterator.h
> @@ -38,7 +38,14 @@
>   * dir_iterator_advance() again.
>   */
> 
> +struct dir_iterator_options {
> +	unsigned iterate_dirs_after_files : 1;
> +};
> +
>  struct dir_iterator {
> +	/* Options for dir_iterator */
> +	struct dir_iterator_options options;
> +
>  	/* The current path: */
>  	struct strbuf path;

Another thing I noticed: the name of this option, `iterate_dirs_after_files`, is a little bit misleading. If I understand correctly, it doesn't make the iteration process files before directories within a single directory; rather, it ensures that subdirectories and their contents are processed before the containing directory. Therefore, a better name might be something like "depth_first".

I should mention that I like the overall idea to add this new feature and use it to simplify `remove_subtree()`.

Michael
Previous: Michael HaggertyNext: Stefan Beller
Message 5 of 6 in “[GSoC] remove_subtree(): reimplement using iterators”
  1. 0/2 [GSoC] remove_subtree(): reimplement using iteratorsDaniel Ferreira, Mar 25, 2017
  2. 2/2 [GSoC] remove_subtree(): reimplement using iteratorsDaniel Ferreira, Mar 25, 2017
  3. 1/2 [GSoC] dir_iterator: iterate over dir after its contentsDaniel Ferreira, Mar 25, 2017
  4. Michael HaggertyMar 26, 2017
  5. Michael HaggertyMar 26, 2017
  6. Stefan BellerMar 28, 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.