changeDirectory method
Changes the current working directory to the specified path. Implementations must handle path resolution and update the internal state correctly.
Implementation
@override
void changeDirectory(String path) {
try {
// First, resolve the target path (virtual or potentially physical relative)
// to its absolute physical representation or the virtual root.
final targetPhysicalPath = resolvePath(path);
// Case 1: Resolved path is the virtual root.
if (targetPhysicalPath == rootDirectory) {
currentDirectory = rootDirectory;
return;
}
// Case 2: Resolved path is a physical path. Check if it's a valid directory.
final dir = Directory(targetPhysicalPath);
if (!dir.existsSync()) {
throw FileSystemException(
"Directory not found: '$path' (resolved to '$targetPhysicalPath')",
path);
}
// Ensure it's actually a directory (though existsSync often suffices).
// Consider adding: if (FileSystemEntity.typeSync(targetPhysicalPath) != FileSystemEntityType.directory) { ... }
// Update currentDirectory to the corresponding *virtual* path.
// This ensures that subsequent relative operations work correctly within the virtual structure.
currentDirectory = _getVirtualPath(targetPhysicalPath);
} catch (e) {
// Rethrow specific exceptions or wrap others for clarity.
if (e is FileSystemException || e is StateError) {
rethrow;
}
throw FileSystemException(
"Failed to change directory to '$path': ${e.toString()}", path);
}
}