resolvePath method

  1. @override
String resolvePath(
  1. String path
)
override

Resolves the given path relative to the currentDirectory and the specific file system rules (physical or virtual).

Implementations must handle normalization, security checks (staying within allowed boundaries), and mapping (for virtual systems).

Implementation

@override
String resolvePath(String path) {
  // Handle empty path or '.' - resolves relative to currentDirectory
  if (path.isEmpty || path == '.') {
    // If current directory is virtual root, return it.
    if (currentDirectory == rootDirectory) {
      return rootDirectory;
    }
    // Otherwise, resolve the current virtual directory to its physical path.
    return _virtualToPhysical(currentDirectory);
  }

  // Normalize input path separators and clean the path
  // Replace all backslashes with forward slashes for consistency
  // final normalizedPath = path.replaceAll('\\', '/');
  final cleanPath = p.normalize(path);

  // Determine the absolute virtual path
  final absoluteVirtualPath = p.isAbsolute(cleanPath)
      ? cleanPath
      : p.normalize(p.join(currentDirectory, cleanPath));

  // Handle the case where the resolved path is the virtual root directory
  if (absoluteVirtualPath == rootDirectory) {
    return rootDirectory;
  }

  // Optimization: If we are at the virtual root and the input path is relative,
  // try to directly resolve it against the physical base directories first
  if (currentDirectory == rootDirectory && !p.isAbsolute(cleanPath)) {
    final physicalPath = _tryDirectPhysicalResolution(cleanPath);
    if (physicalPath != null) {
      return physicalPath;
    }
  }

  // Standard resolution: Convert the absolute virtual path to its corresponding physical path
  return _virtualToPhysical(absoluteVirtualPath);
}