open static method

Future<AsyncFile> open(
  1. String path, {
  2. FileOptions options = const FileOptions(),
})

Implementation

static Future<AsyncFile> open(
  String path, {
  FileOptions options = const FileOptions(),
}) async {
  if (options.exclusive && options.create) {
    var exists = false;
    try {
      final probe = await openBackend(
        path,
        options.copyWith(create: false, mode: FileMode.read),
      );
      await probe.close();
      exists = true;
    } catch (_) {
      // Missing file: proceed with the exclusive create.
    }
    if (exists) {
      throw BlitIOException('file already exists', path: path);
    }
  }
  final BytesFile backend;
  try {
    backend = await openBackend(path, options);
  } catch (error, stack) {
    Error.throwWithStackTrace(
      BlitIOException('failed to open $path', path: path, cause: error),
      stack,
    );
  }
  final file = AsyncFile._(path, backend, options);
  if (options.truncate && options.writable) {
    await backend.truncate(0);
  }
  return file;
}