open static method

Future<FileByteSink> open(
  1. String path, {
  2. bool overwrite = true,
  3. FileByteSinkTestHooks? testHooks,
})

Opens a staged sink that commits to path when close succeeds.

Implementation

static Future<FileByteSink> open(
  String path, {
  bool overwrite = true,
  FileByteSinkTestHooks? testHooks,
}) async {
  final target = File(path);
  _StagingOwnership? stagingOwnership;
  RandomAccessFile? handle;
  try {
    stagingOwnership = await _createStagingFile(target);
    handle = await stagingOwnership.file.open(mode: FileMode.write);
    stagingOwnership = await stagingOwnership.withCurrentFileFingerprint();
    await testHooks?.afterStagingOwnershipEstablished?.call(
      stagingOwnership.file,
    );
    return FileByteSink._(
      target,
      stagingOwnership,
      handle,
      overwrite,
      testHooks,
    );
  } on Object catch (error) {
    await handle?.close();
    Object? cleanupError;
    if (stagingOwnership != null) {
      try {
        await stagingOwnership.deleteIfOwned();
      } on Object catch (caught) {
        cleanupError = caught;
      }
    }
    throw ByteSinkIoException(
      operation: 'open staged byte sink',
      cause: cleanupError == null
          ? error
          : _CommitAndCleanupException(error, cleanupError),
      location: path,
    );
  }
}