openSync method

Result<RandomAccessFile, IoError> openSync(
  1. Path path
)

Implementation

Result<RandomAccessFile, IoError> openSync(Path path) {
  return Fs.ioGuardResultSync(() {
    final file = File(path.asString());
    if (_willCreateNew) {
      if (file.existsSync()) {
        return Err(IoError.ioException(FileSystemException(
            "File already exists, but was expected to create new.",
            path.asString())));
      }
    } else if (!_canCreate) {
      if (!(file.existsSync())) {
        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 = file.openSync(mode: mode);
    bool doTruncate = !_willCreateNew &&
        _willTruncate &&
        (_hasAppendAccess || _hasWriteAccess);
    if (doTruncate) {
      randomAccessFile.truncateSync(0);
    }
    return Ok(randomAccessFile);
  });
}