path-util: make path_compare() and path_hash_func() ignore "."

This also makes path_compare() may return arbitrary integer as it now
simply pass the result of strcmp() or memcmp().

This changes the behavior of path_extract_filename/directory() when
e.g. "/." or "/./" are input. But the change should be desired.
This commit is contained in:
Yu Watanabe
2021-05-28 13:41:23 +09:00
parent fe69c41ee8
commit 353df4438e
4 changed files with 97 additions and 77 deletions
+21 -20
View File
@@ -17,7 +17,7 @@ DEFINE_HASH_OPS_FULL(string_hash_ops_free_free,
void, free);
void path_hash_func(const char *q, struct siphash *state) {
size_t n;
bool add_slash = false;
assert(q);
assert(state);
@@ -27,30 +27,31 @@ void path_hash_func(const char *q, struct siphash *state) {
* similar checks and also doesn't care for trailing slashes. Note that relative and absolute paths (i.e. those
* which begin in a slash or not) will hash differently though. */
n = strspn(q, "/");
if (n > 0) { /* Eat up initial slashes, and add one "/" to the hash for all of them */
siphash24_compress(q, 1, state);
q += n;
}
/* if path is absolute, add one "/" to the hash. */
if (path_is_absolute(q))
siphash24_compress("/", 1, state);
for (;;) {
/* Determine length of next component */
n = strcspn(q, "/");
if (n == 0) /* Reached the end? */
break;
const char *e;
int r;
/* Add this component to the hash and skip over it */
siphash24_compress(q, n, state);
q += n;
r = path_find_first_component(&q, true, &e);
if (r == 0)
return;
/* How many slashes follow this component? */
n = strspn(q, "/");
if (q[n] == 0) /* Is this a trailing slash? If so, we are at the end, and don't care about the slashes anymore */
break;
if (add_slash)
siphash24_compress_byte('/', state);
/* We are not add the end yet. Hash exactly one slash for all of the ones we just encountered. */
siphash24_compress(q, 1, state);
q += n;
if (r < 0) {
/* if a component is invalid, then add remaining part as a string. */
string_hash_func(q, state);
return;
}
/* Add this component to the hash. */
siphash24_compress(e, r, state);
add_slash = true;
}
}