delete method
Deletes the file or directory at path.
Implementation
@override
Future<Result<void>> delete(String path, {bool recursive = false}) async {
if (_deleteResults.isNotEmpty) {
return _deleteResults.removeFirst();
}
final String normalized = normalizePath(path);
if (_files.remove(normalized) != null) {
return const Ok<void>(null);
}
if (!_directories.contains(normalized)) {
return _notFound<void>('delete', normalized);
}
final String prefix = normalized == '/' ? '/' : '$normalized/';
final bool hasChildren =
_files.keys.any((String key) => key.startsWith(prefix)) ||
_directories.any(
(String directory) =>
directory != normalized && directory.startsWith(prefix),
);
if (hasChildren && !recursive) {
return portFailure<void>(
port: PortName.vfs,
code: PortErrorCode.directoryNotEmpty,
operation: 'delete',
message: 'Directory not empty',
details: <String, Object?>{'path': normalized},
);
}
_files.removeWhere((String key, List<int> _) => key.startsWith(prefix));
_directories.removeWhere(
(String directory) =>
directory == normalized || directory.startsWith(prefix),
);
_directories.add('/');
return const Ok<void>(null);
}