checkWritable method

  1. @override
Future<void> checkWritable(
  1. String atsign
)
override

Throws FileSystemException naming the keyfile's path when anything, such as a directory, is already at that path, or when a new file cannot be created beside it.

It creates the keyfile's directory when missing, which the write needs and which stays, and deletes the file it creates to find out before it returns, leaving no file behind.

Implementation

@override
Future<void> checkWritable(String atsign) async {
  final path = filePath!(atsign);
  final existing = await FileSystemEntity.type(path);
  if (existing != FileSystemEntityType.notFound) {
    throw FileSystemException(
        'Cannot write the atKeys file: a $existing is already at its path',
        path);
  }
  final probe = File(
      '$path.writable.$pid.${_random.nextInt(1 << 32).toRadixString(16)}');
  var created = false;
  try {
    await probe.parent.create(recursive: true);
    await probe.create();
    created = true;
  } on FileSystemException catch (e) {
    throw _notWritable(path, e);
  } finally {
    if (created) await _deleteProbe(probe, path);
  }
}