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

Re: [PATCH] [GSOC] remove_temporary_files(): reimplement using iterators

From
Jeff King <peff@peff.net>
Date
Apr 1, 2017, 05:37 UTC
Message-ID
<20170401053716.yzxqxokuz3awqki4@sigill.intra.peff.net>
In-Reply-To
<20170401002458.3494-1-robert.stanca7@gmail.com>
On Sat, Apr 01, 2017 at 03:24:58AM +0300, Robert Stanca wrote:
Show 29 quoted lines
> @@ -49,12 +51,7 @@ static void remove_temporary_files(void)
>  {
>  	struct strbuf buf = STRBUF_INIT;
>  	size_t dirlen, prefixlen;
> -	DIR *dir;
> -	struct dirent *e;
> -
> -	dir = opendir(packdir);
> -	if (!dir)
> -		return;
> +	struct dir_iterator *diter = dir_iterator_begin(packdir);
>  
>  	/* Point at the slash at the end of ".../objects/pack/" */
>  	dirlen = strlen(packdir) + 1;
> @@ -62,14 +59,13 @@ static void remove_temporary_files(void)
>  	/* Hold the length of  ".tmp-%d-pack-" */
>  	prefixlen = buf.len - dirlen;
>  
> -	while ((e = readdir(dir))) {
> -		if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
> +	while (dir_iterator_advance(diter) == ITER_OK) {
> +		if (strncmp(diter->relative_path, buf.buf + dirlen, prefixlen))
>  			continue;
>  		strbuf_setlen(&buf, dirlen);
> -		strbuf_addstr(&buf, e->d_name);
> +		strbuf_addstr(&buf, diter->relative_path);
>  		unlink(buf.buf);
>  	}
> -	closedir(dir);

I think you could actually clean this code up more. The dir_iterator already does this strbuf magic to hold the full path, so you should be able to just run "unlink(iter->path.buf)", get rid of the extra strbuf entirely.

We use that strbuf for the prefix-comparison, too, but the way it is done is rather confusing. AFAICT, we could just be comparing against "packtmp + strlen(packdir) + 1". Though it would be simpler still to make "packtmp" just the basename, rather than the full path.

I do agree with the point Junio raised, though, that this loop isn't recursive, but dir_iterator is. I think there was talk elsewhere of giving it more options, so perhaps it could be taught a non-recursive version. Until we have that, though, I'm not sure this is a good spot to convert.

-Peff
Previous: Robert StancaNext: Jeff King
Message 2 of 3 in “[GSOC] remove_temporary_files(): reimplement using iterators”
  1. [GSOC] remove_temporary_files(): reimplement using iteratorsRobert Stanca, Apr 1, 2017
  2. Jeff KingApr 1, 2017
  3. Jeff KingApr 1, 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.