abort method

Future<void> abort()

Discards staged bytes. Calling this more than once is harmless.

Implementation

Future<void> abort() => _synchronized(() async {
  if (_state == _FileSinkState.aborted) return;
  if (_state == _FileSinkState.committed) {
    throw const ByteSinkClosedException();
  }
  try {
    Object? ownershipError;
    if (_stagingSnapshot == null && !_handleClosed) {
      try {
        await _handle.flush();
        _stagingSnapshot = await _captureStagingSnapshot();
      } on Object catch (error) {
        ownershipError = error;
      }
    }
    if (!_handleClosed) {
      await _handle.close();
      _handleClosed = true;
    }
    _state = _FileSinkState.aborted;
    if (ownershipError != null || _stagingSnapshot == null) {
      await _stagingOwnership.deleteMarkerIfOwned();
      throw FileSystemException(
        'Staging cleanup refused because ownership could not be established'
        '${ownershipError == null ? '' : ': $ownershipError'}',
        _staging.path,
      );
    }
    await _deleteStagingIfOwned();
  } on FileSystemException catch (error) {
    throw ByteSinkIoException(
      operation: 'discard staged bytes',
      cause: error,
      location: _staging.path,
    );
  }
}, allowClosed: true);