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/