remove method
Removes a file or directory. With force, a missing path is not an
error.
Implementation
@override
Future<Result<void, FileError>> remove(
String path, {
bool recursive = false,
bool force = false,
}) async {
final resolved = _normalize(path);
final isFile = _files.containsKey(resolved);
final isDir = _dirs.contains(resolved);
if (!isFile && !isDir) {
if (force) return const Ok(null);
return Err(
FileError(
FileErrorCode.notFound,
'No such file or directory',
path: resolved,
),
);
}
if (isFile) {
_files.remove(resolved);
return const Ok(null);
}
final prefix = '$resolved/';
final hasChildren =
_dirs.any((d) => d.startsWith(prefix)) ||
_files.keys.any((f) => f.startsWith(prefix));
if (hasChildren && !recursive) {
return Err(
FileError(FileErrorCode.invalid, 'Directory not empty', path: resolved),
);
}
_dirs.removeWhere((d) => d == resolved || d.startsWith(prefix));
_files.removeWhere((f, _) => f.startsWith(prefix));
return const Ok(null);
}