open static method

Future<IoStorageBackend> open(
  1. String path, {
  2. required bool createIfMissing,
  3. required bool readOnly,
  4. IoMode ioMode = .hybrid,
  5. bool? keepHandleOpen,
})

Opens path as an IoStorageBackend.

When keepHandleOpen is omitted, defaults to true on POSIX and false on Windows (closing the file handle between operations so an external process's compact() rename() never hits Windows sharing violations).

Throws WalPackNotFoundException when the file does not exist and createIfMissing is false (or when readOnly is true).

Implementation

static Future<IoStorageBackend> open(
  String path, {
  required bool createIfMissing,
  required bool readOnly,
  IoMode ioMode = .hybrid,
  bool? keepHandleOpen,
}) {
  final file = File(path);
  final exists = file.existsSync();
  if (!exists && (!createIfMissing || readOnly)) {
    throw WalPackNotFoundException(path);
  }
  if (!exists && !file.parent.existsSync()) {
    file.parent.createSync(recursive: true);
  }

  final keepOpen = keepHandleOpen ?? !Platform.isWindows;
  return Future<IoStorageBackend>.value(
    IoStorageBackend._(
      path,
      null,
      readOnly: readOnly,
      ioMode: ioMode,
      keepHandleOpen: keepOpen,
    ).._needsDirectorySync = !exists,
  );
}