open static method

Future<WebOpfsBytesFile> open(
  1. String path, {
  2. bool create = true,
  3. bool readOnly = false,
  4. int chunkSize = kDefaultChunkSize,
  5. int? workers,
})

Opens the logical file at path.

  • create: true creates an empty file when the manifest is missing.
  • readOnly: true rejects every mutating call with a StateError and implies create: false (matching dart:io's FileMode.read).
  • chunkSize applies only to newly created files; an existing file keeps the chunk size recorded in its manifest.

Implementation

static Future<WebOpfsBytesFile> open(
  String path, {
  bool create = true,
  bool readOnly = false,
  int chunkSize = kDefaultChunkSize,
  int? workers,
}) async {
  final normalized = normalizeLogicalPath(path);
  if (chunkSize < kMinChunkSize || chunkSize > kMaxChunkSize) {
    throw RangeError.range(
      chunkSize,
      kMinChunkSize,
      kMaxChunkSize,
      'chunkSize',
    );
  }
  final pool = await OpfsWorkerPool.instance(workers: workers);
  final loaded = await _loadBestManifest(pool.manifest, normalized);
  final WebManifestState manifest;
  if (loaded == null) {
    if (!create || readOnly) {
      throw WebFileSystemException(
        'NotFoundError',
        'File does not exist',
        path: normalized,
      );
    }
    manifest = WebManifestState(path: normalized, chunkSize: chunkSize);
  } else {
    manifest = loaded.state;
  }
  final file = WebOpfsBytesFile._(
    path: normalized,
    readOnly: readOnly,
    chunkSize: manifest.chunkSize,
    pool: pool,
    manifest: manifest,
    slot: loaded?.slot ?? -1,
  );
  if (loaded == null) {
    // Materialize the store directory and slot A so `create` is durable.
    await file._persist();
  }
  return file;
}