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

Re: [PATCH] real_path: make real_path thread-safe

From
BWBrandon Williams <bmwill@google.com>
Date
Dec 7, 2016, 00:10 UTC
Message-ID
<20161207001018.GD103573@google.com>
In-Reply-To
<xmqqtwagy65q.fsf@gitster.mtv.corp.google.com>
On 12/06, Junio C Hamano wrote:
Show 16 quoted lines
> Brandon Williams <bmwill@google.com> writes:
> 
> > +/* removes the last path component from 'path' except if 'path' is root */
> > +static void strip_last_component(struct strbuf *path)
> > +{
> > +	if (path->len > 1) {
> > +		char *last_slash = find_last_dir_sep(path->buf);
> > +		strbuf_setlen(path, last_slash - path->buf);
> > +	}
> > +}
> 
> You use find_last_dir_sep() which takes care of "Windows uses
> backslash" issue.  Is this function expected to be fed something
> like "C:\My Files\foo.txt" and more importantly "C:\My Files"?  Or
> is that handled by a lot higher level up in the callchain?  I am
> reacting the comparison of path->len and 1 here.

This function should accept both absolute and relative paths, which means it should probably accept "C:\My Files". I wasn't thinking about windows 100% of the time while writing this so I'm hoping that a windows expert will point things like this out to me :). So in reality this check shouldn't be (path->len > 1) but rather some function is_only_root which would check if the strbuf only contains a string which looks like root.

