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

Re: [PATCH 1/2] Introduce remove_dir_recursively()

From
David Kastrup <dak@gnu.org>
Date
Sep 28, 2007, 22:14 UTC
Message-ID
<85k5qabfop.fsf@lola.goethe.zz>
In-Reply-To
<Pine.LNX.4.64.0709281323300.28395@racer.site>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 42 quoted lines
> On Fri, 28 Sep 2007, Junio C Hamano wrote:
>
>> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
>> 
>> > +int remove_dir_recursively(char *path, int len, int only_empty)
>> > +{
>> > ...
>> > +		namlen = strlen(e->d_name);
>> > +		if (len + namlen > PATH_MAX ||
>> > +				!memcpy(path + len, e->d_name, namlen) ||
>> > +				(path[len + namlen] = '\0') ||
>> > +				lstat(path, &st))
>> > +			; /* fall thru */
>> > +		else if (S_ISDIR(st.st_mode)) {
>> > +			if (!remove_dir_recursively(path, len + namlen,
>> > +						only_empty))
>> > +				continue; /* happy */
>> > +		} else if (!only_empty &&
>> > +				len + namlen + 1 < PATH_MAX &&
>> > +				!unlink(path))
>> > +			continue; /* happy, too */
>> > +
>> > +		/* path too long, stat fails, or non-directory still exists */
>> > +		ret = -1;
>> > +		break;
>> 
>> Is it only me who finds the first if () condition way too
>> convoluted and needs to read three times to convince oneself
>> that it is doing a sane thing?
>> 
>> Please, especially...
>> 
>>  * For $DEITY's sake, memcpy() returns pointer to dst which you
>>    know is not NULL. so !memcpy() is always false here, which
>>    might be _convenient_ for you and the compiler but not for
>>    a human reader of the code who needs to blink twice wondering
>>    if you meant !memcmp().
>> 
>>  * Same for (path[] = '\0'), wondering if it is misspelled
>>    (path[] == '\0').
>
> Okay, will fix (with an evil goto).

Uh, C has a comma operator. And inverting the first condition makes for a nicer flow, no goto.

		namlen = strlen(e->d_name);
		if (len + namlen <= PATH_MAX
			&& (memcpy(path + len, e->d_name, namlen),
			    path[len + namlen] = '\0',
			    lstat(path, &st) == 0)
		if (S_ISDIR(st.st_mode)) {
			if (!remove_dir_recursively(path, len + namlen,
						only_empty))
			continue; /* happy */
		} else if (!only_empty &&
				len + namlen + 1 < PATH_MAX &&
				!unlink(path))
			continue; /* happy, too */
		/* path too long, stat fails, or non-directory still exists */
		ret = -1;
		break;
-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 5 of 19 in “rsync support”
  1. 0/2 rsync supportJohannes Schindelin, Sep 28, 2007
  2. 1/2 Introduce remove_dir_recursively()Johannes Schindelin, Sep 28, 2007
  3. Junio C HamanoSep 28, 2007
  4. Johannes SchindelinSep 28, 2007
  5. David KastrupSep 28, 2007
  6. 2/2 fetch/push: readd rsync supportJohannes Schindelin, Sep 28, 2007
  7. Shawn O. PearceSep 28, 2007
  8. Johannes SchindelinSep 28, 2007
  9. Junio C HamanoSep 28, 2007
  10. Johannes SchindelinSep 28, 2007
  11. 0/2 rsync support, was Re: [PATCH 0/2] rsync supportJohannes Schindelin, Sep 28, 2007
  12. 1/2 Introduce remove_dir_recursively()Johannes Schindelin, Sep 28, 2007
  13. 2/2 fetch/push: readd rsync supportJohannes Schindelin, Sep 28, 2007
  14. Johannes SchindelinSep 28, 2007
  15. Junio C HamanoSep 28, 2007
  16. Johannes SchindelinSep 28, 2007
  17. 2/2 fetch/push: readd rsync supportJohannes Schindelin, Sep 29, 2007
  18. Junio C HamanoSep 30, 2007
  19. Johannes SchindelinSep 30, 2007

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.