open method

FutureResult<RandomAccessFile, IoError> open(
  1. Path path
)

Implementation

FutureResult<RandomAccessFile, IoError> open(Path path) async {
  return Fs.ioGuardResult(() async {
    final file = File(path.asString());
    if (_willCreateNew) {
      if (await file.exists()) {
        return Err(IoError.ioException(FileSystemException(
            "File already exists, but was expected to create new.",
            path.asString())));
      }
    } else if (!_canCreate) {
      if (!(await file.exists())) {
        return Err(IoError.ioException(FileSystemException(
            "File does not exist and does not have create permission.",
            path.asString())));
      }
    }
    FileMode mode;
    if (_hasAppendAccess) {
      if (_hasReadAccess) {
        mode = FileMode.writeOnlyAppend;
      } else {
        mode = FileMode.append;
      }
    } else if (_hasWriteAccess) {
      if (_hasReadAccess) {
        mode = FileMode.write;
      } else {
        mode = FileMode.writeOnly;
      }
    } else if (_hasReadAccess) {
      mode = FileMode.read;
    } else {
      return Err(IoError.ioException(
          FileSystemException("No access mode specified.", path.asString())));
    }
    final randomAccessFile = await file.open(mode: mode);
    bool doTruncate = !_willCreateNew &&
        _willTruncate &&
        (_hasAppendAccess || _hasWriteAccess);
    if (doTruncate) {
      return Ok(await randomAccessFile.truncate(0));
    }
    return Ok(randomAccessFile);
  });
}