open method

  1. @experimental
Future<void> open(
  1. FileSystemDirectoryHandle root, {
  2. bool readWriteUnsafe = false,
})

Re-opens a file system previously closed with close.

Implementation

@experimental
Future<void> open(
  FileSystemDirectoryHandle root, {
  bool readWriteUnsafe = false,
}) async {
  assert(_files == null);

  Future<FileSystemSyncAccessHandle> open(String name) async {
    final handle = await root.openFile(name, create: true);

    final syncHandlePromise = readWriteUnsafe
        ? ProposedLockingSchemeApi(handle).createSyncAccessHandle(
            FileSystemCreateSyncAccessHandleOptions.unsafeReadWrite(),
          )
        : handle.createSyncAccessHandle();

    return await syncHandlePromise.toDart;
  }

  final meta = await open('meta');
  // The meta file did not exist before, this can happen when migrating from
  // OPFS with atomics to this VFS.
  final migratingFromOpfsAtomics = meta.getSize() == 0;
  meta.truncate(2);

  final database = await open(FileType.database.name);
  final journal = await open(FileType.journal.name);

  final files = _files = _OpfsFiles(meta, database, journal);
  if (migratingFromOpfsAtomics) {
    files.markExists(FileType.database, database.getSize() > 0);
    files.markExists(FileType.journal, journal.getSize() > 0);
  }
}