normalizeLogicalPath function
Validates and normalizes a logical path.
Paths are relative, POSIX-like (dir/name.bin): no leading/trailing slash,
no empty segments, and no ./.. segments. Everything else — including
spaces, unicode and non-ASCII — is allowed; the OPFS directory name is
derived by hashing, so unsafe characters never reach the file system.
Implementation
String normalizeLogicalPath(String path) {
if (path.isEmpty) {
throw ArgumentError.value(path, 'path', 'must not be empty');
}
if (path.length > 4096) {
throw ArgumentError.value(path, 'path', 'longer than 4096 characters');
}
if (path.contains('\u0000')) {
throw ArgumentError.value(path, 'path', 'must not contain NUL');
}
if (path.contains('\\')) {
throw ArgumentError.value(
path,
'path',
'must use "/" as the segment separator',
);
}
final segments = path.split('/');
for (final segment in segments) {
if (segment.isEmpty) {
throw ArgumentError.value(path, 'path', 'contains an empty segment');
}
if (segment == '.' || segment == '..') {
throw ArgumentError.value(path, 'path', 'contains a relative segment');
}
}
return path;
}