code: def _rmtree_safe_fd(topfd, path, onexc):
      try:
          with os.scandir(topfd) as scandir_it:
              entries = list(scandir_it)
      except FileNotFoundError:
          return
      except OSError as err:
          err.filename = path
          onexc(os.scandir, path, err)
          return
      for entry in entries:
          fullname = os.path.join(path, entry.name)
          try:
              is_dir = entry.is_dir(follow_symlinks=False)
          except FileNotFoundError:
              continue
          except OSError:
              is_dir = False
          else:
              if is_dir:
                  try:
                      orig_st = entry.stat(follow_symlinks=False)
                      is_dir = stat.S_ISDIR(orig_st.st_mode)
                  except FileNotFoundError:
                      continue
                  except OSError as err:
                      onexc(os.lstat, fullname, err)
                      continue
          if is_dir:
              try:
                  dirfd = os.open(entry.name, os.O_RDONLY, dir_fd=topfd)
                  dirfd_closed = False
              except FileNotFoundError:
                  continue
              except OSError as err:
                  onexc(os.open, fullname, err)
              else:
                  try:
                      if os.path.samestat(orig_st, os.fstat(dirfd)):
                          _rmtree_safe_fd(dirfd, fullname, onexc)
                          try:
                              os.close(dirfd)
                          except OSError as err:
                              # close() should not be retried after an error.
                              dirfd_closed = True
                              onexc(os.close, fullname, err)
                          dirfd_closed = True
                          try:
                              os.rmdir(entry.name, dir_fd=topfd)
                          except FileNotFoundError:
                              continue
                          except OSError as err:
                              onexc(os.rmdir, fullname, err)
                      else:
                          try:
                              # This can only happen if someone replaces
                              # a directory with a symlink after the call to
                              # os.scandir or stat.S_ISDIR above.
                              raise OSError(
                                  "Cannot call rmtree on a symbolic "
                                  "link"
                              )
                          except OSError as err:
                              onexc(os.path.islink, fullname, err)
                  finally:
                      if not dirfd_closed:
                          try:
                              os.close(dirfd)
                          except OSError as err:
                              onexc(os.close, fullname, err)
          else:
              try:
                  os.unlink(entry.name, dir_fd=topfd)
              except FileNotFoundError:
                  continue
              except OSError as err:
                  onexc(os.unlink, fullname, err)
  
imports: import os
  import sys
  import stat
  import fnmatch
  import collections
  import errno
label_desc: Use image node_id 17 for calling a global function with example usage: _rmtree_safe_fd(topfd, path, onexc) without return types