relativePathWithinRoot function

  1. @visibleForTesting
String? relativePathWithinRoot({
  1. required String rootDirectory,
  2. required String absolutePath,
})

Returns the normalized path of absolutePath relative to rootDirectory.

Returns null when absolutePath is outside rootDirectory.

Implementation

@visibleForTesting
String? relativePathWithinRoot({
  required String rootDirectory,
  required String absolutePath,
}) {
  final relative = p.normalize(p.relative(absolutePath, from: rootDirectory));
  if (p.isAbsolute(relative)) {
    return null;
  }
  final normalized = normalizeIgnoreRelativePath(relative);
  if (normalized == '..' || normalized.startsWith('../')) {
    return null;
  }
  return normalized;
}