code: def _fiff_open(fname, fid, preload):
      # do preloading of entire file
      if preload:
          # note that StringIO objects instantiated this way are read-only,
          # but that's okay here since we are using mode "rb" anyway
          with fid as fid_old:
              fid = BytesIO(fid_old.read())
  
      tag = _read_tag_header(fid, 0)
  
      #   Check that this looks like a fif file
      prefix = f"file {repr(fname)} does not"
      if tag.kind != FIFF.FIFF_FILE_ID:
          raise ValueError(f"{prefix} start with a file id tag")
  
      if tag.type != FIFF.FIFFT_ID_STRUCT:
          raise ValueError(f"{prefix} start with a file id tag")
  
      if tag.size != 20:
          raise ValueError(f"{prefix} start with a file id tag")
  
      tag = read_tag(fid, tag.next_pos)
  
      if tag.kind != FIFF.FIFF_DIR_POINTER:
          raise ValueError(f"{prefix} have a directory pointer")
  
      #   Read or create the directory tree
      logger.debug("    Creating tag directory for %s..." % fname)
  
      dirpos = int(tag.data.item())
      read_slow = True
      if dirpos > 0:
          dir_tag = read_tag(fid, dirpos)
          if dir_tag is None or dir_tag.data is None:
              fid.seek(0, 2)  # move to end of file
              size = fid.tell()
              extra = "" if size > dirpos else f" > file size {size}"
              warn(
                  "FIF tag directory missing at the end of the file "
                  f"(at byte {dirpos}{extra}), possibly corrupted file: {fname}"
              )
          else:
              directory = dir_tag.data
              read_slow = False
      if read_slow:
          pos = 0
          fid.seek(pos, 0)
          directory = list()
          while pos is not None:
              tag = _read_tag_header(fid, pos)
              if tag is None:
                  break  # HACK : to fix file ending with empty tag...
              pos = tag.next_pos
              directory.append(tag)
  
      tree, _ = make_dir_tree(fid, directory)
  
      logger.debug("[done]")
  
      #   Back to the beginning
      fid.seek(0)
  
      return fid, tree, directory
  
imports: import os.path
  from gzip import GzipFile
  from io import SEEK_SET, BytesIO
  import numpy
  from scipy.sparse import issparse
  from ..utils import _file_like, logger, verbose, warn
  from .constants import FIFF
  from .tag import Tag, _call_dict_names, _matrix_info, _read_tag_header, read_tag
  from .tree import dir_tree_find, make_dir_tree
label_desc: Use image node_id 4 for calling a global function with example usage: _fiff_open(fname, fid, preload) and returns: fid,  tree,  directory