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

Proposed function path_in_directory() [was: Re: [PATCH v2 8/9] longest_ancestor_length(): resolve symlinks before comparing paths]

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Oct 6, 2012, 08:04 UTC
Message-ID
<506FE619.1020608@alum.mit.edu>
In-Reply-To
<50692150.8080504@alum.mit.edu>
On 10/01/2012 06:51 AM, Michael Haggerty wrote:
Show 11 quoted lines
> I think I would advocate that the prefix has to match the front of the
> path exactly (including any trailing slashes) and either
> 
>     strlen(prefix) == 0
>     or the prefix ended with a '/'
>     or the prefix and path are identical
>     or the character in path following the matching part is a '/'
> 
> This would allow the "is path its own prefix" policy to be decided by
> the caller by either including or omitting a trailing slash on the
> prefix argument.

Thinking about this more, I don't think it will work. As usual, the special cases around "/" and "//" make things awkward. I think it is necessary to have a separate argument to specify whether "path is its own prefix".

So I am trying to decide how a function path_in_directory() should work, and would like to get some feedback, especially on the following two points:

1. How should "//" be handled?  I don't really have experience with the
peculiar paths that start with "//", so I'm not sure how they should be
handled (or even if the handling needs to be platform-dependent).  My
working hypothesis is that the inputs should be normalized by the
caller, so if the caller wants "//" to be treated as equivalent to "/"
then the caller should normalize them *before* calling this function.
Conversely, if the caller passes "//" to the function, that implies that
"//" is distinct from "/" and "//" is considered a proper subdirectory
of "/".  See the cases marked with "??????" below.
2. Does there need to be any special related to DOS paths?
Show 26 quoted lines
> /*
>  * Return true iff path is within dir.  The comparison is textual,
>  * meaning that path and dir should be normalized and either both be
>  * absolute or both be relative to the same directory.  If path and
>  * dir represent the *same* path, then return true iff allow_equal is
>  * true.  Single trailing slashes on either path or dir are ignored,
>  * (except for the special case "//"); i.e., "a/b" and "a/b/" are
>  * treated equivalently, as are "" and "/".  Examples (* means "don't
>  * care"):
>  *
>  * - path_in_directory("a/b", "a", *) -> true
>  * - path_in_directory("a", "a/b", *) -> false
>  * - path_in_directory("ab", "a", *) -> false
>  * - path_in_directory("a/b", "a/b", 0) -> false
>  *   (same if either argument is replaced with "a/b/")
>  * - path_in_directory("a/b", "a/b", 1) -> true
>  *   (same if either argument is replaced with "a/b/")
>  * - path_in_directory(*, "/", 1) -> true
>  * - path_in_directory("/", "/", 0) -> false
>  * - path_in_directory("//", "/", 0) -> true    ??????
>  * - path_in_directory("//", "/", 1) -> true
>  * - path_in_directory("/", "//", 0) -> false
>  * - path_in_directory("/", "//", 1) -> false   ??????
>  * - path_in_directory("/a/b", "//", *) -> false
>  */
> int path_in_directory(const char *path, const char *dir, int allow_equal);
Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
Previous: Junio C HamanoNext: Junio C Hamano
Message 13 of 17 in “Fix GIT_CEILING_DIRECTORIES that contain symlinks”
  1. 0/9 Fix GIT_CEILING_DIRECTORIES that contain symlinksMichael Haggerty, Sep 29, 2012
  2. 1/9 Introduce new static function real_path_internal()Michael Haggerty, Sep 29, 2012
  3. 2/9 real_path_internal(): add comment explaining use of cwdMichael Haggerty, Sep 29, 2012
  4. 3/9 Introduce new function real_path_if_valid()Michael Haggerty, Sep 29, 2012
  5. 4/9 longest_ancestor_length(): use string_list_split()Michael Haggerty, Sep 29, 2012
  6. 5/9 longest_ancestor_length(): explicitly filter list before loopMichael Haggerty, Sep 29, 2012
  7. 6/9 longest_ancestor_length(): always add a slash to the end of prefixesMichael Haggerty, Sep 29, 2012
  8. 7/9 longest_ancestor_length(): use string_list_longest_prefix()Michael Haggerty, Sep 29, 2012
  9. 8/9 longest_ancestor_length(): resolve symlinks before comparing pathsMichael Haggerty, Sep 29, 2012
  10. Junio C HamanoSep 30, 2012
  11. Michael HaggertyOct 1, 2012
  12. Junio C HamanoOct 1, 2012
  13. Proposed function path_in_directory() [was: Re: [PATCH v2 8/9] longest_ancestor_length(): resolve symlinks before comparing paths]Michael Haggerty, Oct 6, 2012
  14. Junio C HamanoOct 8, 2012
  15. Johannes SixtOct 8, 2012
  16. Junio C HamanoOct 8, 2012
  17. 9/9 t1504: stop resolving symlinks in GIT_CEILING_DIRECTORIESMichael Haggerty, Sep 29, 2012

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.