* path = Canonical absolute path * prefixes = string_list containing normalized, absolute paths without * trailing slashes (except for the root directory, which is denoted by "/"). * * Determines, for each path in prefixes, whether the "prefix" * is an ancestor directory of path. Returns the length of the longest * ancestor directory, excluding any trailing slashes, or -1 if no prefix * is
| 1243 | * allowed. |
| 1244 | */ |
| 1245 | int longest_ancestor_length(const char *path, struct string_list *prefixes) |
| 1246 | { |
| 1247 | int max_len = -1; |
| 1248 | |
| 1249 | if (!strcmp(path, "/")) |
| 1250 | return -1; |
| 1251 | |
| 1252 | for (size_t i = 0; i < prefixes->nr; i++) { |
| 1253 | const char *ceil = prefixes->items[i].string; |
| 1254 | int len = strlen(ceil); |
| 1255 | |
| 1256 | /* |
| 1257 | * For root directories (`/`, `C:/`, `//server/share/`) |
| 1258 | * adjust the length to exclude the trailing slash. |
| 1259 | */ |
| 1260 | if (len > 0 && ceil[len - 1] == '/') |
| 1261 | len--; |
| 1262 | |
| 1263 | if (strncmp(path, ceil, len) || |
| 1264 | path[len] != '/' || !path[len + 1]) |
| 1265 | continue; /* no match */ |
| 1266 | |
| 1267 | if (len > max_len) |
| 1268 | max_len = len; |
| 1269 | } |
| 1270 | |
| 1271 | return max_len; |
| 1272 | } |
| 1273 | |
| 1274 | /* strip arbitrary amount of directory separators at end of path */ |
| 1275 | static inline int chomp_trailing_dir_sep(const char *path, int len) |
no outgoing calls