Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

io/file/open.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature io.file.open
#
# -----------------------------------------------------------------------


# effect for manipulating open files
#
module:public open(fd File_Descriptor,
                   public file_name path) : effect is


  # install seek effect an run code
  #
  public seek(R type, code ()->R) outcome R =>

    hndlr :=
      _ : Seek_Handler is
        public redef seek(offset i64) outcome unit =>
          fuzion.sys.fileio.seek fd offset

        public redef file_position outcome i64 =>
          fuzion.sys.fileio.file_position fd

    file.this.seek hndlr ! code


  # install mmap effect an run code
  #
  # note: the offset must be a multiple of the `os.mmap_offset_multiple` which usually is 4096, windows 65536?
  # note: offset+size must not exceed size of file
  #
  # example usage:
  #
  #     _ := io.file.use unit "/some_file" io.file.mode.append ()->
  #       io.file.open.mmap (i64 0) (i64 100) ()->
  #         io.file.mapped_buffer.env[99] := 42
  #
  public mmap(R type, offset i64, size i64, code ()->R) outcome R
    pre
      safety: offset % os.mmap_offset_multiple = 0
      safety: size > 0
  =>

    mapped_memory := fzE_mmap fd offset size

    if fzE_is_valid_mapped_memory mapped_memory
      error "mmap failed, error code: $(fzE_last_error)"
    else
      (mapped_buffer mapped_memory size) ! code


  # close file when de-instating effect
  #
  public redef finally unit =>
    match fuzion.sys.fileio.close fd
      e error => fuzion.runtime.fault.env.cause ("io.file.open.finally", "closing file failed:  $(e)")
      unit =>



# short hand to get the currently
# installed open effect
#
public open open ! open =>
  open.env

last changed: 2026-06-17