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

io/file/stat.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 stat
#
#  Author: Wael Youssfi (wael.youssfi@tokiwa.software)
#
# -----------------------------------------------------------------------

# meta_data will be a feature storing the metadata of file/dir
#
private:public meta_data(
          # the size of the file/dir
          public size i64,
          # the time of last modification of the file/dir
          public time i64,
          # boolean to check if the pathname is a regular file
          public is_regular bool,
          # boolean to check if the pathname is a directory
          public is_dir bool) is

# stat -- effect providing operations to retrieve file stats
#
public stat(ps Stat_Handler) : simple_effect is

  # returns stats of the file/dir passed in the pathname
  # in success it will return a meta_data outcome storing stats regarding the file/dir
  # in case of failure an error will be returned
  # this feature resolves symbolic links
  #
  public stats(
        # the (relative or absolute) file name, using platform specific path separators
        path String) outcome meta_data =>
    arr := fuzion.sys.internal_array_init i64 4  # will contain: [size, time of last modification in seconds, regular file? 1 : 0, dir? 1 : 0]
    res := ps.stats (fuzion.sys.c_string path) arr.data
    data := arr.as_array
    if res
      replace
      meta_data data[0] data[1] (data[2] = i64 1) (data[3] = i64 1)
    else
      replace
      error "stat error {data[0]} for {path}"

  # returns stats of the file/dir passed in the pathname
  # in success it will return a meta_data outcome storing stats regarding the file/dir
  # in case of failure an error will be returned
  # this feature does not resolve symbolic links and returns stats of the link itself
  #
  public lstats(
         # the (relative or absolute) file name, using platform specific path separators
         path String) outcome meta_data => # NYI : not sure whether to use meta_data or introduce a new feature for lstats metadata
    arr := fuzion.sys.internal_array_init i64 4  # will contain: [size, time of last modification in seconds, regular file? 1 : 0, dir? 1 : 0]
    res := ps.stats (fuzion.sys.c_string path) arr.data
    data := arr.as_array
    if res
      replace
      meta_data data[0] data[1] (data[2] = i64 1) (data[3] = i64 1)
    else
      replace
      error "lstat error {data[0]} for {path}"


  # the default file stat provided
  #
  type.default_stat_handler : io.file.Stat_Handler is
    stats(path fuzion.sys.Pointer, meta_data_arr fuzion.sys.Pointer) =>
      fuzion.sys.fileio.stats path meta_data_arr

    lstats(path fuzion.sys.Pointer, meta_data_arr fuzion.sys.Pointer) =>
      fuzion.sys.fileio.lstats path meta_data_arr


  # install default effect io.file.stat
  type.install_default =>
    (io.file.stat default_stat_handler).default


# short-hand for accessing the stat effect in current environment
#
public stat =>
  stat.install_default
  stat.env


# short-hand for accessing and using stats/lstats provided by the stat effect in current environment
# in success it will return a meta_data outcome storing stats regarding the file/dir
# in case of failure an error will be returned
# resolve flag is used to indicate whether to resolve sym links or not
# NYI: lstats behaves the same as stats in the interpreter
#
public stat(
     # the (relative or absolute) file name, using platform specific path separators
     path String,
     # a boolean resolve flag to resolve symbolic links or not
     resolve bool) =>
  stat.install_default
  if resolve
    stat.env.stats path
  else
    stat.env.lstats path


# reference to the stats that could be provided
#
private:public Stat_Handler ref is
  stats(path fuzion.sys.Pointer, meta_data_arr fuzion.sys.Pointer) bool => abstract
  lstats(path fuzion.sys.Pointer, meta_data_arr fuzion.sys.Pointer) bool => abstract