> 
> Also is the input expected to be normalized?  Are we expected to be
> fed something like "/a//b/./c/../d/e" and react sensibly, or is that
> handled by a lot higher level up in the callchain?
Yes it should handle paths like that sensibly.
Show 15 quoted lines
> > +/* gets the next component in 'remaining' and places it in 'next' */
> > +static void get_next_component(struct strbuf *next, struct strbuf *remaining)
> > +{
> > +	char *start = NULL;
> > +	char *end = NULL;
> > +
> > +	strbuf_reset(next);
> > +
> > +	/* look for the next component */
> > +	/* Skip sequences of multiple path-separators */
> > +	for (start = remaining->buf; is_dir_sep(*start); start++)
> > +		/* nothing */;
> 
> Style:
> 		; /* nothing */
k will fix.
Show 13 quoted lines
> 
> > +	/* Find end of the path component */
> > +	for (end = start; *end && !is_dir_sep(*end); end++)
> > +		/* nothing */;
> > +
> > +	strbuf_add(next, start, end - start);
> 
> OK, so this was given "///foo/bar" in "remaining" and appended
> 'foo/' to "next".  I.e. deduping of slashes is handled here.
> 
> POSIX cares about treating "//" at the very beginning of the path
> specially.  Is that supposed to be handled here, or by a lot higher
> level up in the callchain?

What exactly does "//" mean in this context? (I'm just naive in this area)

Show 26 quoted lines
> 
> > +	/* remove the component from 'remaining' */
> > +	strbuf_remove(remaining, 0, end - remaining->buf);
> > +}
> > +
> >  /* We allow "recursive" symbolic links. Only within reason, though. */
> > -#define MAXDEPTH 5
> > +#define MAXSYMLINKS 5
> >  
> >  /*
> >   * Return the real path (i.e., absolute path, with symlinks resolved
> > @@ -21,7 +51,6 @@ int is_directory(const char *path)
> >   * absolute_path().)  The return value is a pointer to a static
> >   * buffer.
> >   *
> >   * The directory part of path (i.e., everything up to the last
> >   * dir_sep) must denote a valid, existing directory, but the last
> >   * component need not exist.  If die_on_error is set, then die with an
> > @@ -33,22 +62,16 @@ int is_directory(const char *path)
> >   */
> >  static const char *real_path_internal(const char *path, int die_on_error)
> >  {
> > +	static struct strbuf resolved = STRBUF_INIT;
> 
> This being 'static' would probably mean that this is not reentrant,
> which goes against the title of the patch.

Yes I mentioned in the cover letter that this was the one last thing that needed to be changed to make it reentrant. I wanted to get this out for review sooner since this is the biggest change (getting rid of the chdir() calls) and dropping the static here would just require making the callers own the memory which should hopefully be an easy refactor. It just would have taken a bit more time to actually do that refactor now. I'm slowly working on it while this is being reviewed and could be ready to send out when rerolling this patch. Sorry for having a slightly misleading patch title :)

-- 
Brandon Williams
Previous: Junio C HamanoNext: Ramsay Jones
Message 11 of 83 in “making real_path thread-safe”
  1. making real_path thread-safeBrandon Williams, Dec 5, 2016
  2. real_path: make real_path thread-safeBrandon Williams, Dec 5, 2016
  3. Stefan BellerDec 5, 2016
  4. Brandon WilliamsDec 5, 2016
  5. Stefan BellerDec 5, 2016
  6. Stefan BellerDec 5, 2016
  7. Brandon WilliamsDec 5, 2016
  8. Duy NguyenDec 8, 2016
  9. Brandon WilliamsDec 8, 2016
  10. Junio C HamanoDec 6, 2016
  11. Brandon WilliamsDec 7, 2016
  12. Ramsay JonesDec 7, 2016
  13. Torsten BögershausenDec 7, 2016
  14. Junio C HamanoDec 7, 2016
  15. Brandon WilliamsDec 7, 2016
  16. Torsten BögershausenDec 8, 2016
  17. Johannes SixtDec 8, 2016
  18. Brandon WilliamsDec 8, 2016
  19. Johannes SixtDec 7, 2016
  20. Brandon WilliamsDec 7, 2016
  21. Johannes SixtDec 8, 2016
  22. Junio C HamanoDec 8, 2016
  23. 0/4 road to reentrant real_pathBrandon Williams, Dec 8, 2016
  24. 1/4 real_path: resolve symlinks by handBrandon Williams, Dec 8, 2016
  25. Jacob KellerDec 9, 2016
  26. Johannes SixtDec 9, 2016
  27. Brandon WilliamsDec 9, 2016
  28. 2/4 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Dec 8, 2016
  29. 3/4 real_path: create real_pathdupBrandon Williams, Dec 8, 2016
  30. Johannes SixtDec 9, 2016
  31. 4/4 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Dec 8, 2016
  32. Duy NguyenDec 9, 2016
  33. Brandon WilliamsDec 9, 2016
  34. Duy NguyenDec 10, 2016
  35. 0/4 road to reentrant real_pathBrandon Williams, Dec 12, 2016
  36. 1/4 real_path: resolve symlinks by handBrandon Williams, Dec 12, 2016
  37. Junio C HamanoDec 12, 2016
  38. Brandon WilliamsDec 12, 2016
  39. Junio C HamanoDec 12, 2016
  40. 4/4 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Dec 12, 2016
  41. Junio C HamanoDec 12, 2016
  42. Junio C HamanoDec 12, 2016
  43. Stefan BellerDec 12, 2016
  44. Brandon WilliamsDec 13, 2016
  45. Junio C HamanoDec 13, 2016
  46. 2/4 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Dec 12, 2016
  47. Junio C HamanoDec 12, 2016
  48. 3/4 real_path: create real_pathdupBrandon Williams, Dec 12, 2016
  49. Junio C HamanoDec 12, 2016
  50. real_path: canonicalize directory separators in root partsJohannes Sixt, Dec 21, 2016
  51. Brandon WilliamsDec 21, 2016
  52. Johannes SixtDec 22, 2016
  53. Brandon WilliamsDec 22, 2016
  54. Johannes SixtDec 22, 2016
  55. Junio C HamanoDec 22, 2016
  56. 0/5 road to reentrant real_pathBrandon Williams, Jan 3, 2017
  57. 2/5 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Jan 3, 2017
  58. 4/5 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Jan 3, 2017
  59. Jacob KellerJan 4, 2017
  60. Brandon WilliamsJan 4, 2017
  61. 5/5 real_path: canonicalize directory separators in root partsBrandon Williams, Jan 3, 2017
  62. 1/5 real_path: resolve symlinks by handBrandon Williams, Jan 3, 2017
  63. 3/5 real_path: create real_pathdupBrandon Williams, Jan 3, 2017
  64. Jeff KingJan 4, 2017
  65. Torsten BögershausenJan 4, 2017
  66. Jeff KingJan 4, 2017
  67. Brandon WilliamsJan 4, 2017
  68. Stefan BellerJan 4, 2017
  69. Jacob KellerJan 4, 2017
  70. Brandon WilliamsJan 4, 2017
  71. 0/5 road to reentrant real_pathBrandon Williams, Jan 4, 2017
  72. 3/5 real_path: create real_pathdupBrandon Williams, Jan 4, 2017
  73. 4/5 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Jan 4, 2017
  74. 5/5 real_path: canonicalize directory separators in root partsBrandon Williams, Jan 4, 2017
  75. 2/5 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Jan 4, 2017
  76. 1/5 real_path: resolve symlinks by handBrandon Williams, Jan 4, 2017
  77. Junio C HamanoJan 8, 2017
  78. Brandon WilliamsJan 9, 2017
  79. Junio C HamanoJan 9, 2017
  80. Brandon WilliamsJan 9, 2017
  81. Junio C HamanoJan 9, 2017
  82. 1/2 real_path: prevent redefinition of MAXSYMLINKSBrandon Williams, Jan 9, 2017
  83. 2/2 real_path: set errno when max number of symlinks is exceededBrandon Williams, Jan 9, 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.