VirtualFileOperations constructor

VirtualFileOperations(
  1. List<String> allowedDirectories, {
  2. String startingDirectory = '/',
})

Constructs a VirtualFileOperations object for managing directories.

The sharedDirectories specifies directories that are accessible in this virtual file system. Throws ArgumentError if no directories are provided.

Implementation

VirtualFileOperations(
  List<String> allowedDirectories, {
  String startingDirectory = '/',
}) : super(p.separator) {
  // rootDirectory is '/'

  // --- Start of changes ---
  // Normalize startingDirectory to be an absolute virtual path
  final String initialCurrentDirectory;
  if (p.isAbsolute(startingDirectory)) {
    initialCurrentDirectory = p.normalize(startingDirectory);
  } else {
    // Assume relative path is relative to root
    initialCurrentDirectory =
        p.normalize(p.join(rootDirectory, startingDirectory));
  }
  // --- End of changes ---

  if (allowedDirectories.isEmpty) {
    throw ArgumentError("Allowed directories cannot be empty");
  }

  for (String dir in allowedDirectories) {
    final normalizedDir = p.normalize(dir);
    final dirName = p.basename(normalizedDir);
    if (dirName == '.' || dirName == '..') {
      throw ArgumentError("Mapped directory name cannot be '.' or '..'");
    }
    if (directoryMappings.containsKey(dirName)) {
      throw ArgumentError("Duplicate mapped directory name: $dirName");
    }
    directoryMappings[dirName] = normalizedDir;
  }

  // --- Start of changes ---
  // Set currentDirectory AFTER mappings are potentially available for validation (optional)
  // We could add validation here using resolvePath, but let's try the simple fix first.
  currentDirectory = initialCurrentDirectory;
  // Optional validation:
  // try {
  //   resolvePath(currentDirectory); // Check if it resolves without error
  // } catch (e) {
  //   throw ArgumentError("Invalid starting directory '$startingDirectory': $e");
  // }
  // --- End of changes ---
}